diff --git a/bip32/bip32.py b/bip32/bip32.py index a77508c..44c9ad1 100644 --- a/bip32/bip32.py +++ b/bip32/bip32.py @@ -182,13 +182,17 @@ def get_pubkey_from_path(self, path): """ return self.get_extended_pubkey_from_path(path)[1] - def get_xpriv_from_path(self, path): + def get_xpriv_from_path(self, path, script="p2pkh"): """Get an encoded extended privkey from a derivation path. :param path: A list of integers (index of each depth) or a string with m/x/x'/x notation. (e.g. m/0'/1/2'/2 or m/0H/1/2H/2). + :param script: Can be "p2pkh" (legacy, xpub), "p2wpkh-p2sh" (ypub), + "p2wpkh" (zpub). Defaults to "p2pkh". :return: The encoded extended pubkey as str. """ + assert script in ["p2pkh", "p2wpkh-p2sh", "p2wpkh"] + if self.privkey is None: raise PrivateDerivationError @@ -208,18 +212,22 @@ def get_xpriv_from_path(self, path): parent_pubkey, path[-1], chaincode, - self.network, + "main-"+script if script != "p2pkh" and script else self.network, ) return base58.b58encode_check(extended_key).decode() - def get_xpub_from_path(self, path): + def get_xpub_from_path(self, path, script="p2pkh"): """Get an encoded extended pubkey from a derivation path. :param path: A list of integers (index of each depth) or a string with m/x/x'/x notation. (e.g. m/0'/1/2'/2 or m/0H/1/2H/2). + :param script: Can be "p2pkh" (legacy, xpub), "p2wpkh-p2sh" (ypub), + "p2wpkh" (zpub). Defaults to "p2pkh". :return: The encoded extended pubkey as str. """ + assert script in ["p2pkh", "p2wpkh-p2sh", "p2wpkh"] + if isinstance(path, str): path = _deriv_path_str_to_list(path) @@ -239,7 +247,7 @@ def get_xpub_from_path(self, path): parent_pubkey, path[-1], chaincode, - self.network, + "main-"+script if script != "p2pkh" and script else self.network, ) return base58.b58encode_check(extended_key).decode() diff --git a/bip32/utils.py b/bip32/utils.py index 9601992..b07c664 100644 --- a/bip32/utils.py +++ b/bip32/utils.py @@ -8,9 +8,17 @@ HARDENED_INDEX = 0x80000000 ENCODING_PREFIX = { "main": { - "private": 0x0488ADE4, + "private": 0x0488ADE4, # xpub/xprv "public": 0x0488B21E, }, + "main-p2wpkh-p2sh": { # ypub/yprv + "private": 0x049d7878, + "public": 0x049d7cb2, + }, + "main-p2wpkh": { + "private": 0x04b2430c, # zpub/zprv (bech32) + "public": 0x04b24746, + }, "test": { "private": 0x04358394, "public": 0x043587CF,