Skip to content

Commit

Permalink
fix for #6 and docstring, typehints, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtraub91 committed Nov 26, 2023
1 parent 14e0479 commit 205c7d5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
14 changes: 14 additions & 0 deletions src/bits/bips/bip32.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,19 @@ def deserialized_extended_key(
) -> Union[Tuple[bytes, bytes, bytes, bytes, bytes, Union[int, Tuple[int, int]]], dict]:
"""
De-serialize extended key. Checks for invalid keys
Returns:
(version, depth, parent_key_fingerprint, child_no, key)
Or (if return_dict=True)
{
"version"
"depth"
"parent_key_fingerprint"
"child_no"
"chaincode"
"key"
}
"""
decoded = base58check_decode(xkey)
assert len(decoded) == 78
Expand Down Expand Up @@ -247,6 +260,7 @@ def deserialized_extended_key(
"depth": int.from_bytes(depth, "big"),
"parent_key_fingerprint": parent_key_fingerprint.hex(),
"child_no": int.from_bytes(child_no, "big"),
"chaincode": chaincode.hex(),
"key": key.hex(),
}
return version, depth, parent_key_fingerprint, child_no, chaincode, key
2 changes: 1 addition & 1 deletion src/bits/ecmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def sqrt_mod_p(x: int, p: int = SECP256K1_P) -> int:
raise NotImplementedError


def y_from_x(x: int, a: int = SECP256K1_A, b: int = SECP256K1_B) -> int:
def y_from_x(x: int, a: int = SECP256K1_A, b: int = SECP256K1_B) -> Tuple[int]:
"""
Find y from x
y^2 = x^3 + ax + b
Expand Down
8 changes: 4 additions & 4 deletions src/bits/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ def point(pubkey_: bytes) -> typing.Tuple[int]:
payload = pubkey_[1:]
x = int.from_bytes(payload[:32], "big")
if version == 2:
# compressed, y
y = bits.ecmath.y_from_x(x)[0]
# compressed, y even
y = [i for i in bits.ecmath.y_from_x(x) if not i % 2][0]
elif version == 3:
# compressed, -y
y = bits.ecmath.y_from_x(x)[1]
# compressed, y odd
y = [i for i in bits.ecmath.y_from_x(x) if i % 2][0]
elif version == 4:
# uncompressed
y = int.from_bytes(payload[32:], "big")
Expand Down
13 changes: 12 additions & 1 deletion src/bits/wallet/hd.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def get_xpub(xkey: bytes):
Return xpub from xprv (or xpub)
Args:
xprv_: bytes, serialized extended private key
>>> xpub = b'xpub6H5w431hKsmLXYzEwpyuZ2BmqGzbf7zBanX8Fg8dQFUDmKNLMGKom15X1go8QBoQn2BrZrNweRwVVPjYaFtbpuk7MzCdMkHdnNB8fywmjyh'
>>> get_xpub(xpub)
b'xpub6H5w431hKsmLXYzEwpyuZ2BmqGzbf7zBanX8Fg8dQFUDmKNLMGKom15X1go8QBoQn2BrZrNweRwVVPjYaFtbpuk7MzCdMkHdnNB8fywmjyh'
"""
(
version,
Expand All @@ -29,7 +33,14 @@ def get_xpub(xkey: bytes):
if type(key) is int:
key = bip32.point(key)
return bip32.serialized_extended_key(
key, chaincode, depth, parent_key_fingerprint, child_no
key,
chaincode,
depth,
parent_key_fingerprint,
child_no,
testnet=True
if version in [bip32.VERSION_PRIVATE_TESTNET, bip32.VERSION_PUBLIC_TESTNET]
else False,
)


Expand Down

0 comments on commit 205c7d5

Please sign in to comment.