forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 5
/
spv-sync-wallet.js
56 lines (42 loc) · 1.15 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
54
55
56
'use strict';
const bcoin = require('../..');
const Chain = bcoin.chain;
const Pool = bcoin.pool;
const WalletDB = bcoin.walletdb;
bcoin.set('testnet');
// SPV chains only store the chain headers.
const chain = Chain({
db: 'leveldb',
location: process.env.HOME + '/spvchain',
spv: true
});
const pool = new Pool({
chain: chain,
spv: true,
maxPeers: 8
});
const walletdb = new WalletDB({ db: 'memory' });
(async () => {
await pool.open();
await walletdb.open();
const wallet = await walletdb.create();
console.log('Created wallet with address %s', wallet.getAddress('base58'));
// Add our address to the spv filter.
pool.watchAddress(wallet.getAddress());
// 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);
});