Skip to content

Instantly share code, notes, and snippets.

@pinheadmz
Last active November 11, 2019 17:00
Show Gist options
  • Select an option

  • Save pinheadmz/aab2799420e1e0b1a59293285b6e9231 to your computer and use it in GitHub Desktop.

Select an option

Save pinheadmz/aab2799420e1e0b1a59293285b6e9231 to your computer and use it in GitHub Desktop.
'use strict';
const assert = require('bsert');
const bcoin = require('bcoin');
const plugin = bcoin.wallet.plugin;
const network = bcoin.Network.get('regtest');
const node = new bcoin.FullNode({
network: 'regtest',
memory: true
});
node.use(plugin);
const walletClient = new bcoin.WalletClient({
port: network.walletPort
});
const nodeClient = new bcoin.NodeClient({
port: network.rpcPort
});
const destAddr = 'n23HhQCJtFbKCtzLgCpkrU85U5vQnZRhMt';
(async () => {
await node.open();
// get primary wallet
const primary = walletClient.wallet('primary');
const acct = await primary.getAccount('default');
// create watch only wallet from primary/default wallet xpub
const create = await walletClient.createWallet('test', {
watchOnly: true,
accountKey: acct.accountKey
});
console.log(create);
const wallet = await walletClient.wallet('test');
// get wallet address
let addr = await wallet.createAddress('default');
addr = addr.address;
console.log(addr);
// generate blocks to wallet address
await nodeClient.execute('generatetoaddress', [100, addr]);
// wait for blocks to be mined
await new Promise(r => setTimeout(r, 1000));
const bal = await wallet.getBalance();
console.log(bal);
// create tx
const tx1 = await wallet.createTX({
account: 'default',
rate: 45383,
smart: false,
subtractFee: false,
subtractIndex: -1,
depth: 1,
outputs: [{address: destAddr, subtractFee: false, value: 56097}],
sign: false
});
console.log(tx1);
// lock input used in tx1
const input = tx1.inputs[0];
const prevout = input.prevout;
const lock = await wallet.lockCoin(prevout.hash, prevout.index);
console.log(lock);
// display locked coins
const locked = await wallet.getLocked();
console.log(locked);
// sign tx1
const signed = await primary.sign({tx: tx1.hex});
console.log(signed);
// broadcast tx1
await nodeClient.broadcast(signed.hex);
// confirm the change output from tx1
await nodeClient.execute('generatetoaddress', [100, destAddr]);
// attempt to create tx again
const tx2 = await wallet.createTX({
account: 'default',
rate: 45383,
smart: false,
subtractFee: false,
subtractIndex: -1,
depth: 1,
outputs: [{address: destAddr, subtractFee: false, value: 56097}],
sign: false
});
console.log(tx2);
// Did we re-use an input? This should always fail
assert.deepStrictEqual(tx1.inputs[0], tx2.inputs[0]);
await node.close();
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment