Sitelet https://github.com/bitaps-com/pybtc/commit/458f84606c81320ced1e8fa5e8051d08d85170a4
Skip to content

Commit 458f846

Browse files
committed
Added address_from_xkey_path function
1 parent fc3c531 commit 458f846

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎pybtc/functions/bip32.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from struct import pack
22
from pybtc.functions.key import private_to_public_key, private_key_to_wif
33
from pybtc.functions.hash import hmac_sha512, double_sha256, hash160
4+
from pybtc.functions.address import public_key_to_address
45
from pybtc.functions.encode import encode_base58, decode_base58
56
from pybtc.constants import *
67
from pybtc.crypto import __secp256k1_ec_pubkey_tweak_add__
@@ -454,3 +455,18 @@ def bip32_xkey_to_path_xkey(key, path_type, base58=True, hex=False):
454455
else:
455456
return key
456457

458+
459+
def address_from_xkey_path(key, path, address_type="P2WPKH", testnet=None):
460+
if testnet is None:
461+
testnet = xkey_network_type(key) == "testnet"
462+
k = derive_xkey(key, path)
463+
if xkey_type(key) == "private":
464+
k = xprivate_to_xpublic_key(k)
465+
k = public_from_xpublic_key(k)
466+
if address_type == "P2SH_P2WPKH":
467+
return public_key_to_address(k, testnet=testnet, p2sh_p2wpkh=True)
468+
elif address_type == "P2WPKH":
469+
return public_key_to_address(k, testnet=testnet)
470+
elif address_type == "P2PKH" or address_type == "PUBKEY" :
471+
return public_key_to_address(k, testnet=testnet, witness_version=None)
472+

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def run(self):
122122
return _build_ext.run(self)
123123

124124
setup(name='pybtc',
125-
version='2.3.7',
125+
version='2.3.8',
126126
description='Python Bitcoin library',
127127
keywords='bitcoin',
128128
url='https://github.com/bitaps-com/pybtc',

‎tests/test_bip32_functions.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pybtc.functions.bip32 import derive_xkey
1818
from pybtc.functions.bip32 import derive_child_xprivate_key
1919
from pybtc.functions.bip32 import derive_child_xpublic_key
20+
from pybtc.functions.bip32 import address_from_xkey_path
2021

2122

2223
def test_create_master_xprivate_key():
@@ -381,3 +382,21 @@ def test_bip32_xkey_to_path_xkey():
381382
with pytest.raises(ValueError):
382383
bip32_xkey_to_path_xkey(decode_base58("pprv9xPgprbtHAtyKqFegHcy7WoUJ7tTsrL3D36Zf4LcXCCNEWfszpQReMWMdSpjE9qos"
383384
"HonUirqo418nd6vG46yi34nbHQ8wvWjLLjzMBFKNqM", checksum=True), "BIP84")
385+
386+
def test_address_from_xkey_path():
387+
k = "xprv9s21ZrQH143K4MmJePgCWa3DxkUQuy8LZ33gVVMLjvbSiPBAjmK6XZovvLMGFGQNFUSmGpjGUDyP5nF48WAgkMt6QM39YCriYVzssd92xTP"
388+
assert address_from_xkey_path(k,"m/0") == "bc1qn0ulwfzfvv7m73czwtsk480ukuwyy7wyw6j0rq"
389+
assert address_from_xkey_path(k,"m/0", address_type="P2SH_P2WPKH") == "39e4J9P5qDMNtdPi9XSrtH9bxTuraN9vTj"
390+
assert address_from_xkey_path(k,"m/0", address_type="P2PKH") == "1FDjBdmokvK4oMTNf9oKtFC2ziXi9cXz3a"
391+
k = "xpub661MyMwAqRbcGqqmkRDCshyxWnJuKRrBvFyHHskxJG8RbBWKHJdM5N8QmcB1MDRNcUvuy4zT7Bxp2hXbRprs8LKfhKwqkGmqSVNTiwr4Tnz"
392+
assert address_from_xkey_path(k,"m/0") == "bc1qn0ulwfzfvv7m73czwtsk480ukuwyy7wyw6j0rq"
393+
assert address_from_xkey_path(k,"m/0", address_type="P2SH_P2WPKH") == "39e4J9P5qDMNtdPi9XSrtH9bxTuraN9vTj"
394+
assert address_from_xkey_path(k,"m/0", address_type="P2PKH") == "1FDjBdmokvK4oMTNf9oKtFC2ziXi9cXz3a"
395+
k = "xprv9s21ZrQH143K4MmJePgCWa3DxkUQuy8LZ33gVVMLjvbSiPBAjmK6XZovvLMGFGQNFUSmGpjGUDyP5nF48WAgkMt6QM39YCriYVzssd92xTP"
396+
assert address_from_xkey_path(k,"m/0'/0'/2") == "bc1q2usrj3v56gj5xe9a65n9fs07ea0jekzp4cuxue"
397+
assert address_from_xkey_path(k,"m/0'/0'/2", address_type="P2SH_P2WPKH") == "39CPPBDqLvcAaEKr5sJoVHrDx4UTbuZHJ6"
398+
assert address_from_xkey_path(k,"m/0'/0'/2", address_type="P2PKH") == "18wgPdgUHfbWGZFu9cz7qhFwyxnYgubk32"
399+
k = "xpub69tzdgZfM35wv4hJoZapdJfDErQhdt2m7MrUcFhCK1UeGXu8cQDiHuGuZUyyYYY5nb69QZbQ7RYYQCcFCLEJdSrmrUoBtpc8Tx2ypkSsvkp"
400+
assert address_from_xkey_path(k,"m/2") == "bc1q2usrj3v56gj5xe9a65n9fs07ea0jekzp4cuxue"
401+
assert address_from_xkey_path(k,"m/2", address_type="P2SH_P2WPKH") == "39CPPBDqLvcAaEKr5sJoVHrDx4UTbuZHJ6"
402+
assert address_from_xkey_path(k,"m/2", address_type="P2PKH") == "18wgPdgUHfbWGZFu9cz7qhFwyxnYgubk32"

0 commit comments

Comments
 (0)