MetaMask uses BIP39 standard for seed phrase.
This BIP describes the implementation of a mnemonic code or mnemonic sentence – a group of easy to remember words – for the generation of deterministic wallets.
It consists of two parts: generating the mnemonic and converting it into a binary seed.
Theoretically, you can generate the mnemonic yourself and pass it as a parameter to:
const HDWalletProvider = require("@truffle/hdwallet-provider");
let provider = new HDWalletProvider({
mnemonic: {
phrase: mnemonicPhrase
},
providerOrUrl: "http://localhost:8545"
});
How is the creation of mnemonics under the hood?
-
Generate 128 bits of entropy (a random hash).
-
Each character can then be represented in binary form.
-
We then generate a check sum by running our above entropy through the SHA256 algorithm (this hashing algorithm is [also used in bitcoin address generation]
-
Taking the first
*n*
bits where n is calculated as*the length of our entropy/32
.
S* o as 128/32=4 we’re taking the first 4 bits which is equivalent to the first character*f*
. This is represented as*1111*
in binary, so we append this to the end of our entropy. -
We then need to split into groups of 11 and convert to decimal as this is going to allow us to match these up to our wordlist!
-
Each of these 12 11-bit groups are now represented by a number between 0 and 2047 which acts as an index to a word list of 2048 unique words which make up the seed phrase.
tip: You can find the English dictionary on github in the bitcoin repository
We have created the 12 word mnemonic which can be used to restore access to our wallet. In the case that we need to regenerate the entropy from the seed phrase we simply do the whole process backwards!