Update examples
This commit is contained in:
parent
3bc47f5a3c
commit
d7c1210515
@ -1,35 +0,0 @@
|
||||
``` js
|
||||
var bcoin = require('bcoin');
|
||||
|
||||
bcoin.set({
|
||||
// Default network (so we can avoid passing
|
||||
// the `network` option into every object below.
|
||||
network: 'regtest',
|
||||
// Enable the global worker pool
|
||||
// for mining and transaction verification.
|
||||
useWorkers: true
|
||||
});
|
||||
|
||||
// Start up a blockchain, mempool, and miner using in-memory
|
||||
// databases (stored in a red-black tree instead of on-disk).
|
||||
var chain = new bcoin.chain({ db: 'memory' });
|
||||
var mempool = new bcoin.mempool({ chain: chain });
|
||||
var miner = new bcoin.miner({ chain: chain, mempool: mempool });
|
||||
|
||||
// Open the miner (initialize the databases, etc).
|
||||
// Miner will implicitly call `open` on chain and mempool.
|
||||
miner.open().then(function() {
|
||||
// Create a Cpu miner job
|
||||
return miner.createJob();
|
||||
}).then(function(job) {
|
||||
// Mine the block on the worker pool (use mine() for the master process)
|
||||
return job.mineAsync();
|
||||
}).then(function(block) {
|
||||
// Add the block to the chain
|
||||
console.log('Adding %s to the blockchain.', block.rhash);
|
||||
console.log(block);
|
||||
return chain.add(block);
|
||||
}).then(function() {
|
||||
console.log('Added block!');
|
||||
});
|
||||
```
|
||||
@ -1,72 +0,0 @@
|
||||
``` js
|
||||
var bcoin = require('bcoin').set('main');
|
||||
|
||||
var node = bcoin.fullnode({
|
||||
checkpoints: true,
|
||||
// Primary wallet passphrase
|
||||
passsphrase: 'node',
|
||||
logLevel: 'info'
|
||||
});
|
||||
|
||||
// We get a lot of errors sometimes,
|
||||
// usually from peers hanging up on us.
|
||||
// Just ignore them for now.
|
||||
node.on('error', function(err) {
|
||||
;
|
||||
});
|
||||
|
||||
// Start the node
|
||||
node.open().then(function() {
|
||||
// Create a new wallet (or get an existing one with the same ID)
|
||||
var options = {
|
||||
id: 'mywallet',
|
||||
passphrase: 'foo',
|
||||
witness: false,
|
||||
type: 'pubkeyhash'
|
||||
};
|
||||
|
||||
return node.walletdb.create(options);
|
||||
}).then(function(wallet) {
|
||||
console.log('Created wallet with address: %s', wallet.getAddress('base58'));
|
||||
|
||||
node.connect().then(function() {
|
||||
// Start syncing the blockchain
|
||||
node.startSync();
|
||||
});
|
||||
|
||||
// Wait for balance and send it to a new address.
|
||||
wallet.once('balance', function(balance) {
|
||||
// Create a transaction, fill
|
||||
// it with coins, and sign it.
|
||||
var options = {
|
||||
subtractFee: true,
|
||||
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');
|
||||
}).then(function(tx) {
|
||||
console.log('sending tx:');
|
||||
console.log(tx);
|
||||
return node.sendTX(tx);
|
||||
}).then(function() {
|
||||
console.log('tx sent!');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
node.chain.on('block', function(block) {
|
||||
;
|
||||
});
|
||||
|
||||
node.mempool.on('tx', function(tx) {
|
||||
;
|
||||
});
|
||||
|
||||
node.chain.on('full', function() {
|
||||
node.mempool.getHistory().then(console.log);
|
||||
});
|
||||
```
|
||||
@ -1,44 +0,0 @@
|
||||
``` js
|
||||
var bcoin = require('bcoin').set('testnet');
|
||||
|
||||
// SPV chains only store the chain headers.
|
||||
var chain = new bcoin.chain({
|
||||
db: 'leveldb',
|
||||
location: process.env.HOME + '/spvchain',
|
||||
spv: true
|
||||
});
|
||||
|
||||
var pool = new bcoin.pool({
|
||||
chain: chain,
|
||||
spv: true,
|
||||
maxPeers: 8
|
||||
});
|
||||
|
||||
var walletdb = new bcoin.walletdb({ db: 'memory' });
|
||||
|
||||
pool.open().then(function() {
|
||||
return walletdb.open();
|
||||
}).then(function() {
|
||||
return walletdb.create();
|
||||
}).then(function(wallet) {
|
||||
console.log('Created wallet with address %s', wallet.getAddress('base58'));
|
||||
|
||||
// Add our address to the spv filter.
|
||||
pool.watchAddress(wallet.getAddress());
|
||||
|
||||
// Connect, start retrieving and relaying txs
|
||||
pool.connect().then(function() {
|
||||
// Start the blockchain sync.
|
||||
pool.startSync();
|
||||
|
||||
pool.on('tx', function(tx) {
|
||||
walletdb.addTX(tx);
|
||||
});
|
||||
|
||||
wallet.on('balance', function(balance) {
|
||||
console.log('Balance updated.');
|
||||
console.log(bcoin.amount.btc(balance.unconfirmed));
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
@ -1,15 +1,26 @@
|
||||
``` js
|
||||
var bcoin = require('bcoin').set('main');
|
||||
'use strict';
|
||||
const bcoin = require('../..').set('main');
|
||||
const Chain = bcoin.chain;
|
||||
const Mempool = bcoin.mempool;
|
||||
const Pool = bcoin.pool;
|
||||
|
||||
// Create a blockchain and store it in leveldb.
|
||||
// `db` also accepts `rocksdb` and `lmdb`.
|
||||
var prefix = process.env.HOME + '/my-bcoin-environment';
|
||||
var chain = new bcoin.chain({ db: 'leveldb', location: prefix + '/chain' });
|
||||
const prefix = process.env.HOME + '/my-bcoin-environment';
|
||||
const chain = new Chain({
|
||||
db: 'leveldb',
|
||||
location: prefix + '/chain',
|
||||
network: 'main'
|
||||
});
|
||||
|
||||
var mempool = new bcoin.mempool({ chain: chain });
|
||||
const mempool = new Mempool({ chain: chain });
|
||||
|
||||
// Create a network pool of peers with a limit of 8 peers.
|
||||
var pool = new bcoin.pool({ chain: chain, mempool: mempool, maxPeers: 8 });
|
||||
const pool = new Pool({
|
||||
chain: chain,
|
||||
mempool: mempool,
|
||||
maxPeers: 8
|
||||
});
|
||||
|
||||
// Open the pool (implicitly opens mempool and chain).
|
||||
(async function() {
|
||||
@ -22,44 +33,44 @@ var pool = new bcoin.pool({ chain: chain, mempool: mempool, maxPeers: 8 });
|
||||
pool.startSync();
|
||||
|
||||
// Watch the action
|
||||
chain.on('block', function(block) {
|
||||
chain.on('block', (block) => {
|
||||
console.log('Connected block to blockchain:');
|
||||
console.log(block);
|
||||
});
|
||||
|
||||
mempool.on('tx', function(tx) {
|
||||
mempool.on('tx', (tx) => {
|
||||
console.log('Added tx to mempool:');
|
||||
console.log(tx);
|
||||
});
|
||||
|
||||
pool.on('tx', function(tx) {
|
||||
pool.on('tx', (tx) => {
|
||||
console.log('Saw transaction:');
|
||||
console.log(tx.rhash);
|
||||
});
|
||||
})();
|
||||
|
||||
// Start up a segnet4 sync in-memory
|
||||
// Start up a testnet sync in-memory
|
||||
// while we're at it (because we can).
|
||||
|
||||
var tchain = new bcoin.chain({
|
||||
network: 'segnet4',
|
||||
const tchain = new Chain({
|
||||
network: 'testnet',
|
||||
db: 'memory'
|
||||
});
|
||||
|
||||
var tmempool = new bcoin.mempool({
|
||||
network: 'segnet4',
|
||||
const tmempool = new Mempool({
|
||||
network: 'testnet',
|
||||
chain: tchain
|
||||
});
|
||||
|
||||
var tpool = new bcoin.pool({
|
||||
network: 'segnet4',
|
||||
const tpool = new Pool({
|
||||
network: 'testnet',
|
||||
chain: tchain,
|
||||
mempool: tmempool,
|
||||
size: 8
|
||||
});
|
||||
|
||||
(async function() {
|
||||
await pool.open();
|
||||
await tpool.open();
|
||||
|
||||
// Connect, start retrieving and relaying txs
|
||||
await tpool.connect();
|
||||
@ -67,20 +78,18 @@ var tpool = new bcoin.pool({
|
||||
// Start the blockchain sync.
|
||||
tpool.startSync();
|
||||
|
||||
tchain.on('block', function(block) {
|
||||
console.log('Added segnet4 block:');
|
||||
tchain.on('block', (block) => {
|
||||
console.log('Added testnet block:');
|
||||
console.log(block);
|
||||
});
|
||||
|
||||
tmempool.on('tx', function(tx) {
|
||||
console.log('Added segnet4 tx to mempool:');
|
||||
tmempool.on('tx', (tx) => {
|
||||
console.log('Added testnet tx to mempool:');
|
||||
console.log(tx);
|
||||
});
|
||||
|
||||
tpool.on('tx', function(tx) {
|
||||
console.log('Saw segnet4 transaction:');
|
||||
tpool.on('tx', (tx) => {
|
||||
console.log('Saw testnet transaction:');
|
||||
console.log(tx);
|
||||
});
|
||||
})();
|
||||
|
||||
```
|
||||
39
docs/Examples/create-a-blockchain-and-mempool.js
Normal file
39
docs/Examples/create-a-blockchain-and-mempool.js
Normal file
@ -0,0 +1,39 @@
|
||||
'use strict';
|
||||
const bcoin = require('../..');
|
||||
const Chain = bcoin.chain;
|
||||
const Mempool = bcoin.mempool;
|
||||
const Miner = bcoin.miner;
|
||||
|
||||
// Default network (so we can avoid passing
|
||||
// the `network` option into every object below.)
|
||||
bcoin.set('regtest');
|
||||
|
||||
// Start up a blockchain, mempool, and miner using in-memory
|
||||
// databases (stored in a red-black tree instead of on-disk).
|
||||
const chain = new Chain({ db: 'memory' });
|
||||
const mempool = new Mempool({ chain: chain });
|
||||
const miner = new Miner({
|
||||
chain: chain,
|
||||
mempool: mempool,
|
||||
|
||||
// Make sure miner won't block the main thread.
|
||||
useWorkers: true
|
||||
});
|
||||
|
||||
(async () => {
|
||||
// Open the miner (initialize the databases, etc).
|
||||
// Miner will implicitly call `open` on chain and mempool.
|
||||
await miner.open();
|
||||
|
||||
// Create a Cpu miner job
|
||||
const job = await miner.createJob();
|
||||
|
||||
// run miner
|
||||
const block = await job.mineAsync();
|
||||
|
||||
// Add the block to the chain
|
||||
console.log('Adding %s to the blockchain.', block.rhash);
|
||||
console.log(block);
|
||||
await chain.add(block);
|
||||
console.log('Added block!');
|
||||
})();
|
||||
79
docs/Examples/fullnode-and-wallet.js
Normal file
79
docs/Examples/fullnode-and-wallet.js
Normal file
@ -0,0 +1,79 @@
|
||||
'use strict';
|
||||
const bcoin = require('../..').set('main');
|
||||
const WalletDB = bcoin.walletdb;
|
||||
|
||||
const node = bcoin.fullnode({
|
||||
checkpoints: true,
|
||||
// Primary wallet passphrase
|
||||
passsphrase: 'node',
|
||||
logLevel: 'info'
|
||||
});
|
||||
|
||||
// We get a lot of errors sometimes,
|
||||
// usually from peers hanging up on us.
|
||||
// Just ignore them for now.
|
||||
node.on('error', (err) => {
|
||||
;
|
||||
});
|
||||
|
||||
// New Address we'll be sending to.
|
||||
const newReceiving = 'AddressHere';
|
||||
|
||||
// Start the node
|
||||
(async () => {
|
||||
await node.open();
|
||||
|
||||
const options = {
|
||||
id: 'mywallet',
|
||||
passphrase: 'foo',
|
||||
witness: false,
|
||||
type: 'pubkeyhash'
|
||||
};
|
||||
|
||||
console.log(node);
|
||||
const walletdb = new WalletDB();
|
||||
|
||||
await walletdb.open();
|
||||
const wallet = await walletdb.create(options);
|
||||
|
||||
console.log('Created wallet with address: %s', wallet.getAddress('base58'));
|
||||
|
||||
await node.connect();
|
||||
|
||||
// Start syncing the blockchain
|
||||
node.startSync();
|
||||
|
||||
// Wait for balance and send it to a new address.
|
||||
wallet.once('balance', async (balance) => {
|
||||
// Create a transaction, fill
|
||||
// it with coins, and sign it.
|
||||
const options = {
|
||||
subtractFee: true,
|
||||
outputs: [{
|
||||
address: newReceiving,
|
||||
value: balance.total
|
||||
}]
|
||||
};
|
||||
|
||||
const tx = await wallet.createTX(options);
|
||||
const stx = await wallet.sign(tx, 'foo');
|
||||
|
||||
console.log('sending tx:');
|
||||
console.log(stx);
|
||||
|
||||
await node.sendTX(stx);
|
||||
console.log('tx sent!');
|
||||
});
|
||||
|
||||
node.chain.on('block', (block) => {
|
||||
;
|
||||
});
|
||||
|
||||
node.mempool.on('tx', (tx) => {
|
||||
;
|
||||
});
|
||||
|
||||
node.chain.on('full', () => {
|
||||
node.mempool.getHistory().then(console.log);
|
||||
});
|
||||
})();
|
||||
51
docs/Examples/spv-sync-wallet.js
Normal file
51
docs/Examples/spv-sync-wallet.js
Normal file
@ -0,0 +1,51 @@
|
||||
'use strict';
|
||||
|
||||
const bcoin = require('../..').set('testnet');
|
||||
const Chain = bcoin.chain;
|
||||
const Pool = bcoin.pool;
|
||||
const WalletDB = bcoin.walletdb;
|
||||
|
||||
// SPV chains only store the chain headers.
|
||||
const chain = Chain({
|
||||
db: 'leveldb',
|
||||
location: process.env.HOME + '/spvchain',
|
||||
spv: true
|
||||
});
|
||||
|
||||
const pool = new Pool({
|
||||
chain: chain,
|
||||
spv: true,
|
||||
maxPeers: 8
|
||||
});
|
||||
|
||||
const walletdb = new WalletDB({ db: 'memory' });
|
||||
|
||||
(async () => {
|
||||
await pool.open();
|
||||
await walletdb.open();
|
||||
|
||||
const wallet = await walletdb.create();
|
||||
|
||||
console.log('Created wallet with address %s', wallet.getAddress('base58'));
|
||||
|
||||
// Add our address to the spv filter.
|
||||
pool.watchAddress(wallet.getAddress());
|
||||
|
||||
// Connect, start retrieving and relaying txs
|
||||
await pool.connect();
|
||||
|
||||
// Start the blockchain sync.
|
||||
pool.startSync();
|
||||
|
||||
pool.on('tx', async (tx) => {
|
||||
console.log('received TX');
|
||||
|
||||
await walletdb.addTX(tx);
|
||||
console.log('Transaction added to walletDB');
|
||||
});
|
||||
|
||||
wallet.on('balance', (balance) => {
|
||||
console.log('Balance updated.');
|
||||
console.log(bcoin.amount.btc(balance.unconfirmed));
|
||||
});
|
||||
})();
|
||||
Loading…
Reference in New Issue
Block a user