try to sweep p2pk outputs from old type WIF privkeys

This commit is contained in:
SomberNight 2017-10-25 15:14:55 +02:00
parent 06f18f9078
commit 1c31177d03
2 changed files with 29 additions and 15 deletions

View File

@ -405,6 +405,9 @@ def address_to_script(addr):
def address_to_scripthash(addr): def address_to_scripthash(addr):
script = address_to_script(addr) script = address_to_script(addr)
return script_to_scripthash(script)
def script_to_scripthash(script):
h = sha256(bytes.fromhex(script))[0:32] h = sha256(bytes.fromhex(script))[0:32]
return bh2u(bytes(reversed(h))) return bh2u(bytes(reversed(h)))

View File

@ -870,20 +870,21 @@ class Abstract_Wallet(PrintError):
self.sign_transaction(tx, password) self.sign_transaction(tx, password)
return tx return tx
def sweep(self, privkeys, network, config, recipient, fee=None, imax=100): def _append_utxos_to_inputs(self, inputs, network, pubkey, txin_type, imax):
inputs = [] address = None
keypairs = {} if txin_type != 'p2pk':
for sec in privkeys:
txin_type, privkey, compressed = bitcoin.deserialize_privkey(sec)
pubkey = bitcoin.public_key_from_private_key(privkey, compressed)
address = bitcoin.pubkey_to_address(txin_type, pubkey) address = bitcoin.pubkey_to_address(txin_type, pubkey)
sh = bitcoin.address_to_scripthash(address) sh = bitcoin.address_to_scripthash(address)
else:
script = bitcoin.public_key_to_p2pk_script(pubkey)
sh = bitcoin.script_to_scripthash(script)
u = network.synchronous_get(('blockchain.scripthash.listunspent', [sh])) u = network.synchronous_get(('blockchain.scripthash.listunspent', [sh]))
for item in u: for item in u:
if len(inputs) >= imax: if len(inputs) >= imax:
break break
item['type'] = txin_type if address is not None:
item['address'] = address item['address'] = address
item['type'] = txin_type
item['prevout_hash'] = item['tx_hash'] item['prevout_hash'] = item['tx_hash']
item['prevout_n'] = item['tx_pos'] item['prevout_n'] = item['tx_pos']
item['pubkeys'] = [pubkey] item['pubkeys'] = [pubkey]
@ -891,6 +892,16 @@ class Abstract_Wallet(PrintError):
item['signatures'] = [None] item['signatures'] = [None]
item['num_sig'] = 1 item['num_sig'] = 1
inputs.append(item) inputs.append(item)
def sweep(self, privkeys, network, config, recipient, fee=None, imax=100):
inputs = []
keypairs = {}
for sec in privkeys:
txin_type, privkey, compressed = bitcoin.deserialize_privkey(sec)
pubkey = bitcoin.public_key_from_private_key(privkey, compressed)
self._append_utxos_to_inputs(inputs, network, pubkey, txin_type, imax)
if txin_type == 'p2pkh': # WIF serialization is ambiguous :(
self._append_utxos_to_inputs(inputs, network, pubkey, 'p2pk', imax)
keypairs[pubkey] = privkey, compressed keypairs[pubkey] = privkey, compressed
if not inputs: if not inputs: