itns-sidechain/docs/Examples/client-api.js

120 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-03-24 18:03:36 -07:00
'use strict';
2017-08-09 16:38:20 +04:00
const bcoin = require('../..');
const encoding = bcoin.encoding;
const Outpoint = bcoin.outpoint;
const MTX = bcoin.mtx;
const HTTP = bcoin.http;
const FullNode = bcoin.fullnode;
const plugin = bcoin.wallet.plugin;
2017-03-24 18:03:36 -07:00
const node = new FullNode({
2017-03-24 18:03:36 -07:00
network: 'regtest',
apiKey: 'foo',
walletAuth: true,
db: 'memory'
});
node.use(plugin);
const wallet = new HTTP.Wallet({
2017-03-24 18:03:36 -07:00
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();
2017-03-24 18:03:36 -07:00
2017-06-29 20:54:07 -07:00
wallet.once('balance', (balance) => {
2017-03-24 18:03:36 -07:00
console.log('New Balance:');
console.log(balance);
});
2017-06-29 20:54:07 -07:00
wallet.once('address', (receive) => {
2017-03-24 18:03:36 -07:00
console.log('New Receiving Address:');
console.log(receive);
});
2017-06-29 20:54:07 -07:00
wallet.once('tx', (details) => {
2017-03-24 18:03:36 -07:00
console.log('New Wallet TX:');
console.log(details);
});
await wdb.addTX(tx);
2017-11-01 14:12:18 -07:00
await new Promise(r => setTimeout(r, 300));
2017-03-24 18:03:36 -07:00
}
async function sendTX(addr, value) {
const options = {
2017-03-24 18:03:36 -07:00
rate: 10000,
outputs: [{
value: value,
address: addr
}]
};
const tx = await wallet.send(options);
2017-03-24 18:03:36 -07:00
return tx.hash;
}
async function callNodeApi() {
2017-07-27 09:27:00 -07:00
const info = await wallet.client.getInfo();
2017-03-24 18:03:36 -07:00
console.log('Server Info:');
console.log(info);
const json = await wallet.client.rpc.execute('getblocktemplate', []);
2017-03-24 18:03:36 -07:00
console.log('Block Template (RPC):');
console.log(json);
}
2017-07-17 16:36:01 -07:00
(async () => {
2017-07-27 09:27:00 -07:00
const wdb = node.require('walletdb');
2017-07-17 16:36:01 -07:00
await node.open();
const w = await wallet.create({ id: 'test' });
2017-07-17 16:36:01 -07:00
console.log('Wallet:');
console.log(w);
// Fund default account.
await fundWallet(wdb, w.account.receiveAddress);
const balance = await wallet.getBalance();
2017-07-17 16:36:01 -07:00
console.log('Balance:');
console.log(balance);
const acct = await wallet.createAccount('foo');
2017-07-17 16:36:01 -07:00
console.log('Account:');
console.log(acct);
// Send to our new account.
const hash = await sendTX(acct.receiveAddress, 10000);
2017-07-17 16:36:01 -07:00
console.log('Sent TX:');
console.log(hash);
const tx = await wallet.getTX(hash);
2017-07-17 16:36:01 -07:00
console.log('Sent TX details:');
console.log(tx);
await callNodeApi();
2017-08-09 16:38:20 +04:00
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});