TxBuilder: pass network through fromTransaction, fixes failing tests

This commit is contained in:
Daniel Cousens 2015-08-07 16:55:13 +10:00
parent 1e90e1620a
commit a4eb58f103
2 changed files with 29 additions and 25 deletions

View file

@ -95,7 +95,7 @@ function TransactionBuilder (network) {
}
TransactionBuilder.fromTransaction = function (transaction, network) {
var txb = new TransactionBuilder(/* remove after failing test added: network*/)
var txb = new TransactionBuilder(network)
// Copy other transaction fields
txb.tx.version = transaction.version

View file

@ -80,6 +80,29 @@ describe('TransactionBuilder', function () {
privScript = Address.toOutputScript(privAddress)
})
describe('fromTransaction', function () {
fixtures.valid.build.forEach(function (f) {
it('builds the correct TransactionBuilder for ' + f.description, function () {
var network = NETWORKS[f.network || 'bitcoin']
var tx = Transaction.fromHex(f.txHex)
var txb = TransactionBuilder.fromTransaction(tx, network)
assert.strictEqual(txb.build().toHex(), f.txHex)
assert.strictEqual(txb.network, network)
})
})
fixtures.invalid.fromTransaction.forEach(function (f) {
it('throws on ' + f.exception, function () {
var tx = Transaction.fromHex(f.txHex)
assert.throws(function () {
TransactionBuilder.fromTransaction(tx)
}, new RegExp(f.exception))
})
})
})
describe('addInput', function () {
it('accepts a txHash, index [and sequence number]', function () {
var vin = txb.addInput(prevTxHash, 1, 54)
@ -281,37 +304,18 @@ describe('TransactionBuilder', function () {
describe('multisig edge case', function () {
it('should handle badly pre-filled OP_0s', function () {
txb = TransactionBuilder.fromTransaction(Transaction.fromHex('0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f0714900000000fd16010000483045022100daf0f4f3339d9fbab42b098045c1e4958ee3b308f4ae17be80b63808558d0adb02202f07e3d1f79dc8da285ae0d7f68083d769c11f5621ebd9691d6b48c0d4283d7d014cc952410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a4104f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e67253aeffffffff01e8030000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000'))
var lameTx = Transaction.fromHex('0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f0714900000000fd16010000483045022100daf0f4f3339d9fbab42b098045c1e4958ee3b308f4ae17be80b63808558d0adb02202f07e3d1f79dc8da285ae0d7f68083d769c11f5621ebd9691d6b48c0d4283d7d014cc952410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a4104f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e67253aeffffffff01e8030000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000')
var network = NETWORKS.testnet
txb = TransactionBuilder.fromTransaction(lameTx, network)
var redeemScript = Script.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG')
var keyPair = ECPair.fromWIF('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe', NETWORKS.testnet)
var keyPair = ECPair.fromWIF('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe', network)
txb.sign(0, keyPair, redeemScript)
var tx = txb.build()
assert.equal(tx.toHex(), '0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f0714900000000fd5e0100483045022100daf0f4f3339d9fbab42b098045c1e4958ee3b308f4ae17be80b63808558d0adb02202f07e3d1f79dc8da285ae0d7f68083d769c11f5621ebd9691d6b48c0d4283d7d01483045022100a346c61738304eac5e7702188764d19cdf68f4466196729db096d6c87ce18cdd022018c0e8ad03054b0e7e235cda6bedecf35881d7aa7d94ff425a8ace7220f38af0014cc952410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a4104f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e67253aeffffffff01e8030000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000')
})
})
describe('fromTransaction', function () {
fixtures.valid.build.forEach(function (f) {
it('builds the correct TransactionBuilder for ' + f.description, function () {
var network = NETWORKS[f.network]
var tx = Transaction.fromHex(f.txHex)
var txb = TransactionBuilder.fromTransaction(tx, network)
assert.strictEqual(txb.build().toHex(), f.txHex)
})
})
fixtures.invalid.fromTransaction.forEach(function (f) {
it('throws on ' + f.exception, function () {
var tx = Transaction.fromHex(f.txHex)
assert.throws(function () {
TransactionBuilder.fromTransaction(tx)
}, new RegExp(f.exception))
})
})
})
})