BSV.Mnemonic (bsv_sdk v1.5.1)

Copy Markdown View Source

BIP-39 mnemonic phrase generation and seed derivation.

A mnemonic is a human-readable representation of entropy used to create deterministic wallets. Words are drawn from a standard 2048-word list and include a built-in checksum.

Note: The wordlist language is fixed at compile time and defaults to English. Non-English BIP-39 wordlists are not currently supported.

Examples

iex> mnemonic = BSV.Mnemonic.generate()
iex> is_binary(mnemonic) and length(String.split(mnemonic)) == 12
true

iex> seed = BSV.Mnemonic.to_seed("abandon " |> String.duplicate(11) |> Kernel.<>("about") |> String.trim())
iex> byte_size(seed) == 64
true

Summary

Types

Entropy bit length

Binary seed derived from mnemonic

t()

Mnemonic phrase (space-separated words)

Functions

Create a mnemonic phrase from raw entropy bytes.

Generate a random mnemonic phrase.

Extract the raw entropy bytes from a mnemonic phrase.

Derive a 512-bit seed from a mnemonic phrase using PBKDF2-HMAC-SHA512.

Validate a mnemonic phrase (correct word count, valid words, valid checksum).

Types

entropy_bits()

@type entropy_bits() :: 128 | 160 | 192 | 224 | 256

Entropy bit length

seed()

@type seed() :: <<_::512>>

Binary seed derived from mnemonic

t()

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

Mnemonic phrase (space-separated words)

Functions

from_entropy(entropy)

@spec from_entropy(binary()) :: t()

Create a mnemonic phrase from raw entropy bytes.

The entropy must be 16, 20, 24, 28, or 32 bytes (128–256 bits).

generate(entropy_bits \\ 128)

@spec generate(entropy_bits()) :: t()

Generate a random mnemonic phrase.

Options

  • entropy_bits — one of 128 (12 words), 160 (15), 192 (18), 224 (21), 256 (24). Defaults to 128.

to_entropy(mnemonic)

@spec to_entropy(t()) :: binary()

Extract the raw entropy bytes from a mnemonic phrase.

to_seed(mnemonic, opts \\ [])

@spec to_seed(
  t(),
  keyword()
) :: seed() | String.t()

Derive a 512-bit seed from a mnemonic phrase using PBKDF2-HMAC-SHA512.

Options

  • :passphrase — optional passphrase (BIP-39 "25th word"). Default "".
  • :encoding:hex or :base64 to encode the result. Default nil (raw binary).

valid?(mnemonic)

@spec valid?(t()) :: boolean()

Validate a mnemonic phrase (correct word count, valid words, valid checksum).