Skip to content

Class BSV::Primitives::Mnemonic

Inherits: Object

BIP-39 mnemonic phrase generation and seed derivation.

Generates human-readable mnemonic phrases from random entropy, validates existing phrases (word count, vocabulary, checksum), and derives 512-bit seeds for HD key generation via PBKDF2.

@example Generate a mnemonic and derive a master key

mnemonic = BSV::Primitives::Mnemonic.generate
mnemonic.phrase #=> "abandon ability able ..."
master = mnemonic.to_extended_key
master.derive_path("m/44'/0'/0'/0/0")

@example Import an existing mnemonic phrase

mnemonic = BSV::Primitives::Mnemonic.from_phrase('zoo zoo ... wrong')
mnemonic.valid? #=> true

Constants

ENGLISH_WORDLIST

rubocop:disable Metrics/CollectionLiteralLength

ENGLISH_WORD_MAP

rubocop:enable Metrics/CollectionLiteralLength

PBKDF2_ITERATIONS

PBKDF2 iteration count per BIP-39 specification.

PBKDF2_KEY_LENGTH

Output key length in bytes for PBKDF2 (512 bits).

VALID_STRENGTHS

Valid entropy strengths in bits (128 = 12 words, 256 = 24 words).

Attributes

phrase [R]

  • @return [String] the space-separated mnemonic phrase

words [R]

  • @return [Array] the individual mnemonic words

Public Class Methods

from_entropy(entropy)

Create a mnemonic from raw entropy bytes. - @param entropy [String] 16, 20, 24, 28, or 32 bytes of entropy - @raise [ArgumentError] if entropy length is invalid - @return [Mnemonic]

from_phrase(phrase)

Import and validate an existing mnemonic phrase.

Normalises the phrase (NFKD, whitespace), validates word count, vocabulary, and checksum. - @param phrase [String] space-separated mnemonic words - @raise [ArgumentError] if word count, vocabulary, or checksum is invalid - @return [Mnemonic]

generate(strength: = 128)

Generate a new random mnemonic phrase. - @param strength [Integer] entropy bits (128, 160, 192, 224, or 256) - @raise [ArgumentError] if strength is not a valid value - @return [Mnemonic] a new mnemonic with valid checksum

Public Instance Methods

==(other)

  • @param other [Object] the object to compare
  • @return [Boolean] +true+ if both mnemonics have the same phrase

to_entropy()

Extract the original entropy bytes from this mnemonic. - @return [String] the entropy bytes

to_extended_key(passphrase: = '', network: = :mainnet)

Derive a BIP-32 master extended key from this mnemonic.

Convenience method equivalent to +ExtendedKey.from_seed(to_seed)+. - @param passphrase [String] optional BIP-39 passphrase - @param network [Symbol] +:mainnet+ or +:testnet+ - @return [ExtendedKey] the master private extended key

to_s()

  • @return [String] the mnemonic phrase

to_seed(passphrase: = '')

Derive a 512-bit seed from this mnemonic using PBKDF2-HMAC-SHA-512. - @param passphrase [String] optional passphrase (default: empty string) - @return [String] 64-byte seed

valid?()

Validate the mnemonic's word count, vocabulary, and checksum. - @return [Boolean] +true+ if the mnemonic is valid