commit 18182998b3d5f8cc738da9ffe912ea12ceab0c34
parent a8a993d4904c36258205195ec7ee0a5c485b00f7
Author: ThomasV <thomasv@electrum.org>
Date: Sat, 16 Sep 2017 06:07:16 +0200
Merge pull request #2877 from SomberNight/bech32_scriptpubkeys
fix bech32 scriptPubKeys (OP_n), and tests
Diffstat:
2 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/lib/bitcoin.py b/lib/bitcoin.py
@@ -337,7 +337,10 @@ def script_to_p2wsh(script):
def address_to_script(addr):
if is_segwit_address(addr):
witver, witprog = segwit_addr.decode(SEGWIT_HRP, addr)
- script = bytes([witver]).hex() + push_script(bytes(witprog).hex())
+ assert (0 <= witver <= 16)
+ OP_n = witver + 0x50 if witver > 0 else 0
+ script = bytes([OP_n]).hex()
+ script += push_script(bytes(witprog).hex())
return script
addrtype, hash_160 = b58_address_to_hash160(addr)
if addrtype == ADDRTYPE_P2PKH:
diff --git a/lib/tests/test_bitcoin.py b/lib/tests/test_bitcoin.py
@@ -13,7 +13,7 @@ from lib.bitcoin import (
bip32_root, bip32_public_derivation, bip32_private_derivation, pw_encode,
pw_decode, Hash, public_key_from_private_key, address_from_private_key,
is_address, is_private_key, xpub_from_xprv, is_new_seed, is_old_seed,
- var_int, op_push)
+ var_int, op_push, address_to_script)
from lib.util import bfh
try:
@@ -155,6 +155,23 @@ class Test_bitcoin(unittest.TestCase):
self.assertEqual(op_push(0x10000), '4e00000100')
self.assertEqual(op_push(0x12345678), '4e78563412')
+ # TODO testnet addresses
+ def test_address_to_script(self):
+ # bech32 native segwit
+ # test vectors from BIP-0173
+ self.assertEqual(address_to_script('BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4'), '0014751e76e8199196d454941c45d1b3a323f1433bd6')
+ self.assertEqual(address_to_script('bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx'), '5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6')
+ self.assertEqual(address_to_script('BC1SW50QA3JX3S'), '6002751e')
+ self.assertEqual(address_to_script('bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj'), '5210751e76e8199196d454941c45d1b3a323')
+
+ # base58 P2PKH
+ self.assertEqual(address_to_script('14gcRovpkCoGkCNBivQBvw7eso7eiNAbxG'), '76a91428662c67561b95c79d2257d2a93d9d151c977e9188ac')
+ self.assertEqual(address_to_script('1BEqfzh4Y3zzLosfGhw1AsqbEKVW6e1qHv'), '76a914704f4b81cadb7bf7e68c08cd3657220f680f863c88ac')
+
+ # base58 P2SH
+ self.assertEqual(address_to_script('35ZqQJcBQMZ1rsv8aSuJ2wkC7ohUCQMJbT'), 'a9142a84cf00d47f699ee7bbc1dea5ec1bdecb4ac15487')
+ self.assertEqual(address_to_script('3PyjzJ3im7f7bcV724GR57edKDqoZvH7Ji'), 'a914f47c8954e421031ad04ecd8e7752c9479206b9d387')
+
class Test_keyImport(unittest.TestCase):
""" The keys used in this class are TEST keys from