forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fullnode-and-wallet.js
83 lines (65 loc) · 1.66 KB
/
fullnode-and-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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
const bcoin = require('../..').set('main');
const walletPlugin = bcoin.wallet.plugin;
const node = bcoin.fullnode({
checkpoints: true,
// Primary wallet passphrase
passsphrase: 'node',
logLevel: 'info'
});
node.use(walletPlugin);
// We get a lot of errors sometimes,
// usually from peers hanging up on us.
// Just ignore them for now.
node.on('error', (err) => {
;
});
// New Address we'll be sending to.
const newReceiving = 'AddressHere';
// Start the node
(async () => {
await node.open();
const options = {
id: 'mywallet',
passphrase: 'foo',
witness: false,
type: 'pubkeyhash'
};
const walletdb = node.require('walletdb');
await walletdb.open();
const wallet = await walletdb.create(options);
console.log('Created wallet with address: %s', wallet.getAddress('base58'));
await node.connect();
// Start syncing the blockchain
node.startSync();
// Wait for balance and send it to a new address.
wallet.once('balance', async (balance) => {
// Create a transaction, fill
// it with coins, and sign it.
const options = {
subtractFee: true,
outputs: [{
address: newReceiving,
value: balance.total
}]
};
const tx = await wallet.createTX(options);
const stx = await wallet.sign(tx, 'foo');
console.log('sending tx:');
console.log(stx);
await node.sendTX(stx);
console.log('tx sent!');
});
node.chain.on('block', (block) => {
;
});
node.mempool.on('tx', (tx) => {
;
});
node.chain.on('full', () => {
node.mempool.getHistory().then(console.log);
});
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});