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:

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.

1 Answer 1


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.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.