<p>As commented <a href="https://github.com/bcoin-org/bcoin/issues/570#issuecomment-413719125" rel="nofollow noreferrer">here</a>, the cause of the problem is that I</p>

<blockquote>
  <p>sorta have created two wallets. The first is instantiated by the plugin statement towards the top, and then you also create a new walletDB right after.</p>
</blockquote>

<p>So the solution is using <code>WalletClient</code>:</p>

<pre><code>const {WalletClient} = require('bclient');
const {Network} = require('bcoin');
const network = Network.get('regtest');

const walletOptions = {
  network: network.type,
  port: network.walletPort,
  apiKey: 'api-key'
}

const walletClient = new WalletClient(walletOptions);
const id = 'primary'; // or whatever your wallet name is
const wallet = walletClient.wallet(id);
</code></pre>

<p>Instead of <code>walletdb.create()</code>.</p>

CC BY-SA 4.0
2018-08-17T09:11:47.780Z
2018-08-19T11:17:21.983Z
2018-08-19T11:17:21.983Z
Ales Tsurko
<p>I created a wallet on a SPV node. The wallet is initialized from a mnemonic. After receiving some transactions, the wallet's balance looks like:</p>



<pre><code>{ account: -1, tx: 3, coin: 3, unconfirmed: 136300, confirmed: 0 }
</code></pre>

<p>But when I re-initialize the wallet, it looks so:
</p>

<pre><code>{ account: -1, tx: 0, coin: 0, unconfirmed: 0, confirmed: 0 }
</code></pre>

<p>On the each initialization the wallet have the same address.</p>

<p>Here is my code:</p>



<pre><code>const bcoin = require('bcoin');
bcoin.set('testnet');
const KeyRing = bcoin.keyring;
const Mnemonic = bcoin.hd.Mnemonic;
const HD = bcoin.hd;

const node = new bcoin.node.SPVNode({
    config: true,
    argv: true,
    env: true,
    logFile: true,
    logConsole: true,
    logLevel: 'debug',
    db: 'leveldb',
    memory: false,
    persistent: true,
    workers: true,
    listen: true,
    loader: require,
    network: 'testnet'
});

// Temporary hack
if (!node.has('walletdb')) {
    const plugin = require('./node_modules/bcoin/lib/wallet/plugin');
    node.use(plugin);
}

process.on('unhandledRejection', (err, promise) =&gt; {
  throw err;
});

const walletdb = new bcoin.wallet.WalletDB({ memory: false, network: 'testnet', prefix: '/Users/alestsurko/.bcoin/spvchain' });

(async () =&gt; {
    await node.ensure();
    await node.open();
    await node.connect();
    await walletdb.open();
    const mnemonic = new Mnemonic('uncover cash coral neglect upon nurse argue deal right song hood tennis');
    const masterKey = HD.fromMnemonic(mnemonic);

    const wallet = await walletdb.create({master: masterKey});

    console.log('Created wallet with address %s', await wallet.receiveAddress());
    const bl = await wallet.getBalance();
    console.log(bl.toJSON());

    // Add our address to the spv filter.
    node.pool.watchAddress(await wallet.receiveAddress());

    node.startSync();

    node.on('error', async (err) =&gt; {
        console.log(err);
    });

    node.pool.on('tx', async (tx) =&gt; {
        console.log('------ New tx. Adding to walletdb...');
        console.log(tx);
        await walletdb.addTX(tx);
    });

    wallet.on('balance', async (balance) =&gt; {
        console.log('Balance updated.');
        console.log(balance.toJSON());
    });
})().catch((err) =&gt; {
    console.error(err.stack);
    process.exit(1);
});
</code></pre>

<wallet><balance><spv><javascript><bcoin>
How to make a persistent SPV wallet? (bcoin)
Ales Tsurko
# How to make a persistent SPV wallet? (bcoin)

(Asked by: username_0 on 2018-08-16)

I created a wallet on a SPV node. The wallet is initialized from a mnemonic. After receiving some transactions, the wallet's balance looks like:

```
{ account: -1, tx: 3, coin: 3, unconfirmed: 136300, confirmed: 0 }

```

But when I re-initialize the wallet, it looks so:

```
{ account: -1, tx: 0, coin: 0, unconfirmed: 0, confirmed: 0 }

```

On the each initialization the wallet have the same address.

Here is my code:

```
const bcoin = require('bcoin');
bcoin.set('testnet');
const KeyRing = bcoin.keyring;
const Mnemonic = bcoin.hd.Mnemonic;
const HD = bcoin.hd;

const node = new bcoin.node.SPVNode({
    config: true,
    argv: true,
    env: true,
    logFile: true,
    logConsole: true,
    logLevel: 'debug',
    db: 'leveldb',
    memory: false,
    persistent: true,
    workers: true,
    listen: true,
    loader: require,
    network: 'testnet'
});

// Temporary hack
if (!node.has('walletdb')) {
    const plugin = require('./node_modules/bcoin/lib/wallet/plugin');
    node.use(plugin);
}

process.on('unhandledRejection', (err, promise) => {
  throw err;
});

const walletdb = new bcoin.wallet.WalletDB({ memory: false, network: 'testnet', prefix: '/Users/username_0/.bcoin/spvchain' });

(async () => {
    await node.ensure();
    await node.open();
    await node.connect();
    await walletdb.open();
    const mnemonic = new Mnemonic('uncover cash coral neglect upon nurse argue deal right song hood tennis');
    const masterKey = HD.fromMnemonic(mnemonic);

    const wallet = await walletdb.create({master: masterKey});

    console.log('Created wallet with address %s', await wallet.receiveAddress());
    const bl = await wallet.getBalance();
    console.log(bl.toJSON());

    // Add our address to the spv filter.
    node.pool.watchAddress(await wallet.receiveAddress());

    node.startSync();

    node.on('error', async (err) => {
        console.log(err);
    });

    node.pool.on('tx', async (tx) => {
        console.log('------ New tx. Adding to walletdb...');
        console.log(tx);
        await walletdb.addTX(tx);
    });

    wallet.on('balance', async (balance) => {
        console.log('Balance updated.');
        console.log(balance.toJSON());
    });
})().catch((err) => {
    console.error(err.stack);
    process.exit(1);
});

```

**Tags:** wallet, balance, spv, javascript, bcoin


---

## 1 answers

### Answer by username_0 on 2018-08-17. Score: 0 | Accepted answer

As commented [here](https://github.com/bcoin-org/bcoin/issues/570#issuecomment-413719125), the cause of the problem is that I

> sorta have created two wallets. The first is instantiated by the plugin statement towards the top, and then you also create a new walletDB right after.

So the solution is using `WalletClient`:

```
const {WalletClient} = require('bclient');
const {Network} = require('bcoin');
const network = Network.get('regtest');

const walletOptions = {
  network: network.type,
  port: network.walletPort,
  apiKey: 'api-key'
}

const walletClient = new WalletClient(walletOptions);
const id = 'primary'; // or whatever your wallet name is
const wallet = walletClient.wallet(id);

```

Instead of `walletdb.create()`.


---

