Junaidb commited on
Commit
bf76a4c
·
verified ·
1 Parent(s): def47b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -12
app.py CHANGED
@@ -19,6 +19,53 @@ seed = hashlib.sha256(seed_text.encode()).digest()
19
  SERVER_KEY = Keypair.from_seed(seed)
20
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  @app.post("/relay-txn")
23
  async def relay_txn(payload: dict):
24
  # 1. Verification Math
@@ -56,6 +103,9 @@ async def relay_txn(payload: dict):
56
 
57
 
58
 
 
 
 
59
  @app.post("/sign-message")
60
  async def sign_message(req: dict):
61
  #message = bytes(payload["message"])
@@ -83,20 +133,12 @@ async def sign_message(req: dict):
83
  signature = payload["signature"] # list[str]
84
  public_key = payload["public_key"] # list[str]
85
 
86
- msg_hash = hashlib.sha256(message).digest()
87
-
88
- for i in range(len(signature)):
89
- sig_i = base58.b58decode(signature[i])
90
- pk_i = base58.b58decode(public_key[i])
91
 
92
- check = sig_i
93
- for _ in range(CHAIN_LENGTH - msg_hash[i]):
94
- check = hashlib.sha256(check).digest()
95
-
96
- if check != pk_i:
97
- return {"status":False}
98
 
99
- return {"status":True}
100
 
101
 
102
 
 
19
  SERVER_KEY = Keypair.from_seed(seed)
20
 
21
 
22
+
23
+
24
+ CHAIN_LENGTH = 256
25
+ N = 32 # SHA256 output bytes
26
+
27
+ def verify_wots(signature, message, public_key):
28
+ """
29
+ signature: list[str] (base58 encoded, length 32)
30
+ message: bytes
31
+ public_key: list[str] (base58 encoded, length 32)
32
+ """
33
+
34
+ # Basic sanity checks
35
+ if len(signature) != N or len(public_key) != N:
36
+ print("Invalid lengths")
37
+ return False
38
+
39
+ # Hash the message
40
+ msg_hash = hashlib.sha256(message).digest()
41
+
42
+ for i in range(N):
43
+ try:
44
+ sig_i = base58.b58decode(signature[i])
45
+ pk_i = base58.b58decode(public_key[i])
46
+ except Exception as e:
47
+ print(f"Base58 decode error at index {i}: {e}")
48
+ return False
49
+
50
+ check = sig_i
51
+
52
+ # Walk forward in hash chain
53
+ steps = CHAIN_LENGTH - msg_hash[i]
54
+
55
+ for _ in range(steps):
56
+ check = hashlib.sha256(check).digest()
57
+
58
+ # Compare with public key element
59
+ if check != pk_i:
60
+ print(f"Mismatch at index {i}")
61
+ return False
62
+
63
+ return True
64
+
65
+
66
+
67
+
68
+
69
  @app.post("/relay-txn")
70
  async def relay_txn(payload: dict):
71
  # 1. Verification Math
 
103
 
104
 
105
 
106
+
107
+
108
+
109
  @app.post("/sign-message")
110
  async def sign_message(req: dict):
111
  #message = bytes(payload["message"])
 
133
  signature = payload["signature"] # list[str]
134
  public_key = payload["public_key"] # list[str]
135
 
136
+ is_valid = verify_wots(signature, message, public_key)
 
 
 
 
137
 
138
+ if not is_valid:
139
+ return {"success": False, "error": "Invalid WOTS signature"}
 
 
 
 
140
 
141
+ return {"success": True}
142
 
143
 
144