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 keyPair = bitcoin.ECPair.fromWIF(process.env.BITCOINJS_TESTNET_WIF, kpNetwork)
var kpAddress = keyPair.getAddress() var kpAddress = keyPair.getAddress()
function fundAddress (unspents, address, amount, callback) { function fundAddress (unspents, outputs, callback) {
var result = coinSelect(unspents, [{ var result = coinSelect(unspents, outputs, 10)
address: address,
value: amount
}], 10)
if (!result.inputs) return callback(new Error('Faucet empty')) if (!result.inputs) return callback(new Error('Faucet empty'))
var txb = new bitcoin.TransactionBuilder(kpNetwork) var txb = new bitcoin.TransactionBuilder(kpNetwork)
@ -34,24 +30,32 @@ function fundAddress (unspents, address, amount, callback) {
}) })
var tx = txb.build() var tx = txb.build()
var txId = tx.getId()
testnet.transactions.propagate(tx.toHex(), function (err) { testnet.transactions.propagate(tx.toHex(), function (err) {
callback(err, { callback(err, outputs.map(function (x, i) {
txId: tx.getId(), return { txId: txId, vout: i }
vout: 0 }))
}, 0)
}) })
} }
testnet.faucet = function faucet (address, amount, done) { testnet.faucetMany = function faucetMany (outputs, callback) {
testnet.addresses.unspents(kpAddress, function (err, unspents) { testnet.addresses.unspents(kpAddress, function (err, unspents) {
if (err) return done(err) if (err) return callback(err)
typeforce([{ typeforce([{
txId: types.Hex, txId: types.Hex,
vout: types.UInt32, vout: types.UInt32,
value: types.Satoshi value: types.Satoshi
}], unspents) }], 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 alicesAddress = alice.getAddress()
var bobsAddress = bob.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) if (err) return done(err)
blockchain.t.faucet(bobsAddress, 2e4, function (err, unspentB) { var tx = new bitcoin.TransactionBuilder(network)
if (err) return done(err) 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) blockchain.t.transactions.propagate(tx.build().toHex(), done)
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)
})
}) })
}) })
}) })

Loading…
Cancel
Save