ot_btc_mainnet.py 6.6 KB

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