Update app.py
Browse files
app.py
CHANGED
|
@@ -28,6 +28,11 @@ SERVER_KEY = Keypair.from_seed(seed)
|
|
| 28 |
|
| 29 |
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
CHAIN_LENGTH = 256
|
| 32 |
N = 32 # SHA256 output bytes
|
| 33 |
|
|
@@ -71,6 +76,67 @@ def verify_wots(signature, message, public_key):
|
|
| 71 |
|
| 72 |
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
@app.get("/challenge")
|
| 76 |
def get_challenge():
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
'''
|
| 36 |
CHAIN_LENGTH = 256
|
| 37 |
N = 32 # SHA256 output bytes
|
| 38 |
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
+
'''
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
CHAIN_LENGTH = 256
|
| 85 |
+
N_MESSAGE = 32 # Standard hash length
|
| 86 |
+
N_TOTAL = 34 # 32 (Message) + 2 (Checksum)
|
| 87 |
+
|
| 88 |
+
def verify_wots(signature, message, public_key):
|
| 89 |
+
"""
|
| 90 |
+
signature: list[str] (base58 encoded, length 34)
|
| 91 |
+
public_key: list[str] (base58 encoded, length 34)
|
| 92 |
+
"""
|
| 93 |
+
if len(signature) != N_TOTAL or len(public_key) != N_TOTAL:
|
| 94 |
+
print(f"Invalid lengths: expected {N_TOTAL}")
|
| 95 |
+
return False
|
| 96 |
+
|
| 97 |
+
# 1. Re-calculate the message hash
|
| 98 |
+
msg_hash = hashlib.sha256(message).digest()
|
| 99 |
+
|
| 100 |
+
# 2. Re-calculate the SAME Checksum as the client
|
| 101 |
+
checksum = 0
|
| 102 |
+
for byte in msg_hash:
|
| 103 |
+
checksum += (255 - byte)
|
| 104 |
+
|
| 105 |
+
# Convert checksum to 2 bytes
|
| 106 |
+
cks_bytes = bytes([(checksum >> 8) & 0xff, checksum & 0xff])
|
| 107 |
+
|
| 108 |
+
# 3. Combine them exactly like the client did
|
| 109 |
+
combined = msg_hash + cks_bytes
|
| 110 |
+
|
| 111 |
+
# 4. Verify all 34 chains
|
| 112 |
+
for i in range(N_TOTAL):
|
| 113 |
+
try:
|
| 114 |
+
sig_i = base58.b58decode(signature[i])
|
| 115 |
+
pk_i = base58.b58decode(public_key[i])
|
| 116 |
+
except Exception as e:
|
| 117 |
+
return False
|
| 118 |
+
|
| 119 |
+
# Current revealed link
|
| 120 |
+
check = sig_i
|
| 121 |
+
|
| 122 |
+
# WALK FORWARD to the anchor (Public Key)
|
| 123 |
+
# The client walked 'combined[i]' steps.
|
| 124 |
+
# We walk the remaining steps to reach 256.
|
| 125 |
+
steps = CHAIN_LENGTH - combined[i]
|
| 126 |
+
|
| 127 |
+
for _ in range(steps):
|
| 128 |
+
check = hashlib.sha256(check).digest()
|
| 129 |
+
|
| 130 |
+
if check != pk_i:
|
| 131 |
+
print(f"Mismatch at index {i}")
|
| 132 |
+
return False
|
| 133 |
+
|
| 134 |
+
return True
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
|
| 141 |
@app.get("/challenge")
|
| 142 |
def get_challenge():
|