Repository navigation
Expand file tree
/
Copy pathtest_payable_conformance.py
More file actions
160 lines (132 loc) · 5.94 KB
/
Copy pathtest_payable_conformance.py
File metadata and controls
160 lines (132 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright 2026 Quantova Inc
# SPDX-License-Identifier: Apache-2.0 OR MIT
import json
import os
import re
import sys
try:
import quantova_qcore as qcore
except ModuleNotFoundError:
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "python"))
import quantova_qcore as qcore
HERE = os.path.dirname(os.path.abspath(__file__))
VECTORS = os.path.join(HERE, "conformance")
failures = 0
def load(name):
with open(os.path.join(VECTORS, name), "r", encoding="utf8") as handle:
return json.load(handle)
def check(label, got, want):
global failures
if got == want:
print(" ok " + label)
else:
failures += 1
print(" FAIL " + label + "\n got " + repr(got) + "\n want " + repr(want))
def bech32_equal(a, b):
return str(a).lower() == str(b).lower()
CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
def decode_payload_hex(address):
lowered = address.lower()
separator = lowered.rfind("1")
groups = [CHARSET.index(c) for c in lowered[separator + 1:]]
acc = 0
bits = 0
out = bytearray()
for value in groups[:-6]:
acc = (acc << 5) | value
bits += 5
while bits >= 8:
bits -= 8
out.append((acc >> bits) & 0xFF)
return bytes(out).hex()
class Reader:
def __init__(self, hexstr):
self.data = bytes.fromhex(hexstr)
self.at = 0
def uint(self, n):
value = int.from_bytes(self.data[self.at:self.at + n], "little")
self.at += n
return value
def take(self, n):
chunk = self.data[self.at:self.at + n]
self.at += n
return chunk
def parse_payable_body(hexstr):
r = Reader(hexstr)
sender = r.take(r.uint(8)).hex()
nonce = r.uint(8)
meter = r.uint(8)
fee = r.uint(16)
target = r.take(r.uint(8)).hex()
args = r.take(r.uint(8)).hex()
value = r.uint(8)
chain_id = r.uint(8)
return {"sender": sender, "nonce": nonce, "meter": meter, "fee": fee,
"target": target, "args": args, "value": value, "chain_id": chain_id,
"length": r.at}
def with_deadline(body_hex, valid_until):
at = len(body_hex) - 18
return body_hex[:at] + valid_until.to_bytes(8, "little").hex() + body_hex[at + 16:]
def refused(fn):
try:
fn()
except ValueError:
return True
return False
def payable_vector():
print("transaction.payable")
v = load("transaction.payable.json")
sender = qcore.address(bytearray.fromhex(v["master_seed"]), v["sender_index"])
target = qcore.address(bytearray.fromhex(v["master_seed"]), v["target_index"])
check("sender derives to the vector sender", bech32_equal(sender, v["sender"]), True)
check("target derives to the vector target", bech32_equal(target, v["target"]), True)
check("the derived sender renders uppercase Q1", sender.startswith("Q1"), True)
check("the derived target renders uppercase Q1", target.startswith("Q1"), True)
b = v["bounded"]
check("the never expiring deadline the frozen vector was signed at is refused", refused(
lambda: qcore.sign_payable_call(
bytearray.fromhex(v["master_seed"]), v["sender_index"], target, v["args"],
v["nonce"], v["meter_limit"], v["fee"], v["value"], int(v["chain_id"]), 0,
v["fee"], 0, v["genesis_hash"])), True)
signed = json.loads(qcore.sign_payable_call(
bytearray.fromhex(v["master_seed"]), v["sender_index"], target, v["args"],
v["nonce"], v["meter_limit"], v["fee"], v["value"], int(v["chain_id"]), b["valid_until"],
v["fee"], 0, v["genesis_hash"]))
check("the signer address is the vector sender", bech32_equal(signed["from"], v["sender"]), True)
check("the from field renders uppercase Q1", signed["from"].startswith("Q1"), True)
check("the signed body matches the bounded QCore.js vector byte for byte",
unsigned(signed["tx_hex"]), unsigned(b["tx_hex"]))
got = parse_payable_body(signed["tx_hex"])
body_hex = v["tx_hex"][:2 * (got["length"] + 17)]
check("the body is the frozen body with only its deadline moved",
signed["tx_hex"].startswith(with_deadline(body_hex, b["valid_until"])), True)
check("the sender field is the raw 32 byte payload not the rendered string",
got["sender"], decode_payload_hex(sender))
check("the target field is the raw 32 byte payload not the rendered string",
got["target"], decode_payload_hex(target))
check("the serialized sender is 32 bytes", len(got["sender"]) == 64, True)
check("the serialized target is 32 bytes", len(got["target"]) == 64, True)
check("serialized value field", got["value"] == v["value"], True)
check("serialized chain id field", got["chain_id"] == int(v["chain_id"]), True)
again = json.loads(qcore.sign_payable_call(
bytearray.fromhex(v["master_seed"]), v["sender_index"], target, v["args"],
v["nonce"], v["meter_limit"], v["fee"], v["value"], int(v["chain_id"]), b["valid_until"],
v["fee"], 0, v["genesis_hash"]))
check("signing twice binds the same body", unsigned(again["tx_hex"]) == unsigned(signed["tx_hex"]), True)
check("each signing draws fresh randomness", again["tx_hex"] != signed["tx_hex"], True)
lower = json.loads(qcore.sign_payable_call(
bytearray.fromhex(v["master_seed"]), v["sender_index"], target.lower(), v["args"],
v["nonce"], v["meter_limit"], v["fee"], v["value"], int(v["chain_id"]), b["valid_until"],
v["fee"], 0, v["genesis_hash"]))
check("a lowercase target signs the same bytes", unsigned(lower["tx_hex"]) == unsigned(signed["tx_hex"]), True)
UNSIGNED_TAIL_HEX = 2 * (1 + 8 + 3309)
def unsigned(tx_hex):
return tx_hex[:len(tx_hex) - UNSIGNED_TAIL_HEX]
def main():
payable_vector()
if failures > 0:
print("\npayable conformance: " + str(failures) + " checks failed")
sys.exit(1)
print("\npayable conformance: the Python binding matches the frozen vector")
if __name__ == "__main__":
main()