Skip to content

bitcoinjs/electrum-mnemonic

Repository files navigation

Electrum Mnemonics (electrum v2 and greater)

Build Status NPM

code style: prettier

Install

npm install electrum-mnemonic

Usage

  • Generate a mnemonic (English only for now)
const mn = require('electrum-mnemonic')
console.log(mn.generateMnemonic()) // default segwit bech32 wallet
console.log(mn.generateMnemonic({ prefix: mn.PREFIXES.segwit })) // explicit segwit bech32 wallet
console.log(mn.generateMnemonic({ prefix: mn.PREFIXES.standard })) // legacy p2pkh wallet (base58 address starting with 1)
console.log(mn.generateMnemonic({ prefix: mn.PREFIXES['2fa'] })) // 2fa legacy
console.log(mn.generateMnemonic({ prefix: mn.PREFIXES['2fa-segwit'] })) // legacy p2pkh wallet (base58 address starting with 1)
  • Validate mnemonic phrase (default prefix: segwit)
const mn = require('electrum-mnemonic')
const segwitPhrase = mn.generateMnemonic();
const standardPhrase = mn.generateMnemonic({ prefix: mn.PREFIXES.standard });
console.log(mn.validateMnemonic(segwitPhrase, mn.PREFIXES.segwit)); // true
console.log(mn.validateMnemonic(standardPhrase, mn.PREFIXES.standard)); // true
  • Convert mnemonic into 64 byte seed (for bip32)
const mn = require('electrum-mnemonic')
const phrase = mn.generateMnemonic()
const seed = mn.mnemonicToSeedSync(phrase, { passphrase: 'extrapassword' })
console.log(seed)

// async
;(async () => {
  const seed2 = await mn.mnemonicToSeed(phrase)
  // no extra password so seed and seed2 should be different
  console.log(seed2)
})()
  • mnemonicToSeed prefix must match the prefix used to generate (default: segwit)
const mn = require('electrum-mnemonic')
const phrase = mn.generateMnemonic({ prefix: mn.PREFIXES.standard })
const seed = mn.mnemonicToSeedSync(phrase, { prefix: mn.PREFIXES.standard })
console.log(seed)
const segwitPhrase = mn.generateMnemonic()
const segwitSeed = mn.mnemonicToSeedSync(phrase)
console.log(segwitSeed)
  • Skip valid version prefix check
const mn = require('electrum-mnemonic')
const seed = mn.mnemonicToSeedSync('abc', { skipCheck: true })
console.log(seed)
  • Custom prefixes for checking
const mn = require('electrum-mnemonic')
const phrase = mn.generateMnemonic({ prefix: 'fed' })
const seed = mn.mnemonicToSeedSync(phrase, { prefix: 'fed' })
console.log(seed)

About

Electrum Mnemonics (electrum v2 and greater)

Resources

License

Stars

Watchers

Forks

Packages

No packages published