forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-api.js
120 lines (89 loc) · 2.31 KB
/
client-api.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'use strict';
const bcoin = require('../..');
const encoding = bcoin.encoding;
const co = bcoin.co;
const Outpoint = bcoin.outpoint;
const MTX = bcoin.mtx;
const HTTP = bcoin.http;
const FullNode = bcoin.fullnode;
const plugin = bcoin.wallet.plugin;
const node = new FullNode({
network: 'regtest',
apiKey: 'foo',
walletAuth: true,
db: 'memory'
});
node.use(plugin);
const wallet = new HTTP.Wallet({
network: 'regtest',
apiKey: 'foo'
});
async function fundWallet(wdb, addr) {
// Coinbase
const mtx = new MTX();
mtx.addOutpoint(new Outpoint(encoding.NULL_HASH, 0));
mtx.addOutput(addr, 50460);
mtx.addOutput(addr, 50460);
mtx.addOutput(addr, 50460);
mtx.addOutput(addr, 50460);
const tx = mtx.toTX();
wallet.once('balance', (balance) => {
console.log('New Balance:');
console.log(balance);
});
wallet.once('address', (receive) => {
console.log('New Receiving Address:');
console.log(receive);
});
wallet.once('tx', (details) => {
console.log('New Wallet TX:');
console.log(details);
});
await wdb.addTX(tx);
await co.timeout(300);
}
async function sendTX(addr, value) {
const options = {
rate: 10000,
outputs: [{
value: value,
address: addr
}]
};
const tx = await wallet.send(options);
return tx.hash;
}
async function callNodeApi() {
const info = await wallet.client.getInfo();
console.log('Server Info:');
console.log(info);
const json = await wallet.client.rpc.execute('getblocktemplate', []);
console.log('Block Template (RPC):');
console.log(json);
}
(async () => {
const wdb = node.require('walletdb');
await node.open();
const w = await wallet.create({ id: 'test' });
console.log('Wallet:');
console.log(w);
// Fund default account.
await fundWallet(wdb, w.account.receiveAddress);
const balance = await wallet.getBalance();
console.log('Balance:');
console.log(balance);
const acct = await wallet.createAccount('foo');
console.log('Account:');
console.log(acct);
// Send to our new account.
const hash = await sendTX(acct.receiveAddress, 10000);
console.log('Sent TX:');
console.log(hash);
const tx = await wallet.getTX(hash);
console.log('Sent TX details:');
console.log(tx);
await callNodeApi();
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});