ot_btc_mainnet.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2020 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. ssm = str(SeedShareCount.max_val)
  12. tests = {
  13. 'Int': {
  14. 'bad': ('1L',0.0,'0.0','1.0',1.0,'s',1.1,'1.1'),
  15. 'good': (
  16. ('0',0),('-1',-1),('7',7),-1,0,1,9999999,
  17. {'n':'0x0','base':16,'ret':0},
  18. {'n':'0x1','base':16,'ret':1},
  19. {'n':'0xf','base':16,'ret':15},
  20. {'n':'0xff','base':16,'ret':255},
  21. )
  22. },
  23. 'AddrIdx': {
  24. 'bad': ('s',1.1,10000000,-1,0),
  25. 'good': (('7',7),(1,1),(9999999,9999999))
  26. },
  27. 'SeedShareIdx': {
  28. 'bad': ('s',1.1,1025,-1,0),
  29. 'good': (('7',7),(1,1),(1024,1024))
  30. },
  31. 'SeedShareCount': {
  32. 'bad': ('s',2.1,1025,-1,0,1),
  33. 'good': (('7',7),(2,2),(1024,1024))
  34. },
  35. 'MasterShareIdx': {
  36. 'bad': ('s',1.1,1025,-1,0),
  37. 'good': (('7',7),(1,1),(1024,1024))
  38. },
  39. 'AddrIdxList': {
  40. 'bad': ('x','5,9,1-2-3','8,-11','66,3-2'),
  41. 'good': (
  42. ('3,2,2',[2,3]),
  43. ('101,1,3,5,2-7,99',[1,2,3,4,5,6,7,99,101]),
  44. ({'idx_list':AddrIdxList('1-5')},[1,2,3,4,5])
  45. )
  46. },
  47. 'SubSeedIdxRange': {
  48. 'bad': (33,'x','-11','66,3','0','3-2','8000000','100000000',(1,2,3)),
  49. 'good': (
  50. ('3',(3,3)),
  51. ((3,5),(3,5)),
  52. ('1-2',(1,2)),
  53. (str(g.subseeds),(g.subseeds,g.subseeds)),
  54. (str(SubSeedIdxRange.max_idx),(SubSeedIdxRange.max_idx,SubSeedIdxRange.max_idx)),
  55. )
  56. },
  57. 'BTCAmt': {
  58. 'bad': ('-3.2','0.123456789',123,'123L','22000000',20999999.12345678,
  59. {'num':'1','from_decimal':True},
  60. {'num':1,'from_decimal':True},
  61. ),
  62. 'good': (('20999999.12345678',Decimal('20999999.12345678')),
  63. {'num':Decimal('1.23456789623456789'),'from_decimal':True,'ret':Decimal('1.23456790')}, # rounding
  64. {'num':Decimal('1.234'),'from_decimal':True,'ret':Decimal('1.234')},
  65. {'num':Decimal('0.0'),'from_decimal':True,'ret':Decimal('0')},
  66. # emulate network fee estimation:
  67. # BTC/kB tx_fee_adj tx size
  68. { 'num':Decimal('0.00053249') * Decimal('0.9') * 109 / 1024 , # ≈53 sat/byte
  69. 'from_decimal':True,
  70. 'ret':Decimal('0.00005101') },
  71. { 'num':Decimal('0.00003249') * Decimal('1.1') * 109 / 1024 , # ≈3 sat/byte
  72. 'from_decimal':True,
  73. 'ret':Decimal('0.00000380') },
  74. { 'num':Decimal('0.00011249') * Decimal('1.0') * 221 / 1024 , # ≈11 sat/byte
  75. 'from_decimal':True,
  76. 'ret':Decimal('0.00002428') },
  77. {'num':1234,'from_unit':'satoshi','ret':Decimal('0.00001234')},
  78. )
  79. },
  80. 'CoinAddr': {
  81. 'bad': (1,'x','я'),
  82. 'good': ('1MjjELEy6EJwk8fSNfpS8b5teFRo4X5fZr','32GiSWo9zJQgkCmjAaLRrbPwXhKry2jHhj'),
  83. },
  84. 'SeedID': {
  85. 'bad': (
  86. {'sid':'я'},
  87. {'sid':'F00F00'},
  88. {'sid':'xF00F00x'},
  89. {'sid':1},
  90. {'sid':'F00BAA123'},
  91. {'sid':'f00baa12'},
  92. 'я',r32,'abc'),
  93. 'good': (({'sid':'F00BAA12'},'F00BAA12'),(Seed(r16),Seed(r16).sid))
  94. },
  95. 'SubSeedIdx': {
  96. 'bad': (33,'x','я','1x',200,'1ss','L','s','200LS','30ll','s100',str(SubSeedIdxRange.max_idx+1),'0'),
  97. 'good': (('1','1L'),('1s','1S'),'20S','30L',('300l','300L'),('200','200L'),str(SubSeedIdxRange.max_idx)+'S')
  98. },
  99. 'MMGenID': {
  100. 'bad': ('x',1,'f00f00f','a:b','x:L:3','F00BAA12:0','F00BAA12:Z:99'),
  101. 'good': (('F00BAA12:99','F00BAA12:L:99'),'F00BAA12:L:99','F00BAA12:S:99')
  102. },
  103. 'TwMMGenID': {
  104. 'bad': ('x','я','я:я',1,'f00f00f','a:b','x:L:3','F00BAA12:0','F00BAA12:Z:99',tw_pfx,tw_pfx+'я'),
  105. 'good': (('F00BAA12:99','F00BAA12:L:99'),'F00BAA12:L:99','F00BAA12:S:9999999',tw_pfx+'x')
  106. },
  107. 'TwLabel': {
  108. 'bad': ('x x','x я','я:я',1,'f00f00f','a:b','x:L:3','F00BAA12:0 x',
  109. 'F00BAA12:Z:99',tw_pfx+' x',tw_pfx+'я x',
  110. 'F00BAA12:S:1 '+ utf8_ctrl[:40],
  111. {'s':'F00BAA12:S:1 '+ utf8_ctrl[:40],'on_fail':'raise','ExcType':'BadTwComment'},
  112. ),
  113. 'good': (
  114. ('F00BAA12:99 a comment','F00BAA12:L:99 a comment'),
  115. 'F00BAA12:L:99 comment (UTF-8) α',
  116. 'F00BAA12:S:9999999 comment',
  117. tw_pfx+'x comment')
  118. },
  119. 'MMGenTxID': {
  120. 'bad': (1,[],'\0','\1','я','g','gg','FF','f00','F00F0012'),
  121. 'good': ('DEADBE','F00BAA')
  122. },
  123. 'CoinTxID':{
  124. 'bad': (1,[],'\0','\1','я','g','gg','FF','f00','F00F0012',r16.hex(),r32.hex()+'ee'),
  125. 'good': (r32.hex(),)
  126. },
  127. 'WifKey': {
  128. 'bad': (1,[],'\0','\1','я','g','gg','FF','f00',r16.hex(),'2MspvWFjBbkv2wzQGqhxJUYPCk3Y2jMaxLN'),
  129. 'good': ('5KXEpVzjWreTcQoG5hX357s1969MUKNLuSfcszF6yu84kpsNZKb',
  130. 'KwWr9rDh8KK5TtDa3HLChEvQXNYcUXpwhRFUPc5uSNnMtqNKLFhk'),
  131. },
  132. 'PubKey': {
  133. 'bad': ({'arg':1,'compressed':False},{'arg':'F00BAA12','compressed':False},),
  134. 'good': ({'arg':'deadbeef','compressed':True},) # TODO: add real pubkeys
  135. },
  136. 'PrivKey': {
  137. 'bad': (
  138. {'wif':1},
  139. {'wif':'1'},
  140. {'wif':'cMsqcmDYZP1LdKgqRh9L4ZRU9br28yvdmTPwW2YQwVSN9aQiMAoR'},
  141. {'s':r32,'wif':'5KXEpVzjWreTcQoG5hX357s1969MUKNLuSfcszF6yu84kpsNZKb'},
  142. {'pubkey_type':'std'},
  143. {'s':r32},
  144. {'s':r32,'compressed':'yes'},
  145. {'s':r32,'compressed':'yes','pubkey_type':'std'},
  146. {'s':r32,'compressed':True,'pubkey_type':'nonstd'},
  147. {'s':r32+b'x','compressed':True,'pubkey_type':'std'}
  148. ),
  149. 'good': (
  150. {'wif':'5KXEpVzjWreTcQoG5hX357s1969MUKNLuSfcszF6yu84kpsNZKb',
  151. 'ret':'e0aef965b905a2fedf907151df8e0a6bac832aa697801c51f58bd2ecb4fd381c'},
  152. {'wif':'KwWr9rDh8KK5TtDa3HLChEvQXNYcUXpwhRFUPc5uSNnMtqNKLFhk',
  153. 'ret':'08d0ed83b64b68d56fa064be48e2385060ed205be2b1e63cd56d218038c3a05f'},
  154. {'s':r32,'compressed':False,'pubkey_type':'std','ret':r32.hex()},
  155. {'s':r32,'compressed':True,'pubkey_type':'std','ret':r32.hex()}
  156. )
  157. },
  158. 'AddrListID': { # a rather pointless test, but do it anyway
  159. 'bad': (
  160. {'sid':SeedID(sid='F00BAA12'),'mmtype':'Z','ret':'F00BAA12:Z'},
  161. ),
  162. 'good': (
  163. {'sid':SeedID(sid='F00BAA12'),'mmtype':MMGenAddrType('S'),'ret':'F00BAA12:S'},
  164. {'sid':SeedID(sid='F00BAA12'),'mmtype':MMGenAddrType('L'),'ret':'F00BAA12:L'},
  165. )
  166. },
  167. 'MMGenWalletLabel': {
  168. 'bad': (utf8_text[:49],utf8_combining[:48],utf8_ctrl[:48],gr_uc_w_ctrl),
  169. 'good': (utf8_text[:48],)
  170. },
  171. 'TwComment': {
  172. 'bad': ( utf8_combining[:40],
  173. utf8_ctrl[:40],
  174. text_jp[:41],
  175. text_zh[:41],
  176. gr_uc_w_ctrl,
  177. utf8_text[:81] ),
  178. 'good': ( utf8_text[:80],
  179. (ru_uc + gr_uc + utf8_text)[:80],
  180. text_jp[:40],
  181. text_zh[:40] )
  182. },
  183. 'MMGenTXLabel':{
  184. 'bad': (utf8_text[:73],utf8_combining[:72],utf8_ctrl[:72],gr_uc_w_ctrl),
  185. 'good': (utf8_text[:72],)
  186. },
  187. 'MMGenPWIDString': { # forbidden = list(u' :/\\')
  188. 'bad': ('foo/','foo:','foo:\\'),
  189. 'good': ('qwerty@яяя',)
  190. },
  191. 'MMGenAddrType': {
  192. 'bad': ('U','z','xx',1,'dogecoin'),
  193. 'good': (
  194. {'s':'legacy','ret':'L'},
  195. {'s':'L','ret':'L'},
  196. {'s':'compressed','ret':'C'},
  197. {'s':'C','ret':'C'},
  198. {'s':'segwit','ret':'S'},
  199. {'s':'S','ret':'S'},
  200. {'s':'bech32','ret':'B'},
  201. {'s':'B','ret':'B'}
  202. )
  203. },
  204. 'MMGenPasswordType': {
  205. 'bad': ('U','z','я',1,'passw0rd'),
  206. 'good': (
  207. {'s':'password','ret':'P'},
  208. {'s':'P','ret':'P'},
  209. )
  210. },
  211. 'SeedSplitSpecifier': {
  212. 'bad': ('M','αβ:2',1,'0:1','1:1','2:1','3:2','1:2000','abc:0:2'),
  213. 'good': (
  214. ('1:2','2:2','alice:2:2','αβ:2:2','1:'+ssm,ssm+':'+ssm)
  215. )
  216. },
  217. }