ot_btc_mainnet.py 11 KB

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