Browse Source

integration: avoid multiple faucet calls

hk-custom-address
Daniel Cousens 8 years ago
committed by Daniel Cousens
parent
commit
e10a23fb3e
  1. 30
      test/integration/_blockchain.js
  2. 31
      test/integration/basic.js

30
test/integration/_blockchain.js

@ -12,12 +12,8 @@ var kpNetwork = bitcoin.networks.testnet
var keyPair = bitcoin.ECPair.fromWIF(process.env.BITCOINJS_TESTNET_WIF, kpNetwork)
var kpAddress = keyPair.getAddress()
function fundAddress (unspents, address, amount, callback) {
var result = coinSelect(unspents, [{
address: address,
value: amount
}], 10)
function fundAddress (unspents, outputs, callback) {
var result = coinSelect(unspents, outputs, 10)
if (!result.inputs) return callback(new Error('Faucet empty'))
var txb = new bitcoin.TransactionBuilder(kpNetwork)
@ -34,24 +30,32 @@ function fundAddress (unspents, address, amount, callback) {
})
var tx = txb.build()
var txId = tx.getId()
testnet.transactions.propagate(tx.toHex(), function (err) {
callback(err, {
txId: tx.getId(),
vout: 0
}, 0)
callback(err, outputs.map(function (x, i) {
return { txId: txId, vout: i }
}))
})
}
testnet.faucet = function faucet (address, amount, done) {
testnet.faucetMany = function faucetMany (outputs, callback) {
testnet.addresses.unspents(kpAddress, function (err, unspents) {
if (err) return done(err)
if (err) return callback(err)
typeforce([{
txId: types.Hex,
vout: types.UInt32,
value: types.Satoshi
}], unspents)
fundAddress(unspents, address, amount, done)
fundAddress(unspents, outputs, callback)
})
}
testnet.faucet = function faucet (address, value, callback) {
testnet.faucetMany([{ address: address, value: value }], function (err, unspents) {
callback(err, unspents && unspents[0])
})
}

31
test/integration/basic.js

@ -68,22 +68,27 @@ describe('bitcoinjs-lib (basic)', function () {
var alicesAddress = alice.getAddress()
var bobsAddress = bob.getAddress()
blockchain.t.faucet(alicesAddress, 2e4, function (err, unspentA) {
blockchain.t.faucetMany([
{
address: alicesAddress,
value: 2e4
},
{
address: bobsAddress,
value: 2e4
}
], function (err, unspents) {
if (err) return done(err)
blockchain.t.faucet(bobsAddress, 2e4, function (err, unspentB) {
if (err) return done(err)
var tx = new bitcoin.TransactionBuilder(network)
tx.addInput(unspents[0].txId, unspents[0].vout)
tx.addInput(unspents[1].txId, unspents[1].vout)
tx.addOutput('n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi', 1e4)
tx.addOutput('mvGVHWi6gbkBZZPaqBVRcxvKVPYd9r3fp7', 1e4)
tx.sign(0, alice)
tx.sign(1, bob)
var tx = new bitcoin.TransactionBuilder(network)
tx.addInput(unspentA.txId, unspentA.vout)
tx.addInput(unspentB.txId, unspentB.vout)
tx.addOutput('n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi', 1e4)
tx.addOutput('mvGVHWi6gbkBZZPaqBVRcxvKVPYd9r3fp7', 1e4)
tx.sign(0, alice)
tx.sign(1, bob)
blockchain.t.transactions.propagate(tx.build().toHex(), done)
})
blockchain.t.transactions.propagate(tx.build().toHex(), done)
})
})
})

Loading…
Cancel
Save