tests: integration test to use TxBuilder
This commit is contained in:
parent
418a56cbdc
commit
f3199b6fce
1 changed files with 11 additions and 17 deletions
|
@ -1,14 +1,12 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
|
|
||||||
var bitcoin = require('../../')
|
var bitcoin = require('../../')
|
||||||
var crypto = bitcoin.crypto
|
|
||||||
var networks = bitcoin.networks
|
var networks = bitcoin.networks
|
||||||
var scripts = bitcoin.scripts
|
var scripts = bitcoin.scripts
|
||||||
|
|
||||||
var Address = bitcoin.Address
|
var Address = bitcoin.Address
|
||||||
var ECKey = bitcoin.ECKey
|
var ECKey = bitcoin.ECKey
|
||||||
var Transaction = bitcoin.Transaction
|
var TransactionBuilder = bitcoin.TransactionBuilder
|
||||||
var Script = bitcoin.Script
|
|
||||||
|
|
||||||
var helloblock = require('helloblock-js')({
|
var helloblock = require('helloblock-js')({
|
||||||
network: 'testnet'
|
network: 'testnet'
|
||||||
|
@ -43,35 +41,31 @@ describe('Bitcoin-js', function() {
|
||||||
var targetAddress = ECKey.makeRandom().pub.getAddress(networks.testnet).toString()
|
var targetAddress = ECKey.makeRandom().pub.getAddress(networks.testnet).toString()
|
||||||
|
|
||||||
// get latest unspents from the multisigAddress
|
// get latest unspents from the multisigAddress
|
||||||
helloblock.addresses.getUnspents(multisigAddress, function(err, resp, resource) {
|
helloblock.addresses.getUnspents(multisigAddress, function(err, res, unspents) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
// use the oldest unspent
|
// use the oldest unspent
|
||||||
var unspent = resource[resource.length - 1]
|
var unspent = unspents[unspents.length - 1]
|
||||||
var spendAmount = Math.min(unspent.value, outputAmount)
|
var spendAmount = Math.min(unspent.value, outputAmount)
|
||||||
|
|
||||||
var tx = new Transaction()
|
var txb = new TransactionBuilder()
|
||||||
tx.addInput(unspent.txHash, unspent.index)
|
txb.addInput(unspent.txHash, unspent.index)
|
||||||
tx.addOutput(targetAddress, spendAmount)
|
txb.addOutput(targetAddress, spendAmount)
|
||||||
|
|
||||||
var signatures = privKeys.map(function(privKey) {
|
privKeys.forEach(function(privKey) {
|
||||||
return tx.signInput(0, redeemScript, privKey)
|
txb.sign(0, privKey, redeemScript)
|
||||||
})
|
})
|
||||||
|
|
||||||
var redeemScriptSig = scripts.multisigInput(signatures)
|
|
||||||
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
|
|
||||||
tx.setInputScript(0, scriptSig)
|
|
||||||
|
|
||||||
// broadcast our transaction
|
// broadcast our transaction
|
||||||
helloblock.transactions.propagate(tx.toHex(), function(err, resp, resource) {
|
helloblock.transactions.propagate(txb.build().toHex(), function(err, res) {
|
||||||
// no err means that the transaction has been successfully propagated
|
// no err means that the transaction has been successfully propagated
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
// Check that the funds (spendAmount Satoshis) indeed arrived at the intended address
|
// Check that the funds (spendAmount Satoshis) indeed arrived at the intended address
|
||||||
helloblock.addresses.get(targetAddress, function(err, resp, resource) {
|
helloblock.addresses.get(targetAddress, function(err, res, addrInfo) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
assert.equal(resource.balance, spendAmount)
|
assert.equal(addrInfo.balance, spendAmount)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue