# SPDX-License-Identifier: 0BSD import hashlib import json def digest(intent): fields = {"request_id", "wallet", "net", "to", "amount_sat"} if set(intent) != fields or intent["net"] != "test": raise ValueError("invalid intent") if any(type(intent[k]) is not str or not intent[k] for k in fields - {"amount_sat"}): raise ValueError("invalid identifier") n = intent["amount_sat"] if type(n) is not int or not 0 < n <= 21_000_000 * 100_000_000: raise ValueError("invalid amount") return hashlib.sha256(json.dumps(intent, sort_keys=True, separators=(",", ":"), ensure_ascii=True).encode()).hexdigest() p = dict(request_id="r1", wallet="demo", net="test", to="synthetic-destination", amount_sat=1000) assert digest(p) != digest(dict(p, request_id="r2")) assert digest(p) != digest(dict(p, amount_sat=1001))