Skip to content
LogoLogo

mnemonicToAccount

A function to create a Mnemonic Account.

A Mnemonic Account is a Hierarchical Deterministic (HD) Account that is derived from a BIP-39 mnemonic phrase and an optional HD path.

It has the ability to sign transactions and messages with the private key derived from the HD Node.

Import

import {  } from 'viem/accounts'

Usage

To initialize a Mnemonic Account, you will need to pass a mnemonic phrase to mnemonicToAccount:

import { ,  } from 'viem'
import {  } from 'viem/accounts'
import {  } from 'viem/chains'
 
const  = ('legal winner thank year wave sausage worth useful legal winner thank yellow') 
 
const  = ({
  ,
  : ,
  : ()
})

Note: the above is a valid mnemonic, but it is not a "real" mnemonic. Please do not use it for anything other than testing.

Generating Mnemonics

You can generate a random BIP-39 mnemonic using the generateMnemonic function with a wordlist:

import { ,  } from 'viem/accounts'
 
const  = ()

Available wordlists:

  • czech
  • english
  • french
  • italian
  • japanese
  • korean
  • portuguese
  • simplifiedChinese
  • spanish
  • traditionalChinese

Parameters

mnemonic

  • Type: string

The BIP-39 mnemonic phrase.

const  = (
  'legal winner thank year wave sausage worth useful legal winner thank yellow'
)

options.passphrase

  • Type: string

The BIP-39 passphrase.

import { mnemonicToAccount } from 'viem/accounts'
// ---cut---
const account = mnemonicToAccount(
  'legal winner thank year wave sausage worth useful legal winner thank yellow',
  {
    passphrase: 'passphrase'
  }
)

options.accountIndex

  • Type: number
  • Default: 0

The account index to use in the path ("m/44'/60'/${accountIndex}'/0/0") to derive a private key.

const  = (
  'legal winner thank year wave sausage worth useful legal winner thank yellow',
  {
    : 1
  }
)

options.addressIndex

  • Type: number
  • Default: 0

The address index to use in the path ("m/44'/60'/0'/0/${addressIndex}") to derive a private key.

const  = (
  'legal winner thank year wave sausage worth useful legal winner thank yellow',
  {
    : 1,
    : 6
  }
)

options.changeIndex

  • Type: number
  • Default: 0

The change index to use in the path ("m/44'/60'/0'/${changeIndex}/0") to derive a private key.

const  = (
  'legal winner thank year wave sausage worth useful legal winner thank yellow',
  {
    : 1,
    : 6,
    : 2
  }
)

options.path

  • Type: "m/44'/60'/${string}"

The HD path to use to derive a private key.

const  = (
  'legal winner thank year wave sausage worth useful legal winner thank yellow',
  {
    : "m/44'/60'/5'/0/2"
  }
)