Browse Source

mmgen-autosign: --coin support, validate that secret is well-formed

philemon 7 years ago
parent
commit
2435f60b03
1 changed files with 32 additions and 25 deletions
  1. 32 25
      scripts/mmgen-autosign

+ 32 - 25
scripts/mmgen-autosign

@@ -34,6 +34,7 @@ opts_data = lambda: {
 -l, --led            Use status LED to signal standby, busy and error
 -s, --stealth-led    Stealth LED mode - signal busy and error only, and only
                      after successful authorization.
+-v, --verbose        Produce more verbose output
 """,
 	'notes': """
 
@@ -90,10 +91,13 @@ tx_dir       = os.path.join(mountpoint,'tx')
 part_label   = 'MMGEN_TX'
 shm_dir      = '/dev/shm'
 secret_fn    = 'txsign-secret'
-tn_arg       = ([],['--testnet=1'])[bool(opt.testnet)]
+tn_arg       = ['--testnet={}'.format(opt.testnet or '0')]
+coin_arg     = ['--coin={}'.format(opt.coin or 'btc')]
 
 def check_daemon_running():
-	try: subprocess.check_output(['mmgen-tool'] + tn_arg + ['getbalance'])
+	cmd = ['mmgen-tool'] + tn_arg + coin_arg + ['getbalance']
+	vmsg('Executing: {}'.format(' '.join(cmd)))
+	try: subprocess.check_output(cmd)
 	except: die(1,'Daemon not running')
 
 def get_wallet_files():
@@ -103,11 +107,13 @@ def get_wallet_files():
 	return [os.path.join(shm_dir,w) for w in wfs]
 
 def get_secret_in_dir(d,on_fail='die'):
+	fn = os.path.join(d,secret_fn)
 	try:
-		with open(os.path.join(d,secret_fn)) as f:
-			return f.read().rstrip()
+		with open(fn) as f: ret = f.read().rstrip()
+		assert is_hex_str(ret) and len(ret) == 32
+		return ret
 	except:
-		msg('Unable to read secret file!')
+		msg("Secret file '{}' non-existent, unreadable or in incorrect format!".format(fn))
 		if on_fail == 'die': sys.exit(1)
 		else:                return None
 
@@ -135,7 +141,8 @@ def sign():
 	unsigned = [os.path.join(tx_dir,f) for f in raw if f[:-6] not in signed]
 
 	if unsigned:
-		cmd = ['mmgen-txsign','--yes','--outdir='+tx_dir] + tn_arg + unsigned + wfs
+		cmd = ['mmgen-txsign','--yes','--outdir='+tx_dir] + tn_arg + coin_arg + unsigned + wfs
+		vmsg('Executing: {}'.format(' '.join(cmd)))
 		ret = subprocess.call(cmd)
 		msg('')
 		time.sleep(0.3)
@@ -156,7 +163,7 @@ def wipe_existing_secret_files():
 			msg('\nWiping existing key {}'.format(fn))
 			subprocess.call(['wipe','-c',fn])
 
-def	create_secret_files():
+def create_secret_files():
 	from binascii import hexlify
 	secret = hexlify(os.urandom(16))
 	for d in (tx_dir,shm_dir):
@@ -239,7 +246,7 @@ def do_loop():
 			do_sign()
 		prev_status = status
 		if not n % 10:
-			msg_r('\r{}\rwaiting'.format(' '*17))
+			msg_r('\r{}\rWaiting'.format(' '*17))
 		time.sleep(1)
 		msg_r('.')
 		n += 1
@@ -262,6 +269,23 @@ def check_wipe_present():
 	except:
 		die(2,"The 'wipe' utility must be installed before running this program")
 
+def at_exit(nl=True):
+	if opt.led:
+		set_led('off')
+		ev.set()
+		led_thread.join()
+		if board == 'rpi':
+			with open(trigger[board],'w') as f: f.write('mmc0\n')
+	if nl: msg('')
+	raise SystemExit
+
+def handler(a,b): at_exit()
+
+# main()
+if len(cmd_args) == 1 and cmd_args[0] == 'gen_secret':
+	do_create_secret_files()
+	sys.exit()
+
 if opt.led:
 	import threading
 	status = {
@@ -291,28 +315,11 @@ if opt.led:
 	if board == 'rpi':
 		with open(trigger[board],'w') as f: f.write('none\n')
 
-# main()
-if len(cmd_args) == 1 and cmd_args[0] == 'gen_secret':
-	do_create_secret_files()
-	sys.exit()
-
 check_wipe_present()
 wfs = get_wallet_files()
 secret = get_secret_in_dir(shm_dir,on_fail='die')
 check_daemon_running()
 
-def at_exit(nl=True):
-	if opt.led:
-		set_led('off')
-		ev.set()
-		led_thread.join()
-		if board == 'rpi':
-			with open(trigger[board],'w') as f: f.write('mmc0\n')
-	if nl: msg('')
-	raise SystemExit
-
-def handler(a,b): at_exit()
-
 signal.signal(signal.SIGTERM,handler)
 signal.signal(signal.SIGINT,handler)