<p>There is a few problem in your code, in the <code>hendecadsFromBits</code> function, you need to use the numeric value of bit and in the <code>generateMnemonic</code> function, when you call <code>hendecadsFromBits</code>, you need to pass the result to <code>Array.from()</code></p>
<p>Here is how you could fix your code:</p>
<pre class="lang-js prettyprint-override"><code>export const generateMnemonic = async () =&gt; {
    const ent = window.crypto.getRandomValues(new Uint8Array(16));
    const entBits = toBinString(ent);
    const entHash = Array.from(new Uint8Array(
        await window.crypto.subtle.digest(&quot;SHA-256&quot;, 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 &lt; chunks.length; i++) {
        words.push(wordlist[chunks[i]]);
    }
    return words.join(' ');
};

const toBinString = (bytes) =&gt; bytes.reduce((str, byte) =&gt; 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) &lt;&lt; i++;
    }
    if (i &gt; 0) yield val;
}
</code></pre>

CC BY-SA 4.0
2023-04-03T12:03:56.553Z
2024-02-22T17:37:40.810Z
2024-02-22T17:37:40.810Z
Saxtheowl
<p>I am trying to generate a mnemonic seed in Javascript, and while everything seems to be working, I am not getting a mnemonic with a correct checksum. Here is my implementation:</p>
<pre class="lang-js prettyprint-override"><code>export const generateMnemonic = async () =&gt; {
    const ent = window.crypto.getRandomValues(new Uint8Array(16));
    const entBits = toBinString(ent);
    const entHash = Array.from(new Uint8Array(
        await window.crypto.subtle.digest(&quot;SHA-256&quot;, 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 &lt; chunks.length; i++) {
        words.push(wordlist[chunks[i]]);
    }
    return words;
};

const toBinString = (bytes) =&gt; bytes.reduce((str, byte) =&gt; 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 |= bit &lt;&lt; i++;
    }
    if (i &gt; 0) yield val;
}
</code></pre>
<p>Is it because I am padding the bytes with zeros?</p>
<p>I would really appreciate your help!</p>

<mnemonic-seed><javascript>
Invalid checksum when generating BIP-39 Mnemonic
Egge
# Invalid checksum when generating BIP-39 Mnemonic

(Asked by: username_0 on 2023-04-01)

I am trying to generate a mnemonic seed in Javascript, and while everything seems to be working, I am not getting a mnemonic with a correct checksum. Here is my implementation:
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;
};

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 |= bit << i++;
    }
    if (i > 0) yield val;
}

Is it because I am padding the bytes with zeros?
I would really appreciate your help!

**Tags:** mnemonic-seed, javascript


---

## 1 answers

### Answer by username_1 on 2023-04-03. Score: 0

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;
}


---

