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';
const bcoin = require('../..');
const encoding = bcoin.encoding;
const Outpoint = bcoin.outpoint;
const MTX = bcoin.mtx;
const HTTP = bcoin.http;
const FullNode = bcoin.fullnode;
const client = require('bclient');
const plugin = bcoin.wallet.plugin;
const network = bcoin.Network.get('regtest');
const node = new FullNode({
const node = new bcoin.FullNode({
network: 'regtest',
apiKey: 'foo',
walletAuth: true,
@ -17,15 +14,15 @@ const node = new FullNode({
node.use(plugin);
const wallet = new HTTP.Wallet({
network: 'regtest',
const wallet = new client.WalletClient({
port: network.walletPort,
apiKey: 'foo'
});
async function fundWallet(wdb, addr) {
// Coinbase
const mtx = new MTX();
mtx.addOutpoint(new Outpoint(encoding.NULL_HASH, 0));
const mtx = new bcoin.MTX();
mtx.addOutpoint(new bcoin.Outpoint());
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;
}
@ -79,24 +76,25 @@ async function callNodeApi() {
}
(async () => {
const wdb = node.require('walletdb');
const wdb = node.require('walletdb').wdb;
await node.open();
const w = await wallet.create({ id: 'test' });
const w = await wallet.createWallet('test');
console.log('Wallet:');
console.log(w);
// 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);
const acct = await wallet.createAccount('foo');
const acct = await wallet.createAccount('test', 'foo');
console.log('Account:');
console.log(acct);

View File

@ -3,12 +3,9 @@
// Usage: $ node ./docs/Examples/connect-to-peer.js [ip]:[port]
const bcoin = require('../..');
const Peer = bcoin.peer;
const NetAddress = bcoin.netaddress;
const Network = bcoin.network;
const network = Network.get('testnet');
const network = bcoin.Network.get('testnet');
const peer = Peer.fromOptions({
const peer = bcoin.Peer.fromOptions({
network: 'testnet',
agent: 'my-subversion',
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}`);

View File

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

View File

@ -1,8 +1,5 @@
'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.)
@ -10,9 +7,9 @@ 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({
const chain = new bcoin.Chain({ network: 'regtest', memory: true });
const mempool = new bcoin.Mempool({ chain: chain });
const miner = new bcoin.Miner({
chain: chain,
mempool: mempool,
@ -21,8 +18,11 @@ const miner = new Miner({
});
(async () => {
// Open the chain
await chain.open();
// 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();
// Create a Cpu miner job

View File

@ -8,12 +8,12 @@ const assert = require('assert');
(async () => {
const master = bcoin.hd.generate();
const key = master.derivePath('m/44/0/0/0/0');
const keyring = new bcoin.keyring(key.privateKey);
const cb = new bcoin.mtx();
const keyring = new bcoin.wallet.WalletKey(key.privateKey);
const cb = new bcoin.MTX();
cb.addInput({
prevout: new bcoin.outpoint(),
script: new bcoin.script(),
prevout: new bcoin.Outpoint(),
script: new bcoin.Script(),
sequence: 0xffffffff
});
@ -29,11 +29,11 @@ const assert = require('assert');
// Convert the coinbase output to a Coin
// object and add it to our available coins.
// 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);
// Create our redeeming transaction.
const mtx = new bcoin.mtx();
const mtx = new bcoin.MTX();
// Send 10,000 satoshis to ourself.
mtx.addOutput({

View File

@ -2,7 +2,7 @@
const bcoin = require('../..').set('main');
const walletPlugin = bcoin.wallet.plugin;
const node = bcoin.fullnode({
const node = new bcoin.FullNode({
checkpoints: true,
// Primary wallet passphrase
passsphrase: 'node',
@ -32,12 +32,11 @@ const newReceiving = 'AddressHere';
type: 'pubkeyhash'
};
const walletdb = node.require('walletdb');
const walletdb = node.require('walletdb').wdb;
await walletdb.open();
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();

View File

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

View File

@ -1,12 +1,7 @@
'use strict';
const path = require('path');
const bcoin = require('../..');
const Chain = bcoin.chain;
const Logger = bcoin.logger;
const util = bcoin.util;
const HOME = process.env.HOME;
const Logger = require('blgr');
// Setup logger to see what's Bcoin doing.
const logger = new Logger({
@ -14,11 +9,11 @@ const logger = new Logger({
});
// Create chain for testnet, specify chain directory
const chain = new Chain({
const chain = new bcoin.Chain({
logger: logger,
network: 'testnet',
db: 'leveldb',
prefix: path.join(HOME, '.bcoin/testnet'),
prefix: '/tmp/bcon-testnet',
indexTX: true,
indexAddress: true
});
@ -34,7 +29,7 @@ const chain = new Chain({
// eslint-disable-next-line max-len
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 coinview = await chain.db.getSpentView(tx);
@ -44,7 +39,7 @@ const chain = new Chain({
// eslint-disable-next-line max-len
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);
})().catch((err) => {
console.error(err.stack);

View File

@ -1,23 +1,19 @@
'use strict';
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
});
const chain = new Chain({
const chain = new bcoin.Chain({
network: 'regtest',
workers: workers
});
const miner = new Miner({
const miner = new bcoin.Miner({
chain: chain,
addresses: [key.getAddress()],
coinbaseFlags: 'my-miner',

View File

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

View File

@ -1,26 +1,23 @@
'use strict';
const bcoin = require('../..');
const Chain = bcoin.chain;
const Pool = bcoin.pool;
const WalletDB = bcoin.walletdb;
bcoin.set('testnet');
// SPV chains only store the chain headers.
const chain = Chain({
db: 'leveldb',
location: process.env.HOME + '/spvchain',
const chain = new bcoin.Chain({
memory: false,
location: '/tmp/bcoin/spvchain',
spv: true
});
const pool = new Pool({
const pool = new bcoin.Pool({
chain: chain,
spv: true,
maxPeers: 8
});
const walletdb = new WalletDB({ db: 'memory' });
const walletdb = new bcoin.wallet.WalletDB({ memory: true });
(async () => {
await pool.open();
@ -28,10 +25,10 @@ const walletdb = new WalletDB({ db: 'memory' });
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.
pool.watchAddress(wallet.getAddress());
pool.watchAddress(await wallet.receiveAddress());
// Connect, start retrieving and relaying txs
await pool.connect();

View File

@ -1,17 +1,14 @@
'use strict';
const bcoin = require('../..');
const random = bcoin.crypto.random;
const WalletDB = bcoin.walletdb;
const MTX = bcoin.mtx;
const Outpoint = bcoin.outpoint;
const random = require('bcrypto/lib/random');
function dummy() {
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',
db: 'memory'
});
@ -31,9 +28,9 @@ const walletdb = new WalletDB({
console.log('Created account');
console.log(acct);
const mtx = new MTX();
const mtx = new bcoin.MTX();
mtx.addOutpoint(dummy());
mtx.addOutput(acct.getReceive(), 50460);
mtx.addOutput(acct.receiveAddress(), 50460);
const tx = mtx.toTX();