Skip to main content

Crate bip0032

Crate bip0032 

Source
Expand description

§bip0032

Another Rust implementation of BIP-0032 standard.

§Support curves and features

CurveFeatureBackendsHardenedNon-hardened (private)Non-hardened (public)Serialization
secp256k1k256 | secp256k1 | libsecp256k1k256, secp256k1, libsecp256k1yesyesyesyes

§Usage

Seed material is typically derived from a BIP-0039 mnemonic (for example, via bip0039).

use bip0039::{Count, English, Mnemonic};

let mnemonic = <Mnemonic<English>>::generate(Count::Words12);
let seed = mnemonic.to_seed("");

The examples below assume the seed from above.

  1. Private parent key -> private child key (supports hardened).
use bip0032::{DerivationPath, ExtendedPrivateKey, Version, curve::secp256k1::*};

let master = ExtendedPrivateKey::<Secp256k1Curve<K256Backend>>::new(&seed).unwrap();
let path: DerivationPath = "m/0H/1".parse().unwrap();
let child = master.derive_path(&path).unwrap();
let xprv = child
    .encode_with(Version::XPRV)
    .unwrap()
    .to_string();
  1. Private parent key -> public child key.
use bip0032::{DerivationPath, ExtendedPrivateKey, Version, curve::secp256k1::*};

let master = ExtendedPrivateKey::<Secp256k1Curve<K256Backend>>::new(&seed).unwrap();
let path: DerivationPath = "m/0H/1".parse().unwrap();
let child = master.derive_path(&path).unwrap();
let xpub = child
    .public_key()
    .encode_with(Version::XPUB)
    .unwrap()
    .to_string();
  1. Public parent key -> public child key (non-hardened only).
use bip0032::{DerivationPath, ExtendedPublicKey, Version, curve::secp256k1::*};

let parent_xpub = "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8";
let parent: ExtendedPublicKey<Secp256k1Curve<K256Backend>> = parent_xpub.parse().unwrap();
let path: DerivationPath = "m/0/1".parse().unwrap();
let child = parent.derive_path(&path).unwrap();
let xpub = child
    .encode_with(Version::XPUB)
    .unwrap()
    .to_string();
  1. Public parent key -> private child key: impossible (BIP-0032 does not allow it).

§SLIP-0010 (optional)

SLIP-0010 support is available behind the slip10 feature. It shares the same ExtendedPrivateKey/ExtendedPublicKey types but uses SLIP-0010 derivation rules.

Modules§

curve
Curve abstractions and specific curve implementations.
slip10slip10
SLIP-0010

Structs§

ChildNumber
A BIP32 child number with optional hardened flag.
DerivationPath
A parsed BIP32 derivation path.
Error
Error type for bip0032 operations.
ErrorSource
The lower-level source of Error.
ExtendedKeyPayload
Base58Check-encoded extended key payload plus version metadata.
ExtendedPrivateKey
A BIP32 extended private key.
ExtendedPublicKey
A BIP32 extended public key.
HardenedChildNumber
A hardened-only child number.
HardenedDerivationPath
A derivation path containing only hardened components.

Enums§

ErrorKind
Error categories for bip0032 operations.
KnownVersion
Standard extended key versions (BIP32 and common extensions).
Version
Extended key version bytes.

Traits§

IntoErrorSource
Conversion trait for attaching source errors.

Type Aliases§

Result
Result type for bip0032 operations.