![javascript – Invalid checksum when producing BIP-39 Mnemonic javascript – Invalid checksum when producing BIP-39 Mnemonic](https://cillionairee.com/wp-content/uploads/https://cdn.sstatic.net/Sites/bitcoin/Img/apple-touch-icon@2.png?v=b4864ba760f1)
[ad_1]
There’s a few drawback in your code, within the hendecadsFromBits
perform, you have to use the numeric worth of bit and within the generateMnemonic
perform, if you name hendecadsFromBits
, you have to move the end result to Array.from()
Right here is how you would repair your code:
export const generateMnemonic = async () => {
const ent = window.crypto.getRandomValues(new Uint8Array(16));
const entBits = toBinString(ent);
const entHash = Array.from(new Uint8Array(
await window.crypto.delicate.digest("SHA-256", ent)
));
const entHashBits = toBinString(entHash)
const checksum = entHashBits.slice(0, 4);
const entCS = entBits + checksum;
const chunks = Array.from(hendecadsFromBits(entCS));
const phrases = [];
for (let i = 0; i < chunks.size; i++) {
phrases.push(wordlist[chunks[i]]);
}
return phrases.be a part of(' ');
};
const toBinString = (bytes) => bytes.cut back((str, byte) => str + byte.toString(2).padStart(8, '0'), '')
perform* hendecadsFromBits(bits) {
let i = 0;
let val = 0;
for (const little bit of bits) {
if (i == 11) {
yield val;
i = val = 0;
}
val |= parseInt(bit, 10) << i++;
}
if (i > 0) yield val;
}
[ad_2]