commit e552930d349ec1d5fe32113b452ff4bfefccff68
parent 9723a5e9ec35f7dba36f45cbbd0dea11a176280c
Author: ThomasV <thomasv@gitorious>
Date: Fri, 25 Apr 2014 17:23:26 +0200
sweep command (does not broadcast)
Diffstat:
2 files changed, 36 insertions(+), 14 deletions(-)
diff --git a/lib/commands.py b/lib/commands.py
@@ -102,6 +102,7 @@ register_command('verifymessage', 3,-1, False, False, False, 'Verifies a
register_command('daemon', 1, 1, True, False, False, '<stop|status>')
register_command('getproof', 1, 1, True, False, False, 'get merkle proof', 'getproof <address>')
register_command('getutxoaddress', 2, 2, True, False, False, 'get the address of an unspent transaction output','getutxoaddress <txid> <pos>')
+register_command('sweep', 2, 3, True, False, False, 'Sweep a private key.', 'sweep privkey addr [fee]')
@@ -262,6 +263,21 @@ class Commands:
return out
+ def sweep(self, privkey, to_address, fee = 0.0001):
+ pubkey = public_key_from_private_key(privkey)
+ address = address_from_private_key(privkey)
+ pay_script = Transaction.pay_script(address)
+ unspent = self.network.synchronous_get([ ('blockchain.address.listunspent',[address])])[0]
+ if not unspent:
+ return
+ total = sum( map(lambda x:int(x.get('value')), unspent) ) - int(Decimal(fee)*100000000)
+ inputs = map(lambda i: {'prevout_hash': i['tx_hash'], 'prevout_n':i['tx_pos'], 'scriptPubKey':pay_script, 'redeemPubkey':pubkey}, unspent)
+ outputs = [(to_address, total)]
+ tx = Transaction.from_io(inputs, outputs)
+ tx.sign({ pubkey:privkey })
+ return tx
+
+
def signmessage(self, address, message):
return self.wallet.sign_message(address, message, self.password)
diff --git a/lib/transaction.py b/lib/transaction.py
@@ -425,6 +425,25 @@ class Transaction:
return s
+
+ @classmethod
+ def pay_script(self, addr):
+ addrtype, hash_160 = bc_address_to_hash_160(addr)
+ if addrtype == 0:
+ script = '76a9' # op_dup, op_hash_160
+ script += '14' # push 0x14 bytes
+ script += hash_160.encode('hex')
+ script += '88ac' # op_equalverify, op_checksig
+ elif addrtype == 5:
+ script = 'a9' # op_hash_160
+ script += '14' # push 0x14 bytes
+ script += hash_160.encode('hex')
+ script += '87' # op_equal
+ else:
+ raise
+ return script
+
+
@classmethod
def serialize( klass, inputs, outputs, for_sig = None ):
@@ -475,20 +494,7 @@ class Transaction:
for output in outputs:
addr, amount = output
s += int_to_hex( amount, 8) # amount
- addrtype, hash_160 = bc_address_to_hash_160(addr)
- if addrtype == 0:
- script = '76a9' # op_dup, op_hash_160
- script += '14' # push 0x14 bytes
- script += hash_160.encode('hex')
- script += '88ac' # op_equalverify, op_checksig
- elif addrtype == 5:
- script = 'a9' # op_hash_160
- script += '14' # push 0x14 bytes
- script += hash_160.encode('hex')
- script += '87' # op_equal
- else:
- raise
-
+ script = klass.pay_script(addr)
s += var_int( len(script)/2 ) # script length
s += script # script
s += int_to_hex(0,4) # lock time