diff --git a/client/electrum b/client/electrum index 29ff64514..fbee56ac5 100755 --- a/client/electrum +++ b/client/electrum @@ -41,6 +41,8 @@ if __name__ == '__main__': parser.add_option("-b", "--balance", action="store_true", dest="show_balance", default=False, help="show the balance at listed addresses") parser.add_option("-k", "--keys",action="store_true", dest="show_keys",default=False, help="show the private keys of listed addresses") parser.add_option("-f", "--fee", dest="tx_fee", default="0.005", help="set tx fee") + parser.add_option("-s", "--fromaddr", dest="from_addr", default=None, help="set source address for payto/mktx. if it isn't in the wallet, it will ask for the private key unless supplied in the format public_key:private_key. It's not saved in the wallet.") + parser.add_option("-c", "--changeaddr", dest="change_addr", default=None, help="set the change address for payto/mktx. default is a spare address, or the source address if it's not in the wallet") options, args = parser.parse_args() try: cmd = args[0] @@ -141,6 +143,7 @@ if __name__ == '__main__': try: to_address = args[1] amount = int( 100000000 * Decimal(args[2]) ) + change_addr = None label = ' '.join(args[3:]) if options.tx_fee: options.tx_fee = int( 100000000 * Decimal(options.tx_fee) ) @@ -186,6 +189,7 @@ if __name__ == '__main__': print "payto [label]" print "create and broadcast a transaction." print " can be a bitcoin address or a label" + print "options: --fromaddr, --changeaddr" elif cmd2== 'sendtx': print "sendtx " print "broadcast a transaction to the network. must be in hexadecimal" @@ -204,6 +208,7 @@ if __name__ == '__main__': elif cmd2 == 'mktx': print "create a signed transaction. password protected" print "syntax: mktx [label]" + print "options: --fromaddr, --changeaddr" elif cmd2 == 'seed': print "show generation seed of your wallet. password protected." elif cmd2 == 'eval': @@ -296,13 +301,37 @@ if __name__ == '__main__': wallet.save() elif cmd in ['payto', 'mktx']: + if options.from_addr: + #temporally import key and remove the other addresses + addr = options.from_addr + if addr.find(":") == -1: + addr = addr + ":" + getpass.getpass('Private key:') + wallet.imported_keys = {} + if not wallet.import_key(options.from_addr,password): + print "invalid key pair" + exit(1) + addr = wallet.imported_keys.keys()[0] + wallet.history[addr] = wallet.interface.retrieve_history(addr) + wallet.synchronize() + wallet.update_tx_history() + wallet.addresses = [] + wallet.change_addresses = [] + change_addr = addr + save = False + else: + save = True + if options.change_addr: + change_addr = options.change_addr for k, v in wallet.labels.items(): if v == to_address: to_address = k print "alias", to_address break + if change_addr and v == change_addr: + change_addr = k try: - tx = wallet.mktx( to_address, amount, label, password, fee = options.tx_fee ) + tx = wallet.mktx( to_address, amount, label, password, + fee = options.tx_fee, change_addr = change_addr, save = save ) except BaseException, e: print e tx = None diff --git a/client/wallet.py b/client/wallet.py index ea133b615..9123e5028 100644 --- a/client/wallet.py +++ b/client/wallet.py @@ -618,12 +618,14 @@ class Wallet: inputs = [] return inputs, total, fee - def choose_tx_outputs( self, to_addr, amount, fee, total ): + def choose_tx_outputs( self, to_addr, amount, fee, total, change_addr=None ): outputs = [ (to_addr, amount) ] change_amount = total - ( amount + fee ) if change_amount != 0: # normally, the update thread should ensure that the last change address is unused - outputs.append( ( self.change_addresses[-1], change_amount) ) + if not change_addr: + change_addr = self.change_addresses[-1] + outputs.append( ( change_addr, change_amount) ) return outputs def sign_inputs( self, inputs, outputs, password ): @@ -702,13 +704,13 @@ class Wallet: default_label = 'at: ' + o_addr tx['default_label'] = default_label - def mktx(self, to_address, amount, label, password, fee=None): + def mktx(self, to_address, amount, label, password, fee=None, change_addr=None, save=True): if not self.is_valid(to_address): raise BaseException("Invalid address") inputs, total, fee = self.choose_tx_inputs( amount, fee ) if not inputs: raise BaseException("Not enough funds") - outputs = self.choose_tx_outputs( to_address, amount, fee, total ) + outputs = self.choose_tx_outputs( to_address, amount, fee, total, change_addr ) s_inputs = self.sign_inputs( inputs, outputs, password ) tx = filter( raw_tx( s_inputs, outputs ) ) @@ -717,7 +719,8 @@ class Wallet: if label: tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex') self.labels[tx_hash] = label - self.save() + if save: + self.save() return tx def sendtx(self, tx):