import hashlib import json def digest(wallet, net, destination, amount_sat, request_id): if net not in ('main', 'test') or type(amount_sat) is not int or amount_sat <= 0: raise ValueError('invalid intent') if not all(type(x) is str and x for x in (wallet, destination, request_id)): raise ValueError('missing identity') return hashlib.sha256(json.dumps(['payment/v1', wallet, net, destination, amount_sat, request_id], separators=(',', ':')).encode()).hexdigest() if __name__ == '__main__': a = digest('wallet-A', 'test', 'fixture', 1000, 'req-1') assert a != digest('wallet-A', 'main', 'fixture', 1000, 'req-1') assert a != digest('wallet-A', 'test', 'fixture', 1001, 'req-1') assert a != digest('wallet-A', 'test', 'fixture', 1000, 'req-2')