readme: examples.

This commit is contained in:
Christopher Jeffrey 2016-09-24 01:42:33 -07:00
parent ed66af64ea
commit 24b20b8317
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -114,30 +114,19 @@ var miner = new bcoin.miner({ chain: chain, mempool: mempool });
// Open the miner (initialize the databases, etc). // Open the miner (initialize the databases, etc).
// Miner will implicitly call `open` on chain and mempool. // Miner will implicitly call `open` on chain and mempool.
miner.open(function(err) { miner.open().then(function() {
if (err)
throw err;
// Create a block "attempt". // Create a block "attempt".
miner.createBlock(function(err, attempt) { return miner.createBlock();
if (err) }).then(function(attempt) {
throw err;
// Mine the block on the worker pool (use mine() for the master process) // Mine the block on the worker pool (use mine() for the master process)
attempt.mineAsync(function(err, block) { return attempt.mineAsync();
if (err) }).then(function(block) {
throw err;
// Add the block to the chain // Add the block to the chain
chain.add(block, function(err) { console.log('Adding %s to the blockchain.', block.rhash);
if (err)
throw err;
console.log('Added %s to the blockchain.', block.rhash);
console.log(block); console.log(block);
}); return chain.add(block);
}); }).then(function() {
}); console.log('Added block!');
}); });
``` ```
@ -157,10 +146,7 @@ var mempool = new bcoin.mempool({ chain: chain });
var pool = new bcoin.pool({ chain: chain, mempool: mempool, maxPeers: 8 }); var pool = new bcoin.pool({ chain: chain, mempool: mempool, maxPeers: 8 });
// Open the pool (implicitly opens mempool and chain). // Open the pool (implicitly opens mempool and chain).
pool.open(function(err) { pool.open().then(function() {
if (err)
throw err;
// Connect, start retrieving and relaying txs // Connect, start retrieving and relaying txs
pool.connect(); pool.connect();
@ -204,10 +190,7 @@ var tpool = new bcoin.pool({
size: 8 size: 8
}); });
tpool.open(function(err) { tpool.open().then(function() {
if (err)
throw err;
// Connect, start retrieving and relaying txs // Connect, start retrieving and relaying txs
tpool.connect(); tpool.connect();
@ -252,18 +235,11 @@ var pool = new bcoin.pool({
var walletdb = new bcoin.walletdb({ db: 'memory' }); var walletdb = new bcoin.walletdb({ db: 'memory' });
pool.open(function(err) { pool.open().then(function() {
if (err) return walletdb.open();
throw err; }).then(function() {
return walletdb.create();
walletdb.open(function(err) { }).then(function(wallet) {
if (err)
throw err;
walletdb.create(function(err, wallet) {
if (err)
throw err;
console.log('Created wallet with address %s', wallet.getAddress('base58')); console.log('Created wallet with address %s', wallet.getAddress('base58'));
// Add our address to the spv filter. // Add our address to the spv filter.
@ -283,8 +259,6 @@ pool.open(function(err) {
console.log('Balance updated.'); console.log('Balance updated.');
console.log(bcoin.utils.btc(balance.unconfirmed)); console.log(bcoin.utils.btc(balance.unconfirmed));
}); });
});
});
}); });
``` ```
@ -310,10 +284,7 @@ node.on('error', function(err) {
}); });
// Start the node // Start the node
node.open(function(err) { node.open().then(function() {
if (err)
throw err;
// Create a new wallet (or get an existing one with the same ID) // Create a new wallet (or get an existing one with the same ID)
var options = { var options = {
id: 'mywallet', id: 'mywallet',
@ -322,10 +293,8 @@ node.open(function(err) {
type: 'pubkeyhash' type: 'pubkeyhash'
}; };
node.walletdb.create(options, function(err, wallet) { return node.walletdb.create(options);
if (err) }).then(function(wallet) {
throw err;
console.log('Created wallet with address: %s', wallet.getAddress('base58')); console.log('Created wallet with address: %s', wallet.getAddress('base58'));
// Start syncing the blockchain // Start syncing the blockchain
@ -342,30 +311,17 @@ node.open(function(err) {
value: balance.total value: balance.total
}] }]
}; };
wallet.createTX(options, function(err, tx) { wallet.createTX(options).then(function(tx) {
if (err)
throw err;
// Need to pass our passphrase back in to sign! // Need to pass our passphrase back in to sign!
wallet.sign(tx, 'foo', function(err) { return wallet.sign(tx, 'foo');
if (err) }).then(function(tx) {
throw err;
console.log('sending tx:'); console.log('sending tx:');
console.log(tx); console.log(tx);
return node.sendTX(tx);
node.sendTX(tx, function(err) { }).then(function() {
if (err) {
// Could be a reject
// packet or a timeout.
return console.log(err);
}
console.log('tx sent!'); console.log('tx sent!');
}); });
}); });
});
});
});
}); });
node.chain.on('block', function(block) { node.chain.on('block', function(block) {
@ -377,12 +333,7 @@ node.mempool.on('tx', function(tx) {
}); });
node.chain.on('full', function() { node.chain.on('full', function() {
node.mempool.getHistory(function(err, txs) { node.mempool.getHistory().then(console.log);
if (err)
throw err;
console.log(txs);
});
}); });
``` ```