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 bitcoin = require('../../')
|
||||
var crypto = bitcoin.crypto
|
||||
var networks = bitcoin.networks
|
||||
var scripts = bitcoin.scripts
|
||||
|
||||
var Address = bitcoin.Address
|
||||
var ECKey = bitcoin.ECKey
|
||||
var Transaction = bitcoin.Transaction
|
||||
var Script = bitcoin.Script
|
||||
var TransactionBuilder = bitcoin.TransactionBuilder
|
||||
|
||||
var helloblock = require('helloblock-js')({
|
||||
network: 'testnet'
|
||||
|
@ -43,35 +41,31 @@ describe('Bitcoin-js', function() {
|
|||
var targetAddress = ECKey.makeRandom().pub.getAddress(networks.testnet).toString()
|
||||
|
||||
// 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)
|
||||
|
||||
// use the oldest unspent
|
||||
var unspent = resource[resource.length - 1]
|
||||
var unspent = unspents[unspents.length - 1]
|
||||
var spendAmount = Math.min(unspent.value, outputAmount)
|
||||
|
||||
var tx = new Transaction()
|
||||
tx.addInput(unspent.txHash, unspent.index)
|
||||
tx.addOutput(targetAddress, spendAmount)
|
||||
var txb = new TransactionBuilder()
|
||||
txb.addInput(unspent.txHash, unspent.index)
|
||||
txb.addOutput(targetAddress, spendAmount)
|
||||
|
||||
var signatures = privKeys.map(function(privKey) {
|
||||
return tx.signInput(0, redeemScript, privKey)
|
||||
privKeys.forEach(function(privKey) {
|
||||
txb.sign(0, privKey, redeemScript)
|
||||
})
|
||||
|
||||
var redeemScriptSig = scripts.multisigInput(signatures)
|
||||
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
|
||||
tx.setInputScript(0, scriptSig)
|
||||
|
||||
// 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
|
||||
if (err) return done(err)
|
||||
|
||||
// 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)
|
||||
|
||||
assert.equal(resource.balance, spendAmount)
|
||||
assert.equal(addrInfo.balance, spendAmount)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue