This commit is contained in:
Christopher Jeffrey 2016-06-05 19:38:36 -07:00
parent 6a4beafa87
commit 062db4eb1d
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -174,30 +174,34 @@ pool.open(function(err) {
if (err) if (err)
throw err; throw err;
var wallet = new bcoin.wallet({ walletdb.open(function(err) {
provider: walletdb.provider(), if (err)
derivation: 'bip44', throw err;
type: 'pubkeyhash'
});
walletdb.save(wallet); walletdb.create(function(err, wallet) {
pool.watchWallet(wallet); if (err)
throw err;
console.log('Created wallet with address %s', wallet.getAddress()); console.log('Created wallet with address %s', wallet.getAddress());
// Connect, start retrieving and relaying txs // Add our address to the spv filter.
pool.connect(); pool.watchAddress(wallet.getAddress());
// Start the blockchain sync. // Connect, start retrieving and relaying txs
pool.startSync(); pool.connect();
pool.on('tx', function(tx) { // Start the blockchain sync.
wallet.addTX(tx); pool.startSync();
});
wallet.on('balance', function(balance) { pool.on('tx', function(tx) {
console.log('Balance updated.'); wallet.addTX(tx);
console.log(bcoin.utils.btc(balance.unconfirmed)); });
wallet.on('balance', function(balance) {
console.log('Balance updated.');
console.log(bcoin.utils.btc(balance.unconfirmed));
});
});
}); });
}); });
``` ```
@ -225,9 +229,7 @@ node.open(function(err) {
id: 'mywallet', id: 'mywallet',
passphrase: 'foo', passphrase: 'foo',
witness: false, witness: false,
type: 'pubkeyhash', type: 'pubkeyhash'
derivation: 'bip44',
accountIndex: 0
}; };
node.createWallet(options, function(err, wallet) { node.createWallet(options, function(err, wallet) {
@ -242,34 +244,31 @@ node.open(function(err) {
// Wait for balance and send it to a new address. // Wait for balance and send it to a new address.
wallet.once('balance', function(balance) { wallet.once('balance', function(balance) {
var newReceiving = wallet.createAddress();
console.log('Created new receiving address: %s', newReceiving);
// Create a transaction, fill // Create a transaction, fill
// it with coins, and sign it. // it with coins, and sign it.
wallet.createTX({ var to = {
address: newReceiving, address: newReceiving,
value: balance.confirmed value: balance.total
}, function(err, tx) { };
wallet.createTX({ subtractFee: true }, [to], function(err, tx) {
if (err) if (err)
throw err; throw err;
console.log('sending tx:'); wallet.sign(tx, function(err) {
console.log(tx); if (err)
throw err;
// Destroy wallet to clean up the listeners. console.log('sending tx:');
wallet.destroy(); console.log(tx);
// Broadcast the transaction (alternatively, node.sendTX(tx, function(err) {
// we could just add it to our own mempool if (err) {
// and have the mempool object relay it). // Could be a reject
node.broadcast(tx, function(err) { // packet or a timeout.
if (err) { return console.log(err);
// Could be a reject }
// packet or a timeout. console.log('tx sent!');
return console.log(err); });
}
console.log('tx sent!');
}); });
}); });
}); });
@ -301,7 +300,9 @@ $ BCOIN_NETWORK=segnet4 node bin/node
# View the genesis block # View the genesis block
$ node bin/bcoin-cli block 0 $ node bin/bcoin-cli block 0
# View primary wallet # View primary wallet
$ node bin/bcoin-cli wallet primary --passphrase=node $ node bin/bcoin-cli wallet primary
# Send a tx
$ node bin/bcoin-cli send [address] 0.01
# View the mempool # View the mempool
$ node bin/bcoin-cli mempool $ node bin/bcoin-cli mempool
``` ```