tool.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  5. # Copyright (C)2013-2018 The MMGen Project <mmgen@tuta.io>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. tool.py: Routines and data for the 'mmgen-tool' utility
  21. """
  22. import binascii
  23. from mmgen.protocol import hash160
  24. from mmgen.common import *
  25. from mmgen.crypto import *
  26. from mmgen.tx import *
  27. from mmgen.addr import *
  28. pnm = g.proj_name
  29. from collections import OrderedDict
  30. cmd_data = OrderedDict([
  31. ('Help', ['<tool command> [str]']),
  32. ('Usage', ['<tool command> [str]']),
  33. ('Strtob58', ['<string> [str-]','pad [int=0]']),
  34. ('B58tostr', ['<b58 number> [str-]']),
  35. ('Hextob58', ['<hex number> [str-]','pad [int=0]']),
  36. ('B58tohex', ['<b58 number> [str-]','pad [int=0]']),
  37. ('B58randenc', []),
  38. ('B32tohex', ['<b32 num> [str-]','pad [int=0]']),
  39. ('Hextob32', ['<hex num> [str-]','pad [int=0]']),
  40. ('Randhex', ['nbytes [int=32]']),
  41. ('Id8', ['<infile> [str]']),
  42. ('Id6', ['<infile> [str]']),
  43. ('Hash160', ['<hexadecimal string> [str-]']),
  44. ('Hash256', ['<str, hexstr or filename> [str]', # TODO handle stdin
  45. 'hex_input [bool=False]','file_input [bool=False]']),
  46. ('Str2id6', ['<string (spaces are ignored)> [str-]']),
  47. ('Hexdump', ['<infile> [str]', 'cols [int=8]', 'line_nums [bool=True]']),
  48. ('Unhexdump', ['<infile> [str]']),
  49. ('Hexreverse', ['<hexadecimal string> [str-]']),
  50. ('Hexlify', ['<string> [str-]']),
  51. ('Rand2file', ['<outfile> [str]','<nbytes> [str]','threads [int=4]','silent [bool=False]']),
  52. ('Randwif', []),
  53. ('Randpair', []),
  54. ('Hex2wif', ['<private key in hex format> [str-]']),
  55. ('Wif2hex', ['<wif> [str-]']),
  56. ('Wif2addr', ['<wif> [str-]']),
  57. ('Wif2segwit_pair',['<wif> [str-]']),
  58. ('Pubhash2addr', ['<coin address in hex format> [str-]']),
  59. ('Addr2hexaddr', ['<coin address> [str-]']),
  60. ('Privhex2addr', ['<private key in hex format> [str-]']),
  61. ('Privhex2pubhex',['<private key in hex format> [str-]']),
  62. ('Pubhex2addr', ['<public key in hex format> [str-]']), # new
  63. ('Pubhex2redeem_script',['<public key in hex format> [str-]']), # new
  64. ('Wif2redeem_script', ['<private key in WIF format> [str-]']), # new
  65. ('Hex2mn', ['<hexadecimal string> [str-]',"wordlist [str='electrum']"]),
  66. ('Mn2hex', ['<mnemonic> [str-]', "wordlist [str='electrum']"]),
  67. ('Mn_rand128', ["wordlist [str='electrum']"]),
  68. ('Mn_rand192', ["wordlist [str='electrum']"]),
  69. ('Mn_rand256', ["wordlist [str='electrum']"]),
  70. ('Mn_stats', ["wordlist [str='electrum']"]),
  71. ('Mn_printlist', ["wordlist [str='electrum']"]),
  72. ('Listaddress',['<{} address> [str]'.format(pnm),'minconf [int=1]','pager [bool=False]','showempty [bool=True]','showbtcaddr [bool=True]','show_age [bool=False]','show_days [bool=True]']),
  73. ('Listaddresses',["addrs [str='']",'minconf [int=1]','showempty [bool=False]','pager [bool=False]','showbtcaddrs [bool=True]','all_labels [bool=False]',"sort [str=''] (options: reverse, age)",'show_age [bool=False]','show_days [bool=True]']),
  74. ('Getbalance', ['minconf [int=1]','quiet [bool=False]']),
  75. ('Txview', ['<{} TX file(s)> [str]'.format(pnm),'pager [bool=False]','terse [bool=False]',"sort [str='mtime'] (options: ctime, atime)",'MARGS']),
  76. ('Twview', ["sort [str='age']",'reverse [bool=False]','show_days [bool=True]','show_mmid [bool=True]','minconf [int=1]','wide [bool=False]','pager [bool=False]']),
  77. ('Add_label', ['<{} or coin address> [str]'.format(pnm),'<label> [str]']),
  78. ('Remove_label', ['<{} or coin address> [str]'.format(pnm)]),
  79. ('Addrfile_chksum', ['<{} addr file> [str]'.format(pnm),"mmtype [str='']"]),
  80. ('Keyaddrfile_chksum', ['<{} addr file> [str]'.format(pnm),"mmtype [str='']"]),
  81. ('Passwdfile_chksum', ['<{} password file> [str]'.format(pnm)]),
  82. ('Find_incog_data', ['<file or device name> [str]','<Incog ID> [str]','keep_searching [bool=False]']),
  83. ('Encrypt', ['<infile> [str]',"outfile [str='']","hash_preset [str='']"]),
  84. ('Decrypt', ['<infile> [str]',"outfile [str='']","hash_preset [str='']"]),
  85. ('Bytespec', ['<bytespec> [str]']),
  86. ('Keyaddrlist2monerowallets',['<{} XMR key-address file> [str]'.format(pnm),'blockheight [int=(current height)]',"addrs [str=''] (addr idx list or range)"]),
  87. ('Syncmonerowallets', ['<{} XMR key-address file> [str]'.format(pnm),"addrs [str=''] (addr idx list or range)"]),
  88. ])
  89. def usage(command):
  90. for v in cmd_data.values():
  91. if v and v[0][-2:] == '-]':
  92. v[0] = v[0][:-2] + ' or STDIN]'
  93. if 'MARGS' in v: v.remove('MARGS')
  94. if not command:
  95. Msg('Usage information for mmgen-tool commands:')
  96. for k,v in cmd_data.items():
  97. Msg(' {:18} {}'.format(k.lower(),' '.join(v)))
  98. from mmgen.main_tool import stdin_msg
  99. Msg('\n '+'\n '.join(stdin_msg.split('\n')))
  100. sys.exit(0)
  101. Command = command.capitalize()
  102. if Command in cmd_data:
  103. import re
  104. from mmgen.main_tool import cmd_help
  105. for line in cmd_help.split('\n'):
  106. if re.match(r'\s+{}\s+'.format(command),line):
  107. c,h = line.split('-',1)
  108. Msg('MMGEN-TOOL {}: {}'.format(c.strip().upper(),h.strip()))
  109. cd = cmd_data[Command]
  110. msg('USAGE: {} {} {}'.format(g.prog_name,command,' '.join(cd)))
  111. else:
  112. msg("'{}': no such tool command".format(command))
  113. sys.exit(1)
  114. Help = usage
  115. def process_args(command,cmd_args):
  116. if 'MARGS' in cmd_data[command]:
  117. cmd_data[command].remove('MARGS')
  118. margs = True
  119. else:
  120. margs = False
  121. c_args = [[i.split(' [')[0],i.split(' [')[1][:-1]]
  122. for i in cmd_data[command] if '=' not in i]
  123. c_kwargs = dict([[
  124. i.split(' [')[0],
  125. [i.split(' [')[1].split('=')[0],i.split(' [')[1].split('=')[1][:-1]]
  126. ] for i in cmd_data[command] if '=' in i])
  127. if not margs:
  128. u_args = [a for a in cmd_args[:len(c_args)]]
  129. if c_args and c_args[0][1][-1] == '-':
  130. c_args[0][1] = c_args[0][1][:-1] # [str-] -> [str]
  131. # If we're reading from a pipe, replace '-' with output of previous command
  132. if u_args and u_args[0] == '-':
  133. if not sys.stdin.isatty():
  134. u_args[0] = sys.stdin.read().strip()
  135. if not u_args[0]:
  136. die(2,'{}: ERROR: no output from previous command in pipe'.format(command.lower()))
  137. if not margs and len(u_args) < len(c_args):
  138. m1 = 'Command requires exactly {} non-keyword argument{}'
  139. msg(m1.format(len(c_args),suf(c_args,'s')))
  140. usage(command)
  141. extra_args = len(cmd_args) - len(c_args)
  142. u_kwargs = {}
  143. if margs:
  144. t = [a.split('=') for a in cmd_args if '=' in a]
  145. tk = [a[0] for a in t]
  146. tk_bad = [a for a in tk if a not in c_kwargs]
  147. if set(tk_bad) != set(tk[:len(tk_bad)]):
  148. die(1,"'{}': illegal keyword argument".format(tk_bad[-1]))
  149. u_kwargs = dict(t[len(tk_bad):])
  150. u_args = cmd_args[:-len(u_kwargs) or None]
  151. elif extra_args > 0:
  152. u_kwargs = dict([a.split('=') for a in cmd_args[len(c_args):] if '=' in a])
  153. if len(u_kwargs) != extra_args:
  154. msg('Command requires exactly {} non-keyword argument{}'.format(len(c_args),suf(c_args,'s')))
  155. usage(command)
  156. if len(u_kwargs) > len(c_kwargs):
  157. msg('Command requires exactly {} keyword argument{}'.format(len(c_kwargs),suf(c_kwargs,'s')))
  158. usage(command)
  159. # mdie(c_args,c_kwargs,u_args,u_kwargs)
  160. for k in u_kwargs:
  161. if k not in c_kwargs:
  162. msg("'{}': invalid keyword argument".format(k))
  163. usage(command)
  164. def conv_type(arg,arg_name,arg_type):
  165. if arg_type == 'str': arg_type = 'unicode'
  166. if arg_type == 'bool':
  167. if arg.lower() in ('true','yes','1','on'): arg = True
  168. elif arg.lower() in ('false','no','0','off'): arg = False
  169. else:
  170. msg("'{}': invalid boolean value for keyword argument".format(arg))
  171. usage(command)
  172. try:
  173. return __builtins__[arg_type](arg)
  174. except:
  175. die(1,"'{}': Invalid argument for argument {} ('{}' required)".format(arg,arg_name,arg_type))
  176. if margs:
  177. args = [conv_type(u_args[i],c_args[0][0],c_args[0][1]) for i in range(len(u_args))]
  178. else:
  179. args = [conv_type(u_args[i],c_args[i][0],c_args[i][1]) for i in range(len(c_args))]
  180. kwargs = dict([(k,conv_type(u_kwargs[k],k,c_kwargs[k][0])) for k in u_kwargs])
  181. return args,kwargs
  182. # Individual cmd_data
  183. def are_equal(a,b,dtype=''):
  184. if dtype == 'str': return a.lstrip('\0') == b.lstrip('\0')
  185. if dtype == 'hex': return a.lstrip('0') == b.lstrip('0')
  186. if dtype == 'b58': return a.lstrip('1') == b.lstrip('1')
  187. else: return a == b
  188. def print_convert_results(indata,enc,dec,dtype):
  189. error = (True,False)[are_equal(indata,dec,dtype)]
  190. if error or opt.verbose:
  191. Msg('Input: {}'.format(repr(indata)))
  192. Msg('Encoded data: {}'.format(repr(enc)))
  193. Msg('Recoded data: {}'.format(repr(dec)))
  194. else: Msg(enc)
  195. if error:
  196. die(3,"Error! Recoded data doesn't match input!")
  197. from mmgen.obj import MMGenAddrType
  198. at = MMGenAddrType((hasattr(opt,'type') and opt.type) or g.proto.dfl_mmtype)
  199. kg = KeyGenerator(at)
  200. ag = AddrGenerator(at)
  201. def Hexdump(infile,cols=8,line_nums=True):
  202. Msg(pretty_hexdump(
  203. get_data_from_file(infile,dash=True,silent=True,binary=True),
  204. cols=cols,line_nums=line_nums))
  205. def Unhexdump(infile):
  206. if g.platform == 'win':
  207. import msvcrt
  208. msvcrt.setmode(sys.stdout.fileno(),os.O_BINARY)
  209. sys.stdout.write(decode_pretty_hexdump(
  210. get_data_from_file(infile,dash=True,silent=True)))
  211. def B58randenc():
  212. r = get_random(32)
  213. enc = baseconv.b58encode(r,pad=True)
  214. dec = baseconv.b58decode(enc,pad=True)
  215. print_convert_results(r,enc,dec,'str')
  216. def Randhex(nbytes='32'):
  217. Msg(binascii.hexlify(get_random(int(nbytes))))
  218. def Randwif():
  219. Msg(PrivKey(get_random(32),pubkey_type=at.pubkey_type,compressed=at.compressed).wif)
  220. def Randpair():
  221. privhex = PrivKey(get_random(32),pubkey_type=at.pubkey_type,compressed=at.compressed)
  222. addr = ag.to_addr(kg.to_pubhex(privhex))
  223. Vmsg('Key (hex): {}'.format(privhex))
  224. Vmsg_r('Key (WIF): '); Msg(privhex.wif)
  225. Vmsg_r('Addr: '); Msg(addr)
  226. def Wif2addr(wif):
  227. privhex = PrivKey(wif=wif)
  228. addr = ag.to_addr(kg.to_pubhex(privhex))
  229. Vmsg_r('Addr: '); Msg(addr)
  230. def Wif2segwit_pair(wif):
  231. pubhex = kg.to_pubhex(PrivKey(wif=wif))
  232. addr = ag.to_addr(pubhex)
  233. rs = ag.to_segwit_redeem_script(pubhex)
  234. Msg('{}\n{}'.format(rs,addr))
  235. def Pubhash2addr(pubhash):
  236. if opt.type == 'bech32':
  237. ret = g.proto.pubhash2bech32addr(pubhash)
  238. else:
  239. ret = g.proto.pubhash2addr(pubhash,at.addr_fmt=='p2sh')
  240. Msg(ret)
  241. def Addr2hexaddr(addr): Msg(g.proto.verify_addr(addr,CoinAddr.hex_width,return_dict=True)['hex'])
  242. def Hash160(pubkeyhex): Msg(hash160(pubkeyhex))
  243. def Pubhex2addr(pubkeyhex): Pubhash2addr(hash160(pubkeyhex))
  244. def Wif2hex(wif): Msg(PrivKey(wif=wif))
  245. def Hex2wif(hexpriv):
  246. Msg(g.proto.hex2wif(hexpriv,pubkey_type=at.pubkey_type,compressed=at.compressed))
  247. def Privhex2addr(privhex,output_pubhex=False):
  248. pk = PrivKey(binascii.unhexlify(privhex),compressed=at.compressed,pubkey_type=at.pubkey_type)
  249. ph = kg.to_pubhex(pk)
  250. Msg(ph if output_pubhex else ag.to_addr(ph))
  251. def Privhex2pubhex(privhex): # new
  252. Privhex2addr(privhex,output_pubhex=True)
  253. def Pubhex2redeem_script(pubhex): # new
  254. Msg(g.proto.pubhex2redeem_script(pubhex))
  255. def Wif2redeem_script(wif): # new
  256. privhex = PrivKey(wif=wif)
  257. Msg(ag.to_segwit_redeem_script(kg.to_pubhex(privhex)))
  258. wordlists = 'electrum','tirosh'
  259. dfl_wl_id = 'electrum'
  260. def do_random_mn(nbytes,wordlist):
  261. hexrand = binascii.hexlify(get_random(nbytes))
  262. Vmsg('Seed: {}'.format(hexrand))
  263. for wl_id in ([wordlist],wordlists)[wordlist=='all']:
  264. if wordlist == 'all':
  265. Msg('{} mnemonic:'.format(capfirst(wl_id)))
  266. mn = baseconv.fromhex(hexrand,wl_id)
  267. Msg(' '.join(mn))
  268. def Mn_rand128(wordlist=dfl_wl_id): do_random_mn(16,wordlist)
  269. def Mn_rand192(wordlist=dfl_wl_id): do_random_mn(24,wordlist)
  270. def Mn_rand256(wordlist=dfl_wl_id): do_random_mn(32,wordlist)
  271. def Hex2mn(s,wordlist=dfl_wl_id): Msg(' '.join(baseconv.fromhex(s,wordlist)))
  272. def Mn2hex(s,wordlist=dfl_wl_id): Msg(baseconv.tohex(s.split(),wordlist))
  273. def Strtob58(s,pad=None): Msg(baseconv.fromhex(binascii.hexlify(s),'b58',pad,tostr=True))
  274. def Hextob58(s,pad=None): Msg(baseconv.fromhex(s,'b58',pad,tostr=True))
  275. def Hextob32(s,pad=None): Msg(baseconv.fromhex(s,'b32',pad,tostr=True))
  276. def B58tostr(s): Msg(binascii.unhexlify(baseconv.tohex(s,'b58')))
  277. def B58tohex(s,pad=None): Msg(baseconv.tohex(s,'b58',pad))
  278. def B32tohex(s,pad=None): Msg(baseconv.tohex(s.upper(),'b32',pad))
  279. from mmgen.seed import Mnemonic
  280. def Mn_stats(wordlist=dfl_wl_id):
  281. wordlist in baseconv.digits or die(1,"'{}': not a valid wordlist".format(wordlist))
  282. baseconv.check_wordlist(wordlist)
  283. def Mn_printlist(wordlist=dfl_wl_id):
  284. wordlist in baseconv.digits or die(1,"'{}': not a valid wordlist".format(wordlist))
  285. Msg('\n'.join(baseconv.digits[wordlist]))
  286. def Id8(infile):
  287. Msg(make_chksum_8(
  288. get_data_from_file(infile,dash=True,silent=True,binary=True)
  289. ))
  290. def Id6(infile):
  291. Msg(make_chksum_6(
  292. get_data_from_file(infile,dash=True,silent=True,binary=True)
  293. ))
  294. def Str2id6(s): # retain ignoring of space for backwards compat
  295. Msg(make_chksum_6(''.join(s.split())))
  296. def Addrfile_chksum(infile,mmtype=''):
  297. from mmgen.addr import AddrList
  298. mmtype = None if not mmtype else MMGenAddrType(mmtype)
  299. AddrList(infile,chksum_only=True,mmtype=mmtype)
  300. def Keyaddrfile_chksum(infile,mmtype=''):
  301. from mmgen.addr import KeyAddrList
  302. mmtype = None if not mmtype else MMGenAddrType(mmtype)
  303. KeyAddrList(infile,chksum_only=True,mmtype=mmtype)
  304. def Passwdfile_chksum(infile):
  305. from mmgen.addr import PasswordList
  306. PasswordList(infile=infile,chksum_only=True)
  307. def Hexreverse(s):
  308. Msg(binascii.hexlify(binascii.unhexlify(s.strip())[::-1]))
  309. def Hexlify(s):
  310. Msg(binascii.hexlify(s))
  311. def Hash256(s,file_input=False,hex_input=False):
  312. from hashlib import sha256
  313. if file_input: b = get_data_from_file(s,binary=True)
  314. elif hex_input: b = decode_pretty_hexdump(s)
  315. else: b = s
  316. Msg(sha256(sha256(b).digest()).hexdigest())
  317. def Encrypt(infile,outfile='',hash_preset=''):
  318. data = get_data_from_file(infile,'data for encryption',binary=True)
  319. enc_d = mmgen_encrypt(data,'user data',hash_preset)
  320. if not outfile:
  321. outfile = '{}.{}'.format(os.path.basename(infile),g.mmenc_ext)
  322. write_data_to_file(outfile,enc_d,'encrypted data',binary=True)
  323. def Decrypt(infile,outfile='',hash_preset=''):
  324. enc_d = get_data_from_file(infile,'encrypted data',binary=True)
  325. while True:
  326. dec_d = mmgen_decrypt(enc_d,'user data',hash_preset)
  327. if dec_d: break
  328. msg('Trying again...')
  329. if not outfile:
  330. o = os.path.basename(infile)
  331. outfile = remove_extension(o,g.mmenc_ext)
  332. if outfile == o: outfile += '.dec'
  333. write_data_to_file(outfile,dec_d,'decrypted data',binary=True)
  334. def Find_incog_data(filename,iv_id,keep_searching=False):
  335. ivsize,bsize,mod = g.aesctr_iv_len,4096,4096*8
  336. n,carry = 0,' '*ivsize
  337. flgs = os.O_RDONLY|os.O_BINARY if g.platform == 'win' else os.O_RDONLY
  338. f = os.open(filename,flgs)
  339. for ch in iv_id:
  340. if ch not in '0123456789ABCDEF':
  341. die(2,"'{}': invalid Incog ID".format(iv_id))
  342. while True:
  343. d = os.read(f,bsize)
  344. if not d: break
  345. d = carry + d
  346. for i in range(bsize):
  347. if sha256(d[i:i+ivsize]).hexdigest()[:8].upper() == iv_id:
  348. if n+i < ivsize: continue
  349. msg('\rIncog data for ID {} found at offset {}'.format(iv_id,n+i-ivsize))
  350. if not keep_searching: sys.exit(0)
  351. carry = d[len(d)-ivsize:]
  352. n += bsize
  353. if not n % mod:
  354. msg_r('\rSearched: {} bytes'.format(n))
  355. msg('')
  356. os.close(f)
  357. def Rand2file(outfile,nbytes,threads=4,silent=False):
  358. nbytes = parse_nbytes(nbytes)
  359. from Crypto import Random
  360. rh = Random.new()
  361. from Queue import Queue
  362. from threading import Thread
  363. bsize = 2**20
  364. roll = bsize * 4
  365. if opt.outdir: outfile = make_full_path(opt.outdir,outfile)
  366. f = open(outfile,'wb')
  367. from Crypto.Cipher import AES
  368. from Crypto.Util import Counter
  369. key = get_random(32)
  370. def encrypt_worker(wid):
  371. while True:
  372. i,d = q1.get()
  373. c = AES.new(key,AES.MODE_CTR,counter=Counter.new(g.aesctr_iv_len*8,initial_value=i))
  374. enc_data = c.encrypt(d)
  375. q2.put(enc_data)
  376. q1.task_done()
  377. def output_worker():
  378. while True:
  379. data = q2.get()
  380. f.write(data)
  381. q2.task_done()
  382. q1 = Queue()
  383. for i in range(max(1,threads-2)):
  384. t = Thread(target=encrypt_worker,args=(i,))
  385. t.daemon = True
  386. t.start()
  387. q2 = Queue()
  388. t = Thread(target=output_worker)
  389. t.daemon = True
  390. t.start()
  391. i = 1; rbytes = nbytes
  392. while rbytes > 0:
  393. d = rh.read(min(bsize,rbytes))
  394. q1.put((i,d))
  395. rbytes -= bsize
  396. i += 1
  397. if not (bsize*i) % roll:
  398. msg_r('\rRead: {} bytes'.format(bsize*i))
  399. if not silent:
  400. msg('\rRead: {} bytes'.format(nbytes))
  401. qmsg("\r{} bytes of random data written to file '{}'".format(nbytes,outfile))
  402. q1.join()
  403. q2.join()
  404. f.close()
  405. def Bytespec(s): Msg(str(parse_nbytes(s)))
  406. def Keyaddrlist2monerowallets(infile,blockheight=None,addrs=None):
  407. monero_wallet_ops(infile=infile,op='create',blockheight=blockheight,addrs=addrs)
  408. def Syncmonerowallets(infile,addrs=None):
  409. monero_wallet_ops(infile=infile,op='sync',addrs=addrs)
  410. def monero_wallet_ops(infile,op,blockheight=None,addrs=None):
  411. def run_cmd(cmd):
  412. import subprocess as sp
  413. p = sp.Popen(cmd,stdin=sp.PIPE,stdout=sp.PIPE,stderr=sp.PIPE)
  414. return p
  415. def test_rpc():
  416. p = run_cmd(['monero-wallet-cli','--version'])
  417. if p.stdout.read()[:6] != 'Monero':
  418. die(1,"Unable to run 'monero-wallet-cli'!")
  419. p = run_cmd(['monerod','status'])
  420. ret = p.stdout.read()
  421. if ret[:7] != 'Height:':
  422. die(1,'Unable to connect to monerod!')
  423. return int(ret[8:].split('/')[0])
  424. def my_expect(p,m,s,regex=False):
  425. if m: msg_r(' {}...'.format(m))
  426. ret = (p.expect_exact,p.expect)[regex](s)
  427. if not (ret == 0 or (type(s) == list and ret in (0,1))):
  428. die(2,"Expect failed: '{}' (return value: {})".format(s,ret))
  429. if m: msg('OK')
  430. return ret
  431. def my_sendline(p,m,s,usr_ret):
  432. if m: msg_r(' {}...'.format(m))
  433. ret = p.sendline(s)
  434. if ret != usr_ret:
  435. die(2,"Unable to send line '{}' (return value {})".format(s,ret))
  436. if m: msg('OK')
  437. vmsg("sendline: '{}' => {}".format(s,ret))
  438. def create(n,d,fn):
  439. try: os.stat(fn)
  440. except: pass
  441. else: die(1,"Wallet '{}' already exists!".format(fn))
  442. p = pexpect.spawn('monero-wallet-cli --generate-from-spend-key {}'.format(fn))
  443. my_expect(p,'Awaiting initial prompt','Secret spend key: ')
  444. my_sendline(p,'',d.sec,65)
  445. my_expect(p,'','Enter new wallet password: ')
  446. my_sendline(p,'Sending password',d.wallet_passwd,33)
  447. my_expect(p,'','Confirm password: ')
  448. my_sendline(p,'Sending password again',d.wallet_passwd,33)
  449. my_expect(p,'','of your choice: ')
  450. my_sendline(p,'','1',2)
  451. my_expect(p,'monerod generating wallet','Generated new wallet: ')
  452. my_expect(p,'','\n')
  453. if d.addr not in p.before:
  454. die(3,'Addresses do not match!\n MMGen: {}\n Monero: {}'.format(d.addr,p.before))
  455. my_expect(p,'','View key: ')
  456. my_expect(p,'','\n')
  457. if d.viewkey not in p.before:
  458. die(3,'View keys do not match!\n MMGen: {}\n Monero: {}'.format(d.viewkey,p.before))
  459. my_expect(p,'','(YYYY-MM-DD): ')
  460. h = str(blockheight or cur_height-1)
  461. my_sendline(p,'',h,len(h)+1)
  462. ret = my_expect(p,'',['Starting refresh','Still apply restore height? (Y/Yes/N/No): '])
  463. if ret == 1:
  464. my_sendline(p,'','Y',2)
  465. m = ' Warning: {}: blockheight argument is higher than current blockheight'
  466. ymsg(m.format(blockheight))
  467. elif blockheight != None:
  468. p.logfile = sys.stderr
  469. my_expect(p,'Syncing wallet','\[wallet.*$',regex=True)
  470. p.logfile = None
  471. my_sendline(p,'Exiting','exit',5)
  472. p.read()
  473. def sync(n,d,fn):
  474. try: os.stat(fn)
  475. except: die(1,"Wallet '{}' does not exist!".format(fn))
  476. p = pexpect.spawn('monero-wallet-cli --wallet-file={}'.format(fn))
  477. my_expect(p,'Awaiting password prompt','Wallet password: ')
  478. my_sendline(p,'Sending password',d.wallet_passwd,33)
  479. msg(' Starting refresh...')
  480. height = None
  481. while True:
  482. ret = p.expect([r' / .*',r'\[wallet.*:.*'])
  483. if ret == 0: # TODO: coverage
  484. height = p.after
  485. msg_r('\r Block {}{}'.format(p.before.split()[-1],height))
  486. elif ret == 1:
  487. if height:
  488. height = height.split()[-1]
  489. msg('\r Block {h} / {h}'.format(h=height))
  490. else:
  491. msg(' Wallet in sync')
  492. b = [l for l in p.before.splitlines() if l[:8] == 'Balance:'][0].split()
  493. msg(' Balance: {} Unlocked balance: {}'.format(b[1],b[4]))
  494. bals[0] += float(b[1][0:-1])
  495. bals[1] += float(b[4])
  496. my_sendline(p,'Exiting','exit',5)
  497. p.read()
  498. break
  499. else:
  500. die(2,"\nExpect failed: (return value: {})".format(ret))
  501. def process_wallets():
  502. m = { 'create': ('Creat','Generat',create,False),
  503. 'sync': ('Sync', 'Sync', sync, True) }
  504. opt.accept_defaults = opt.accept_defaults or m[op][3]
  505. from mmgen.protocol import init_coin
  506. init_coin('xmr')
  507. from mmgen.addr import AddrList
  508. al = KeyAddrList(infile)
  509. data = [d for d in al.data if addrs == None or d.idx in AddrIdxList(addrs)]
  510. dl = len(data)
  511. assert dl,"No addresses in addrfile within range '{}'".format(addrs)
  512. gmsg('\n{}ing {} wallet{}'.format(m[op][0],dl,suf(dl)))
  513. for n,d in enumerate(data): # [d.sec,d.wallet_passwd,d.viewkey,d.addr]
  514. fn = '{}{}-{}-MoneroWallet'.format(
  515. (opt.outdir+'/' if opt.outdir else ''),
  516. al.al_id.sid,
  517. d.idx)
  518. gmsg('\n{}ing wallet {}/{} ({})'.format(m[op][1],n+1,dl,fn))
  519. m[op][2](n,d,fn)
  520. gmsg('\n{} wallet{} {}ed'.format(dl,suf(dl),m[op][0].lower()))
  521. if op == 'sync':
  522. msg('Balance: {:.12f}, Unlocked balance: {:.12f}'.format(*bals))
  523. os.environ['LANG'] = 'C'
  524. import pexpect
  525. if blockheight != None and int(blockheight) < 0:
  526. blockheight = 0 # TODO: non-zero coverage
  527. cur_height = test_rpc()
  528. bals = [0.0,0.0] # locked,unlocked
  529. try:
  530. process_wallets()
  531. except KeyboardInterrupt:
  532. rdie(1,'\nUser interrupt\n')
  533. except EOFError:
  534. rdie(2,'\nEnd of file\n')
  535. except Exception as e:
  536. try:
  537. die(1,'Error: {}'.format(e[0]))
  538. except:
  539. rdie(1,'Error: {!r}'.format(e))
  540. # ================ RPC commands ================== #
  541. def Listaddress(addr,minconf=1,pager=False,showempty=True,showbtcaddr=True,show_age=False,show_days=None):
  542. return Listaddresses(addrs=addr,minconf=minconf,pager=pager,
  543. showempty=showempty,showbtcaddrs=showbtcaddr,show_age=show_age,show_days=show_days)
  544. # List MMGen addresses and their balances. TODO: move this code to AddrList
  545. def Listaddresses(addrs='',minconf=1,
  546. showempty=False,pager=False,showbtcaddrs=True,all_labels=False,sort=None,show_age=False,show_days=None):
  547. if show_days == None: show_days = False # user-set show_days triggers show_age
  548. else: show_age = True
  549. if sort:
  550. sort = set(sort.split(','))
  551. sort_params = set(['reverse','age'])
  552. if not sort.issubset(sort_params):
  553. die(1,"The sort option takes the following parameters: '{}'".format("','".join(sort_params)))
  554. rpc_init()
  555. def check_dup_mmid(acct_labels):
  556. mmid_prev,err = None,False
  557. for mmid in sorted(a.mmid for a in acct_labels if a):
  558. if mmid == mmid_prev:
  559. err = True
  560. msg('Duplicate MMGen ID ({}) discovered in tracking wallet!\n'.format(mmid))
  561. mmid_prev = mmid
  562. if err: rdie(3,'Tracking wallet is corrupted!')
  563. def check_addr_array_lens(acct_pairs):
  564. err = False
  565. for label,addrs in acct_pairs:
  566. if not label: continue
  567. if len(addrs) != 1:
  568. err = True
  569. if len(addrs) == 0:
  570. msg("Label '{}': has no associated address!".format(label))
  571. else:
  572. msg("'{}': more than one {} address in account!".format(addrs,g.coin))
  573. if err: rdie(3,'Tracking wallet is corrupted!')
  574. usr_addr_list = []
  575. if addrs:
  576. a = addrs.rsplit(':',1)
  577. if len(a) != 2:
  578. m = "'{}': invalid address list argument (must be in form <seed ID>:[<type>:]<idx list>)"
  579. die(1,m.format(addrs))
  580. usr_addr_list = [MMGenID('{}:{}'.format(a[0],i)) for i in AddrIdxList(a[1])]
  581. class TwAddrList(dict,MMGenObject): pass
  582. addrs = TwAddrList() # reusing name!
  583. total = g.proto.coin_amt('0')
  584. for d in g.rpch.listunspent(0):
  585. if not 'account' in d: continue # skip coinbase outputs with missing account
  586. if d['confirmations'] < minconf: continue
  587. label = TwLabel(d['account'],on_fail='silent')
  588. if label:
  589. if usr_addr_list and (label.mmid not in usr_addr_list): continue
  590. if label.mmid in addrs:
  591. if addrs[label.mmid]['addr'] != d['address']:
  592. die(2,'duplicate {} address ({}) for this MMGen address! ({})'.format(
  593. g.coin,d['address'],addrs[label.mmid]['addr']))
  594. else:
  595. addrs[label.mmid] = {'amt': g.proto.coin_amt('0'),
  596. 'lbl': label,
  597. 'addr': CoinAddr(d['address'])}
  598. addrs[label.mmid]['lbl'].mmid.confs = d['confirmations']
  599. addrs[label.mmid]['amt'] += d['amount']
  600. total += d['amount']
  601. # We use listaccounts only for empty addresses, as it shows false positive balances
  602. if showempty or all_labels:
  603. # for compatibility with old mmids, must use raw RPC rather than native data for matching
  604. # args: minconf,watchonly, MUST use keys() so we get list, not dict
  605. acct_list = g.rpch.listaccounts(0,True).keys() # raw list, no 'L'
  606. acct_labels = MMGenList([TwLabel(a,on_fail='silent') for a in acct_list])
  607. check_dup_mmid(acct_labels)
  608. acct_addrs = g.rpch.getaddressesbyaccount([[a] for a in acct_list],batch=True) # use raw list here
  609. assert len(acct_list) == len(acct_addrs),'listaccounts() and getaddressesbyaccount() not equal in length'
  610. addr_pairs = zip(acct_labels,acct_addrs)
  611. check_addr_array_lens(addr_pairs)
  612. for label,addr_arr in addr_pairs:
  613. if not label: continue
  614. if all_labels and not showempty and not label.comment: continue
  615. if usr_addr_list and (label.mmid not in usr_addr_list): continue
  616. if label.mmid not in addrs:
  617. addrs[label.mmid] = { 'amt':g.proto.coin_amt('0'), 'lbl':label, 'addr':'' }
  618. if showbtcaddrs:
  619. addrs[label.mmid]['addr'] = CoinAddr(addr_arr[0])
  620. if not addrs:
  621. die(0,('No tracked addresses with balances!','No tracked addresses!')[showempty])
  622. out = ([],[green('Chain: {}'.format(g.chain.upper()))])[g.chain in ('testnet','regtest')]
  623. fs = '{{mid}}{} {{cmt}} {{amt}}{}'.format(('',' {addr}')[showbtcaddrs],('',' {age}')[show_age])
  624. mmaddrs = [k for k in addrs.keys() if k.type == 'mmgen']
  625. max_mmid_len = max(len(k) for k in mmaddrs) + 2 if mmaddrs else 10
  626. max_cmt_len = max(max(len(v['lbl'].comment) for v in addrs.values()),7)
  627. addr_width = max(len(addrs[mmid]['addr']) for mmid in addrs)
  628. # pmsg([a.split('.')[1] for a in [str(v['amt']) for v in addrs.values()] if '.' in a])
  629. # fp: fractional part
  630. max_fp_len = max([len(a.split('.')[1]) for a in [str(v['amt']) for v in addrs.values()] if '.' in a] or [1])
  631. out += [fs.format(
  632. mid=MMGenID.fmtc('MMGenID',width=max_mmid_len),
  633. addr=CoinAddr.fmtc('ADDRESS',width=addr_width),
  634. cmt=TwComment.fmtc('COMMENT',width=max_cmt_len+1),
  635. amt='BALANCE'.ljust(max_fp_len+4),
  636. age=('CONFS','DAYS')[show_days],
  637. )]
  638. def sort_algo(j):
  639. if sort and 'age' in sort:
  640. return '{}_{:>012}_{}'.format(
  641. j.obj.rsplit(':',1)[0],
  642. (1000000000-j.confs if hasattr(j,'confs') else 0), # Hack, but OK for the foreseeable future
  643. j.sort_key)
  644. else:
  645. return j.sort_key
  646. al_id_save = None
  647. confs_per_day = 60*60*24 / g.proto.secs_per_block
  648. for mmid in sorted(addrs,key=sort_algo,reverse=bool(sort and 'reverse' in sort)):
  649. if mmid.type == 'mmgen':
  650. if al_id_save and al_id_save != mmid.obj.al_id:
  651. out.append('')
  652. al_id_save = mmid.obj.al_id
  653. mmid_disp = mmid
  654. else:
  655. if al_id_save:
  656. out.append('')
  657. al_id_save = None
  658. mmid_disp = 'Non-MMGen'
  659. e = addrs[mmid]
  660. out.append(fs.format(
  661. mid=MMGenID.fmtc(mmid_disp,width=max_mmid_len,color=True),
  662. addr=(e['addr'].fmt(color=True,width=addr_width) if showbtcaddrs else None),
  663. cmt=e['lbl'].comment.fmt(width=max_cmt_len,color=True,nullrepl='-'),
  664. amt=e['amt'].fmt('4.{}'.format(max(max_fp_len,3)),color=True),
  665. age=mmid.confs / (1,confs_per_day)[show_days] if hasattr(mmid,'confs') else '-'
  666. ))
  667. out.append('\nTOTAL: {} {}'.format(total.hl(color=True),g.coin))
  668. o = '\n'.join(out)
  669. return do_pager(o) if pager else Msg(o)
  670. def Getbalance(minconf=1,quiet=False,return_val=False):
  671. rpc_init()
  672. accts = {}
  673. for d in g.rpch.listunspent(0):
  674. ma = split2(d['account'] if 'account' in d else '')[0] # include coinbase outputs if spendable
  675. keys = ['TOTAL']
  676. if d['spendable']: keys += ['SPENDABLE']
  677. if is_mmgen_id(ma): keys += [ma.split(':')[0]]
  678. confs = d['confirmations']
  679. i = (1,2)[confs >= minconf]
  680. for key in keys:
  681. if key not in accts: accts[key] = [g.proto.coin_amt('0')] * 3
  682. for j in ([],[0])[confs==0] + [i]:
  683. accts[key][j] += d['amount']
  684. if quiet:
  685. o = ['{}'.format(accts['TOTAL'][2] if accts else g.proto.coin_amt('0'))]
  686. else:
  687. fs = '{:13} {} {} {}'
  688. mc,lbl = str(minconf),'confirms'
  689. o = [fs.format(
  690. 'Wallet',
  691. *[s.ljust(16) for s in ' Unconfirmed',' <{} {}'.format(mc,lbl),' >={} {}'.format(mc,lbl)])]
  692. for key in sorted(accts.keys()):
  693. o += [fs.format(key+':', *[a.fmt(color=True,suf=' '+g.coin) for a in accts[key]])]
  694. if 'SPENDABLE' in accts:
  695. Msg(red('Warning: this wallet contains PRIVATE KEYS for the SPENDABLE balance!'))
  696. o = '\n'.join(o)
  697. if return_val: return o
  698. else: Msg(o)
  699. def Txview(*infiles,**kwargs):
  700. from mmgen.filename import MMGenFileList
  701. pager = 'pager' in kwargs and kwargs['pager']
  702. terse = 'terse' in kwargs and kwargs['terse']
  703. sort_key = kwargs['sort'] if 'sort' in kwargs else 'mtime'
  704. flist = MMGenFileList(infiles,ftype=MMGenTX)
  705. flist.sort_by_age(key=sort_key) # in-place sort
  706. from mmgen.term import get_terminal_size
  707. sep = u'—'*77+'\n'
  708. out = sep.join([MMGenTX(fn).format_view(terse=terse) for fn in flist.names()])
  709. (Msg,do_pager)[pager](out.rstrip())
  710. def Twview(pager=False,reverse=False,wide=False,minconf=1,sort='age',show_days=True,show_mmid=True):
  711. rpc_init()
  712. from mmgen.tw import MMGenTrackingWallet
  713. tw = MMGenTrackingWallet(minconf=minconf)
  714. tw.do_sort(sort,reverse=reverse)
  715. tw.show_days = show_days
  716. tw.show_mmid = show_mmid
  717. out = tw.format_for_printing(color=True) if wide else tw.format_for_display()
  718. (Msg_r,do_pager)[pager](out)
  719. def Add_label(mmaddr_or_coin_addr,label):
  720. rpc_init()
  721. from mmgen.tw import MMGenTrackingWallet
  722. MMGenTrackingWallet.add_label(mmaddr_or_coin_addr,label) # dies on failure
  723. def Remove_label(mmaddr_or_coin_addr): Add_label(mmaddr_or_coin_addr,'')