forked from bcoin-org/bcash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spv-sync-wallet.js
53 lines (39 loc) · 1.1 KB
/
spv-sync-wallet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'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);
});