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

167
README.md
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)
return attempt.mineAsync();
// Mine the block on the worker pool (use mine() for the master process) }).then(function(block) {
attempt.mineAsync(function(err, block) { // Add the block to the chain
if (err) console.log('Adding %s to the blockchain.', block.rhash);
throw err; console.log(block);
return chain.add(block);
// Add the block to the chain }).then(function() {
chain.add(block, function(err) { console.log('Added block!');
if (err)
throw err;
console.log('Added %s to the blockchain.', block.rhash);
console.log(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,38 +235,29 @@ 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();
}).then(function(wallet) {
console.log('Created wallet with address %s', wallet.getAddress('base58'));
walletdb.open(function(err) { // Add our address to the spv filter.
if (err) pool.watchAddress(wallet.getAddress());
throw err;
walletdb.create(function(err, wallet) { // Connect, start retrieving and relaying txs
if (err) pool.connect();
throw err;
console.log('Created wallet with address %s', wallet.getAddress('base58')); // Start the blockchain sync.
pool.startSync();
// Add our address to the spv filter. pool.on('tx', function(tx) {
pool.watchAddress(wallet.getAddress()); wallet.addTX(tx);
});
// Connect, start retrieving and relaying txs wallet.on('balance', function(balance) {
pool.connect(); console.log('Balance updated.');
console.log(bcoin.utils.btc(balance.unconfirmed));
// Start the blockchain sync.
pool.startSync();
pool.on('tx', function(tx) {
wallet.addTX(tx);
});
wallet.on('balance', function(balance) {
console.log('Balance updated.');
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,48 +293,33 @@ 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
node.startSync();
// Start syncing the blockchain // Wait for balance and send it to a new address.
node.startSync(); wallet.once('balance', function(balance) {
// Create a transaction, fill
// Wait for balance and send it to a new address. // it with coins, and sign it.
wallet.once('balance', function(balance) { var options = {
// Create a transaction, fill subtractFee: true,
// it with coins, and sign it. outputs: [{
var options = { address: newReceiving,
subtractFee: true, value: balance.total
outputs: [{ }]
address: newReceiving, };
value: balance.total wallet.createTX(options).then(function(tx) {
}] // Need to pass our passphrase back in to sign!
}; return wallet.sign(tx, 'foo');
wallet.createTX(options, function(err, tx) { }).then(function(tx) {
if (err) console.log('sending tx:');
throw err; console.log(tx);
return node.sendTX(tx);
// Need to pass our passphrase back in to sign! }).then(function() {
wallet.sign(tx, 'foo', function(err) { console.log('tx sent!');
if (err)
throw err;
console.log('sending tx:');
console.log(tx);
node.sendTX(tx, function(err) {
if (err) {
// Could be a reject
// packet or a timeout.
return console.log(err);
}
console.log('tx sent!');
});
});
});
}); });
}); });
}); });
@ -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);
});
}); });
``` ```