bitcoinjs/bip39

Recovery using only the first four letters of each word

paulyoung opened this issue · 1 comments

Some backup solutions only allow storing the first 4 letters in accordance with the BIP39 protocol.

My understanding is that the first-four-letter combinations are unique and sufficient to recover the full word.

Does this package support that?

It’s something I’m trying to figure out for dfinity/internet-identity#335

No. This is easily done with 1 line. Maybe a couple extra lines to check inputs.

function findFirstFour(firstFour, wordlist) {
  if (typeof firstFour !== 'string' || firstFour.length !== 4) throw new Error('Need 4 characters')
  if (!Array.isArray(wordlist) || wordlist.length !== 2048 || wordlist[1353] !== 'powder') throw new Error('Use bip39 English wordlist')

  return wordlist.filter(s => s.startsWith(firstFour))[0]
}