electrum

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

commit 22061b4555d4e038a2e783fa5de7efef4b7e06bc
parent 85b36e027f8cada43aa755fdcef9291ab45a5e06
Author: SomberNight <somber.night@protonmail.com>
Date:   Sun, 18 Mar 2018 06:18:48 +0100

transaction.get_address_from_output_script now handles witness version > 0

Diffstat:
Mlib/bitcoin.py | 12++++++------
Mlib/tests/test_transaction.py | 22+++++++++++++++++++++-
Mlib/transaction.py | 8+++++---
3 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/lib/bitcoin.py b/lib/bitcoin.py @@ -44,7 +44,7 @@ from . import constants COINBASE_MATURITY = 100 COIN = 100000000 -# supported types of transction outputs +# supported types of transaction outputs TYPE_ADDRESS = 0 TYPE_PUBKEY = 1 TYPE_SCRIPT = 2 @@ -285,14 +285,14 @@ def hash160_to_p2sh(h160): def public_key_to_p2pkh(public_key): return hash160_to_p2pkh(hash_160(public_key)) -def hash_to_segwit_addr(h): - return segwit_addr.encode(constants.net.SEGWIT_HRP, 0, h) +def hash_to_segwit_addr(h, witver): + return segwit_addr.encode(constants.net.SEGWIT_HRP, witver, h) def public_key_to_p2wpkh(public_key): - return hash_to_segwit_addr(hash_160(public_key)) + return hash_to_segwit_addr(hash_160(public_key), witver=0) def script_to_p2wsh(script): - return hash_to_segwit_addr(sha256(bfh(script))) + return hash_to_segwit_addr(sha256(bfh(script)), witver=0) def p2wpkh_nested_script(pubkey): pkh = bh2u(hash_160(bfh(pubkey))) @@ -306,7 +306,7 @@ def pubkey_to_address(txin_type, pubkey): if txin_type == 'p2pkh': return public_key_to_p2pkh(bfh(pubkey)) elif txin_type == 'p2wpkh': - return hash_to_segwit_addr(hash_160(bfh(pubkey))) + return public_key_to_p2wpkh(bfh(pubkey)) elif txin_type == 'p2wpkh-p2sh': scriptSig = p2wpkh_nested_script(pubkey) return hash160_to_p2sh(hash_160(bfh(scriptSig))) diff --git a/lib/tests/test_transaction.py b/lib/tests/test_transaction.py @@ -3,7 +3,7 @@ import unittest from lib import transaction from lib.bitcoin import TYPE_ADDRESS from lib.keystore import xpubkey_to_address -from lib.util import bh2u +from lib.util import bh2u, bfh unsigned_blob = '01000000012a5c9a94fcde98f5581cd00162c60a13936ceb75389ea65bf38633b424eb4031000000005701ff4c53ff0488b21e03ef2afea18000000089689bff23e1e7fb2f161daa37270a97a3d8c2e537584b2d304ecb47b86d21fc021b010d3bd425f8cf2e04824bfdf1f1f5ff1d51fadd9a41f9e3fb8dd3403b1bfe00000000ffffffff0140420f00000000001976a914230ac37834073a42146f11ef8414ae929feaafc388ac00000000' signed_blob = '01000000012a5c9a94fcde98f5581cd00162c60a13936ceb75389ea65bf38633b424eb4031000000006c493046022100a82bbc57a0136751e5433f41cf000b3f1a99c6744775e76ec764fb78c54ee100022100f9e80b7de89de861dc6fb0c1429d5da72c2b6b2ee2406bc9bfb1beedd729d985012102e61d176da16edd1d258a200ad9759ef63adf8e14cd97f53227bae35cdb84d2f6ffffffff0140420f00000000001976a914230ac37834073a42146f11ef8414ae929feaafc388ac00000000' @@ -166,6 +166,26 @@ class TestTransaction(unittest.TestCase): tx = transaction.Transaction(v2_blob) self.assertEqual(tx.txid(), "b97f9180173ab141b61b9f944d841e60feec691d6daab4d4d932b24dd36606fe") + def test_get_address_from_output_script(self): + # the inverse of this test is in test_bitcoin: test_address_to_script + addr_from_script = lambda script: transaction.get_address_from_output_script(bfh(script)) + ADDR = transaction.TYPE_ADDRESS + + # bech32 native segwit + # test vectors from BIP-0173 + self.assertEqual((ADDR, 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'), addr_from_script('0014751e76e8199196d454941c45d1b3a323f1433bd6')) + self.assertEqual((ADDR, 'bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx'), addr_from_script('5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6')) + self.assertEqual((ADDR, 'bc1sw50qa3jx3s'), addr_from_script('6002751e')) + self.assertEqual((ADDR, 'bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj'), addr_from_script('5210751e76e8199196d454941c45d1b3a323')) + + # base58 p2pkh + self.assertEqual((ADDR, '14gcRovpkCoGkCNBivQBvw7eso7eiNAbxG'), addr_from_script('76a91428662c67561b95c79d2257d2a93d9d151c977e9188ac')) + self.assertEqual((ADDR, '1BEqfzh4Y3zzLosfGhw1AsqbEKVW6e1qHv'), addr_from_script('76a914704f4b81cadb7bf7e68c08cd3657220f680f863c88ac')) + + # base58 p2sh + self.assertEqual((ADDR, '35ZqQJcBQMZ1rsv8aSuJ2wkC7ohUCQMJbT'), addr_from_script('a9142a84cf00d47f699ee7bbc1dea5ec1bdecb4ac15487')) + self.assertEqual((ADDR, '3PyjzJ3im7f7bcV724GR57edKDqoZvH7Ji'), addr_from_script('a914f47c8954e421031ad04ecd8e7752c9479206b9d387')) + ##### def _run_naive_tests_on_tx(self, raw_tx, txid): diff --git a/lib/transaction.py b/lib/transaction.py @@ -423,9 +423,11 @@ def get_address_from_output_script(_bytes): return TYPE_ADDRESS, hash160_to_p2sh(decoded[1][1]) # segwit address - match = [ opcodes.OP_0, opcodes.OP_PUSHDATA4 ] - if match_decoded(decoded, match): - return TYPE_ADDRESS, hash_to_segwit_addr(decoded[1][1]) + possible_witness_versions = [opcodes.OP_0] + list(range(opcodes.OP_1, opcodes.OP_16 + 1)) + for witver, opcode in enumerate(possible_witness_versions): + match = [ opcode, opcodes.OP_PUSHDATA4 ] + if match_decoded(decoded, match): + return TYPE_ADDRESS, hash_to_segwit_addr(decoded[1][1], witver=witver) return TYPE_SCRIPT, bh2u(_bytes)