javascript – Invalid checksum when generating BIP-39 Mnemonic

Free Bitcoins: FreeBitcoin | BonusBitcoin

Coins Kaufen: Bitcoin.deAnycoinDirektCoinbaseCoinMama (mit Kreditkarte)Paxfull

Handelsplätze / Börsen: Bitcoin.de | KuCoinBinanceBitMexBitpandaeToro

Lending / Zinsen erhalten: Celsius NetworkCoinlend (Bot)

Cloud Mining: HashflareGenesis MiningIQ Mining


There is a few problem in your code, in the hendecadsFromBits function, you need to use the numeric value of bit and in the generateMnemonic function, when you call hendecadsFromBits, you need to pass the result to Array.from()

Here is how you could fix 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.subtle.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 words = [];
    for (let i = 0; i < chunks.length; i++) {
        words.push(wordlist[chunks[i]]);
    }
    return words.join(' ');
};

const toBinString = (bytes) => bytes.reduce((str, byte) => str + byte.toString(2).padStart(8, '0'), '')

function* hendecadsFromBits(bits) {
    let i = 0;
    let val = 0;
    for (const bit of bits) {
        if (i == 11) {
            yield val;
            i = val = 0;
        }
        val |= parseInt(bit, 10) << i++;
    }
    if (i > 0) yield val;
}

Source link

Free Bitcoins: FreeBitcoin | BonusBitcoin

Coins Kaufen: Bitcoin.deAnycoinDirektCoinbaseCoinMama (mit Kreditkarte)Paxfull

Handelsplätze / Börsen: Bitcoin.de | KuCoinBinanceBitMexBitpandaeToro

Lending / Zinsen erhalten: Celsius NetworkCoinlend (Bot)

Cloud Mining: HashflareGenesis MiningIQ Mining

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close