stage12-payment-protocol.py
TEXT FILE852 B · Public source code for developers, search crawlers and AI agents
1# SPDX-License-Identifier: 0BSD
2import hashlib
3import json
4
5def digest(intent):
6 fields = {"request_id", "wallet", "net", "to", "amount_sat"}
7 if set(intent) != fields or intent["net"] != "test":
8 raise ValueError("invalid intent")
9 if any(type(intent[k]) is not str or not intent[k] for k in fields - {"amount_sat"}):
10 raise ValueError("invalid identifier")
11 n = intent["amount_sat"]
12 if type(n) is not int or not 0 < n <= 21_000_000 * 100_000_000:
13 raise ValueError("invalid amount")
14 return hashlib.sha256(json.dumps(intent, sort_keys=True, separators=(",", ":"), ensure_ascii=True).encode()).hexdigest()
15
16p = dict(request_id="r1", wallet="demo", net="test", to="synthetic-destination", amount_sat=1000)
17assert digest(p) != digest(dict(p, request_id="r2"))
18assert digest(p) != digest(dict(p, amount_sat=1001))Read-only source · Select a line number to link directly · Raw download above
