-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathclient-api.js
117 lines (86 loc) · 2.34 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
'use strict';
const bcoin = require('../..');
const client = require('bclient');
const plugin = bcoin.wallet.plugin;
const network = bcoin.Network.get('regtest');
const node = new bcoin.FullNode({
network: 'regtest',
apiKey: 'foo',
walletAuth: true,
db: 'memory'
});
node.use(plugin);
const wallet = new client.WalletClient({
port: network.walletPort,
apiKey: 'foo'
});
async function fundWallet(wdb, addr) {
// Coinbase
const mtx = new bcoin.MTX();
mtx.addOutpoint(new bcoin.Outpoint());
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 new Promise(r => setTimeout(r, 300));
}
async function sendTX(addr, value) {
const options = {
rate: 10000,
outputs: [{
value: value,
address: addr
}]
};
const tx = await wallet.send('test', 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').wdb;
await node.open();
const w = await wallet.createWallet('test');
console.log('Wallet:');
console.log(w);
// Fund default account.
const receive = await wallet.createAddress('test', 'default');
await fundWallet(wdb, receive.address);
const balance = await wallet.getBalance('test', 'default');
console.log('Balance:');
console.log(balance);
const acct = await wallet.createAccount('test', '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);
});