tool.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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]']),
  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)"]),
  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', ['<{} address> [str]'.format(pnm),'<label> [str]']),
  78. ('Remove_label', ['<{} 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)]']),
  87. ('Syncmonerowallets', ['<{} XMR key-address file> [str]'.format(pnm)]),
  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. Msg(g.proto.pubhash2addr(pubhash,at.addr_fmt=='p2sh'))
  237. def Addr2hexaddr(addr): Msg(g.proto.verify_addr(addr,CoinAddr.hex_width,return_dict=True)['hex'])
  238. def Hash160(pubkeyhex): Msg(hash160(pubkeyhex))
  239. def Pubhex2addr(pubkeyhex): Pubhash2addr(hash160(pubkeyhex))
  240. def Wif2hex(wif): Msg(PrivKey(wif=wif))
  241. def Hex2wif(hexpriv):
  242. Msg(g.proto.hex2wif(hexpriv,pubkey_type=at.pubkey_type,compressed=at.compressed))
  243. def Privhex2addr(privhex,output_pubhex=False):
  244. pk = PrivKey(binascii.unhexlify(privhex),compressed=at.compressed,pubkey_type=at.pubkey_type)
  245. ph = kg.to_pubhex(pk)
  246. Msg(ph if output_pubhex else ag.to_addr(ph))
  247. def Privhex2pubhex(privhex): # new
  248. Privhex2addr(privhex,output_pubhex=True)
  249. def Pubhex2redeem_script(pubhex): # new
  250. Msg(g.proto.pubhex2redeem_script(pubhex))
  251. def Wif2redeem_script(wif): # new
  252. privhex = PrivKey(wif=wif)
  253. Msg(ag.to_segwit_redeem_script(kg.to_pubhex(privhex)))
  254. wordlists = 'electrum','tirosh'
  255. dfl_wl_id = 'electrum'
  256. def do_random_mn(nbytes,wordlist):
  257. hexrand = binascii.hexlify(get_random(nbytes))
  258. Vmsg('Seed: {}'.format(hexrand))
  259. for wl_id in ([wordlist],wordlists)[wordlist=='all']:
  260. if wordlist == 'all':
  261. Msg('{} mnemonic:'.format(capfirst(wl_id)))
  262. mn = baseconv.fromhex(hexrand,wl_id)
  263. Msg(' '.join(mn))
  264. def Mn_rand128(wordlist=dfl_wl_id): do_random_mn(16,wordlist)
  265. def Mn_rand192(wordlist=dfl_wl_id): do_random_mn(24,wordlist)
  266. def Mn_rand256(wordlist=dfl_wl_id): do_random_mn(32,wordlist)
  267. def Hex2mn(s,wordlist=dfl_wl_id): Msg(' '.join(baseconv.fromhex(s,wordlist)))
  268. def Mn2hex(s,wordlist=dfl_wl_id): Msg(baseconv.tohex(s.split(),wordlist))
  269. def Strtob58(s,pad=None): Msg(baseconv.fromhex(binascii.hexlify(s),'b58',pad,tostr=True))
  270. def Hextob58(s,pad=None): Msg(baseconv.fromhex(s,'b58',pad,tostr=True))
  271. def Hextob32(s,pad=None): Msg(baseconv.fromhex(s,'b32',pad,tostr=True))
  272. def B58tostr(s): Msg(binascii.unhexlify(baseconv.tohex(s,'b58')))
  273. def B58tohex(s,pad=None): Msg(baseconv.tohex(s,'b58',pad))
  274. def B32tohex(s,pad=None): Msg(baseconv.tohex(s.upper(),'b32',pad))
  275. from mmgen.seed import Mnemonic
  276. def Mn_stats(wordlist=dfl_wl_id):
  277. wordlist in baseconv.digits or die(1,"'{}': not a valid wordlist".format(wordlist))
  278. baseconv.check_wordlist(wordlist)
  279. def Mn_printlist(wordlist=dfl_wl_id):
  280. wordlist in baseconv.digits or die(1,"'{}': not a valid wordlist".format(wordlist))
  281. Msg('\n'.join(baseconv.digits[wordlist]))
  282. def Id8(infile):
  283. Msg(make_chksum_8(
  284. get_data_from_file(infile,dash=True,silent=True,binary=True)
  285. ))
  286. def Id6(infile):
  287. Msg(make_chksum_6(
  288. get_data_from_file(infile,dash=True,silent=True,binary=True)
  289. ))
  290. def Str2id6(s): # retain ignoring of space for backwards compat
  291. Msg(make_chksum_6(''.join(s.split())))
  292. def Addrfile_chksum(infile,mmtype=''):
  293. from mmgen.addr import AddrList
  294. mmtype = None if not mmtype else MMGenAddrType(mmtype)
  295. AddrList(infile,chksum_only=True,mmtype=mmtype)
  296. def Keyaddrfile_chksum(infile,mmtype=''):
  297. from mmgen.addr import KeyAddrList
  298. mmtype = None if not mmtype else MMGenAddrType(mmtype)
  299. KeyAddrList(infile,chksum_only=True,mmtype=mmtype)
  300. def Passwdfile_chksum(infile):
  301. from mmgen.addr import PasswordList
  302. PasswordList(infile=infile,chksum_only=True)
  303. def Hexreverse(s):
  304. Msg(binascii.hexlify(binascii.unhexlify(s.strip())[::-1]))
  305. def Hexlify(s):
  306. Msg(binascii.hexlify(s))
  307. def Hash256(s,file_input=False,hex_input=False):
  308. from hashlib import sha256
  309. if file_input: b = get_data_from_file(s,binary=True)
  310. elif hex_input: b = decode_pretty_hexdump(s)
  311. else: b = s
  312. Msg(sha256(sha256(b).digest()).hexdigest())
  313. def Encrypt(infile,outfile='',hash_preset=''):
  314. data = get_data_from_file(infile,'data for encryption',binary=True)
  315. enc_d = mmgen_encrypt(data,'user data',hash_preset)
  316. if not outfile:
  317. outfile = '{}.{}'.format(os.path.basename(infile),g.mmenc_ext)
  318. write_data_to_file(outfile,enc_d,'encrypted data',binary=True)
  319. def Decrypt(infile,outfile='',hash_preset=''):
  320. enc_d = get_data_from_file(infile,'encrypted data',binary=True)
  321. while True:
  322. dec_d = mmgen_decrypt(enc_d,'user data',hash_preset)
  323. if dec_d: break
  324. msg('Trying again...')
  325. if not outfile:
  326. o = os.path.basename(infile)
  327. outfile = remove_extension(o,g.mmenc_ext)
  328. if outfile == o: outfile += '.dec'
  329. write_data_to_file(outfile,dec_d,'decrypted data',binary=True)
  330. def Find_incog_data(filename,iv_id,keep_searching=False):
  331. ivsize,bsize,mod = g.aesctr_iv_len,4096,4096*8
  332. n,carry = 0,' '*ivsize
  333. flgs = os.O_RDONLY|os.O_BINARY if g.platform == 'win' else os.O_RDONLY
  334. f = os.open(filename,flgs)
  335. for ch in iv_id:
  336. if ch not in '0123456789ABCDEF':
  337. die(2,"'{}': invalid Incog ID".format(iv_id))
  338. while True:
  339. d = os.read(f,bsize)
  340. if not d: break
  341. d = carry + d
  342. for i in range(bsize):
  343. if sha256(d[i:i+ivsize]).hexdigest()[:8].upper() == iv_id:
  344. if n+i < ivsize: continue
  345. msg('\rIncog data for ID {} found at offset {}'.format(iv_id,n+i-ivsize))
  346. if not keep_searching: sys.exit(0)
  347. carry = d[len(d)-ivsize:]
  348. n += bsize
  349. if not n % mod:
  350. msg_r('\rSearched: {} bytes'.format(n))
  351. msg('')
  352. os.close(f)
  353. def Rand2file(outfile,nbytes,threads=4,silent=False):
  354. nbytes = parse_nbytes(nbytes)
  355. from Crypto import Random
  356. rh = Random.new()
  357. from Queue import Queue
  358. from threading import Thread
  359. bsize = 2**20
  360. roll = bsize * 4
  361. if opt.outdir: outfile = make_full_path(opt.outdir,outfile)
  362. f = open(outfile,'wb')
  363. from Crypto.Cipher import AES
  364. from Crypto.Util import Counter
  365. key = get_random(32)
  366. def encrypt_worker(wid):
  367. while True:
  368. i,d = q1.get()
  369. c = AES.new(key,AES.MODE_CTR,counter=Counter.new(g.aesctr_iv_len*8,initial_value=i))
  370. enc_data = c.encrypt(d)
  371. q2.put(enc_data)
  372. q1.task_done()
  373. def output_worker():
  374. while True:
  375. data = q2.get()
  376. f.write(data)
  377. q2.task_done()
  378. q1 = Queue()
  379. for i in range(max(1,threads-2)):
  380. t = Thread(target=encrypt_worker,args=(i,))
  381. t.daemon = True
  382. t.start()
  383. q2 = Queue()
  384. t = Thread(target=output_worker)
  385. t.daemon = True
  386. t.start()
  387. i = 1; rbytes = nbytes
  388. while rbytes > 0:
  389. d = rh.read(min(bsize,rbytes))
  390. q1.put((i,d))
  391. rbytes -= bsize
  392. i += 1
  393. if not (bsize*i) % roll:
  394. msg_r('\rRead: {} bytes'.format(bsize*i))
  395. if not silent:
  396. msg('\rRead: {} bytes'.format(nbytes))
  397. qmsg("\r{} bytes of random data written to file '{}'".format(nbytes,outfile))
  398. q1.join()
  399. q2.join()
  400. f.close()
  401. def Bytespec(s): Msg(str(parse_nbytes(s)))
  402. def Syncmonerowallets(infile): monero_wallet_ops(infile=infile,op='sync')
  403. def Keyaddrlist2monerowallets(infile,blockheight=None):
  404. monero_wallet_ops(infile=infile,op='create',blockheight=blockheight)
  405. def monero_wallet_ops(infile,op,blockheight=None):
  406. def run_cmd(cmd):
  407. import subprocess as sp
  408. p = sp.Popen(cmd,stdin=sp.PIPE,stdout=sp.PIPE,stderr=sp.PIPE)
  409. return p
  410. def test_rpc():
  411. p = run_cmd(['monero-wallet-cli','--version'])
  412. if p.stdout.read()[:6] != 'Monero':
  413. die(1,"Unable to run 'monero-wallet-cli'!")
  414. p = run_cmd(['monerod','status'])
  415. ret = p.stdout.read()
  416. if ret[:7] != 'Height:':
  417. die(1,'Unable to connect to monerod!')
  418. return int(ret[8:].split('/')[0])
  419. def my_expect(p,m,s,regex=False):
  420. if m: msg_r(' {}...'.format(m))
  421. ret = (p.expect_exact,p.expect)[regex](s)
  422. if not (ret == 0 or (type(s) == list and ret in (0,1))):
  423. die(2,"Expect failed: '{}' (return value: {})".format(s,ret))
  424. if m: msg('OK')
  425. return ret
  426. def my_sendline(p,m,s,usr_ret):
  427. if m: msg_r(' {}...'.format(m))
  428. ret = p.sendline(s)
  429. if ret != usr_ret:
  430. die(2,"Unable to send line '{}' (return value {})".format(s,ret))
  431. if m: msg('OK')
  432. vmsg("sendline: '{}' => {}".format(s,ret))
  433. def create(n,d,fn):
  434. try: os.stat(fn)
  435. except: pass
  436. else: die(1,"Wallet '{}' already exists!".format(fn))
  437. p = pexpect.spawn('monero-wallet-cli --generate-from-spend-key {}'.format(fn))
  438. my_expect(p,'Awaiting initial prompt','Secret spend key: ')
  439. my_sendline(p,'',d.sec,65)
  440. my_expect(p,'','Enter new wallet password: ')
  441. my_sendline(p,'Sending password',d.wallet_passwd,33)
  442. my_expect(p,'','Confirm password: ')
  443. my_sendline(p,'Sending password again',d.wallet_passwd,33)
  444. my_expect(p,'','of your choice: ')
  445. my_sendline(p,'','1',2)
  446. my_expect(p,'monerod generating wallet','Generated new wallet: ')
  447. my_expect(p,'','\n')
  448. if d.addr not in p.before:
  449. die(3,'Addresses do not match!\n MMGen: {}\n Monero: {}'.format(d.addr,p.before))
  450. my_expect(p,'','View key: ')
  451. my_expect(p,'','\n')
  452. if d.viewkey not in p.before:
  453. die(3,'View keys do not match!\n MMGen: {}\n Monero: {}'.format(d.viewkey,p.before))
  454. my_expect(p,'','(YYYY-MM-DD): ')
  455. h = str(blockheight or cur_height-1)
  456. my_sendline(p,'',h,len(h)+1)
  457. ret = my_expect(p,'',['Starting refresh','Still apply restore height? (Y/Yes/N/No): '])
  458. if ret == 1:
  459. my_sendline(p,'','Y',2)
  460. m = ' Warning: {}: blockheight argument is higher than current blockheight'
  461. ymsg(m.format(blockheight))
  462. elif blockheight != None:
  463. p.logfile = sys.stderr
  464. my_expect(p,'Syncing wallet','\[wallet.*$',regex=True)
  465. p.logfile = None
  466. my_sendline(p,'Exiting','exit',5)
  467. p.read()
  468. def sync(n,d,fn):
  469. try: os.stat(fn)
  470. except: die(1,"Wallet '{}' does not exist!".format(fn))
  471. p = pexpect.spawn('monero-wallet-cli --wallet-file={}'.format(fn))
  472. my_expect(p,'Awaiting password prompt','Wallet password: ')
  473. my_sendline(p,'Sending password',d.wallet_passwd,33)
  474. msg(' Starting refresh...')
  475. height = None
  476. while True:
  477. ret = p.expect([r' / .*',r'\[wallet.*:.*'])
  478. if ret == 0: # TODO: coverage
  479. height = p.after
  480. msg_r('\r Block {}{}'.format(p.before.split()[-1],height))
  481. elif ret == 1:
  482. if height:
  483. height = height.split()[-1]
  484. msg('\r Block {h} / {h}'.format(h=height))
  485. else:
  486. msg(' Wallet in sync')
  487. msg(' '+[l for l in p.before.splitlines() if l[:8] == 'Balance:'][0])
  488. my_sendline(p,'Exiting','exit',5)
  489. p.read()
  490. break
  491. else:
  492. die(2,"\nExpect failed: (return value: {})".format(ret))
  493. def process_wallets():
  494. m = { 'create': ('Creat','Generat',create,False),
  495. 'sync': ('Sync', 'Sync', sync, True) }
  496. opt.accept_defaults = opt.accept_defaults or m[op][3]
  497. from mmgen.protocol import init_coin
  498. init_coin('xmr')
  499. from mmgen.addr import AddrList
  500. al = KeyAddrList(infile)
  501. dl = len(al.data)
  502. gmsg('\n{}ing {} wallet{}'.format(m[op][0],dl,suf(dl)))
  503. for n,d in enumerate(al.data): # [d.sec,d.wallet_passwd,d.viewkey,d.addr]
  504. fn = '{}{}-{}-MoneroWallet'.format(
  505. (opt.outdir+'/' if opt.outdir else ''),
  506. al.al_id.sid,
  507. d.idx)
  508. gmsg('\n{}ing wallet {}/{} ({})'.format(m[op][1],n+1,dl,fn))
  509. m[op][2](n,d,fn)
  510. gmsg('\n{} wallet{} {}ed'.format(dl,suf(dl),m[op][0].lower()))
  511. os.environ['LANG'] = 'C'
  512. import pexpect
  513. if blockheight != None and int(blockheight) < 0: blockheight = 0 # TODO: non-zero coverage
  514. cur_height = test_rpc()
  515. try:
  516. process_wallets()
  517. except KeyboardInterrupt:
  518. rdie(1,'\nUser interrupt\n')
  519. except EOFError:
  520. rdie(2,'\nEnd of file\n')
  521. except Exception as e:
  522. rdie(1,'Program died: {!r}'.format(e))
  523. # ================ RPC commands ================== #
  524. def Listaddress(addr,minconf=1,pager=False,showempty=True,showbtcaddr=True):
  525. return Listaddresses(addrs=addr,minconf=minconf,pager=pager,showempty=showempty,showbtcaddrs=showbtcaddr)
  526. # List MMGen addresses and their balances. TODO: move this code to AddrList
  527. def Listaddresses(addrs='',minconf=1,showempty=False,pager=False,showbtcaddrs=True,all_labels=False,sort=None):
  528. if sort:
  529. sort = set(sort.split(','))
  530. sort_params = set(['reverse','age'])
  531. if not sort.issubset(sort_params):
  532. die(1,"The sort option takes the following parameters: '{}'".format("','".join(sort_params)))
  533. rpc_init()
  534. def check_dup_mmid(acct_labels):
  535. mmid_prev,err = None,False
  536. for mmid in sorted(a.mmid for a in acct_labels if a):
  537. if mmid == mmid_prev:
  538. err = True
  539. msg('Duplicate MMGen ID ({}) discovered in tracking wallet!\n'.format(mmid))
  540. mmid_prev = mmid
  541. if err: rdie(3,'Tracking wallet is corrupted!')
  542. def check_addr_array_lens(acct_pairs):
  543. err = False
  544. for label,addrs in acct_pairs:
  545. if not label: continue
  546. if len(addrs) != 1:
  547. err = True
  548. if len(addrs) == 0:
  549. msg("Label '{}': has no associated address!".format(label))
  550. else:
  551. msg("'{}': more than one {} address in account!".format(addrs,g.coin))
  552. if err: rdie(3,'Tracking wallet is corrupted!')
  553. usr_addr_list = []
  554. if addrs:
  555. a = addrs.rsplit(':',1)
  556. if len(a) != 2:
  557. m = "'{}': invalid address list argument (must be in form <seed ID>:[<type>:]<idx list>)"
  558. die(1,m.format(addrs))
  559. usr_addr_list = [MMGenID('{}:{}'.format(a[0],i)) for i in AddrIdxList(a[1])]
  560. class TwAddrList(dict,MMGenObject): pass
  561. addrs = TwAddrList() # reusing name!
  562. total = g.proto.coin_amt('0')
  563. for d in g.rpch.listunspent(0):
  564. if not 'account' in d: continue # skip coinbase outputs with missing account
  565. if d['confirmations'] < minconf: continue
  566. label = TwLabel(d['account'],on_fail='silent')
  567. if label:
  568. if usr_addr_list and (label.mmid not in usr_addr_list): continue
  569. if label.mmid in addrs:
  570. if addrs[label.mmid]['addr'] != d['address']:
  571. die(2,'duplicate {} address ({}) for this MMGen address! ({})'.format(
  572. g.coin,d['address'],addrs[label.mmid]['addr']))
  573. else:
  574. addrs[label.mmid] = {'amt': g.proto.coin_amt('0'),
  575. 'lbl': label,
  576. 'addr': CoinAddr(d['address'])}
  577. addrs[label.mmid]['lbl'].mmid.confs = d['confirmations']
  578. addrs[label.mmid]['amt'] += d['amount']
  579. total += d['amount']
  580. # We use listaccounts only for empty addresses, as it shows false positive balances
  581. if showempty or all_labels:
  582. # for compatibility with old mmids, must use raw RPC rather than native data for matching
  583. # args: minconf,watchonly, MUST use keys() so we get list, not dict
  584. acct_list = g.rpch.listaccounts(0,True).keys() # raw list, no 'L'
  585. acct_labels = MMGenList([TwLabel(a,on_fail='silent') for a in acct_list])
  586. check_dup_mmid(acct_labels)
  587. acct_addrs = g.rpch.getaddressesbyaccount([[a] for a in acct_list],batch=True) # use raw list here
  588. assert len(acct_list) == len(acct_addrs),'listaccounts() and getaddressesbyaccount() not equal in length'
  589. addr_pairs = zip(acct_labels,acct_addrs)
  590. check_addr_array_lens(addr_pairs)
  591. for label,addr_arr in addr_pairs:
  592. if not label: continue
  593. if all_labels and not showempty and not label.comment: continue
  594. if usr_addr_list and (label.mmid not in usr_addr_list): continue
  595. if label.mmid not in addrs:
  596. addrs[label.mmid] = { 'amt':g.proto.coin_amt('0'), 'lbl':label, 'addr':'' }
  597. if showbtcaddrs:
  598. addrs[label.mmid]['addr'] = CoinAddr(addr_arr[0])
  599. if not addrs:
  600. die(0,('No tracked addresses with balances!','No tracked addresses!')[showempty])
  601. out = ([],[green('Chain: {}'.format(g.chain.upper()))])[g.chain in ('testnet','regtest')]
  602. fs = ('{mid} {cmt} {amt}','{mid} {addr} {cmt} {amt}')[showbtcaddrs]
  603. mmaddrs = [k for k in addrs.keys() if k.type == 'mmgen']
  604. max_mmid_len = max(len(k) for k in mmaddrs) + 2 if mmaddrs else 10
  605. max_cmt_len = max(max(len(addrs[k]['lbl'].comment) for k in addrs),7)
  606. out += [fs.format(
  607. mid=MMGenID.fmtc('MMGenID',width=max_mmid_len),
  608. addr=CoinAddr.fmtc('ADDRESS'),
  609. cmt=TwComment.fmtc('COMMENT',width=max_cmt_len),
  610. amt='BALANCE'
  611. )]
  612. def sort_algo(j):
  613. if sort and 'age' in sort:
  614. return '{}_{:>012}_{}'.format(
  615. j.obj.rsplit(':',1)[0],
  616. (1000000000-j.confs if hasattr(j,'confs') else 0), # Hack, but OK for the foreseeable future
  617. j.sort_key)
  618. else:
  619. return j.sort_key
  620. al_id_save = None
  621. for mmid in sorted(addrs,key=sort_algo,reverse=bool(sort and 'reverse' in sort)):
  622. if mmid.type == 'mmgen':
  623. if al_id_save and al_id_save != mmid.obj.al_id:
  624. out.append('')
  625. al_id_save = mmid.obj.al_id
  626. mmid_disp = mmid
  627. else:
  628. if al_id_save:
  629. out.append('')
  630. al_id_save = None
  631. mmid_disp = 'Non-MMGen'
  632. out.append(fs.format(
  633. mid = MMGenID.fmtc(mmid_disp,width=max_mmid_len,color=True),
  634. addr=(addrs[mmid]['addr'].fmt(color=True) if showbtcaddrs else None),
  635. cmt=addrs[mmid]['lbl'].comment.fmt(width=max_cmt_len,color=True,nullrepl='-'),
  636. amt=addrs[mmid]['amt'].fmt('3.0',color=True)))
  637. out.append('\nTOTAL: {} {}'.format(total.hl(color=True),g.coin))
  638. o = '\n'.join(out)
  639. return do_pager(o) if pager else Msg(o)
  640. def Getbalance(minconf=1,quiet=False,return_val=False):
  641. rpc_init()
  642. accts = {}
  643. for d in g.rpch.listunspent(0):
  644. ma = split2(d['account'] if 'account' in d else '')[0] # include coinbase outputs if spendable
  645. keys = ['TOTAL']
  646. if d['spendable']: keys += ['SPENDABLE']
  647. if is_mmgen_id(ma): keys += [ma.split(':')[0]]
  648. confs = d['confirmations']
  649. i = (1,2)[confs >= minconf]
  650. for key in keys:
  651. if key not in accts: accts[key] = [g.proto.coin_amt('0')] * 3
  652. for j in ([],[0])[confs==0] + [i]:
  653. accts[key][j] += d['amount']
  654. if quiet:
  655. o = ['{}'.format(accts['TOTAL'][2] if accts else g.proto.coin_amt('0'))]
  656. else:
  657. fs = '{:13} {} {} {}'
  658. mc,lbl = str(minconf),'confirms'
  659. o = [fs.format(
  660. 'Wallet',
  661. *[s.ljust(16) for s in ' Unconfirmed',' <{} {}'.format(mc,lbl),' >={} {}'.format(mc,lbl)])]
  662. for key in sorted(accts.keys()):
  663. o += [fs.format(key+':', *[a.fmt(color=True,suf=' '+g.coin) for a in accts[key]])]
  664. if 'SPENDABLE' in accts:
  665. Msg(red('Warning: this wallet contains PRIVATE KEYS for the SPENDABLE balance!'))
  666. o = '\n'.join(o)
  667. if return_val: return o
  668. else: Msg(o)
  669. def Txview(*infiles,**kwargs):
  670. from mmgen.filename import MMGenFileList
  671. pager = 'pager' in kwargs and kwargs['pager']
  672. terse = 'terse' in kwargs and kwargs['terse']
  673. sort_key = kwargs['sort'] if 'sort' in kwargs else 'mtime'
  674. flist = MMGenFileList(infiles,ftype=MMGenTX)
  675. flist.sort_by_age(key=sort_key) # in-place sort
  676. from mmgen.term import get_terminal_size
  677. sep = u'—'*77+'\n'
  678. out = sep.join([MMGenTX(fn).format_view(terse=terse) for fn in flist.names()])
  679. (Msg,do_pager)[pager](out.rstrip())
  680. def Twview(pager=False,reverse=False,wide=False,minconf=1,sort='age',show_days=True,show_mmid=True):
  681. rpc_init()
  682. from mmgen.tw import MMGenTrackingWallet
  683. tw = MMGenTrackingWallet(minconf=minconf)
  684. tw.do_sort(sort,reverse=reverse)
  685. tw.show_days = show_days
  686. tw.show_mmid = show_mmid
  687. out = tw.format_for_printing(color=True) if wide else tw.format_for_display()
  688. (Msg_r,do_pager)[pager](out)
  689. def Add_label(mmaddr,label):
  690. rpc_init()
  691. from mmgen.tw import MMGenTrackingWallet
  692. MMGenTrackingWallet.add_label(mmaddr,label) # dies on failure
  693. def Remove_label(mmaddr): Add_label(mmaddr,'')