bcoin-org / bcoin

Javascript bitcoin library for node.js and browsers
https://bcoin.io
Other
3.01k stars 811 forks source link

Running bcoin and bcash SPV wallets simultaneously #590

Closed ales-tsurko closed 6 years ago

ales-tsurko commented 6 years ago

Hi everyone, I'm wondering how to configure bcoin and bcash nodes to run them simultaneously in order to implement a multi-currency SPV wallet. I understand that I could achieve that by editing the *.conf files. But I need a way to do that in my code. I'm using a SPVNode instance and a WalletClient as described in this comment.

For now it's intuitive for me to pass an option (port or httpPort) when I'm initializing a node. And looks like I'm fine with the node itself. But where I get stuck is WalletClient configuration. Should I configure the wallet (server-side) somehow to change its port number? And how to do that in code then? My prototype looks like:

const bcoin = require('bcoin');
bcoin.set('testnet');

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'
    // port: portNumber ?
});

// 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 {WalletClient} = require('bclient');
const {Network} = require('bcoin');
const network = Network.get('testnet');

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

(async () => {
    await node.ensure();
    await node.open();
    await node.connect();

    await walletClient.open();
    await wallet.open();

    let addr = await wallet.createAddress('default');
    addr = addr['address'];
    console.log('Created wallet with address %s', addr);
    const bl = await wallet.getBalance();
    console.log(bl);

    node.startSync();

    wallet.on('confirmed', (tx) => {
        console.log('---------------- confirmed', tx);
    });

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

    node.chain.on('block', async (block) => {
        console.log('------ block', block);
    });

})().catch((err) => {
    console.error(err.stack);
    process.exit(1);
});

Just can't find a point, where I can configure the wallet's port.

Additional question on running multiple nodes is: how can I differentiate the logs of the nodes in the console? Is it possible to add some prefix to the logger?

pinheadmz commented 6 years ago

Ok as far as specifying a port for the wallet, it's actually somewhat similar to the object path we had to cook up in https://github.com/bcoin-org/bcoin/issues/570 to get the wallet events:

Add this after the wallet plugin is added to the node: node.plugins.walletdb.http.port = 40004;

Then:

const walletOptions = {
  network: network.type,
  port: 40004,
  apiKey: 'api-key'
}
pinheadmz commented 6 years ago

See also: https://github.com/bcoin-org/bcfg/pull/2

braydonf commented 6 years ago

And also https://github.com/bcoin-org/bcoin/pull/562#issuecomment-417523199 I had a similar issue with configuring the wallet port.

ales-tsurko commented 6 years ago

Thanks a lot, guys!