diff --git a/examples/chain.js b/examples/chain.js index 3576d5b1..703b7dc2 100644 --- a/examples/chain.js +++ b/examples/chain.js @@ -6,7 +6,7 @@ const chain = new Chain({ network: 'testnet' }); -async function main() { +(async () => { let entry; await chain.open(); @@ -14,6 +14,4 @@ async function main() { entry = await chain.getEntry(0); console.log(entry); -} - -main(); +})(); diff --git a/examples/client.js b/examples/client.js index a91389bd..bce7efae 100644 --- a/examples/client.js +++ b/examples/client.js @@ -23,44 +23,6 @@ wallet = new HTTP.Wallet({ apiKey: 'foo' }); -async function main() { - let wdb = node.require('walletdb'); - let w, acct, hash, balance, tx; - - await node.open(); - - w = await wallet.create({ id: 'test' }); - - console.log('Wallet:'); - console.log(w); - - // Fund default account. - await fundWallet(wdb, w.account.receiveAddress); - - balance = await wallet.getBalance(); - - console.log('Balance:'); - console.log(balance); - - acct = await wallet.createAccount('foo'); - - console.log('Account:'); - console.log(acct); - - // Send to our new account. - hash = await sendTX(acct.receiveAddress, 10000); - - console.log('Sent TX:'); - console.log(hash); - - tx = await wallet.getTX(hash); - - console.log('Sent TX details:'); - console.log(tx); - - await callNodeApi(); -} - async function fundWallet(wdb, addr) { let tx; @@ -121,4 +83,40 @@ async function callNodeApi() { console.log(json); } -main(); +(async () => { + let wdb = node.require('walletdb'); + let w, acct, hash, balance, tx; + + await node.open(); + + w = await wallet.create({ id: 'test' }); + + console.log('Wallet:'); + console.log(w); + + // Fund default account. + await fundWallet(wdb, w.account.receiveAddress); + + balance = await wallet.getBalance(); + + console.log('Balance:'); + console.log(balance); + + acct = await wallet.createAccount('foo'); + + console.log('Account:'); + console.log(acct); + + // Send to our new account. + hash = await sendTX(acct.receiveAddress, 10000); + + console.log('Sent TX:'); + console.log(hash); + + tx = await wallet.getTX(hash); + + console.log('Sent TX details:'); + console.log(tx); + + await callNodeApi(); +})(); diff --git a/examples/miner.js b/examples/miner.js index b8b9ecb8..e9ad7efe 100644 --- a/examples/miner.js +++ b/examples/miner.js @@ -23,7 +23,7 @@ const miner = new Miner({ workers: workers }); -async function main() { +(async () => { let tmpl, job, block; await miner.open(); @@ -44,6 +44,4 @@ async function main() { console.log('New tip:'); console.log(chain.tip); -} - -main(); +})(); diff --git a/examples/node.js b/examples/node.js index 6e6a10c8..e9f2694e 100644 --- a/examples/node.js +++ b/examples/node.js @@ -4,12 +4,12 @@ const FullNode = require('bcoin/lib/node/fullnode'); const node = new FullNode({ network: 'testnet', - db: 'memory' + db: 'memory', + workers: true }); -async function main() { +(async () => { await node.open(); - await node.connect(); node.on('connect', (entry, block) => { @@ -21,6 +21,4 @@ async function main() { }); node.startSync(); -} - -main(); +})(); diff --git a/examples/plugin.js b/examples/plugin.js index 2c273913..3f9361be 100644 --- a/examples/plugin.js +++ b/examples/plugin.js @@ -28,12 +28,13 @@ MyPlugin.prototype.sayPeers = function sayPeers() { const node = new FullNode({ network: 'testnet', - db: 'memory' + db: 'memory', + workers: true }); node.use(MyPlugin); -async function main() { +(async () => { let plugin = node.require('my-plugin'); await node.open(); @@ -49,6 +50,6 @@ async function main() { node.on('tx', (tx) => { console.log('%s added to mempool.', tx.txid()); }); -} -main(); + node.startSync(); +})(); diff --git a/examples/tx.js b/examples/tx.js index 3cedea27..42cfed45 100644 --- a/examples/tx.js +++ b/examples/tx.js @@ -3,56 +3,58 @@ const bcoin = require('bcoin'); const assert = require('assert'); -let master = bcoin.hd.generate(); -let key = master.derive('m/44/0/0/0/0'); -let keyring = new bcoin.keyring(key.privateKey); -let cb = new bcoin.mtx(); +(async () => { + let master = bcoin.hd.generate(); + let key = master.derivePath('m/44/0/0/0/0'); + let keyring = new bcoin.keyring(key.privateKey); + let cb = new bcoin.mtx(); -cb.addInput({ - prevout: new bcoin.outpoint(), - script: new bcoin.script(), - sequence: 0xffffffff -}); + cb.addInput({ + prevout: new bcoin.outpoint(), + script: new bcoin.script(), + sequence: 0xffffffff + }); -// Send 50,000 satoshis to ourselves. -cb.addOutput({ - address: keyring.getAddress(), - value: 50000 -}); + // Send 50,000 satoshis to ourselves. + cb.addOutput({ + address: keyring.getAddress(), + value: 50000 + }); -// Our available coins. -let coins = []; + // Our available coins. + let coins = []; -// 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. -let coin = bcoin.coin.fromTX(cb, 0, -1); -coins.push(coin); + // 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. + let coin = bcoin.coin.fromTX(cb, 0, -1); + coins.push(coin); -// Create our redeeming transaction. -let mtx = new bcoin.mtx(); + // Create our redeeming transaction. + let mtx = new bcoin.mtx(); -// Send 10,000 satoshis to ourself. -mtx.addOutput({ - address: keyring.getAddress(), - value: 10000 -}); + // Send 10,000 satoshis to ourself. + mtx.addOutput({ + address: keyring.getAddress(), + value: 10000 + }); -// Now that we've created the output, we can do some coin selection (the output -// must be added first so we know how much money is needed and also so we can -// accurately estimate the size for fee calculation). + // Now that we've created the output, we can do some coin selection (the output + // must be added first so we know how much money is needed and also so we can + // accurately estimate the size for fee calculation). + + // Select coins from our array and add inputs. + // Calculate fee and add a change output. + await mtx.fund(coins, { + // Use a rate of 10,000 satoshis per kb. + // With the `fullnode` object, you can + // use the fee estimator for this instead + // of blindly guessing. + rate: 10000, + // Send the change back to ourselves. + changeAddress: keyring.getAddress() + }); -// Select coins from our array and add inputs. -// Calculate fee and add a change output. -mtx.fund(coins, { - // Use a rate of 10,000 satoshis per kb. - // With the `fullnode` object, you can - // use the fee estimator for this instead - // of blindly guessing. - rate: 10000, - // Send the change back to ourselves. - changeAddress: keyring.getAddress() -}).then(() => { // Sign input 0 mtx.sign(keyring); @@ -69,4 +71,7 @@ mtx.fund(coins, { assert(tx.verify(mtx.view)); console.log(mtx); +})().catch((err) => { + console.error(err.stack); + process.exit(1); }); diff --git a/examples/wallet.js b/examples/wallet.js index 0e0cf480..7268dfe7 100644 --- a/examples/wallet.js +++ b/examples/wallet.js @@ -4,19 +4,18 @@ const random = require('bcoin/lib/crypto/random'); const WalletDB = require('bcoin/lib/wallet/walletdb'); const MTX = require('bcoin/lib/primitives/mtx'); const Outpoint = require('bcoin/lib/primitives/outpoint'); -let walletdb; function dummy() { let hash = random.randomBytes(32).toString('hex'); return new Outpoint(hash, 0); } -walletdb = new WalletDB({ +const walletdb = new WalletDB({ network: 'testnet', db: 'memory' }); -async function main() { +(async () => { let wallet, acct, mtx, tx, wtx; await walletdb.open(); @@ -44,6 +43,4 @@ async function main() { console.log('Added transaction'); console.log(wtx); -} - -main(); +})();