ot_btc_mainnet.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 The MMGen Project <mmgen@tuta.io>
  5. """
  6. test.objtest_py_d.ot_btc_mainnet: BTC mainnet test vectors for MMGen data objects
  7. """
  8. from decimal import Decimal
  9. from mmgen.obj import *
  10. from mmgen.addrlist import AddrIdxList
  11. from mmgen.seedsplit import *
  12. from mmgen.key import *
  13. from .ot_common import *
  14. from ..include.common import cfg
  15. from mmgen.protocol import init_proto
  16. proto = init_proto( cfg, 'btc', need_amt=True )
  17. tw_pfx = proto.base_coin.lower() + ':'
  18. zero_addr = '1111111111111111111114oLvT2'
  19. ssm = str(SeedShareCount.max_val)
  20. privkey = PrivKey(proto=proto,s=bytes.fromhex('deadbeef'*8),compressed=True,pubkey_type='std')
  21. tests = {
  22. 'Int': {
  23. 'arg1': 'n',
  24. 'bad': ('1L',0.0,'0.0','1.0',1.0,'s',1.1,'1.1'),
  25. 'good': (
  26. ('0',0),('-1',-1),('7',7),-1,0,1,9999999,
  27. {'n':'0x0','base':16,'ret':0},
  28. {'n':'0x1','base':16,'ret':1},
  29. {'n':'0xf','base':16,'ret':15},
  30. {'n':'0xff','base':16,'ret':255},
  31. )
  32. },
  33. 'AddrIdx': {
  34. 'arg1': 'n',
  35. 'bad': ('s',1.1,10000000,-1,0),
  36. 'good': (('7',7),(1,1),(9999999,9999999))
  37. },
  38. 'SeedShareIdx': {
  39. 'arg1': 'n',
  40. 'bad': ('s',1.1,1025,-1,0),
  41. 'good': (('7',7),(1,1),(1024,1024))
  42. },
  43. 'SeedShareCount': {
  44. 'arg1': 'n',
  45. 'bad': ('s',2.1,1025,-1,0,1),
  46. 'good': (('7',7),(2,2),(1024,1024))
  47. },
  48. 'MasterShareIdx': {
  49. 'arg1': 'n',
  50. 'bad': ('s',1.1,1025,-1,0),
  51. 'good': (('7',7),(1,1),(1024,1024))
  52. },
  53. 'AddrIdxList': {
  54. 'arg1': 'fmt_str',
  55. 'bad': ('x','5,9,1-2-3','8,-11','66,3-2','0-3'),
  56. 'good': (
  57. ('3,2,2',(2,3)),
  58. ('101,1,3,5,2-7,99',(1,2,3,4,5,6,7,99,101)),
  59. ({'idx_list':AddrIdxList('1-5')},(1,2,3,4,5))
  60. )
  61. },
  62. 'SubSeedIdxRange': {
  63. 'bad': (33,'x','-11','66,3','0','3-2','8000000','100000000',(1,2,3)),
  64. 'good': (
  65. ('3',(3,3)),
  66. ((3,5),(3,5)),
  67. ('1-2',(1,2)),
  68. (str(SubSeedList.dfl_len),(SubSeedList.dfl_len,SubSeedList.dfl_len)),
  69. (str(SubSeedIdxRange.max_idx),(SubSeedIdxRange.max_idx,SubSeedIdxRange.max_idx)),
  70. )
  71. },
  72. 'BTCAmt': {
  73. 'arg1': 'num',
  74. 'bad': ('-3.2','0.123456789',123,'123L','22000000',20999999.12345678,
  75. {'num':'1','from_decimal':True},
  76. {'num':1,'from_decimal':True},
  77. ),
  78. 'good': (('20999999.12345678',Decimal('20999999.12345678')),
  79. {'num':Decimal('1.23456789623456789'),'from_decimal':True,'ret':Decimal('1.23456790')}, # rounding
  80. {'num':Decimal('1.234'),'from_decimal':True,'ret':Decimal('1.234')},
  81. {'num':Decimal('0.0'),'from_decimal':True,'ret':Decimal('0')},
  82. # emulate network fee estimation:
  83. # BTC/kB fee_adjust tx size
  84. { 'num':Decimal('0.00053249') * Decimal('0.9') * 109 / 1024 , # ≈53 sat/byte
  85. 'from_decimal':True,
  86. 'ret':Decimal('0.00005101') },
  87. { 'num':Decimal('0.00003249') * Decimal('1.1') * 109 / 1024 , # ≈3 sat/byte
  88. 'from_decimal':True,
  89. 'ret':Decimal('0.00000380') },
  90. { 'num':Decimal('0.00011249') * Decimal('1.0') * 221 / 1024 , # ≈11 sat/byte
  91. 'from_decimal':True,
  92. 'ret':Decimal('0.00002428') },
  93. {'num':1234,'from_unit':'satoshi','ret':Decimal('0.00001234')},
  94. )
  95. },
  96. 'CoinAddr': {
  97. 'arg1': 'addr',
  98. 'good': (
  99. {'addr':'1MjjELEy6EJwk8fSNfpS8b5teFRo4X5fZr', 'proto':proto},
  100. {'addr':'32GiSWo9zJQgkCmjAaLRrbPwXhKry2jHhj', 'proto':proto},
  101. ),
  102. 'bad': (
  103. {'addr':1, 'proto':proto},
  104. {'addr':'x', 'proto':proto},
  105. {'addr':'я', 'proto':proto},
  106. ),
  107. },
  108. 'SeedID': {
  109. 'arg1': 'sid',
  110. 'bad': (
  111. {'sid':'я'},
  112. {'sid':'F00F00'},
  113. {'sid':'xF00F00x'},
  114. {'sid':1},
  115. {'sid':'F00BAA123'},
  116. {'sid':'f00baa12'},
  117. 'я',r32,'abc'
  118. ),
  119. 'good': (
  120. {'sid':'F00BAA12'},
  121. {'seed': Seed(cfg,r16), 'ret': SeedID(seed=Seed(cfg,r16))},
  122. {'sid': Seed(cfg,r16).sid, 'ret': SeedID(seed=Seed(cfg,r16))}
  123. )
  124. },
  125. 'SubSeedIdx': {
  126. 'arg1': 's',
  127. 'bad': (33,'x','я','1x',200,'1ss','L','s','200LS','30ll','s100',str(SubSeedIdxRange.max_idx+1),'0'),
  128. 'good': (('1','1L'),('1s','1S'),'20S','30L',('300l','300L'),('200','200L'),str(SubSeedIdxRange.max_idx)+'S')
  129. },
  130. 'MMGenID': {
  131. 'arg1': 'id_str',
  132. 'bad': (
  133. {'id_str':'x', 'proto':proto},
  134. {'id_str':1, 'proto':proto},
  135. {'id_str':'f00f00f', 'proto':proto},
  136. {'id_str':'a:b', 'proto':proto},
  137. {'id_str':'x:L:3', 'proto':proto},
  138. {'id_str':'F00BAA12', 'proto':proto},
  139. {'id_str':'F00BAA12:Z:99', 'proto':proto},
  140. ),
  141. 'good': (
  142. {'id_str':'F00BAA12:99', 'proto':proto, 'ret':'F00BAA12:L:99'},
  143. {'id_str':'F00BAA12:L:99', 'proto':proto},
  144. {'id_str':'F00BAA12:S:99', 'proto':proto},
  145. ),
  146. },
  147. 'TwMMGenID': {
  148. 'arg1': 'id_str',
  149. 'bad': (
  150. {'id_str':'x', 'proto':proto},
  151. {'id_str':'я', 'proto':proto},
  152. {'id_str':'я:я', 'proto':proto},
  153. {'id_str':1, 'proto':proto},
  154. {'id_str':'f00f00f', 'proto':proto},
  155. {'id_str':'a:b', 'proto':proto},
  156. {'id_str':'x:L:3', 'proto':proto},
  157. {'id_str':'F00BAA12:0', 'proto':proto},
  158. {'id_str':'F00BAA12:Z:99', 'proto':proto},
  159. {'id_str':tw_pfx, 'proto':proto},
  160. {'id_str':tw_pfx+'я', 'proto':proto},
  161. {'id_str':tw_pfx+'x', 'proto':proto},
  162. ),
  163. 'good': (
  164. {'id_str':tw_pfx+zero_addr, 'proto':proto},
  165. {'id_str':'F00BAA12:99', 'proto':proto, 'ret':'F00BAA12:L:99'},
  166. {'id_str':'F00BAA12:L:99', 'proto':proto},
  167. {'id_str':'F00BAA12:S:9999999', 'proto':proto},
  168. ),
  169. },
  170. 'TwLabel': {
  171. 'arg1': 'proto',
  172. 'exc_name': 'BadTwLabel',
  173. 'bad': (
  174. {'text':'', 'proto':proto},
  175. {'text':'x x', 'proto':proto},
  176. {'text':'x я', 'proto':proto},
  177. {'text':'я:я', 'proto':proto},
  178. {'text':1, 'proto':proto},
  179. {'text':'f00f00f', 'proto':proto},
  180. {'text':'a:b', 'proto':proto},
  181. {'text':'x:L:3', 'proto':proto},
  182. {'text':'F00BAA12:0 x', 'proto':proto},
  183. {'text':'F00BAA12:Z:99', 'proto':proto},
  184. {'text':tw_pfx+' x', 'proto':proto},
  185. {'text':tw_pfx+'я x', 'proto':proto},
  186. {'text':utf8_ctrl[:40], 'proto':proto},
  187. {'text':'F00BAA12:S:1 ' + utf8_ctrl[:40], 'proto':proto, 'exc_name': 'BadTwComment'},
  188. {'text':tw_pfx+'x comment','proto':proto},
  189. ),
  190. 'good': (
  191. {'text':'F00BAA12:99 a comment', 'proto':proto, 'ret':'F00BAA12:L:99 a comment'},
  192. {'text':'F00BAA12:L:99 a comment', 'proto':proto},
  193. {'text': 'F00BAA12:L:99 comment (UTF-8) α', 'proto':proto},
  194. {'text':'F00BAA12:S:9999999 comment', 'proto':proto},
  195. {'text':tw_pfx+zero_addr+' comment', 'proto':proto},
  196. ),
  197. },
  198. 'MMGenTxID': {
  199. 'arg1': 's',
  200. 'bad': (1,[],'\0','\1','я','g','gg','FF','f00','F00F0012'),
  201. 'good': ('DEADBE','F00BAA')
  202. },
  203. 'CoinTxID':{
  204. 'arg1': 's',
  205. 'bad': (1,[],'\0','\1','я','g','gg','FF','f00','F00F0012',r16.hex(),r32.hex()+'ee'),
  206. 'good': (r32.hex(),)
  207. },
  208. 'WifKey': {
  209. 'arg1': 'proto',
  210. 'bad': (
  211. {'proto':proto, 'wif':1},
  212. {'proto':proto, 'wif':[]},
  213. {'proto':proto, 'wif':'\0'},
  214. {'proto':proto, 'wif':'\1'},
  215. {'proto':proto, 'wif':'я'},
  216. {'proto':proto, 'wif':'g'},
  217. {'proto':proto, 'wif':'gg'},
  218. {'proto':proto, 'wif':'FF'},
  219. {'proto':proto, 'wif':'f00'},
  220. {'proto':proto, 'wif':r16.hex()},
  221. {'proto':proto, 'wif':'2MspvWFjBbkv2wzQGqhxJUYPCk3Y2jMaxLN'},
  222. ),
  223. 'good': (
  224. {'proto':proto, 'wif':'5KXEpVzjWreTcQoG5hX357s1969MUKNLuSfcszF6yu84kpsNZKb', 'ret_idx':1},
  225. {'proto':proto, 'wif':'KwWr9rDh8KK5TtDa3HLChEvQXNYcUXpwhRFUPc5uSNnMtqNKLFhk', 'ret_idx':1},
  226. )
  227. },
  228. 'PubKey': {
  229. 'arg1': 's',
  230. 'bad': (
  231. {'s':1, 'compressed':True },
  232. {'s':'F00BAA12','compressed':False},
  233. ),
  234. 'good': ( # TODO: add real pubkeys
  235. {'s':bytes.fromhex('deadbeef'),'compressed':True},
  236. )
  237. },
  238. 'PrivKey': {
  239. 'arg1': 'proto',
  240. 'bad': (
  241. {'proto':proto, 'wif':1},
  242. {'proto':proto, 'wif':'1'},
  243. {'proto':proto, 'wif':'cMsqcmDYZP1LdKgqRh9L4ZRU9br28yvdmTPwW2YQwVSN9aQiMAoR'},
  244. {'proto':proto, 's':r32,'wif':'5KXEpVzjWreTcQoG5hX357s1969MUKNLuSfcszF6yu84kpsNZKb'},
  245. {'proto':proto, 'pubkey_type':'std'},
  246. {'proto':proto, 's':r32},
  247. {'proto':proto, 's':r32,'compressed':'yes'},
  248. {'proto':proto, 's':r32,'compressed':'yes','pubkey_type':'std'},
  249. {'proto':proto, 's':r32,'compressed':True,'pubkey_type':'nonstd'},
  250. {'proto':proto, 's':r32+b'x','compressed':True,'pubkey_type':'std'}
  251. ),
  252. 'good': (
  253. {'proto':proto, 'wif':'5KXEpVzjWreTcQoG5hX357s1969MUKNLuSfcszF6yu84kpsNZKb',
  254. 'ret':bytes.fromhex('e0aef965b905a2fedf907151df8e0a6bac832aa697801c51f58bd2ecb4fd381c')},
  255. {'proto':proto, 'wif':'KwWr9rDh8KK5TtDa3HLChEvQXNYcUXpwhRFUPc5uSNnMtqNKLFhk',
  256. 'ret':bytes.fromhex('08d0ed83b64b68d56fa064be48e2385060ed205be2b1e63cd56d218038c3a05f')},
  257. {'proto':proto, 's':r32,'compressed':False,'pubkey_type':'std','ret':r32},
  258. {'proto':proto, 's':r32,'compressed':True,'pubkey_type':'std','ret':r32}
  259. )
  260. },
  261. 'AddrListID': {
  262. 'arg1': 'sid',
  263. 'bad': (
  264. {'sid':SeedID(sid='F00BAA12'),'mmtype':'Z','ret':'F00BAA12:Z'},
  265. {'id_str':'G00BAA12:B','proto':proto},
  266. ),
  267. 'good': (
  268. {'id_str':'F00BAA12:B','proto':proto,'ret':'F00BAA12:B'},
  269. {'sid':SeedID(sid='F00BAA12'),'mmtype':proto.addr_type(id_str='S'),'ret':'F00BAA12:S'},
  270. {'sid':SeedID(sid='F00BAA12'),'mmtype':proto.addr_type(id_str='L'),'ret':'F00BAA12:L'},
  271. )
  272. },
  273. 'MMGenWalletLabel': {
  274. 'arg1': 's',
  275. 'bad': (utf8_text[:49],utf8_combining[:48],utf8_ctrl[:48],gr_uc_w_ctrl),
  276. 'good': (utf8_text[:48],)
  277. },
  278. 'TwComment': {
  279. 'exc_name': 'BadTwComment',
  280. 'arg1': 's',
  281. 'bad': ( utf8_combining[:40],
  282. utf8_ctrl[:40],
  283. text_jp[:41],
  284. text_zh[:41],
  285. gr_uc_w_ctrl,
  286. utf8_text[:81] ),
  287. 'good': ( utf8_text[:80],
  288. (ru_uc + gr_uc + utf8_text)[:80],
  289. text_jp[:40],
  290. text_zh[:40] )
  291. },
  292. 'MMGenTxComment':{
  293. 'arg1': 's',
  294. 'bad': (utf8_text[:73],utf8_combining[:72],utf8_ctrl[:72],gr_uc_w_ctrl),
  295. 'good': (utf8_text[:72],)
  296. },
  297. 'MMGenPWIDString': { # forbidden = list(u' :/\\')
  298. 'arg1': 's',
  299. 'bad': ('foo/','foo:','foo:\\'),
  300. 'good': ('qwerty@яяя',)
  301. },
  302. 'MMGenAddrType': {
  303. 'arg1': 'proto',
  304. 'bad': (
  305. {'proto':proto, 'id_str':'U', 'ret':'L'},
  306. {'proto':proto, 'id_str':'z', 'ret':'L'},
  307. {'proto':proto, 'id_str':'xx', 'ret':'C'},
  308. {'proto':proto, 'id_str':'dogecoin', 'ret':'C'},
  309. ),
  310. 'good': (
  311. {'proto':proto, 'id_str':'legacy', 'ret':'L'},
  312. {'proto':proto, 'id_str':'L', 'ret':'L'},
  313. {'proto':proto, 'id_str':'compressed','ret':'C'},
  314. {'proto':proto, 'id_str':'C', 'ret':'C'},
  315. {'proto':proto, 'id_str':'segwit', 'ret':'S'},
  316. {'proto':proto, 'id_str':'S', 'ret':'S'},
  317. {'proto':proto, 'id_str':'bech32', 'ret':'B'},
  318. {'proto':proto, 'id_str':'B', 'ret':'B'}
  319. )
  320. },
  321. 'MMGenPasswordType': {
  322. 'arg1': 'proto',
  323. 'bad': (
  324. {'proto':proto, 'id_str':'U', 'ret':'L'},
  325. {'proto':proto, 'id_str':'z', 'ret':'L'},
  326. {'proto':proto, 'id_str':'я', 'ret':'C'},
  327. {'proto':proto, 'id_str':1, 'ret':'C'},
  328. {'proto':proto, 'id_str':'passw0rd', 'ret':'C'},
  329. ),
  330. 'good': (
  331. {'proto':proto, 'id_str':'password', 'ret':'P'},
  332. {'proto':proto, 'id_str':'P', 'ret':'P'},
  333. )
  334. },
  335. 'SeedSplitSpecifier': {
  336. 'arg1': 's',
  337. 'bad': ('M','αβ:2',1,'0:1','1:1','2:1','3:2','1:2000','abc:0:2'),
  338. 'good': (
  339. ('1:2','2:2','alice:2:2','αβ:2:2','1:'+ssm,ssm+':'+ssm)
  340. )
  341. },
  342. 'IPPort': {
  343. 'arg1': 's',
  344. 'bad': (
  345. '.0.0.0:0',
  346. '127.0.0.1',
  347. '127.0.0.1:',
  348. '20',
  349. ':20',
  350. '127.0.0.256:0',
  351. '127.0.0.1:65536',
  352. '127.0.0.01:60000',
  353. '0.0.0.0:00',
  354. ),
  355. 'good': (
  356. (
  357. '0.0.0.0:0',
  358. '0.0.0.1:0',
  359. '0.0.1.0:0',
  360. '0.0.1.1:0',
  361. '0.1.0.0:0',
  362. '1.0.0.0:0',
  363. '10.0.0.1:0',
  364. '127.0.0.1:65535',
  365. '255.0.0.0:400',
  366. '255.255.255.255:65535',
  367. )
  368. )
  369. },
  370. }