examples: fix a few examples.

This commit is contained in:
Christopher Jeffrey 2017-07-17 16:36:01 -07:00
parent ffc7a9bd6f
commit 615adfd3ef
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
7 changed files with 100 additions and 105 deletions

View File

@ -6,7 +6,7 @@ const chain = new Chain({
network: 'testnet'
});
async function main() {
(async () => {
let entry;
await chain.open();
@ -14,6 +14,4 @@ async function main() {
entry = await chain.getEntry(0);
console.log(entry);
}
main();
})();

View File

@ -23,44 +23,6 @@ wallet = new HTTP.Wallet({
apiKey: 'foo'
});
async function main() {
let wdb = node.require('walletdb');
let w, acct, hash, balance, tx;
await node.open();
w = await wallet.create({ id: 'test' });
console.log('Wallet:');
console.log(w);
// Fund default account.
await fundWallet(wdb, w.account.receiveAddress);
balance = await wallet.getBalance();
console.log('Balance:');
console.log(balance);
acct = await wallet.createAccount('foo');
console.log('Account:');
console.log(acct);
// Send to our new account.
hash = await sendTX(acct.receiveAddress, 10000);
console.log('Sent TX:');
console.log(hash);
tx = await wallet.getTX(hash);
console.log('Sent TX details:');
console.log(tx);
await callNodeApi();
}
async function fundWallet(wdb, addr) {
let tx;
@ -121,4 +83,40 @@ async function callNodeApi() {
console.log(json);
}
main();
(async () => {
let wdb = node.require('walletdb');
let w, acct, hash, balance, tx;
await node.open();
w = await wallet.create({ id: 'test' });
console.log('Wallet:');
console.log(w);
// Fund default account.
await fundWallet(wdb, w.account.receiveAddress);
balance = await wallet.getBalance();
console.log('Balance:');
console.log(balance);
acct = await wallet.createAccount('foo');
console.log('Account:');
console.log(acct);
// Send to our new account.
hash = await sendTX(acct.receiveAddress, 10000);
console.log('Sent TX:');
console.log(hash);
tx = await wallet.getTX(hash);
console.log('Sent TX details:');
console.log(tx);
await callNodeApi();
})();

View File

@ -23,7 +23,7 @@ const miner = new Miner({
workers: workers
});
async function main() {
(async () => {
let tmpl, job, block;
await miner.open();
@ -44,6 +44,4 @@ async function main() {
console.log('New tip:');
console.log(chain.tip);
}
main();
})();

View File

@ -4,12 +4,12 @@ const FullNode = require('bcoin/lib/node/fullnode');
const node = new FullNode({
network: 'testnet',
db: 'memory'
db: 'memory',
workers: true
});
async function main() {
(async () => {
await node.open();
await node.connect();
node.on('connect', (entry, block) => {
@ -21,6 +21,4 @@ async function main() {
});
node.startSync();
}
main();
})();

View File

@ -28,12 +28,13 @@ MyPlugin.prototype.sayPeers = function sayPeers() {
const node = new FullNode({
network: 'testnet',
db: 'memory'
db: 'memory',
workers: true
});
node.use(MyPlugin);
async function main() {
(async () => {
let plugin = node.require('my-plugin');
await node.open();
@ -49,6 +50,6 @@ async function main() {
node.on('tx', (tx) => {
console.log('%s added to mempool.', tx.txid());
});
}
main();
node.startSync();
})();

View File

@ -3,56 +3,58 @@
const bcoin = require('bcoin');
const assert = require('assert');
let master = bcoin.hd.generate();
let key = master.derive('m/44/0/0/0/0');
let keyring = new bcoin.keyring(key.privateKey);
let cb = new bcoin.mtx();
(async () => {
let master = bcoin.hd.generate();
let key = master.derivePath('m/44/0/0/0/0');
let keyring = new bcoin.keyring(key.privateKey);
let cb = new bcoin.mtx();
cb.addInput({
prevout: new bcoin.outpoint(),
script: new bcoin.script(),
sequence: 0xffffffff
});
cb.addInput({
prevout: new bcoin.outpoint(),
script: new bcoin.script(),
sequence: 0xffffffff
});
// Send 50,000 satoshis to ourselves.
cb.addOutput({
address: keyring.getAddress(),
value: 50000
});
// Send 50,000 satoshis to ourselves.
cb.addOutput({
address: keyring.getAddress(),
value: 50000
});
// Our available coins.
let coins = [];
// Our available coins.
let coins = [];
// Convert the coinbase output to a Coin
// object and add it to our available coins.
// In reality you might get these coins from a wallet.
let coin = bcoin.coin.fromTX(cb, 0, -1);
coins.push(coin);
// Convert the coinbase output to a Coin
// object and add it to our available coins.
// In reality you might get these coins from a wallet.
let coin = bcoin.coin.fromTX(cb, 0, -1);
coins.push(coin);
// Create our redeeming transaction.
let mtx = new bcoin.mtx();
// Create our redeeming transaction.
let mtx = new bcoin.mtx();
// Send 10,000 satoshis to ourself.
mtx.addOutput({
address: keyring.getAddress(),
value: 10000
});
// Send 10,000 satoshis to ourself.
mtx.addOutput({
address: keyring.getAddress(),
value: 10000
});
// Now that we've created the output, we can do some coin selection (the output
// must be added first so we know how much money is needed and also so we can
// accurately estimate the size for fee calculation).
// Now that we've created the output, we can do some coin selection (the output
// must be added first so we know how much money is needed and also so we can
// accurately estimate the size for fee calculation).
// Select coins from our array and add inputs.
// Calculate fee and add a change output.
await mtx.fund(coins, {
// Use a rate of 10,000 satoshis per kb.
// With the `fullnode` object, you can
// use the fee estimator for this instead
// of blindly guessing.
rate: 10000,
// Send the change back to ourselves.
changeAddress: keyring.getAddress()
});
// Select coins from our array and add inputs.
// Calculate fee and add a change output.
mtx.fund(coins, {
// Use a rate of 10,000 satoshis per kb.
// With the `fullnode` object, you can
// use the fee estimator for this instead
// of blindly guessing.
rate: 10000,
// Send the change back to ourselves.
changeAddress: keyring.getAddress()
}).then(() => {
// Sign input 0
mtx.sign(keyring);
@ -69,4 +71,7 @@ mtx.fund(coins, {
assert(tx.verify(mtx.view));
console.log(mtx);
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});

View File

@ -4,19 +4,18 @@ const random = require('bcoin/lib/crypto/random');
const WalletDB = require('bcoin/lib/wallet/walletdb');
const MTX = require('bcoin/lib/primitives/mtx');
const Outpoint = require('bcoin/lib/primitives/outpoint');
let walletdb;
function dummy() {
let hash = random.randomBytes(32).toString('hex');
return new Outpoint(hash, 0);
}
walletdb = new WalletDB({
const walletdb = new WalletDB({
network: 'testnet',
db: 'memory'
});
async function main() {
(async () => {
let wallet, acct, mtx, tx, wtx;
await walletdb.open();
@ -44,6 +43,4 @@ async function main() {
console.log('Added transaction');
console.log(wtx);
}
main();
})();