fcoin/docs/Examples/spv-sync-wallet.js
2018-04-07 04:05:34 +05:30

54 lines
1.1 KiB
JavaScript

'use strict';
const bcoin = require('../..');
bcoin.set('testnet');
// SPV chains only store the chain headers.
const chain = new bcoin.Chain({
memory: false,
location: '/tmp/bcoin/spvchain',
spv: true
});
const pool = new bcoin.Pool({
chain: chain,
spv: true,
maxPeers: 8
});
const walletdb = new bcoin.wallet.WalletDB({ memory: true });
(async () => {
await pool.open();
await walletdb.open();
const wallet = await walletdb.create();
console.log('Created wallet with address %s', await wallet.receiveAddress());
// Add our address to the spv filter.
pool.watchAddress(await wallet.receiveAddress());
// Connect, start retrieving and relaying txs
await pool.connect();
// Start the blockchain sync.
pool.startSync();
pool.on('tx', async (tx) => {
console.log('received TX');
await walletdb.addTX(tx);
console.log('Transaction added to walletDB');
});
wallet.on('balance', (balance) => {
console.log('Balance updated.');
console.log(bcoin.amount.btc(balance.unconfirmed));
});
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});