Browse Source

read command line arguments from stdin if '-' is passed

283
ThomasV 9 years ago
parent
commit
02e81a3655
  1. 37
      electrum
  2. 17
      lib/commands.py

37
electrum

@ -80,14 +80,11 @@ from electrum.commands import get_parser, known_commands, Commands, config_varia
# get password routine
def prompt_password(prompt, confirm=True):
import getpass
if sys.stdin.isatty():
password = getpass.getpass(prompt)
if password and confirm:
password2 = getpass.getpass("Confirm: ")
if password != password2:
sys.exit("Error: Passwords do not match.")
else:
password = raw_input(prompt)
password = getpass.getpass(prompt)
if password and confirm:
password2 = getpass.getpass("Confirm: ")
if password != password2:
sys.exit("Error: Passwords do not match.")
if not password:
password = None
return password
@ -204,6 +201,20 @@ def run_cmdline(config):
# options
args += map(lambda x: config.get(x), cmd.options)
# pipe data
for i, arg in enumerate(args):
if arg == '-':
if not sys.stdin.isatty():
pipe_data = sys.stdin.read()
try:
pipe_data = json.loads(pipe_data)
except:
pass
args[i] = pipe_data
break
else:
raise BaseException('Cannot get argument from stdin')
# instanciate wallet for command-line
storage = WalletStorage(config.get_wallet_path())
@ -272,14 +283,16 @@ def run_cmdline(config):
always_hook('cmdline_load_wallet', wallet)
# important warning
if cmd.name in ['dumpprivkey', 'dumpprivkeys']:
if cmd.name in ['getprivatekeys', 'dumpprivkeys']:
print_stderr("WARNING: ALL your private keys are secret.")
print_stderr("Exposing a single private key can compromise your entire wallet!")
print_stderr("In particular, DO NOT use 'redeem private key' services proposed by third parties.")
# commands needing password
if cmd.requires_password:
if wallet.use_encryption:
if cmd.requires_password and wallet.use_encryption:
if config.get('password'):
password = config.get('password')
else:
password = prompt_password('Password:', False)
if not password:
print_msg("Error: Password required")
@ -290,8 +303,6 @@ def run_cmdline(config):
except InvalidPassword:
print_msg("Error: This password does not decode this wallet.")
sys.exit(1)
else:
password = None
else:
password = None

17
lib/commands.py

@ -239,8 +239,11 @@ class Commands:
@command('wp')
def getprivatekeys(self, address):
"""Get the private keys of an address. Address must be in wallet."""
return self.wallet.get_private_key(address, self.password)
"""Get the private keys of a wallet address, or list of wallet addresses."""
is_list = type(address) is list
domain = address if is_list else [address]
out = [self.wallet.get_private_key(address, self.password) for address in domain]
return out if is_list else out[0]
@command('w')
def ismine(self, address):
@ -248,10 +251,9 @@ class Commands:
return self.wallet.is_mine(address)
@command('wp')
def dumpprivkeys(self, domain=None):
def dumpprivkeys(self):
"""Dump private keys from your wallet"""
if domain is None:
domain = self.wallet.addresses(True)
domain = self.wallet.addresses(True)
return [self.wallet.get_private_key(address, self.password) for address in domain]
@command('')
@ -353,15 +355,16 @@ class Commands:
@command('n')
def sweep(self, privkey, destination, tx_fee=None, nocheck=False):
"""Sweep private key. Returns a transaction that spends UTXOs from
"""Sweep private keys. Returns a transaction that spends UTXOs from
privkey to a destination address. The transaction is not
broadcasted."""
privkeys = privkey if type(privkey) is list else [privkey]
self.nocheck = nocheck
dest = self._resolver(destination)
if tx_fee is None:
tx_fee = 0.0001
fee = int(Decimal(tx_fee)*COIN)
return Transaction.sweep([privkey], self.network, dest, fee)
return Transaction.sweep(privkeys, self.network, dest, fee)
@command('wp')
def signmessage(self, address, message):

Loading…
Cancel
Save