BIP-32
Extended keys over secp256k1 with xprv/xpub serialization and path parsing.
from oxcrypto.hd.bip32 import BIP32
master = BIP32.SECP256K1.load_seed(bytes.fromhex("000102030405060708090a0b0c0d0e0f"))
child = master.derive_path("m/0'/1")
assert child.to_xprv().startswith("xprv9wTYmMFdV23N2TdNG573")
public = BIP32.SECP256K1.load_xpub(child.public_key().to_xpub())
assert public.derive_child(2) == child.derive_child(2).public_key()load_seed derives the master key from a 16- to 64-byte seed, and
load_mnemonic from a BIP-39 mnemonic and passphrase.
derive_child(index) derives one child: indexes from 0x80000000 are
hardened and need the private key, so public keys raise ValueError for them.
If a derived key is invalid, which BIP-32 says happens with probability below
2-127, coins-bip32 moves on to the next index, as BIP-32
specifies.
derive_path accepts paths such as m/44'/0'/0'/0/0; ' or h marks a
hardened step. Paths that start with m are only accepted on depth-0 keys;
on other keys, pass a relative path such as 0/1.
to_xprv and to_xpub return the 78-byte Base58Check serialization.
load_xprv and load_xpub accept the BIP-32 mainnet (xprv/xpub) and
testnet (tprv/tpub) versions and keep the network when serializing again.
They raise ValueError for SLIP-132 versions such as zprv, a bad checksum,
a key that is not on the curve or not in 1..n-1, and depth-0 keys with a
nonzero parent fingerprint or child number.
Converting keys
to_ecdsa() returns an ECDSA.SECP256K1 private or public key from
oxcrypto.signature.ecdsa, and to_schnorr() returns a BIP-340 key from
oxcrypto.signature.schnorr. BIP-340 keys are normalized to an even-y public
key, so to_schnorr().to_bytes() can return n - k for the private key k.
Methods
BIP32.load_seed(seed: bytes, /) -> ExtendedPrivateKey
BIP32.load_mnemonic(mnemonic: Mnemonic, /, *, passphrase: str = "") -> ExtendedPrivateKey
BIP32.load_xprv(data: str, /) -> ExtendedPrivateKey
BIP32.load_xpub(data: str, /) -> ExtendedPublicKey
# Both key classes
.algorithm -> BIP32
.depth -> int
.child_number -> int
.parent_fingerprint -> bytes
.fingerprint -> bytes
.derive_child(index: int, /)
.derive_path(path: str, /)
.to_ecdsa()
.to_schnorr()
ExtendedPrivateKey.public_key() -> ExtendedPublicKey
ExtendedPrivateKey.to_xprv() -> str
ExtendedPublicKey.to_xpub() -> strUpstream
- Standard: BIP-32 (Deployed).
- Crate:
coins-bip320.17.2 onk2560.14. - Tests use BIP-32 test vectors 1 to 5.
See Hierarchical deterministic keys for zeroization notes.