Browse Source

rewrite from_addr, allow to select addr from wallet

283
thomasv 13 years ago
parent
commit
8757adac40
  1. 47
      client/electrum
  2. 9
      client/wallet.py

47
client/electrum

@ -191,7 +191,7 @@ if __name__ == '__main__':
print "payto <recipient> <amount> [label]" print "payto <recipient> <amount> [label]"
print "create and broadcast a transaction." print "create and broadcast a transaction."
print "<recipient> can be a bitcoin address or a label" print "<recipient> can be a bitcoin address or a label"
print "options: --fromaddr, --changeaddr" print "options: --fee, --fromaddr, --changeaddr"
elif cmd2== 'sendtx': elif cmd2== 'sendtx':
print "sendtx <tx>" print "sendtx <tx>"
print "broadcast a transaction to the network. <tx> must be in hexadecimal" print "broadcast a transaction to the network. <tx> must be in hexadecimal"
@ -210,7 +210,7 @@ if __name__ == '__main__':
elif cmd2 == 'mktx': elif cmd2 == 'mktx':
print "create a signed transaction. password protected" print "create a signed transaction. password protected"
print "syntax: mktx <recipient> <amount> [label]" print "syntax: mktx <recipient> <amount> [label]"
print "options: --fromaddr, --changeaddr" print "options: --fee, --fromaddr, --changeaddr"
elif cmd2 == 'seed': elif cmd2 == 'seed':
print "show generation seed of your wallet. password protected." print "show generation seed of your wallet. password protected."
elif cmd2 == 'eval': elif cmd2 == 'eval':
@ -303,27 +303,25 @@ if __name__ == '__main__':
wallet.save() wallet.save()
elif cmd in ['payto', 'mktx']: elif cmd in ['payto', 'mktx']:
is_temporary = False
if options.from_addr: if options.from_addr:
#temporally import key and remove the other addresses from_addr = options.from_addr
addr = options.from_addr if from_addr not in wallet.all_addresses():
if addr.find(":") == -1: if from_addr.find(":") == -1:
addr = addr + ":" + getpass.getpass('Private key:') keypair = from_addr + ":" + getpass.getpass('Private key:')
wallet.imported_keys = {} else:
if not wallet.import_key(options.from_addr,password): keypair = from_addr
print "invalid key pair" from_addr = keypair.split(':')[0]
exit(1) if not wallet.import_key(keypair,password):
addr = wallet.imported_keys.keys()[0] print "invalid key pair"
wallet.history[addr] = interface.retrieve_history(addr) exit(1)
wallet.synchronize() is_temporary = True
wallet.update_tx_history()
wallet.addresses = []
wallet.change_addresses = []
change_addr = addr
save = False
else: else:
save = True from_addr = None
if options.change_addr: if options.change_addr:
change_addr = options.change_addr change_addr = options.change_addr
for k, v in wallet.labels.items(): for k, v in wallet.labels.items():
if v == to_address: if v == to_address:
to_address = k to_address = k
@ -333,9 +331,10 @@ if __name__ == '__main__':
change_addr = k change_addr = k
try: try:
tx = wallet.mktx( to_address, amount, label, password, tx = wallet.mktx( to_address, amount, label, password,
fee = options.tx_fee, change_addr = change_addr, save = save ) fee = options.tx_fee, change_addr = change_addr, from_addr = from_addr )
except BaseException, e: except:
print e import traceback
traceback.print_exc(file=sys.stdout)
tx = None tx = None
if tx and cmd=='payto': if tx and cmd=='payto':
@ -344,6 +343,10 @@ if __name__ == '__main__':
else: else:
print tx print tx
if is_temporary:
wallet.imported_keys.pop(from_addr)
wallet.save()
elif cmd == 'sendtx': elif cmd == 'sendtx':
tx = args[1] tx = args[1]
r, h = wallet.sendtx( tx ) r, h = wallet.sendtx( tx )

9
client/wallet.py

@ -574,13 +574,14 @@ class Wallet:
return conf, unconf return conf, unconf
def choose_tx_inputs( self, amount, fixed_fee ): def choose_tx_inputs( self, amount, fixed_fee, from_addr = None ):
""" todo: minimize tx size """ """ todo: minimize tx size """
total = 0 total = 0
fee = self.fee if fixed_fee is None else fixed_fee fee = self.fee if fixed_fee is None else fixed_fee
coins = [] coins = []
for addr in self.all_addresses(): domain = [from_addr] if from_addr else self.all_addresses()
for addr in domain:
h = self.history.get(addr) h = self.history.get(addr)
if h is None: continue if h is None: continue
for item in h: for item in h:
@ -687,10 +688,10 @@ class Wallet:
default_label = 'at: ' + o_addr default_label = 'at: ' + o_addr
tx['default_label'] = default_label tx['default_label'] = default_label
def mktx(self, to_address, amount, label, password, fee=None, change_addr=None, save=True): def mktx(self, to_address, amount, label, password, fee=None, change_addr=None, from_addr= None):
if not self.is_valid(to_address): if not self.is_valid(to_address):
raise BaseException("Invalid address") raise BaseException("Invalid address")
inputs, total, fee = self.choose_tx_inputs( amount, fee ) inputs, total, fee = self.choose_tx_inputs( amount, fee, from_addr )
if not inputs: if not inputs:
raise BaseException("Not enough funds") raise BaseException("Not enough funds")
outputs = self.choose_tx_outputs( to_address, amount, fee, total, change_addr ) outputs = self.choose_tx_outputs( to_address, amount, fee, total, change_addr )

Loading…
Cancel
Save