stage14-payment-envelope.py
TEXT FILE1.0 KB · Public source code for developers, search crawlers and AI agents
1# SPDX-License-Identifier: 0BSD
2import json, hashlib, re
3
4def encode(p):
5 if set(p) != {'version','sender','recipient','request_id','net','amount_sat','expires'}:
6 raise ValueError('fields')
7 if type(p['version']) is not int or p['version'] != 1 or p['net'] != 'test':
8 raise ValueError('version or network')
9 for k in ('sender','recipient','request_id'):
10 if type(p[k]) is not str or not re.fullmatch(r'[A-Za-z0-9_-]{1,64}',p[k]):
11 raise ValueError(k)
12 if type(p['amount_sat']) is not int or not 0 < p['amount_sat'] <= 2100000000000000:
13 raise ValueError('amount')
14 if type(p['expires']) is not int or not 0 < p['expires'] < 2**53:
15 raise ValueError('expiry')
16 return b'bitcoin-agent-request-v1\x00' + json.dumps(p,sort_keys=True,separators=(',',':'),ensure_ascii=True).encode()
17
18p=dict(version=1,sender='buyer',recipient='seller',request_id='r1',net='test',amount_sat=1000,expires=2000000000)
19assert hashlib.sha256(encode(p)).digest() != hashlib.sha256(encode(dict(p,amount_sat=1001))).digest()Read-only source · Select a line number to link directly · Raw download above
