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

View File

@ -23,44 +23,6 @@ wallet = new HTTP.Wallet({
apiKey: 'foo' 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) { async function fundWallet(wdb, addr) {
let tx; let tx;
@ -121,4 +83,40 @@ async function callNodeApi() {
console.log(json); 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 workers: workers
}); });
async function main() { (async () => {
let tmpl, job, block; let tmpl, job, block;
await miner.open(); await miner.open();
@ -44,6 +44,4 @@ async function main() {
console.log('New tip:'); console.log('New tip:');
console.log(chain.tip); console.log(chain.tip);
} })();
main();

View File

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

View File

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

View File

@ -3,56 +3,58 @@
const bcoin = require('bcoin'); const bcoin = require('bcoin');
const assert = require('assert'); const assert = require('assert');
let master = bcoin.hd.generate(); (async () => {
let key = master.derive('m/44/0/0/0/0'); let master = bcoin.hd.generate();
let keyring = new bcoin.keyring(key.privateKey); let key = master.derivePath('m/44/0/0/0/0');
let cb = new bcoin.mtx(); let keyring = new bcoin.keyring(key.privateKey);
let cb = new bcoin.mtx();
cb.addInput({ cb.addInput({
prevout: new bcoin.outpoint(), prevout: new bcoin.outpoint(),
script: new bcoin.script(), script: new bcoin.script(),
sequence: 0xffffffff sequence: 0xffffffff
}); });
// Send 50,000 satoshis to ourselves. // Send 50,000 satoshis to ourselves.
cb.addOutput({ cb.addOutput({
address: keyring.getAddress(), address: keyring.getAddress(),
value: 50000 value: 50000
}); });
// Our available coins. // Our available coins.
let coins = []; let coins = [];
// Convert the coinbase output to a Coin // Convert the coinbase output to a Coin
// object and add it to our available coins. // object and add it to our available coins.
// In reality you might get these coins from a wallet. // In reality you might get these coins from a wallet.
let coin = bcoin.coin.fromTX(cb, 0, -1); let coin = bcoin.coin.fromTX(cb, 0, -1);
coins.push(coin); coins.push(coin);
// Create our redeeming transaction. // Create our redeeming transaction.
let mtx = new bcoin.mtx(); let mtx = new bcoin.mtx();
// Send 10,000 satoshis to ourself. // Send 10,000 satoshis to ourself.
mtx.addOutput({ mtx.addOutput({
address: keyring.getAddress(), address: keyring.getAddress(),
value: 10000 value: 10000
}); });
// Now that we've created the output, we can do some coin selection (the output // 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 // must be added first so we know how much money is needed and also so we can
// accurately estimate the size for fee calculation). // 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 // Sign input 0
mtx.sign(keyring); mtx.sign(keyring);
@ -69,4 +71,7 @@ mtx.fund(coins, {
assert(tx.verify(mtx.view)); assert(tx.verify(mtx.view));
console.log(mtx); 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 WalletDB = require('bcoin/lib/wallet/walletdb');
const MTX = require('bcoin/lib/primitives/mtx'); const MTX = require('bcoin/lib/primitives/mtx');
const Outpoint = require('bcoin/lib/primitives/outpoint'); const Outpoint = require('bcoin/lib/primitives/outpoint');
let walletdb;
function dummy() { function dummy() {
let hash = random.randomBytes(32).toString('hex'); let hash = random.randomBytes(32).toString('hex');
return new Outpoint(hash, 0); return new Outpoint(hash, 0);
} }
walletdb = new WalletDB({ const walletdb = new WalletDB({
network: 'testnet', network: 'testnet',
db: 'memory' db: 'memory'
}); });
async function main() { (async () => {
let wallet, acct, mtx, tx, wtx; let wallet, acct, mtx, tx, wtx;
await walletdb.open(); await walletdb.open();
@ -44,6 +43,4 @@ async function main() {
console.log('Added transaction'); console.log('Added transaction');
console.log(wtx); console.log(wtx);
} })();
main();