From 74e7d1edbc08ff0d753256c0390d4071712862f7 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 8 Sep 2015 22:48:54 +1000 Subject: [PATCH] tests: add further tests for #404 --- test/fixtures/transaction_builder.json | 58 ++++++++++++++++++++++++++ test/transaction_builder.js | 30 ++++++++++++- 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index d85efc7..2046474 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -265,6 +265,64 @@ ] } ], + "fromTransaction": [ + { + "description": "Transaction w/ scriptHash(multisig 2-of-2) -> OP_RETURN | 1 OP_0, no signatures", + "network": "testnet", + "incomplete": true, + "inputs": [ + { + "txId": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", + "vout": 0, + "scriptSig": "OP_0 OP_0 52410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52ae", + "scriptSigAfter": "OP_0 OP_0 OP_0 52410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52ae" + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 1000 + } + ] + }, + { + "description": "Transaction w/ scriptHash(multisig 2-of-2) -> OP_RETURN | missing OP_0, 1 signature", + "network": "testnet", + "incomplete": true, + "inputs": [ + { + "txId": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", + "vout": 0, + "scriptSig": "OP_0 3045022100aa0c323bc639d3d71591be98ccaf7b8cb8c86aa95f060aef5e36fc3f04c4d029022010e2b18de17e307a12ae7e0e88518fe814f18a0ca1ee4510ba23a65628b0657601 52410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52ae", + "scriptSigAfter": "OP_0 OP_0 3045022100aa0c323bc639d3d71591be98ccaf7b8cb8c86aa95f060aef5e36fc3f04c4d029022010e2b18de17e307a12ae7e0e88518fe814f18a0ca1ee4510ba23a65628b0657601 52410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52ae" + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 1000 + } + ] + }, + { + "description": "Transaction w/ scriptHash(multisig 2-of-2) -> OP_RETURN | no OP_0, 2 signatures", + "network": "testnet", + "inputs": [ + { + "txId": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", + "vout": 0, + "scriptSig": "OP_0 3045022100e37e33a4fe5fccfb87afb0e951e83fcea4820d73b327d21edc1adec3b916c437022061c5786908b674e323a1863cc2feeb60e1679f1892c10ee08ac21e51fd50ba9001 3045022100e37e33a4fe5fccfb87afb0e951e83fcea4820d73b327d21edc1adec3b916c437022061c5786908b674e323a1863cc2feeb60e1679f1892c10ee08ac21e51fd50ba9001 52410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52ae", + "scriptSigAfter": "OP_0 3045022100e37e33a4fe5fccfb87afb0e951e83fcea4820d73b327d21edc1adec3b916c437022061c5786908b674e323a1863cc2feeb60e1679f1892c10ee08ac21e51fd50ba9001 3045022100e37e33a4fe5fccfb87afb0e951e83fcea4820d73b327d21edc1adec3b916c437022061c5786908b674e323a1863cc2feeb60e1679f1892c10ee08ac21e51fd50ba9001 52410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52ae" + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 1000 + } + ] + } + ], "multisig": [ { "description": "P2SH 2-of-2 multisig, signed in correct order", diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 06b070e..12c6b3d 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -3,6 +3,7 @@ var assert = require('assert') var baddress = require('../src/address') var bscript = require('../src/script') +var bufferutils = require('../src/bufferutils') var ops = require('../src/opcodes') var BigInteger = require('bigi') @@ -76,7 +77,7 @@ describe('TransactionBuilder', function () { describe('fromTransaction', function () { fixtures.valid.build.forEach(function (f) { - it('builds the correct TransactionBuilder for ' + f.description, function () { + it('builds TransactionBuilder, with ' + f.description, function () { var network = NETWORKS[f.network || 'bitcoin'] var tx = Transaction.fromHex(f.txHex) var txb = TransactionBuilder.fromTransaction(tx, network) @@ -86,6 +87,33 @@ describe('TransactionBuilder', function () { }) }) + fixtures.valid.fromTransaction.forEach(function (f) { + it('builds TransactionBuilder, with ' + f.description, function () { + var tx = new Transaction() + + f.inputs.forEach(function (input) { + var txHash = bufferutils.reverse(new Buffer(input.txId, 'hex')) + + tx.addInput(txHash, input.vout, undefined, bscript.fromASM(input.scriptSig)) + }) + + f.outputs.forEach(function (output) { + tx.addOutput(bscript.fromASM(output.script), output.value) + }) + + var txb = TransactionBuilder.fromTransaction(tx) + var txAfter = f.incomplete ? txb.buildIncomplete() : txb.build() + + txAfter.ins.forEach(function (input, i) { + assert.equal(bscript.toASM(input.script), f.inputs[i].scriptSigAfter) + }) + + txAfter.outs.forEach(function (output, i) { + assert.equal(bscript.toASM(output.script), f.outputs[i].script) + }) + }) + }) + fixtures.invalid.fromTransaction.forEach(function (f) { it('throws on ' + f.exception, function () { var tx = Transaction.fromHex(f.txHex)