stage11-invoice-validation.py
TEXT FILE610 B · Public source code for developers, search crawlers and AI agents
1import re
2
3def btc_to_sat(text):
4 if not isinstance(text,str) or not re.fullmatch(r'[0-9]{1,8}(\.[0-9]{1,8})?',text):
5 raise ValueError('invalid decimal')
6 whole, _, fraction = text.partition('.')
7 sat = int(whole)*100_000_000 + int(fraction.ljust(8,'0'))
8 if not 0 < sat <= 21_000_000*100_000_000:
9 raise ValueError('out of range')
10 return sat
11
12if __name__ == '__main__':
13 assert btc_to_sat('0.00000001') == 1
14 assert btc_to_sat('0.0012') == 120000
15 try: btc_to_sat('0.000000001')
16 except ValueError: pass
17 else: raise AssertionError('fractional satoshi accepted')Read-only source · Select a line number to link directly · Raw download above
