sweep command (does not broadcast)
This commit is contained in:
parent
9723a5e9ec
commit
e552930d34
|
@ -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('daemon', 1, 1, True, False, False, '<stop|status>')
|
||||||
register_command('getproof', 1, 1, True, False, False, 'get merkle proof', 'getproof <address>')
|
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('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
|
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):
|
def signmessage(self, address, message):
|
||||||
return self.wallet.sign_message(address, message, self.password)
|
return self.wallet.sign_message(address, message, self.password)
|
||||||
|
|
||||||
|
|
|
@ -425,6 +425,25 @@ class Transaction:
|
||||||
|
|
||||||
return s
|
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
|
@classmethod
|
||||||
def serialize( klass, inputs, outputs, for_sig = None ):
|
def serialize( klass, inputs, outputs, for_sig = None ):
|
||||||
|
|
||||||
|
@ -475,20 +494,7 @@ class Transaction:
|
||||||
for output in outputs:
|
for output in outputs:
|
||||||
addr, amount = output
|
addr, amount = output
|
||||||
s += int_to_hex( amount, 8) # amount
|
s += int_to_hex( amount, 8) # amount
|
||||||
addrtype, hash_160 = bc_address_to_hash_160(addr)
|
script = klass.pay_script(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
|
|
||||||
|
|
||||||
s += var_int( len(script)/2 ) # script length
|
s += var_int( len(script)/2 ) # script length
|
||||||
s += script # script
|
s += script # script
|
||||||
s += int_to_hex(0,4) # lock time
|
s += int_to_hex(0,4) # lock time
|
||||||
|
|
Loading…
Reference in New Issue