Mnemonic Account
Create an account from a BIP-39 mnemonic phrase with optional HD path and passphrase
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 "github.com/ChefBingbong/viem-go/accounts"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.
stringMust be valid BIP-39; use ValidateMnemonic(mnemonic) to check.
MnemonicToAccountOptions (Go) / options object (viem)Embedded fields match HDOptions: path indices or full path, plus BIP-39 passphrase.
string""BIP-39 passphrase used to derive the seed from the mnemonic.
account, err := accounts.MnemonicToAccount(mnemonic, accounts.MnemonicToAccountOptions{ Passphrase: "my-secret-passphrase",})int (Go) / number (viem)0Account index in the path segment m/44'/60'/{accountIndex}'/0/0.
int (Go) / number (viem)0Address index in the path segment m/44'/60'/0'/0/{addressIndex}.
int (Go) / number (viem)0Change index in the path segment m/44'/60'/0'/{changeIndex}/0.
stringm/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"},})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()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.
Mnemonic-derived accounts support the same operations as Private Key Account: Sign, SignMessage, SignTransaction, SignTypedData, SignAuthorization. See that page for examples.
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())}"english"). viem-go ships with the English wordlist; use accounts.English or 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) // falseMnemonicToAccount(mnemonic string, opts ...MnemonicToAccountOptions) (*HDAccount, error)MustMnemonicToAccount(mnemonic string, opts ...MnemonicToAccountOptions) *HDAccount