Skip to content

Commit

Permalink
Added address_from_xkey_path function
Browse files Browse the repository at this point in the history
  • Loading branch information
4tochka committed Jun 4, 2021
1 parent fc3c531 commit 458f846
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
16 changes: 16 additions & 0 deletions pybtc/functions/bip32.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from struct import pack
from pybtc.functions.key import private_to_public_key, private_key_to_wif
from pybtc.functions.hash import hmac_sha512, double_sha256, hash160
from pybtc.functions.address import public_key_to_address
from pybtc.functions.encode import encode_base58, decode_base58
from pybtc.constants import *
from pybtc.crypto import __secp256k1_ec_pubkey_tweak_add__
Expand Down Expand Up @@ -454,3 +455,18 @@ def bip32_xkey_to_path_xkey(key, path_type, base58=True, hex=False):
else:
return key


def address_from_xkey_path(key, path, address_type="P2WPKH", testnet=None):
if testnet is None:
testnet = xkey_network_type(key) == "testnet"
k = derive_xkey(key, path)
if xkey_type(key) == "private":
k = xprivate_to_xpublic_key(k)
k = public_from_xpublic_key(k)
if address_type == "P2SH_P2WPKH":
return public_key_to_address(k, testnet=testnet, p2sh_p2wpkh=True)
elif address_type == "P2WPKH":
return public_key_to_address(k, testnet=testnet)
elif address_type == "P2PKH" or address_type == "PUBKEY" :
return public_key_to_address(k, testnet=testnet, witness_version=None)

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def run(self):
return _build_ext.run(self)

setup(name='pybtc',
version='2.3.7',
version='2.3.8',
description='Python Bitcoin library',
keywords='bitcoin',
url='https://github.com/bitaps-com/pybtc',
Expand Down
19 changes: 19 additions & 0 deletions tests/test_bip32_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pybtc.functions.bip32 import derive_xkey
from pybtc.functions.bip32 import derive_child_xprivate_key
from pybtc.functions.bip32 import derive_child_xpublic_key
from pybtc.functions.bip32 import address_from_xkey_path


def test_create_master_xprivate_key():
Expand Down Expand Up @@ -381,3 +382,21 @@ def test_bip32_xkey_to_path_xkey():
with pytest.raises(ValueError):
bip32_xkey_to_path_xkey(decode_base58("pprv9xPgprbtHAtyKqFegHcy7WoUJ7tTsrL3D36Zf4LcXCCNEWfszpQReMWMdSpjE9qos"
"HonUirqo418nd6vG46yi34nbHQ8wvWjLLjzMBFKNqM", checksum=True), "BIP84")

def test_address_from_xkey_path():
k = "xprv9s21ZrQH143K4MmJePgCWa3DxkUQuy8LZ33gVVMLjvbSiPBAjmK6XZovvLMGFGQNFUSmGpjGUDyP5nF48WAgkMt6QM39YCriYVzssd92xTP"
assert address_from_xkey_path(k,"m/0") == "bc1qn0ulwfzfvv7m73czwtsk480ukuwyy7wyw6j0rq"
assert address_from_xkey_path(k,"m/0", address_type="P2SH_P2WPKH") == "39e4J9P5qDMNtdPi9XSrtH9bxTuraN9vTj"
assert address_from_xkey_path(k,"m/0", address_type="P2PKH") == "1FDjBdmokvK4oMTNf9oKtFC2ziXi9cXz3a"
k = "xpub661MyMwAqRbcGqqmkRDCshyxWnJuKRrBvFyHHskxJG8RbBWKHJdM5N8QmcB1MDRNcUvuy4zT7Bxp2hXbRprs8LKfhKwqkGmqSVNTiwr4Tnz"
assert address_from_xkey_path(k,"m/0") == "bc1qn0ulwfzfvv7m73czwtsk480ukuwyy7wyw6j0rq"
assert address_from_xkey_path(k,"m/0", address_type="P2SH_P2WPKH") == "39e4J9P5qDMNtdPi9XSrtH9bxTuraN9vTj"
assert address_from_xkey_path(k,"m/0", address_type="P2PKH") == "1FDjBdmokvK4oMTNf9oKtFC2ziXi9cXz3a"
k = "xprv9s21ZrQH143K4MmJePgCWa3DxkUQuy8LZ33gVVMLjvbSiPBAjmK6XZovvLMGFGQNFUSmGpjGUDyP5nF48WAgkMt6QM39YCriYVzssd92xTP"
assert address_from_xkey_path(k,"m/0'/0'/2") == "bc1q2usrj3v56gj5xe9a65n9fs07ea0jekzp4cuxue"
assert address_from_xkey_path(k,"m/0'/0'/2", address_type="P2SH_P2WPKH") == "39CPPBDqLvcAaEKr5sJoVHrDx4UTbuZHJ6"
assert address_from_xkey_path(k,"m/0'/0'/2", address_type="P2PKH") == "18wgPdgUHfbWGZFu9cz7qhFwyxnYgubk32"
k = "xpub69tzdgZfM35wv4hJoZapdJfDErQhdt2m7MrUcFhCK1UeGXu8cQDiHuGuZUyyYYY5nb69QZbQ7RYYQCcFCLEJdSrmrUoBtpc8Tx2ypkSsvkp"
assert address_from_xkey_path(k,"m/2") == "bc1q2usrj3v56gj5xe9a65n9fs07ea0jekzp4cuxue"
assert address_from_xkey_path(k,"m/2", address_type="P2SH_P2WPKH") == "39CPPBDqLvcAaEKr5sJoVHrDx4UTbuZHJ6"
assert address_from_xkey_path(k,"m/2", address_type="P2PKH") == "18wgPdgUHfbWGZFu9cz7qhFwyxnYgubk32"

0 comments on commit 458f846

Please sign in to comment.