<p>You are running into the gap limit.</p>
<p>When restoring a wallet from a mnemonic, the wallet software checks the generated addresses one by one until it finds a long enough gap of addresses that have never been used in a transaction. It then assumes that no other used addresses exist beyond that point. The gap limit is set to 20 in most wallets, which will usually be less than a random number between 0 and 99,999.</p>
<p>References:</p>
<ul>
<li><a href="https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#address-gap-limit" rel="nofollow noreferrer">BIP44: Address gap limit</a></li>
<li><a href="https://electrum.readthedocs.io/en/latest/faq.html#what-is-the-gap-limit" rel="nofollow noreferrer">Electrum Docs: What is the gap limit?</a></li>
</ul>

CC BY-SA 4.0
2023-12-10T09:02:55.867Z
2023-12-10T09:02:55.867Z
Vojtěch Strnad
<p>I'm working on a Node.js app that makes RPC calls to my node, and I'm having trouble restoring a mnemonic created with bitcoinjs-lib, bip32, and bip39.</p>
<p>First, I create a blank wallet that accepts private keys with <code>bitcoin-cli createwallet wallet false true</code></p>
<p>I also create a separate file called &quot;wallet.json&quot; and it stores the mnemonic. Then I run this code to generate a new address:</p>
<pre class="lang-js prettyprint-override"><code>app.get('/getnewaddress', function (req, res) {                                                                                  
    const data = fs.readFileSync(`wallet.json`, 'utf8');            
    const { mnemonic } = JSON.parse(data);                                                                                          
    const seed = bip39.mnemonicToSeedSync(mnemonic);                                                                                
    const node = bip32.fromSeed(seed);                                                                 
    const index = Math.floor(Math.random() * 100000);                                                                               
    const keyPair = node.derivePath(`m/84'/0'/0'/0/${index}`);                                                                       
    const { address } = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey });                                                                                                                       

    res.send(JSON.stringify({address: address}));                                                                                  

    bitcoin_rpc.call('importprivkey', [keyPair.toWIF(), &quot;&quot;, false], function (err, rpcRes) {
        if (err) {
            res.status(500).send({ error: &quot;I have an error :\n&quot; + err });  
        } else {                                                                                                                                
            console.log(`Address: ${address}`);                             
            console.log(`Private key: ${keyPair.toWIF()}`);                                                                                                                                                 
        }                                                              
    });                                                                                                                             
});
</code></pre>
<p>I know the code is working, because when I send bitcoin to the generated address, the balance is confirmed on my node. You can see I'm logging the private keys and importing them into the blank wallet. When I dump the wallet, all the private keys and addresses are there, and match what was logged.</p>
<p>However, when I try to restore this wallet in BlueWallet or Electrum, it says my balance is 0.</p>
<p>Please notice I am not trying to import a mnemonic into Bitcoin Core or set the hdseed. I am only importing individual keys.</p>

<json-rpc><bip32-hd-wallets><mnemonic-seed><bitcoinjs><nodejs>
Mnemonic from Node.js app not restoring in other wallets
Cannabijoy
# Mnemonic from Node.js app not restoring in other wallets

(Asked by: username_0 on 2023-12-10)

I'm working on a Node.js app that makes RPC calls to my node, and I'm having trouble restoring a mnemonic created with bitcoinjs-lib, bip32, and bip39.

First, I create a blank wallet that accepts private keys with `bitcoin-cli createwallet wallet false true`

I also create a separate file called "wallet.json" and it stores the mnemonic. Then I run this code to generate a new address:

```js
app.get('/getnewaddress', function (req, res) {                                                                                  
    const data = fs.readFileSync(`wallet.json`, 'utf8');            
    const { mnemonic } = JSON.parse(data);                                                                                          
    const seed = bip39.mnemonicToSeedSync(mnemonic);                                                                                
    const node = bip32.fromSeed(seed);                                                                 
    const index = Math.floor(Math.random() * 100000);                                                                               
    const keyPair = node.derivePath(`m/84'/0'/0'/0/${index}`);                                                                       
    const { address } = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey });                                                                                                                       

    res.send(JSON.stringify({address: address}));                                                                                  

    bitcoin_rpc.call('importprivkey', [keyPair.toWIF(), "", false], function (err, rpcRes) {
        if (err) {
            res.status(500).send({ error: "I have an error :\n" + err });  
        } else {                                                                                                                                
            console.log(`Address: ${address}`);                             
            console.log(`Private key: ${keyPair.toWIF()}`);                                                                                                                                                 
        }                                                              
    });                                                                                                                             
});

```

I know the code is working, because when I send bitcoin to the generated address, the balance is confirmed on my node. You can see I'm logging the private keys and importing them into the blank wallet. When I dump the wallet, all the private keys and addresses are there, and match what was logged.

However, when I try to restore this wallet in BlueWallet or Electrum, it says my balance is 0.

Please notice I am not trying to import a mnemonic into Bitcoin Core or set the hdseed. I am only importing individual keys.

**Tags:** json-rpc, bip32-hd-wallets, mnemonic-seed, bitcoinjs, nodejs


---

## 1 answers

### Answer by username_1 on 2023-12-10. Score: 3 | Accepted answer

You are running into the gap limit.

When restoring a wallet from a mnemonic, the wallet software checks the generated addresses one by one until it finds a long enough gap of addresses that have never been used in a transaction. It then assumes that no other used addresses exist beyond that point. The gap limit is set to 20 in most wallets, which will usually be less than a random number between 0 and 99,999.

References:

* [BIP44: Address gap limit](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#address-gap-limit)
* [Electrum Docs: What is the gap limit?](https://electrum.readthedocs.io/en/latest/faq.html#what-is-the-gap-limit)


---

