ot_btc_mainnet.py 12 KB

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