Merge pull request #196 from dcousens/intasync

Integration tests running async causing wrong balance
This commit is contained in:
Wei Lu 2014-05-29 00:12:00 +08:00
commit c9ad359520

View file

@ -11,12 +11,12 @@ var Script = bitcoin.Script
var helloblock = require('helloblock-js')({ var helloblock = require('helloblock-js')({
network: 'testnet' network: 'testnet'
}); })
describe('p2sh', function() { describe('Bitcoin-js', function() {
this.timeout(10000); this.timeout(10000)
it('spends from a 2-of-2 address', function(done) { it('can spend from a 2-of-2 address', function(done) {
var privKeys = [ var privKeys = [
'91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx', '91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx',
'91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT' '91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT'
@ -24,52 +24,53 @@ describe('p2sh', function() {
return ECKey.fromWIF(wif) return ECKey.fromWIF(wif)
}) })
var pubKeys = privKeys.map(function(eck) { // how much to withdraw if we run dry
return eck.pub var coldAmount = 2e4
}) var outputAmount = 1e4
var pubKeys = privKeys.map(function(eck) { return eck.pub })
var redeemScript = Script.createMultisigScriptPubKey(2, pubKeys) var redeemScript = Script.createMultisigScriptPubKey(2, pubKeys)
var hash160 = crypto.hash160(new Buffer(redeemScript.buffer)) var multisigAddress = new Address(redeemScript.getHash(), networks.testnet.scriptHash).toString()
var multisigAddress = new Address(hash160, networks.testnet.scriptHash)
// Check what our target address's starting value is // Send some testnet coins to the multisig address to ensure it has some unspents for later
var targetAddress = 'mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r'; helloblock.faucet.withdraw(multisigAddress, coldAmount, function(err) {
helloblock.addresses.get(targetAddress, function(err, resp, resource) { if (err) return done(err)
if (err) done(err); })
var startingBalance = resource.balance
// Send some testnet coins to the multisig address so we ensure it has some unspents // make a random private key
helloblock.faucet.withdraw(multisigAddress.toString(), 100000, function(err, resp, resource) { var targetAddress = ECKey.makeRandom().pub.getAddress(networks.testnet.pubKeyHash).toString()
if (err) done(err);
// Get latest unspents from the mutlsigAddress // get latest unspents from the multisigAddress
helloblock.addresses.getUnspents(multisigAddress.toString(), function(err, resp, resource) { helloblock.addresses.getUnspents(multisigAddress, function(err, resp, resource) {
if (err) done(err); if (err) return done(err)
var tx = new Transaction() // use the oldest unspent
var unspent = resource[0]; var unspent = resource[resource.length - 1]
tx.addInput(unspent.txHash, unspent.index) var spendAmount = Math.min(unspent.value, outputAmount)
tx.addOutput(targetAddress, 100000, networks.testnet)
var signatures = privKeys.map(function(privKey) { var tx = new Transaction()
return tx.signScriptSig(0, redeemScript, privKey) tx.addInput(unspent.txHash, unspent.index)
}) tx.addOutput(targetAddress, spendAmount)
var redeemScriptSig = Script.createMultisigScriptSig(signatures) var signatures = privKeys.map(function(privKey) {
var scriptSig = Script.createP2SHScriptSig(redeemScriptSig, redeemScript) return tx.signScriptSig(0, redeemScript, privKey)
tx.setScriptSig(0, scriptSig) })
// Send from mutlsigAddress to targetAddress var redeemScriptSig = Script.createMultisigScriptSig(signatures)
helloblock.transactions.propagate(tx.toHex(), function(err, resp, resource) { var scriptSig = Script.createP2SHScriptSig(redeemScriptSig, redeemScript)
// no err means that transaction has been successfully propagated tx.setScriptSig(0, scriptSig)
if (err) done(err);
// Check that the funds (100000) indeed arrived at the intended target address // broadcast our transaction
helloblock.addresses.get(targetAddress, function(err, resp, resource) { helloblock.transactions.propagate(tx.toHex(), function(err, resp, resource) {
if (err) done(err); // no err means that the transaction has been successfully propagated
assert.equal(resource.balance, startingBalance + 100000) if (err) return done(err)
done()
}) // Check that the funds (spendAmount Satoshis) indeed arrived at the intended address
}) helloblock.addresses.get(targetAddress, function(err, resp, resource) {
if (err) return done(err)
assert.equal(resource.balance, spendAmount)
done()
}) })
}) })
}) })