BSV.ExtKey (bsv_sdk v1.5.0)

Copy Markdown View Source

BIP-32 hierarchical deterministic (HD) extended keys.

Supports creating master keys from a seed (or mnemonic), deriving child keys via derivation paths (e.g. "m/44'/0'/0'/0/0"), and serializing to/from xprv/xpub format.

Examples

iex> {:ok, master} = BSV.ExtKey.from_seed(<<0::512>>)
iex> master.depth
0

iex> child = BSV.ExtKey.derive(master, "m/44'/0'/0'")
iex> child.depth
3

Summary

Types

BIP-32 derivation path

t()

Extended key

Serialized xprv string

Serialized xpub string

Functions

Derive a child key from a derivation path.

Create a master extended key from a binary seed (16–64 bytes).

As from_seed/2 but raises on error.

Decode an xprv or xpub string into an extended key.

As from_string/1 but raises on error.

Generate a new random master extended key.

Convert an extended private key to its public counterpart (drops the private key).

Serialize an extended key to xprv/xpub string.

Types

derivation_path()

@type derivation_path() :: String.t()

BIP-32 derivation path

t()

@type t() :: %BSV.ExtKey{
  chain_code: <<_::256>>,
  child_index: non_neg_integer(),
  depth: non_neg_integer(),
  fingerprint: <<_::32>>,
  privkey: BSV.PrivateKey.t() | nil,
  pubkey: BSV.PublicKey.t(),
  version: binary()
}

Extended key

xprv()

@type xprv() :: String.t()

Serialized xprv string

xpub()

@type xpub() :: String.t()

Serialized xpub string

Functions

derive(key, path)

@spec derive(t(), derivation_path()) :: t()

Derive a child key from a derivation path.

Paths follow BIP-32 format: "m/44'/0'/0'/0/0" for private derivation, "M/44'/0'/0'/0/0" for public derivation.

Hardened indices use ' suffix. Hardened public derivation is not possible.

from_seed(seed, opts \\ [])

@spec from_seed(
  binary(),
  keyword()
) :: {:ok, t()} | {:error, term()}

Create a master extended key from a binary seed (16–64 bytes).

Typically the seed comes from BSV.Mnemonic.to_seed/2.

from_seed!(seed, opts \\ [])

@spec from_seed!(
  binary(),
  keyword()
) :: t()

As from_seed/2 but raises on error.

from_string(str)

@spec from_string(xprv() | xpub()) :: {:ok, t()} | {:error, term()}

Decode an xprv or xpub string into an extended key.

from_string!(str)

@spec from_string!(xprv() | xpub()) :: t()

As from_string/1 but raises on error.

new()

@spec new() :: t()

Generate a new random master extended key.

to_public(k)

@spec to_public(t()) :: t()

Convert an extended private key to its public counterpart (drops the private key).

to_string(k)

@spec to_string(t()) :: xprv() | xpub()

Serialize an extended key to xprv/xpub string.