Merge pull request #443 from tuxcanfly/update-examples

docs: updated examples
This commit is contained in:
Steven Bower 2018-04-06 15:59:02 -07:00 committed by GitHub
commit 10e6b9a0c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 91 additions and 101 deletions

View File

@ -1,14 +1,11 @@
'use strict'; 'use strict';
const bcoin = require('../..'); const bcoin = require('../..');
const encoding = bcoin.encoding; const client = require('bclient');
const Outpoint = bcoin.outpoint;
const MTX = bcoin.mtx;
const HTTP = bcoin.http;
const FullNode = bcoin.fullnode;
const plugin = bcoin.wallet.plugin; const plugin = bcoin.wallet.plugin;
const network = bcoin.Network.get('regtest');
const node = new FullNode({ const node = new bcoin.FullNode({
network: 'regtest', network: 'regtest',
apiKey: 'foo', apiKey: 'foo',
walletAuth: true, walletAuth: true,
@ -17,15 +14,15 @@ const node = new FullNode({
node.use(plugin); node.use(plugin);
const wallet = new HTTP.Wallet({ const wallet = new client.WalletClient({
network: 'regtest', port: network.walletPort,
apiKey: 'foo' apiKey: 'foo'
}); });
async function fundWallet(wdb, addr) { async function fundWallet(wdb, addr) {
// Coinbase // Coinbase
const mtx = new MTX(); const mtx = new bcoin.MTX();
mtx.addOutpoint(new Outpoint(encoding.NULL_HASH, 0)); mtx.addOutpoint(new bcoin.Outpoint());
mtx.addOutput(addr, 50460); mtx.addOutput(addr, 50460);
mtx.addOutput(addr, 50460); mtx.addOutput(addr, 50460);
mtx.addOutput(addr, 50460); mtx.addOutput(addr, 50460);
@ -61,7 +58,7 @@ async function sendTX(addr, value) {
}] }]
}; };
const tx = await wallet.send(options); const tx = await wallet.send('test', options);
return tx.hash; return tx.hash;
} }
@ -79,24 +76,25 @@ async function callNodeApi() {
} }
(async () => { (async () => {
const wdb = node.require('walletdb'); const wdb = node.require('walletdb').wdb;
await node.open(); await node.open();
const w = await wallet.create({ id: 'test' }); const w = await wallet.createWallet('test');
console.log('Wallet:'); console.log('Wallet:');
console.log(w); console.log(w);
// Fund default account. // Fund default account.
await fundWallet(wdb, w.account.receiveAddress); const receive = await wallet.createAddress('test', 'default');
await fundWallet(wdb, receive.address);
const balance = await wallet.getBalance(); const balance = await wallet.getBalance('test', 'default');
console.log('Balance:'); console.log('Balance:');
console.log(balance); console.log(balance);
const acct = await wallet.createAccount('foo'); const acct = await wallet.createAccount('test', 'foo');
console.log('Account:'); console.log('Account:');
console.log(acct); console.log(acct);

View File

@ -3,12 +3,9 @@
// Usage: $ node ./docs/Examples/connect-to-peer.js [ip]:[port] // Usage: $ node ./docs/Examples/connect-to-peer.js [ip]:[port]
const bcoin = require('../..'); const bcoin = require('../..');
const Peer = bcoin.peer; const network = bcoin.Network.get('testnet');
const NetAddress = bcoin.netaddress;
const Network = bcoin.network;
const network = Network.get('testnet');
const peer = Peer.fromOptions({ const peer = bcoin.Peer.fromOptions({
network: 'testnet', network: 'testnet',
agent: 'my-subversion', agent: 'my-subversion',
hasWitness: () => { hasWitness: () => {
@ -16,7 +13,7 @@ const peer = Peer.fromOptions({
} }
}); });
const addr = NetAddress.fromHostname(process.argv[2], 'testnet'); const addr = bcoin.net.NetAddress.fromHostname(process.argv[2], 'testnet');
console.log(`Connecting to ${addr.hostname}`); console.log(`Connecting to ${addr.hostname}`);

View File

@ -1,29 +1,37 @@
'use strict'; 'use strict';
const bcoin = require('../..').set('main'); 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. const Logger = require('blgr');
// `db` also accepts `rocksdb` and `lmdb`.
const prefix = process.env.HOME + '/my-bcoin-environment'; // Setup logger to see what's Bcoin doing.
const chain = new Chain({ const logger = new Logger({
db: 'leveldb', level: 'debug'
location: prefix + '/chain',
network: 'main'
}); });
const mempool = new Mempool({ chain: chain }); // Create a blockchain and store it in memory.
const chain = new bcoin.Chain({
memory: true,
network: 'main',
logger: logger
});
const mempool = new bcoin.Mempool({
chain: chain,
logger: logger
});
// Create a network pool of peers with a limit of 8 peers. // Create a network pool of peers with a limit of 8 peers.
const pool = new Pool({ const pool = new bcoin.Pool({
chain: chain, chain: chain,
mempool: mempool, mempool: mempool,
maxPeers: 8 maxPeers: 8,
logger: logger
}); });
// Open the pool (implicitly opens mempool and chain).
(async function() { (async function() {
await logger.open();
await chain.open();
await pool.open(); await pool.open();
// Connect, start retrieving and relaying txs // Connect, start retrieving and relaying txs
@ -52,24 +60,29 @@ const pool = new Pool({
// Start up a testnet sync in-memory // Start up a testnet sync in-memory
// while we're at it (because we can). // while we're at it (because we can).
const tchain = new Chain({ const tchain = new bcoin.Chain({
memory: true,
network: 'testnet', network: 'testnet',
db: 'memory' logger: logger
}); });
const tmempool = new Mempool({ const tmempool = new bcoin.Mempool({
network: 'testnet', network: 'testnet',
chain: tchain chain: tchain,
logger: logger
}); });
const tpool = new Pool({ const tpool = new bcoin.Pool({
network: 'testnet', network: 'testnet',
chain: tchain, chain: tchain,
mempool: tmempool, mempool: tmempool,
size: 8 size: 8,
logger: logger
}); });
(async function() { (async function() {
await tchain.open();
await tpool.open(); await tpool.open();
// Connect, start retrieving and relaying txs // Connect, start retrieving and relaying txs

View File

@ -1,8 +1,5 @@
'use strict'; 'use strict';
const bcoin = require('../..'); const bcoin = require('../..');
const Chain = bcoin.chain;
const Mempool = bcoin.mempool;
const Miner = bcoin.miner;
// Default network (so we can avoid passing // Default network (so we can avoid passing
// the `network` option into every object below.) // the `network` option into every object below.)
@ -10,9 +7,9 @@ bcoin.set('regtest');
// Start up a blockchain, mempool, and miner using in-memory // Start up a blockchain, mempool, and miner using in-memory
// databases (stored in a red-black tree instead of on-disk). // databases (stored in a red-black tree instead of on-disk).
const chain = new Chain({ db: 'memory' }); const chain = new bcoin.Chain({ network: 'regtest', memory: true });
const mempool = new Mempool({ chain: chain }); const mempool = new bcoin.Mempool({ chain: chain });
const miner = new Miner({ const miner = new bcoin.Miner({
chain: chain, chain: chain,
mempool: mempool, mempool: mempool,
@ -21,8 +18,11 @@ const miner = new Miner({
}); });
(async () => { (async () => {
// Open the chain
await chain.open();
// 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 mempool.
await miner.open(); await miner.open();
// Create a Cpu miner job // Create a Cpu miner job

View File

@ -8,12 +8,12 @@ const assert = require('assert');
(async () => { (async () => {
const master = bcoin.hd.generate(); const master = bcoin.hd.generate();
const key = master.derivePath('m/44/0/0/0/0'); const key = master.derivePath('m/44/0/0/0/0');
const keyring = new bcoin.keyring(key.privateKey); const keyring = new bcoin.wallet.WalletKey(key.privateKey);
const cb = new bcoin.mtx(); const 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
}); });
@ -29,11 +29,11 @@ const assert = require('assert');
// 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.
const coin = bcoin.coin.fromTX(cb, 0, -1); const coin = bcoin.Coin.fromTX(cb, 0, -1);
coins.push(coin); coins.push(coin);
// Create our redeeming transaction. // Create our redeeming transaction.
const mtx = new bcoin.mtx(); const mtx = new bcoin.MTX();
// Send 10,000 satoshis to ourself. // Send 10,000 satoshis to ourself.
mtx.addOutput({ mtx.addOutput({

View File

@ -2,7 +2,7 @@
const bcoin = require('../..').set('main'); const bcoin = require('../..').set('main');
const walletPlugin = bcoin.wallet.plugin; const walletPlugin = bcoin.wallet.plugin;
const node = bcoin.fullnode({ const node = new bcoin.FullNode({
checkpoints: true, checkpoints: true,
// Primary wallet passphrase // Primary wallet passphrase
passsphrase: 'node', passsphrase: 'node',
@ -32,12 +32,11 @@ const newReceiving = 'AddressHere';
type: 'pubkeyhash' type: 'pubkeyhash'
}; };
const walletdb = node.require('walletdb'); const walletdb = node.require('walletdb').wdb;
await walletdb.open();
const wallet = await walletdb.create(options); const wallet = await walletdb.create(options);
console.log('Created wallet with address: %s', wallet.getAddress('base58')); console.log('Created wallet with address: %s', wallet.receiveAddress());
await node.connect(); await node.connect();

View File

@ -1,11 +1,10 @@
'use strict'; 'use strict';
const bcoin = require('../..'); const bcoin = require('../..');
const FullNode = bcoin.fullnode;
const node = new FullNode({ const node = new bcoin.FullNode({
memory: true,
network: 'testnet', network: 'testnet',
db: 'memory',
workers: true workers: true
}); });

View File

@ -1,12 +1,7 @@
'use strict'; 'use strict';
const path = require('path');
const bcoin = require('../..'); const bcoin = require('../..');
const Chain = bcoin.chain; const Logger = require('blgr');
const Logger = bcoin.logger;
const util = bcoin.util;
const HOME = process.env.HOME;
// Setup logger to see what's Bcoin doing. // Setup logger to see what's Bcoin doing.
const logger = new Logger({ const logger = new Logger({
@ -14,11 +9,11 @@ const logger = new Logger({
}); });
// Create chain for testnet, specify chain directory // Create chain for testnet, specify chain directory
const chain = new Chain({ const chain = new bcoin.Chain({
logger: logger, logger: logger,
network: 'testnet', network: 'testnet',
db: 'leveldb', db: 'leveldb',
prefix: path.join(HOME, '.bcoin/testnet'), prefix: '/tmp/bcon-testnet',
indexTX: true, indexTX: true,
indexAddress: true indexAddress: true
}); });
@ -34,7 +29,7 @@ const chain = new Chain({
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
const txhash = '4dd628123dcde4f2fb3a8b8a18b806721b56007e32497ebe76cde598ce1652af'; const txhash = '4dd628123dcde4f2fb3a8b8a18b806721b56007e32497ebe76cde598ce1652af';
const txmeta = await chain.db.getMeta(util.revHex(txhash)); const txmeta = await chain.db.getMeta(bcoin.util.revHex(txhash));
const tx = txmeta.tx; const tx = txmeta.tx;
const coinview = await chain.db.getSpentView(tx); const coinview = await chain.db.getSpentView(tx);
@ -44,7 +39,7 @@ const chain = new Chain({
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
const bhash = '00000000077eacdd2c803a742195ba430a6d9545e43128ba55ec3c80beea6c0c'; const bhash = '00000000077eacdd2c803a742195ba430a6d9545e43128ba55ec3c80beea6c0c';
const block = await chain.db.getBlock(util.revHex(bhash)); const block = await chain.db.getBlock(bcoin.util.revHex(bhash));
console.log(`Block with hash ${bhash}:`, block); console.log(`Block with hash ${bhash}:`, block);
})().catch((err) => { })().catch((err) => {
console.error(err.stack); console.error(err.stack);

View File

@ -1,23 +1,19 @@
'use strict'; 'use strict';
const bcoin = require('../..'); const bcoin = require('../..');
const KeyRing = bcoin.keyring;
const WorkerPool = bcoin.workerpool;
const Chain = bcoin.chain;
const Miner = bcoin.miner;
const key = KeyRing.generate('regtest'); const key = bcoin.wallet.WalletKey.generate('regtest');
const workers = new WorkerPool({ const workers = new bcoin.WorkerPool({
enabled: true enabled: true
}); });
const chain = new Chain({ const chain = new bcoin.Chain({
network: 'regtest', network: 'regtest',
workers: workers workers: workers
}); });
const miner = new Miner({ const miner = new bcoin.Miner({
chain: chain, chain: chain,
addresses: [key.getAddress()], addresses: [key.getAddress()],
coinbaseFlags: 'my-miner', coinbaseFlags: 'my-miner',

View File

@ -1,7 +1,6 @@
'use strict'; 'use strict';
const bcoin = require('../..'); const bcoin = require('../..');
const FullNode = bcoin.fullnode;
function MyPlugin(node) { function MyPlugin(node) {
this.node = node; this.node = node;
@ -27,9 +26,9 @@ MyPlugin.prototype.sayPeers = function sayPeers() {
console.log('Number of peers: %d', this.node.pool.peers.size()); console.log('Number of peers: %d', this.node.pool.peers.size());
}; };
const node = new FullNode({ const node = new bcoin.FullNode({
memory: true,
network: 'testnet', network: 'testnet',
db: 'memory',
workers: true workers: true
}); });

View File

@ -1,26 +1,23 @@
'use strict'; 'use strict';
const bcoin = require('../..'); const bcoin = require('../..');
const Chain = bcoin.chain;
const Pool = bcoin.pool;
const WalletDB = bcoin.walletdb;
bcoin.set('testnet'); bcoin.set('testnet');
// SPV chains only store the chain headers. // SPV chains only store the chain headers.
const chain = Chain({ const chain = new bcoin.Chain({
db: 'leveldb', memory: false,
location: process.env.HOME + '/spvchain', location: '/tmp/bcoin/spvchain',
spv: true spv: true
}); });
const pool = new Pool({ const pool = new bcoin.Pool({
chain: chain, chain: chain,
spv: true, spv: true,
maxPeers: 8 maxPeers: 8
}); });
const walletdb = new WalletDB({ db: 'memory' }); const walletdb = new bcoin.wallet.WalletDB({ memory: true });
(async () => { (async () => {
await pool.open(); await pool.open();
@ -28,10 +25,10 @@ const walletdb = new WalletDB({ db: 'memory' });
const wallet = await walletdb.create(); const wallet = await walletdb.create();
console.log('Created wallet with address %s', wallet.getAddress('base58')); console.log('Created wallet with address %s', await wallet.receiveAddress());
// Add our address to the spv filter. // Add our address to the spv filter.
pool.watchAddress(wallet.getAddress()); pool.watchAddress(await wallet.receiveAddress());
// Connect, start retrieving and relaying txs // Connect, start retrieving and relaying txs
await pool.connect(); await pool.connect();

View File

@ -1,17 +1,14 @@
'use strict'; 'use strict';
const bcoin = require('../..'); const bcoin = require('../..');
const random = bcoin.crypto.random; const random = require('bcrypto/lib/random');
const WalletDB = bcoin.walletdb;
const MTX = bcoin.mtx;
const Outpoint = bcoin.outpoint;
function dummy() { function dummy() {
const hash = random.randomBytes(32).toString('hex'); const hash = random.randomBytes(32).toString('hex');
return new Outpoint(hash, 0); return new bcoin.Outpoint(hash, 0);
} }
const walletdb = new WalletDB({ const walletdb = new bcoin.wallet.WalletDB({
network: 'testnet', network: 'testnet',
db: 'memory' db: 'memory'
}); });
@ -31,9 +28,9 @@ const walletdb = new WalletDB({
console.log('Created account'); console.log('Created account');
console.log(acct); console.log(acct);
const mtx = new MTX(); const mtx = new bcoin.MTX();
mtx.addOutpoint(dummy()); mtx.addOutpoint(dummy());
mtx.addOutput(acct.getReceive(), 50460); mtx.addOutput(acct.receiveAddress(), 50460);
const tx = mtx.toTX(); const tx = mtx.toTX();