electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 6f3c822867bd512e276ef63ad7b8c2af2fa40927
parent 7dcd01356a10b632516742a86b9d8c6251e9f70a
Author: ThomasV <thomasv@electrum.org>
Date:   Fri, 27 Oct 2017 21:01:56 +0200

Merge pull request #3125 from SomberNight/sweep_p2pk

try to sweep p2pk outputs from old type WIF privkeys
Diffstat:
Mlib/bitcoin.py | 3+++
Mlib/wallet.py | 41++++++++++++++++++++++++++---------------
2 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/lib/bitcoin.py b/lib/bitcoin.py @@ -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))) diff --git a/lib/wallet.py b/lib/wallet.py @@ -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: