util.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2017 Philemon <mmgen-py@yandex.com>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. util.py: Low-level routines imported by other modules in the MMGen suite
  20. """
  21. import sys,os,time,stat,re
  22. from hashlib import sha256
  23. from binascii import hexlify,unhexlify
  24. from string import hexdigits
  25. from mmgen.color import *
  26. def msg(s): sys.stderr.write(s.encode('utf8') + '\n')
  27. def msg_r(s): sys.stderr.write(s.encode('utf8'))
  28. def Msg(s): sys.stdout.write(s.encode('utf8') + '\n')
  29. def Msg_r(s): sys.stdout.write(s.encode('utf8'))
  30. def msgred(s): msg(red(s))
  31. def ymsg(s): msg(yellow(s))
  32. def ymsg_r(s): msg_r(yellow(s))
  33. def gmsg(s): msg(green(s))
  34. def gmsg_r(s): msg_r(green(s))
  35. def mmsg(*args):
  36. for d in args: Msg(repr(d))
  37. def mdie(*args):
  38. mmsg(*args); sys.exit(0)
  39. def die_wait(delay,ev=0,s=''):
  40. assert type(delay) == int
  41. assert type(ev) == int
  42. if s: msg(s)
  43. time.sleep(delay)
  44. sys.exit(ev)
  45. def die_pause(ev=0,s=''):
  46. assert type(ev) == int
  47. if s: msg(s)
  48. raw_input('Press ENTER to exit')
  49. sys.exit(ev)
  50. def die(ev=0,s=''):
  51. assert type(ev) == int
  52. if s: msg(s)
  53. sys.exit(ev)
  54. def Die(ev=0,s=''):
  55. assert type(ev) == int
  56. if s: Msg(s)
  57. sys.exit(ev)
  58. def rdie(ev=0,s=''): die(ev,red(s))
  59. def ydie(ev=0,s=''): die(ev,yellow(s))
  60. def hi(): sys.stdout.write(yellow('hi'))
  61. def pformat(d):
  62. import pprint
  63. return pprint.PrettyPrinter(indent=4).pformat(d)
  64. def pmsg(*args):
  65. if not args: return
  66. Msg(pformat(args if len(args) > 1 else args[0]))
  67. def pdie(*args):
  68. if not args: sys.exit(1)
  69. Die(1,(pformat(args if len(args) > 1 else args[0])))
  70. def set_for_type(val,refval,desc,invert_bool=False,src=None):
  71. src_str = (''," in '{}'".format(src))[bool(src)]
  72. if type(refval) == bool:
  73. v = str(val).lower()
  74. if v in ('true','yes','1'): ret = True
  75. elif v in ('false','no','none','0'): ret = False
  76. else: die(1,"'{}': invalid value for '{}'{} (must be of type '{}')".format(
  77. val,desc,src_str,'bool'))
  78. if invert_bool: ret = not ret
  79. else:
  80. try:
  81. ret = type(refval)((val,not val)[invert_bool])
  82. except:
  83. die(1,"'{}': invalid value for '{}'{} (must be of type '{}')".format(
  84. val,desc,src_str,type(refval).__name__))
  85. return ret
  86. # From 'man dd':
  87. # c=1, w=2, b=512, kB=1000, K=1024, MB=1000*1000, M=1024*1024,
  88. # GB=1000*1000*1000, G=1024*1024*1024, and so on for T, P, E, Z, Y.
  89. def parse_nbytes(nbytes):
  90. import re
  91. m = re.match(r'([0123456789]+)(.*)',nbytes)
  92. smap = ('c',1),('w',2),('b',512),('kB',1000),('K',1024),('MB',1000*1000),\
  93. ('M',1024*1024),('GB',1000*1000*1000),('G',1024*1024*1024)
  94. if m:
  95. if m.group(2):
  96. for k,v in smap:
  97. if k == m.group(2):
  98. return int(m.group(1)) * v
  99. else:
  100. msg("Valid byte specifiers: '%s'" % "' '".join([i[0] for i in smap]))
  101. else:
  102. return int(nbytes)
  103. die(1,"'%s': invalid byte specifier" % nbytes)
  104. def check_or_create_dir(path):
  105. try:
  106. os.listdir(path)
  107. except:
  108. try:
  109. os.makedirs(path,0700)
  110. except:
  111. die(2,"ERROR: unable to read or create path '{}'".format(path))
  112. from mmgen.opts import opt
  113. def qmsg(s,alt=None):
  114. if opt.quiet:
  115. if alt != None: msg(alt)
  116. else: msg(s)
  117. def qmsg_r(s,alt=None):
  118. if opt.quiet:
  119. if alt != None: msg_r(alt)
  120. else: msg_r(s)
  121. def vmsg(s,force=False):
  122. if opt.verbose or force: msg(s)
  123. def vmsg_r(s,force=False):
  124. if opt.verbose or force: msg_r(s)
  125. def Vmsg(s,force=False):
  126. if opt.verbose or force: Msg(s)
  127. def Vmsg_r(s,force=False):
  128. if opt.verbose or force: Msg_r(s)
  129. def dmsg(s):
  130. if opt.debug: msg(s)
  131. def suf(arg,suf_type='s'):
  132. suf_types = { 's': ('s',''), 'es': ('es','') }
  133. assert suf_type in suf_types
  134. t = type(arg)
  135. if t == int:
  136. n = arg
  137. elif any(issubclass(t,c) for c in (list,tuple,set,dict)):
  138. n = len(arg)
  139. else:
  140. die(2,'%s: invalid parameter for suf()' % arg)
  141. return suf_types[suf_type][n==1]
  142. def get_extension(f):
  143. a,b = os.path.splitext(f)
  144. return ('',b[1:])[len(b) > 1]
  145. def remove_extension(f,e):
  146. a,b = os.path.splitext(f)
  147. return (f,a)[len(b)>1 and b[1:]==e]
  148. def make_chksum_N(s,nchars,sep=False):
  149. if nchars%4 or not (4 <= nchars <= 64): return False
  150. s = sha256(sha256(s).digest()).hexdigest().upper()
  151. sep = ('',' ')[bool(sep)]
  152. return sep.join([s[i*4:i*4+4] for i in range(nchars/4)])
  153. def make_chksum_8(s,sep=False):
  154. from mmgen.obj import HexStr
  155. s = HexStr(sha256(sha256(s).digest()).hexdigest()[:8].upper(),case='upper')
  156. return '{} {}'.format(s[:4],s[4:]) if sep else s
  157. def make_chksum_6(s):
  158. from mmgen.obj import HexStr
  159. return HexStr(sha256(s).hexdigest()[:6])
  160. def is_chksum_6(s): return len(s) == 6 and is_hex_str_lc(s)
  161. def make_iv_chksum(s): return sha256(s).hexdigest()[:8].upper()
  162. def splitN(s,n,sep=None): # always return an n-element list
  163. ret = s.split(sep,n-1)
  164. return ret + ['' for i in range(n-len(ret))]
  165. def split2(s,sep=None): return splitN(s,2,sep) # always return a 2-element list
  166. def split3(s,sep=None): return splitN(s,3,sep) # always return a 3-element list
  167. def split_into_cols(col_wid,s):
  168. return ' '.join([s[col_wid*i:col_wid*(i+1)]
  169. for i in range(len(s)/col_wid+1)]).rstrip()
  170. def capfirst(s): # different from str.capitalize() - doesn't downcase any uc in string
  171. return s if len(s) == 0 else s[0].upper() + s[1:]
  172. def decode_timestamp(s):
  173. # with open('/etc/timezone') as f:
  174. # tz_save = f.read().rstrip()
  175. os.environ['TZ'] = 'UTC'
  176. ts = time.strptime(s,'%Y%m%d_%H%M%S')
  177. t = time.mktime(ts)
  178. # os.environ['TZ'] = tz_save
  179. return int(t)
  180. def make_timestamp(secs=None):
  181. t = int(secs) if secs else time.time()
  182. tv = time.gmtime(t)[:6]
  183. return '{:04d}{:02d}{:02d}_{:02d}{:02d}{:02d}'.format(*tv)
  184. def make_timestr(secs=None):
  185. t = int(secs) if secs else time.time()
  186. tv = time.gmtime(t)[:6]
  187. return '{:04d}/{:02d}/{:02d} {:02d}:{:02d}:{:02d}'.format(*tv)
  188. def secs_to_hms(secs):
  189. return '{:02d}:{:02d}:{:02d}'.format(secs/3600, (secs/60) % 60, secs % 60)
  190. def secs_to_ms(secs):
  191. return '{:02d}:{:02d}'.format(secs/60, secs % 60)
  192. def is_int(s):
  193. try:
  194. int(str(s))
  195. return True
  196. except:
  197. return False
  198. # https://en.wikipedia.org/wiki/Base32#RFC_4648_Base32_alphabet
  199. # https://tools.ietf.org/html/rfc4648
  200. def is_hex_str(s): return set(list(s.lower())) <= set(list(hexdigits.lower()))
  201. def is_hex_str_lc(s): return set(list(s)) <= set(list(hexdigits.lower()))
  202. def is_hex_str_uc(s): return set(list(s)) <= set(list(hexdigits.upper()))
  203. def is_b58_str(s): return set(list(s)) <= set(baseconv.digits['b58'])
  204. def is_b32_str(s): return set(list(s)) <= set(baseconv.digits['b32'])
  205. def is_ascii(s,enc='ascii'):
  206. try: s.decode(enc)
  207. except: return False
  208. else: return True
  209. def is_utf8(s): return is_ascii(s,enc='utf8')
  210. class baseconv(object):
  211. mn_base = 1626 # tirosh list is 1633 words long!
  212. digits = {
  213. 'electrum': tuple(__import__('mmgen.mn_electrum',fromlist=['words']).words.split()),
  214. 'tirosh': tuple(__import__('mmgen.mn_tirosh',fromlist=['words']).words.split()[:mn_base]),
  215. 'b58': tuple('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'),
  216. 'b32': tuple('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'),
  217. 'b16': tuple('0123456789abcdef'),
  218. 'b10': tuple('0123456789'),
  219. 'b8': tuple('01234567'),
  220. }
  221. wl_chksums = {
  222. 'electrum': '5ca31424',
  223. 'tirosh': '48f05e1f', # tirosh truncated to mn_base (1626)
  224. # 'tirosh1633': '1a5faeff'
  225. }
  226. b58pad_lens = [(16,22), (24,33), (32,44)]
  227. b58pad_lens_rev = [(v,k) for k,v in b58pad_lens]
  228. @classmethod
  229. def b58encode(cls,s,pad=None):
  230. pad = cls.get_pad(s,pad,'en',cls.b58pad_lens,[bytes])
  231. return ''.join(cls.fromhex(hexlify(s),'b58',pad=pad))
  232. @classmethod
  233. def b58decode(cls,s,pad=None):
  234. pad = cls.get_pad(s,pad,'de',cls.b58pad_lens_rev,[bytes,unicode])
  235. return unhexlify(cls.tohex(s,'b58',pad=pad*2 if pad else None))
  236. @staticmethod
  237. def get_pad(s,pad,op,pad_map,ok_types):
  238. m = "b58{}code() input must be one of {}, not '{}'"
  239. assert type(s) in ok_types, m.format(op,repr([t.__name__ for t in ok_types]),type(s).__name__)
  240. if pad:
  241. assert type(pad) == bool, "'pad' must be boolean type"
  242. d = dict(pad_map)
  243. assert len(s) in d, 'Invalid data length for b58{}code(pad=True)'.format(op)
  244. return d[len(s)]
  245. else:
  246. return None
  247. @classmethod
  248. def get_wordlist_chksum(cls,wl_id):
  249. return sha256(' '.join(cls.digits[wl_id])).hexdigest()[:8]
  250. @classmethod
  251. def check_wordlists(cls):
  252. for k,v in cls.wl_chksums.items(): assert cls.get_wordlist_chksum(k) == v
  253. @classmethod
  254. def check_wordlist(cls,wl_id):
  255. wl = baseconv.digits[wl_id]
  256. Msg('Wordlist: %s\nLength: %i words' % (capfirst(wl_id),len(wl)))
  257. new_chksum = cls.get_wordlist_chksum(wl_id)
  258. a,b = 'generated checksum','saved checksum'
  259. compare_chksums(new_chksum,a,cls.wl_chksums[wl_id],b,die_on_fail=True)
  260. Msg('Checksum %s matches' % new_chksum)
  261. Msg('List is sorted') if tuple(sorted(wl)) == wl else die(3,'ERROR: List is not sorted!')
  262. @classmethod
  263. def tohex(cls,words_arg,wl_id,pad=None):
  264. words = words_arg if type(words_arg) in (list,tuple) else tuple(words_arg.strip())
  265. wl = cls.digits[wl_id]
  266. base = len(wl)
  267. if not set(words) <= set(wl):
  268. die(2,'{} is not in {} (base{}) format'.format(repr(words_arg),wl_id,base))
  269. deconv = [wl.index(words[::-1][i])*(base**i) for i in range(len(words))]
  270. ret = ('{:0{w}x}'.format(sum(deconv),w=pad or 0))
  271. return ('','0')[len(ret) % 2] + ret
  272. @classmethod
  273. def fromhex(cls,hexnum,wl_id,pad=None):
  274. hexnum = hexnum.strip()
  275. if not is_hex_str(hexnum):
  276. die(2,"'%s': not a hexadecimal number" % hexnum)
  277. wl = cls.digits[wl_id]
  278. base = len(wl)
  279. num,ret = int(hexnum,16),[]
  280. while num:
  281. ret.append(num % base)
  282. num /= base
  283. return [wl[n] for n in [0] * ((pad or 0)-len(ret)) + ret[::-1]]
  284. baseconv.check_wordlists()
  285. def match_ext(addr,ext):
  286. return addr.split('.')[-1] == ext
  287. def file_exists(f):
  288. try:
  289. os.stat(f)
  290. return True
  291. except:
  292. return False
  293. def file_is_readable(f):
  294. from stat import S_IREAD
  295. try:
  296. assert os.stat(f).st_mode & S_IREAD
  297. except:
  298. return False
  299. else:
  300. return True
  301. def get_from_brain_opt_params():
  302. l,p = opt.from_brain.split(',')
  303. return(int(l),p)
  304. def pretty_hexdump(data,gw=2,cols=8,line_nums=False):
  305. r = (0,1)[bool(len(data) % gw)]
  306. return ''.join(
  307. [
  308. ('' if (line_nums == False or i % cols) else '{:06x}: '.format(i*gw)) +
  309. hexlify(data[i*gw:i*gw+gw]) + ('\n',' ')[bool((i+1) % cols)]
  310. for i in range(len(data)/gw + r)
  311. ]
  312. ).rstrip() + '\n'
  313. def decode_pretty_hexdump(data):
  314. from string import hexdigits
  315. pat = r'^[%s]+:\s+' % hexdigits
  316. lines = [re.sub(pat,'',l) for l in data.splitlines()]
  317. try:
  318. return unhexlify(''.join((''.join(lines).split())))
  319. except:
  320. msg('Data not in hexdump format')
  321. return False
  322. def strip_comments(line):
  323. return re.sub(ur'\s+$',u'',re.sub(ur'#.*',u'',line,1))
  324. def remove_comments(lines):
  325. return [m for m in [strip_comments(l) for l in lines] if m != '']
  326. from mmgen.globalvars import g
  327. def start_mscolor():
  328. try:
  329. import colorama
  330. colorama.init(strip=True,convert=True)
  331. except:
  332. msg('Import of colorama module failed')
  333. def get_hash_params(hash_preset):
  334. if hash_preset in g.hash_presets:
  335. return g.hash_presets[hash_preset] # N,p,r,buflen
  336. else: # Shouldn't be here
  337. die(3,"%s: invalid 'hash_preset' value" % hash_preset)
  338. def compare_chksums(chk1,desc1,chk2,desc2,hdr='',die_on_fail=False,verbose=False):
  339. if not chk1 == chk2:
  340. m = "%s ERROR: %s checksum (%s) doesn't match %s checksum (%s)"\
  341. % ((hdr+':\n ' if hdr else 'CHECKSUM'),desc2,chk2,desc1,chk1)
  342. if die_on_fail:
  343. die(3,m)
  344. else:
  345. vmsg(m,force=verbose)
  346. return False
  347. vmsg('%s checksum OK (%s)' % (capfirst(desc1),chk1))
  348. return True
  349. def compare_or_die(val1, desc1, val2, desc2, e='Error'):
  350. if cmp(val1,val2):
  351. die(3,"%s: %s (%s) doesn't match %s (%s)"
  352. % (e,desc2,val2,desc1,val1))
  353. dmsg('%s OK (%s)' % (capfirst(desc2),val2))
  354. return True
  355. def open_file_or_exit(filename,mode,silent=False):
  356. try:
  357. f = open(filename, mode)
  358. except:
  359. op = ('writing','reading')['r' in mode]
  360. die(2,("Unable to open file '{}' for {}".format(filename,op),'')[silent])
  361. return f
  362. def check_file_type_and_access(fname,ftype,blkdev_ok=False):
  363. a = ((os.R_OK,'read'),(os.W_OK,'writ'))
  364. access,m = a[ftype in ('output file','output directory')]
  365. ok_types = [
  366. (stat.S_ISREG,'regular file'),
  367. (stat.S_ISLNK,'symbolic link')
  368. ]
  369. if blkdev_ok: ok_types.append((stat.S_ISBLK,'block device'))
  370. if ftype == 'output directory': ok_types = [(stat.S_ISDIR, 'output directory')]
  371. try: mode = os.stat(fname).st_mode
  372. except:
  373. die(1,"Unable to stat requested %s '%s'" % (ftype,fname))
  374. for t in ok_types:
  375. if t[0](mode): break
  376. else:
  377. die(1,"Requested %s '%s' is not a %s" % (ftype,fname,
  378. ' or '.join([t[1] for t in ok_types])))
  379. if not os.access(fname, access):
  380. die(1,"Requested %s '%s' is not %sable by you" % (ftype,fname,m))
  381. return True
  382. def check_infile(f,blkdev_ok=False):
  383. return check_file_type_and_access(f,'input file',blkdev_ok=blkdev_ok)
  384. def check_outfile(f,blkdev_ok=False):
  385. return check_file_type_and_access(f,'output file',blkdev_ok=blkdev_ok)
  386. def check_outdir(f):
  387. return check_file_type_and_access(f,'output directory')
  388. def make_full_path(outdir,outfile):
  389. return os.path.normpath(os.path.join(outdir, os.path.basename(outfile)))
  390. def get_seed_file(cmd_args,nargs,invoked_as=None):
  391. from mmgen.filename import find_file_in_dir
  392. from mmgen.seed import Wallet
  393. wf = find_file_in_dir(Wallet,g.data_dir)
  394. wd_from_opt = bool(opt.hidden_incog_input_params or opt.in_fmt) # have wallet data from opt?
  395. import mmgen.opts as opts
  396. if len(cmd_args) + (wd_from_opt or bool(wf)) < nargs:
  397. if not wf:
  398. msg('No default wallet found, and no other seed source was specified')
  399. opts.usage()
  400. elif len(cmd_args) > nargs:
  401. opts.usage()
  402. elif len(cmd_args) == nargs and wf and invoked_as != 'gen':
  403. msg('Warning: overriding default wallet with user-supplied wallet')
  404. if cmd_args or wf:
  405. check_infile(cmd_args[0] if cmd_args else wf)
  406. return cmd_args[0] if cmd_args else (wf,None)[wd_from_opt]
  407. def get_new_passphrase(desc,passchg=False):
  408. w = '{}passphrase for {}'.format(('','new ')[bool(passchg)], desc)
  409. if opt.passwd_file:
  410. pw = ' '.join(get_words_from_file(opt.passwd_file,w))
  411. elif opt.echo_passphrase:
  412. pw = ' '.join(get_words_from_user('Enter {}: '.format(w)))
  413. else:
  414. for i in range(g.passwd_max_tries):
  415. pw = ' '.join(get_words_from_user('Enter {}: '.format(w)))
  416. pw2 = ' '.join(get_words_from_user('Repeat passphrase: '))
  417. dmsg('Passphrases: [%s] [%s]' % (pw,pw2))
  418. if pw == pw2:
  419. vmsg('Passphrases match'); break
  420. else: msg('Passphrases do not match. Try again.')
  421. else:
  422. die(2,'User failed to duplicate passphrase in %s attempts' %
  423. g.passwd_max_tries)
  424. if pw == '': qmsg('WARNING: Empty passphrase')
  425. return pw
  426. def confirm_or_exit(message,question,expect='YES',exit_msg='Exiting at user request'):
  427. m = message.strip()
  428. if m: msg(m)
  429. a = question+' ' if question[0].isupper() else \
  430. 'Are you sure you want to %s?\n' % question
  431. b = "Type uppercase '%s' to confirm: " % expect
  432. if my_raw_input(a+b).strip() != expect:
  433. die(1,exit_msg)
  434. def write_data_to_file(
  435. outfile,
  436. data,
  437. desc='data',
  438. ask_write=False,
  439. ask_write_prompt='',
  440. ask_write_default_yes=True,
  441. ask_overwrite=True,
  442. ask_tty=True,
  443. no_tty=False,
  444. silent=False,
  445. binary=False
  446. ):
  447. if silent: ask_tty = ask_overwrite = False
  448. if opt.quiet: ask_overwrite = False
  449. if ask_write_default_yes == False or ask_write_prompt:
  450. ask_write = True
  451. if not binary and type(data) == unicode:
  452. data = data.encode('utf8')
  453. def do_stdout():
  454. qmsg('Output to STDOUT requested')
  455. if sys.stdout.isatty():
  456. if no_tty:
  457. die(2,'Printing %s to screen is not allowed' % desc)
  458. if (ask_tty and not opt.quiet) or binary:
  459. confirm_or_exit('','output %s to screen' % desc)
  460. else:
  461. try: of = os.readlink('/proc/%d/fd/1' % os.getpid()) # Linux
  462. except: of = None # Windows
  463. if of:
  464. if of[:5] == 'pipe:':
  465. if no_tty:
  466. die(2,'Writing %s to pipe is not allowed' % desc)
  467. if ask_tty and not opt.quiet:
  468. confirm_or_exit('','output %s to pipe' % desc)
  469. msg('')
  470. of2,pd = os.path.relpath(of),os.path.pardir
  471. msg("Redirecting output to file '%s'" % (of2,of)[of2[:len(pd)] == pd])
  472. else:
  473. msg('Redirecting output to file')
  474. if binary and g.platform == 'win':
  475. import msvcrt
  476. msvcrt.setmode(sys.stdout.fileno(),os.O_BINARY)
  477. sys.stdout.write(data)
  478. def do_file(outfile,ask_write_prompt):
  479. if opt.outdir and not os.path.isabs(outfile):
  480. outfile = make_full_path(opt.outdir,outfile)
  481. if ask_write:
  482. if not ask_write_prompt: ask_write_prompt = 'Save %s?' % desc
  483. if not keypress_confirm(ask_write_prompt,
  484. default_yes=ask_write_default_yes):
  485. die(1,'%s not saved' % capfirst(desc))
  486. hush = False
  487. if file_exists(outfile) and ask_overwrite:
  488. q = "File '%s' already exists\nOverwrite?" % outfile
  489. confirm_or_exit('',q)
  490. msg("Overwriting file '%s'" % outfile)
  491. hush = True
  492. f = open_file_or_exit(outfile,('w','wb')[bool(binary)])
  493. try:
  494. f.write(data)
  495. except:
  496. die(2,"Failed to write %s to file '%s'" % (desc,outfile))
  497. f.close
  498. if not (hush or silent):
  499. msg("%s written to file '%s'" % (capfirst(desc),outfile))
  500. return True
  501. if opt.stdout or outfile in ('','-'):
  502. do_stdout()
  503. elif sys.stdin.isatty() and not sys.stdout.isatty():
  504. do_stdout()
  505. else:
  506. do_file(outfile,ask_write_prompt)
  507. def get_words_from_user(prompt):
  508. # split() also strips
  509. words = my_raw_input(prompt, echo=opt.echo_passphrase).split()
  510. dmsg('Sanitized input: [%s]' % ' '.join(words))
  511. return words
  512. def get_words_from_file(infile,desc,silent=False):
  513. if not silent:
  514. qmsg("Getting %s from file '%s'" % (desc,infile))
  515. f = open_file_or_exit(infile, 'r')
  516. # split() also strips
  517. words = f.read().split()
  518. f.close()
  519. dmsg('Sanitized input: [%s]' % ' '.join(words))
  520. return words
  521. def get_words(infile,desc,prompt):
  522. if infile:
  523. return get_words_from_file(infile,desc)
  524. else:
  525. return get_words_from_user(prompt)
  526. def mmgen_decrypt_file_maybe(fn,desc='',silent=False):
  527. d = get_data_from_file(fn,desc,binary=True,silent=silent)
  528. have_enc_ext = get_extension(fn) == g.mmenc_ext
  529. if have_enc_ext or not is_utf8(d):
  530. m = ('Attempting to decrypt','Decrypting')[have_enc_ext]
  531. msg("%s %s '%s'" % (m,desc,fn))
  532. from mmgen.crypto import mmgen_decrypt_retry
  533. d = mmgen_decrypt_retry(d,desc)
  534. return d
  535. def get_lines_from_file(fn,desc='',trim_comments=False,silent=False):
  536. dec = mmgen_decrypt_file_maybe(fn,desc,silent=silent)
  537. ret = dec.decode('utf8').splitlines() # DOS-safe
  538. if trim_comments: ret = remove_comments(ret)
  539. dmsg(u"Got {} lines from file '{}'".format(len(ret),fn))
  540. return ret
  541. def get_data_from_user(desc='data',silent=False):
  542. p = ('','Enter {}: '.format(desc))[g.stdin_tty]
  543. data = my_raw_input(p,echo=opt.echo_passphrase)
  544. dmsg('User input: [%s]' % data)
  545. return data
  546. def get_data_from_file(infile,desc='data',dash=False,silent=False,binary=False):
  547. if dash and infile == '-': return sys.stdin.read()
  548. if not opt.quiet and not silent and desc:
  549. qmsg("Getting %s from file '%s'" % (desc,infile))
  550. f = open_file_or_exit(infile,('r','rb')[bool(binary)],silent=silent)
  551. data = f.read()
  552. f.close()
  553. return data
  554. def pwfile_reuse_warning():
  555. if 'passwd_file_used' in globals():
  556. qmsg("Reusing passphrase from file '%s' at user request" % opt.passwd_file)
  557. return True
  558. globals()['passwd_file_used'] = True
  559. return False
  560. def get_mmgen_passphrase(desc,passchg=False):
  561. prompt ='Enter {}passphrase for {}: '.format(('','old ')[bool(passchg)],desc)
  562. if opt.passwd_file:
  563. pwfile_reuse_warning()
  564. return ' '.join(get_words_from_file(opt.passwd_file,'passphrase'))
  565. else:
  566. return ' '.join(get_words_from_user(prompt))
  567. def my_raw_input(prompt,echo=True,insert_txt='',use_readline=True):
  568. try: import readline
  569. except: use_readline = False # Windows
  570. if use_readline and sys.stdout.isatty():
  571. def st_hook(): readline.insert_text(insert_txt)
  572. readline.set_startup_hook(st_hook)
  573. else:
  574. msg_r(prompt)
  575. prompt = ''
  576. from mmgen.term import kb_hold_protect
  577. kb_hold_protect()
  578. if echo or not sys.stdin.isatty():
  579. reply = raw_input(prompt.encode('utf8'))
  580. else:
  581. from getpass import getpass
  582. reply = getpass(prompt)
  583. kb_hold_protect()
  584. return reply.strip()
  585. def keypress_confirm(prompt,default_yes=False,verbose=False,no_nl=False):
  586. from mmgen.term import get_char
  587. q = ('(y/N)','(Y/n)')[bool(default_yes)]
  588. p = '{} {}: '.format(prompt,q)
  589. nl = ('\n','\r{}\r'.format(' '*len(p)))[no_nl]
  590. while True:
  591. reply = get_char(p).strip('\n\r')
  592. if not reply:
  593. if default_yes: msg_r(nl); return True
  594. else: msg_r(nl); return False
  595. elif reply in 'yY': msg_r(nl); return True
  596. elif reply in 'nN': msg_r(nl); return False
  597. else:
  598. if verbose: msg('\nInvalid reply')
  599. else: msg_r('\r')
  600. def prompt_and_get_char(prompt,chars,enter_ok=False,verbose=False):
  601. from mmgen.term import get_char
  602. while True:
  603. reply = get_char('%s: ' % prompt).strip('\n\r')
  604. if reply in chars or (enter_ok and not reply):
  605. msg('')
  606. return reply
  607. if verbose: msg('\nInvalid reply')
  608. else: msg_r('\r')
  609. def do_pager(text):
  610. pagers,shell = ['less','more'],False
  611. # --- Non-MSYS Windows code deleted ---
  612. # raw, chop, horiz scroll 8 chars, disable buggy line chopping in MSYS
  613. os.environ['LESS'] = (('--shift 8 -RS'),('-cR -#1'))[g.platform=='win']
  614. if 'PAGER' in os.environ and os.environ['PAGER'] != pagers[0]:
  615. pagers = [os.environ['PAGER']] + pagers
  616. text = text.encode('utf8')
  617. for pager in pagers:
  618. end = ('\n(end of text)\n','')[pager=='less']
  619. try:
  620. from subprocess import Popen,PIPE,STDOUT
  621. p = Popen([pager], stdin=PIPE, shell=shell)
  622. except: pass
  623. else:
  624. p.communicate(text+end+'\n')
  625. msg_r('\r')
  626. break
  627. else: Msg(text+end)
  628. def do_license_msg(immed=False):
  629. if opt.quiet or g.no_license or opt.yes or not g.stdin_tty: return
  630. import mmgen.license as gpl
  631. p = "Press 'w' for conditions and warranty info, or 'c' to continue:"
  632. msg(gpl.warning)
  633. prompt = '%s ' % p.strip()
  634. from mmgen.term import get_char
  635. while True:
  636. reply = get_char(prompt, immed_chars=('','wc')[bool(immed)])
  637. if reply == 'w':
  638. do_pager(gpl.conditions)
  639. elif reply == 'c':
  640. msg(''); break
  641. else:
  642. msg_r('\r')
  643. msg('')
  644. def get_daemon_cfg_options(cfg_keys):
  645. cfg_file = os.path.join(g.proto.daemon_data_dir,g.proto.name+'.conf')
  646. try:
  647. cfg = dict([(k,v) for k,v in [
  648. split2(str(line).translate(None,'\t '),'=')
  649. for line in get_lines_from_file(cfg_file,'',silent=bool(opt.quiet))
  650. ] if k in cfg_keys])
  651. except:
  652. vmsg("Warning: '{}' does not exist or is unreadable".format(cfg_file))
  653. cfg = {}
  654. for k in set(cfg_keys) - set(cfg.keys()): cfg[k] = ''
  655. return cfg
  656. def get_coin_daemon_auth_cookie():
  657. f = os.path.join(g.proto.daemon_data_dir,g.proto.daemon_data_subdir,'.cookie')
  658. return get_lines_from_file(f,'')[0] if file_is_readable(f) else ''
  659. def rpc_init(reinit=False):
  660. if g.rpch != None and not reinit: return g.rpch
  661. def check_chainfork_mismatch(conn):
  662. block0 = conn.getblockhash(0)
  663. latest = conn.getblockcount()
  664. try:
  665. assert block0 == g.proto.block0,'Incorrect Genesis block for {}'.format(g.proto.__name__)
  666. for fork in g.proto.forks:
  667. if latest < fork[0]: break
  668. bhash = conn.getblockhash(fork[0])
  669. assert bhash == fork[1], (
  670. 'Bad block hash at fork block {}. Is this the {} chain?'.format(fork[0],fork[2].upper()))
  671. except Exception as e:
  672. die(2,"{}\n'{c}' requested, but this is not the {c} chain!".format(e,c=g.coin))
  673. def check_chaintype_mismatch():
  674. try:
  675. if g.regtest: assert g.chain == 'regtest','--regtest option selected, but chain is not regtest'
  676. if g.testnet: assert g.chain != 'mainnet','--testnet option selected, but chain is mainnet'
  677. if not g.testnet: assert g.chain == 'mainnet','mainnet selected, but chain is not mainnet'
  678. except Exception as e:
  679. die(1,'{}\nChain is {}!'.format(e,g.chain))
  680. cfg = get_daemon_cfg_options(('rpcuser','rpcpassword'))
  681. import mmgen.rpc
  682. conn = mmgen.rpc.CoinDaemonRPCConnection(
  683. g.rpc_host or 'localhost',
  684. g.rpc_port or g.proto.rpc_port,
  685. g.rpc_user or cfg['rpcuser'], # MMGen's rpcuser,rpcpassword override coin daemon's
  686. g.rpc_password or cfg['rpcpassword'],
  687. auth_cookie=get_coin_daemon_auth_cookie())
  688. if not g.daemon_version: # First call
  689. if g.bob or g.alice:
  690. import regtest as rt
  691. rt.user(('alice','bob')[g.bob],quiet=True)
  692. g.daemon_version = int(conn.getnetworkinfo()['version'])
  693. g.chain = conn.getblockchaininfo()['chain']
  694. if g.chain != 'regtest': g.chain += 'net'
  695. assert g.chain in g.chains
  696. check_chaintype_mismatch()
  697. if g.chain == 'mainnet': # skip this for testnet, as Genesis block may change
  698. check_chainfork_mismatch(conn)
  699. g.rpch = conn
  700. return conn