Mnemonic Account
Create an account from a BIP-39 mnemonic phrase with optional HD path and passphrase
A Mnemonic Account is an HD Account derived from a BIP-39 mnemonic phrase and an optional HD path. It can sign transactions and messages with the private key derived from that path.
viem-go uses go-bip39 and go-bip32 for BIP-39 and BIP-32 HD derivation.
Import
import "github.com/ChefBingbong/viem-go/accounts"Usage
Pass a BIP-39 mnemonic phrase. By default the path m/44'/60'/0'/0/0 is used. Optionally pass MnemonicToAccountOptions for path, passphrase, or index overrides.
import "github.com/ChefBingbong/viem-go/accounts"
account, err := accounts.MnemonicToAccount( "test test test test test test test test test test test junk",)if err != nil { log.Fatal(err)}
fmt.Printf("Address: %s", account.GetAddress())// Address: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266The mnemonic in the example is for testing only. Do not use it for real funds.
Parameters
mnemonic
- Type:
string - Format: BIP-39 mnemonic phrase (e.g. 12 or 24 words from the wordlist).
Must be valid BIP-39; use ValidateMnemonic(mnemonic) to check.
options (optional)
- Type:
MnemonicToAccountOptions(Go) / options object (viem)
Embedded fields match HDOptions: path indices or full path, plus BIP-39 passphrase.
options.passphrase
- Type:
string - Default:
""
BIP-39 passphrase used to derive the seed from the mnemonic.
account, err := accounts.MnemonicToAccount(mnemonic, accounts.MnemonicToAccountOptions{ Passphrase: "my-secret-passphrase",})options.accountIndex
- Type:
int(Go) /number(viem) - Default:
0
Account index in the path segment m/44'/60'/{accountIndex}'/0/0.
options.addressIndex
- Type:
int(Go) /number(viem) - Default:
0
Address index in the path segment m/44'/60'/0'/0/{addressIndex}.
options.changeIndex
- Type:
int(Go) /number(viem) - Default:
0
Change index in the path segment m/44'/60'/0'/{changeIndex}/0.
options.path
- Type:
string - Format:
m/44'/60'/${string}(e.g.m/44'/60'/5'/0/2)
When set, overrides the index-based path. Use this for a fully custom derivation path.
// Default: m/44'/60'/0'/0/0account0, _ := accounts.MnemonicToAccount(mnemonic)
// Second address: m/44'/60'/0'/0/1account1, _ := accounts.MnemonicToAccount(mnemonic, &accounts.MnemonicToAccountOptions{ HDOptions: accounts.HDOptions{AddressIndex: 1},})
// Custom pathaccount, _ := accounts.MnemonicToAccount(mnemonic, &accounts.MnemonicToAccountOptions{ HDOptions: accounts.HDOptions{Path: "m/44'/60'/5'/0/2"},})Generating mnemonics
Generate a BIP-39 mnemonic with GenerateMnemonic. Strength is specified via GenerateMnemonicOptions{Strength: ...}; strength must be 128, 160, 192, 224, or 256 (bits of entropy). Default is 128 (12 words); use accounts.Mnemonic256 for 24 words.
import "github.com/ChefBingbong/viem-go/accounts"
// 12-word mnemonic (128 bits)mnemonic12, err := accounts.GenerateMnemonic()if err != nil { log.Fatal(err)}
// 24-word mnemonic (256 bits)mnemonic24, err := accounts.GenerateMnemonic(accounts.GenerateMnemonicOptions{ Strength: accounts.Mnemonic256,})if err != nil { log.Fatal(err)}
// Must variant (panics on error)mnemonic := accounts.MustGenerateMnemonic()HD path explanation
The standard Ethereum path m/44'/60'/0'/0/0:
| Component | Meaning |
|---|---|
m | Master node |
44' | BIP-44 purpose (hardened) |
60' | Ethereum coin type (hardened) |
0' | Account index (hardened) |
0 | Change (0 = external, 1 = internal) |
0 | Address index |
Use accounts.DefaultHDPath(accountIndex, changeIndex, addressIndex) to build the path string in Go.
Signing and capabilities
Mnemonic-derived accounts support the same operations as Private Key Account: Sign, SignMessage, SignTransaction, SignTypedData, SignAuthorization. See that page for examples.
Deriving multiple accounts
Derive several addresses from one mnemonic by varying AddressIndex or Path:
mnemonic := "test test test test test test test test test test test junk"
accounts := make([]*accounts.HDAccount, 10)for i := 0; i < 10; i++ { accounts[i], _ = accounts.MnemonicToAccount(mnemonic, &accounts.MnemonicToAccountOptions{ HDOptions: accounts.HDOptions{AddressIndex: i}, }) fmt.Printf("Account %d: %s", i, accounts[i].GetAddress())}Validation and wordlists
- ValidateMnemonic(mnemonic string) bool — returns whether the phrase is valid BIP-39.
- GetWordlist(name string) — returns a wordlist by name (e.g.
"english"). viem-go ships with the English wordlist; useaccounts.Englishor get by name for validation/display.
valid := accounts.ValidateMnemonic("test test test test test test test test test test test junk")fmt.Println(valid) // true
valid = accounts.ValidateMnemonic("invalid words here")fmt.Println(valid) // falseReturn type
- Go:
MnemonicToAccount(mnemonic string, opts ...MnemonicToAccountOptions) (*HDAccount, error) - Must variant:
MustMnemonicToAccount(mnemonic string, opts ...MnemonicToAccountOptions) *HDAccount
Error handling
- ErrInvalidMnemonic — mnemonic phrase is not valid BIP-39.
- ErrInvalidHDPath — derivation path is invalid or derivation failed.
Go-only helpers
- MnemonicToSeed(mnemonic, passphrase string) []byte — raw BIP-39 seed.
- SeedToHDKey(seed []byte) (HDKey, error) — master HD key from seed; then use HDKeyToAccount with a path.
- MnemonicToEntropy / EntropyToMnemonic — convert between mnemonic and entropy bytes.