# SPDX-License-Identifier: 0BSD import hashlib, json, hmac def fingerprint(intent): return hashlib.sha256(b'approved-intent-v1\x00'+json.dumps(intent,sort_keys=True,separators=(',',':'),ensure_ascii=True,allow_nan=False).encode()).hexdigest() def matches(record,intent,now): return (record['agent']==intent['agent'] and now < record['expires'] and hmac.compare_digest(record['digest'],fingerprint(intent))) p=dict(agent='buyer',wallet='demo',net='test',to='synthetic',amount_sat=1000,request_id='r1') r=dict(agent='buyer',expires=110,digest=fingerprint(p)) assert matches(r,p,100) assert not matches(r,dict(p,amount_sat=1001),100) assert not matches(r,p,110)