Merge pull request #3125 from SomberNight/sweep_p2pk

try to sweep p2pk outputs from old type WIF privkeys
This commit is contained in:
ThomasV 2017-10-27 21:01:56 +02:00 committed by GitHub
commit 6f3c822867
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 15 deletions

View File

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

View File

@ -870,27 +870,38 @@ class Abstract_Wallet(PrintError):
self.sign_transaction(tx, password)
return tx
def _append_utxos_to_inputs(self, inputs, network, pubkey, txin_type, imax):
address = None
if txin_type != 'p2pk':
address = bitcoin.pubkey_to_address(txin_type, pubkey)
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]))
for item in u:
if len(inputs) >= imax:
break
if address is not None:
item['address'] = address
item['type'] = txin_type
item['prevout_hash'] = item['tx_hash']
item['prevout_n'] = item['tx_pos']
item['pubkeys'] = [pubkey]
item['x_pubkeys'] = [pubkey]
item['signatures'] = [None]
item['num_sig'] = 1
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)
address = bitcoin.pubkey_to_address(txin_type, pubkey)
sh = bitcoin.address_to_scripthash(address)
u = network.synchronous_get(('blockchain.scripthash.listunspent', [sh]))
for item in u:
if len(inputs) >= imax:
break
item['type'] = txin_type
item['address'] = address
item['prevout_hash'] = item['tx_hash']
item['prevout_n'] = item['tx_pos']
item['pubkeys'] = [pubkey]
item['x_pubkeys'] = [pubkey]
item['signatures'] = [None]
item['num_sig'] = 1
inputs.append(item)
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
if not inputs: