itns-sidechain/docs/Examples/wallet.js

50 lines
952 B
JavaScript
Raw Normal View History

2017-03-24 13:52:58 -07:00
'use strict';
2017-08-09 16:38:20 +04:00
const bcoin = require('../..');
const random = bcoin.crypto.random;
const WalletDB = bcoin.walletdb;
const MTX = bcoin.mtx;
const Outpoint = bcoin.outpoint;
2017-03-24 13:52:58 -07:00
function dummy() {
2017-07-27 09:27:00 -07:00
const hash = random.randomBytes(32).toString('hex');
2017-03-24 13:52:58 -07:00
return new Outpoint(hash, 0);
}
2017-07-17 16:36:01 -07:00
const walletdb = new WalletDB({
2017-03-24 13:52:58 -07:00
network: 'testnet',
db: 'memory'
});
2017-07-17 16:36:01 -07:00
(async () => {
2017-03-24 13:52:58 -07:00
await walletdb.open();
const wallet = await walletdb.create();
2017-03-24 13:52:58 -07:00
console.log('Created wallet');
console.log(wallet);
const acct = await wallet.createAccount({
2017-03-24 13:52:58 -07:00
name: 'foo'
});
console.log('Created account');
console.log(acct);
const mtx = new MTX();
2017-03-24 13:52:58 -07:00
mtx.addOutpoint(dummy());
mtx.addOutput(acct.getReceive(), 50460);
const tx = mtx.toTX();
2017-03-24 13:52:58 -07:00
await walletdb.addTX(tx);
const wtx = await wallet.getTX(tx.hash('hex'));
2017-03-24 13:52:58 -07:00
console.log('Added transaction');
console.log(wtx);
2017-08-09 16:38:20 +04:00
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});