Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ A minimalistic implementation of [BIP 32](https://github.com/bitcoin/bips/blob/m
# You can also use "h" or "H" to signal for hardened derivation
>>> bip32.get_xpub_from_path("m/0h/42")
'xpub69uEaVYoN1mZyMon8qwRP41YjYyevp3YxJ68ymBGV7qmXZ9rsbMy9kBZnLNPg3TLjKd2EnMw5BtUFQCGrTVDjQok859LowMV2SEooseLCt1'
>>> bip32.get_key_origin_xkey_from_path("xpub", "m/0h/42")
'[b2b1f0cf/0h/42]xpub69uEaVYoN1mZyMon8qwRP41YjYyevp3YxJ68ymBGV7qmXZ9rsbMy9kBZnLNPg3TLjKd2EnMw5BtUFQCGrTVDjQok859LowMV2SEooseLCt1'
# You can use pubkey-only derivation
>>> bip32 = BIP32.from_xpub("xpub6AKC3u8URPxDojLnFtNdEPFkNsXxHfgRhySvVfEJy9SVvQAn14XQjAoFY48mpjgutJNfA54GbYYRpR26tFEJHTHhfiiZZ2wdBBzydVp12yU")
>>> bip32.get_xpub_from_path([42, 43])
Expand Down
21 changes: 16 additions & 5 deletions bip32/bip32.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ def __init__(
)
if index != 0:
raise InvalidInputError("Index must be 0 if depth is 0 (master xpub)")
if network not in ["main", "test"]:
raise InvalidInputError("Unknown network")

self.chaincode = chaincode
self.privkey = privkey
Expand Down Expand Up @@ -179,7 +177,7 @@ def get_pubkey_from_path(self, 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).
:return: privkey (bytes)
:return: pubkey (bytes)
"""
return self.get_extended_pubkey_from_path(path)[1]

Expand All @@ -198,7 +196,7 @@ def get_xpriv_from_path(self, path):

if len(path) == 0:
return self.get_xpriv()
elif len(path) == 1:
if len(path) == 1:
parent_pubkey = self.pubkey
else:
parent_pubkey = self.get_pubkey_from_path(path[:-1])
Expand Down Expand Up @@ -229,7 +227,7 @@ def get_xpub_from_path(self, path):

if len(path) == 0:
return self.get_xpub()
elif len(path) == 1:
if len(path) == 1:
parent_pubkey = self.pubkey
else:
parent_pubkey = self.get_pubkey_from_path(path[:-1])
Expand All @@ -245,6 +243,19 @@ def get_xpub_from_path(self, path):

return b58encode_check(extended_key).decode()

def get_key_origin_xkey_from_path(self, key_type, path):
"""Get encoded extended key with origin info from a derivation path.
:param key_type: 'xpub' for pubkeys or 'xpriv' for privkeys.
:param path: a string with m/x/x'/x notation. (e.g. m/0'/1/2'/2
or m/0H/1/2H/2).
:return: BIP380 key origin followed by the encoded extended key as str
"""
assert key_type in ["xpub", "xpriv"]
if not isinstance(path, str):
raise InvalidInputError("'path' must be string")
key = getattr(self, f"get_{key_type}_from_path")(path)
return f"[{self.get_fingerprint().hex()}{path[1:]}]{key}"

def get_xpriv(self):
"""Get the base58 encoded extended private key."""
return b58encode_check(self.get_xpriv_bytes()).decode()
Expand Down
Loading