2015-02-23 00:36:57 +01:00
|
|
|
/* global describe, it, beforeEach */
|
|
|
|
|
2014-06-16 08:05:31 +02:00
|
|
|
var assert = require('assert')
|
2015-08-20 05:37:19 +02:00
|
|
|
var baddress = require('../src/address')
|
|
|
|
var bscript = require('../src/script')
|
2016-12-17 15:24:00 +01:00
|
|
|
var ops = require('bitcoin-ops')
|
2015-03-11 01:51:01 +01:00
|
|
|
|
2014-06-16 08:05:31 +02:00
|
|
|
var BigInteger = require('bigi')
|
2015-03-02 06:48:36 +01:00
|
|
|
var ECPair = require('../src/ecpair')
|
2014-06-16 08:05:31 +02:00
|
|
|
var Transaction = require('../src/transaction')
|
|
|
|
var TransactionBuilder = require('../src/transaction_builder')
|
2015-07-28 08:42:57 +02:00
|
|
|
var NETWORKS = require('../src/networks')
|
2014-06-16 08:05:31 +02:00
|
|
|
|
|
|
|
var fixtures = require('./fixtures/transaction_builder')
|
|
|
|
|
2016-12-14 06:47:08 +01:00
|
|
|
function construct (f, dontSign) {
|
2015-07-28 08:42:57 +02:00
|
|
|
var network = NETWORKS[f.network]
|
2015-07-28 08:49:46 +02:00
|
|
|
var txb = new TransactionBuilder(network)
|
2015-07-28 08:42:57 +02:00
|
|
|
|
2016-06-22 06:57:11 +02:00
|
|
|
if (f.version !== undefined) txb.setVersion(f.version)
|
|
|
|
if (f.locktime !== undefined) txb.setLockTime(f.locktime)
|
2016-01-26 17:12:43 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
f.inputs.forEach(function (input) {
|
2016-11-12 03:08:10 +01:00
|
|
|
var prevTx
|
|
|
|
if (input.txRaw) {
|
|
|
|
var constructed = construct(input.txRaw)
|
|
|
|
if (input.txRaw.incomplete) prevTx = constructed.buildIncomplete()
|
|
|
|
else prevTx = constructed.build()
|
|
|
|
} else if (input.txHex) {
|
|
|
|
prevTx = Transaction.fromHex(input.txHex)
|
|
|
|
} else {
|
|
|
|
prevTx = input.txId
|
|
|
|
}
|
2015-01-28 07:31:06 +01:00
|
|
|
|
2016-11-12 03:08:10 +01:00
|
|
|
var prevTxScript
|
|
|
|
if (input.prevTxScript) {
|
2015-08-20 05:37:19 +02:00
|
|
|
prevTxScript = bscript.fromASM(input.prevTxScript)
|
2015-01-28 07:31:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-12 03:08:10 +01:00
|
|
|
txb.addInput(prevTx, input.vout, input.sequence, prevTxScript)
|
2015-01-28 07:31:06 +01:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
f.outputs.forEach(function (output) {
|
2016-11-12 03:08:10 +01:00
|
|
|
if (output.address) {
|
|
|
|
txb.addOutput(output.address, output.value)
|
|
|
|
} else {
|
|
|
|
txb.addOutput(bscript.fromASM(output.script), output.value)
|
|
|
|
}
|
2015-01-28 07:31:06 +01:00
|
|
|
})
|
|
|
|
|
2016-12-14 06:47:08 +01:00
|
|
|
if (dontSign) return txb
|
2015-01-28 07:31:06 +01:00
|
|
|
|
2016-10-12 03:47:54 +02:00
|
|
|
f.inputs.forEach(function (input, index) {
|
2016-11-09 03:01:29 +01:00
|
|
|
if (!input.signs) return
|
2016-10-12 03:47:54 +02:00
|
|
|
input.signs.forEach(function (sign) {
|
|
|
|
var keyPair = ECPair.fromWIF(sign.keyPair, network)
|
|
|
|
var redeemScript
|
2017-01-02 17:45:23 +01:00
|
|
|
var witnessScript
|
|
|
|
var value
|
2016-10-12 03:47:54 +02:00
|
|
|
if (sign.redeemScript) {
|
|
|
|
redeemScript = bscript.fromASM(sign.redeemScript)
|
|
|
|
}
|
2017-01-02 17:45:23 +01:00
|
|
|
if (sign.value) {
|
|
|
|
value = sign.value
|
|
|
|
}
|
|
|
|
if (sign.witnessScript) {
|
|
|
|
witnessScript = bscript.fromASM(sign.witnessScript)
|
|
|
|
}
|
|
|
|
txb.sign(index, keyPair, redeemScript, sign.hashType, value, witnessScript)
|
2015-01-28 07:31:06 +01:00
|
|
|
})
|
2016-10-12 03:47:54 +02:00
|
|
|
})
|
2015-01-28 07:31:06 +01:00
|
|
|
|
2015-07-28 08:49:46 +02:00
|
|
|
return txb
|
2015-01-28 07:31:06 +01:00
|
|
|
}
|
|
|
|
|
2015-08-20 05:12:05 +02:00
|
|
|
describe('TransactionBuilder', function () {
|
2015-08-20 05:03:54 +02:00
|
|
|
// constants
|
|
|
|
var keyPair = new ECPair(BigInteger.ONE)
|
|
|
|
var scripts = [
|
|
|
|
'1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH',
|
|
|
|
'1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP'
|
|
|
|
].map(function (x) {
|
2015-08-20 05:37:19 +02:00
|
|
|
return baddress.toOutputScript(x)
|
2015-08-20 05:03:54 +02:00
|
|
|
})
|
2017-04-19 09:39:16 +02:00
|
|
|
var txHash = Buffer.from('0e7cea811c0be9f73c0aca591034396e7264473fc25c1ca45195d7417b36cbe2', 'hex')
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2015-08-07 08:55:13 +02:00
|
|
|
describe('fromTransaction', function () {
|
|
|
|
fixtures.valid.build.forEach(function (f) {
|
2016-10-12 03:47:54 +02:00
|
|
|
it('returns TransactionBuilder, with ' + f.description, function () {
|
2015-08-07 08:55:13 +02:00
|
|
|
var network = NETWORKS[f.network || 'bitcoin']
|
2017-01-02 17:45:23 +01:00
|
|
|
|
2015-08-07 08:55:13 +02:00
|
|
|
var tx = Transaction.fromHex(f.txHex)
|
|
|
|
var txb = TransactionBuilder.fromTransaction(tx, network)
|
|
|
|
assert.strictEqual(txb.build().toHex(), f.txHex)
|
|
|
|
assert.strictEqual(txb.network, network)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-09-08 14:48:54 +02:00
|
|
|
fixtures.valid.fromTransaction.forEach(function (f) {
|
2016-10-12 03:47:54 +02:00
|
|
|
it('returns TransactionBuilder, with ' + f.description, function () {
|
2015-09-08 14:48:54 +02:00
|
|
|
var tx = new Transaction()
|
|
|
|
|
|
|
|
f.inputs.forEach(function (input) {
|
2017-04-19 09:39:16 +02:00
|
|
|
var txHash2 = Buffer.from(input.txId, 'hex').reverse()
|
2015-09-08 14:48:54 +02:00
|
|
|
|
2017-01-06 03:47:41 +01:00
|
|
|
tx.addInput(txHash2, input.vout, undefined, bscript.fromASM(input.scriptSig))
|
2015-09-08 14:48:54 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-08-07 08:55:13 +02:00
|
|
|
fixtures.invalid.fromTransaction.forEach(function (f) {
|
2016-09-27 16:16:51 +02:00
|
|
|
it('throws ' + f.exception, function () {
|
2015-08-07 08:55:13 +02:00
|
|
|
var tx = Transaction.fromHex(f.txHex)
|
|
|
|
|
|
|
|
assert.throws(function () {
|
|
|
|
TransactionBuilder.fromTransaction(tx)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('addInput', function () {
|
2016-09-27 16:46:14 +02:00
|
|
|
var txb
|
|
|
|
beforeEach(function () {
|
|
|
|
txb = new TransactionBuilder()
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('accepts a txHash, index [and sequence number]', function () {
|
2015-08-20 05:03:54 +02:00
|
|
|
var vin = txb.addInput(txHash, 1, 54)
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(vin, 0)
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2015-01-06 06:13:15 +01:00
|
|
|
var txIn = txb.tx.ins[0]
|
2015-08-20 05:03:54 +02:00
|
|
|
assert.strictEqual(txIn.hash, txHash)
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(txIn.index, 1)
|
|
|
|
assert.strictEqual(txIn.sequence, 54)
|
|
|
|
assert.strictEqual(txb.inputs[0].prevOutScript, undefined)
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('accepts a txHash, index [, sequence number and scriptPubKey]', function () {
|
2015-08-20 05:03:54 +02:00
|
|
|
var vin = txb.addInput(txHash, 1, 54, scripts[1])
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(vin, 0)
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2015-01-06 06:13:15 +01:00
|
|
|
var txIn = txb.tx.ins[0]
|
2015-08-20 05:03:54 +02:00
|
|
|
assert.strictEqual(txIn.hash, txHash)
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(txIn.index, 1)
|
|
|
|
assert.strictEqual(txIn.sequence, 54)
|
2015-08-20 05:03:54 +02:00
|
|
|
assert.strictEqual(txb.inputs[0].prevOutScript, scripts[1])
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('accepts a prevTx, index [and sequence number]', function () {
|
2015-08-20 05:03:54 +02:00
|
|
|
var prevTx = new Transaction()
|
|
|
|
prevTx.addOutput(scripts[0], 0)
|
|
|
|
prevTx.addOutput(scripts[1], 1)
|
|
|
|
|
2014-07-28 06:28:44 +02:00
|
|
|
var vin = txb.addInput(prevTx, 1, 54)
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(vin, 0)
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2015-01-06 06:13:15 +01:00
|
|
|
var txIn = txb.tx.ins[0]
|
2015-08-20 05:03:54 +02:00
|
|
|
assert.deepEqual(txIn.hash, prevTx.getHash())
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(txIn.index, 1)
|
|
|
|
assert.strictEqual(txIn.sequence, 54)
|
2015-08-20 05:03:54 +02:00
|
|
|
assert.strictEqual(txb.inputs[0].prevOutScript, scripts[1])
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns the input index', function () {
|
2015-08-20 05:03:54 +02:00
|
|
|
assert.strictEqual(txb.addInput(txHash, 0), 0)
|
|
|
|
assert.strictEqual(txb.addInput(txHash, 1), 1)
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function () {
|
2015-08-20 05:03:54 +02:00
|
|
|
txb.addInput(txHash, 0)
|
2015-03-02 06:48:36 +01:00
|
|
|
txb.sign(0, keyPair)
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2015-08-20 05:03:54 +02:00
|
|
|
txb.addInput(txHash, 0)
|
2014-06-16 08:05:31 +02:00
|
|
|
}, /No, this would invalidate signatures/)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('addOutput', function () {
|
2016-09-27 16:46:14 +02:00
|
|
|
var txb
|
|
|
|
beforeEach(function () {
|
|
|
|
txb = new TransactionBuilder()
|
|
|
|
})
|
|
|
|
|
2015-03-02 07:18:56 +01:00
|
|
|
it('accepts an address string and value', function () {
|
2015-08-20 05:03:54 +02:00
|
|
|
var vout = txb.addOutput(keyPair.getAddress(), 1000)
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(vout, 0)
|
2015-03-02 07:18:56 +01:00
|
|
|
|
|
|
|
var txout = txb.tx.outs[0]
|
2015-08-20 05:03:54 +02:00
|
|
|
assert.deepEqual(txout.script, scripts[0])
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(txout.value, 1000)
|
2015-03-02 07:18:56 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('accepts a ScriptPubKey and value', function () {
|
2015-08-20 05:03:54 +02:00
|
|
|
var vout = txb.addOutput(scripts[0], 1000)
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(vout, 0)
|
2015-03-02 07:18:56 +01:00
|
|
|
|
|
|
|
var txout = txb.tx.outs[0]
|
2015-08-20 05:03:54 +02:00
|
|
|
assert.deepEqual(txout.script, scripts[0])
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(txout.value, 1000)
|
2015-03-02 07:18:56 +01:00
|
|
|
})
|
|
|
|
|
2015-07-24 04:16:37 +02:00
|
|
|
it('throws if address is of the wrong network', function () {
|
|
|
|
assert.throws(function () {
|
|
|
|
txb.addOutput('2NGHjvjw83pcVFgMcA7QvSMh2c246rxLVz9', 1000)
|
|
|
|
}, /2NGHjvjw83pcVFgMcA7QvSMh2c246rxLVz9 has no matching Script/)
|
|
|
|
})
|
|
|
|
|
2015-11-23 15:05:06 +01:00
|
|
|
it('add second output after signed first input with SIGHASH_NONE', function () {
|
|
|
|
txb.addInput(txHash, 0)
|
|
|
|
txb.addOutput(scripts[0], 2000)
|
|
|
|
txb.sign(0, keyPair, undefined, Transaction.SIGHASH_NONE)
|
|
|
|
assert.equal(txb.addOutput(scripts[1], 9000), 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('add first output after signed first input with SIGHASH_NONE', function () {
|
|
|
|
txb.addInput(txHash, 0)
|
|
|
|
txb.sign(0, keyPair, undefined, Transaction.SIGHASH_NONE)
|
|
|
|
assert.equal(txb.addOutput(scripts[0], 2000), 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('add second output after signed first input with SIGHASH_SINGLE', function () {
|
|
|
|
txb.addInput(txHash, 0)
|
|
|
|
txb.addOutput(scripts[0], 2000)
|
|
|
|
txb.sign(0, keyPair, undefined, Transaction.SIGHASH_SINGLE)
|
|
|
|
assert.equal(txb.addOutput(scripts[1], 9000), 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('add first output after signed first input with SIGHASH_SINGLE', function () {
|
|
|
|
txb.addInput(txHash, 0)
|
|
|
|
txb.sign(0, keyPair, undefined, Transaction.SIGHASH_SINGLE)
|
|
|
|
assert.throws(function () {
|
|
|
|
txb.addOutput(scripts[0], 2000)
|
|
|
|
}, /No, this would invalidate signatures/)
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function () {
|
2015-08-20 05:03:54 +02:00
|
|
|
txb.addInput(txHash, 0)
|
|
|
|
txb.addOutput(scripts[0], 2000)
|
2015-03-02 06:48:36 +01:00
|
|
|
txb.sign(0, keyPair)
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2015-08-20 05:03:54 +02:00
|
|
|
txb.addOutput(scripts[1], 9000)
|
2014-06-16 08:05:31 +02:00
|
|
|
}, /No, this would invalidate signatures/)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-01-27 14:48:44 +01:00
|
|
|
describe('setLockTime', function () {
|
|
|
|
it('throws if if there exist any scriptSigs', function () {
|
2016-09-27 16:46:14 +02:00
|
|
|
var txb = new TransactionBuilder()
|
2016-01-27 14:48:44 +01:00
|
|
|
txb.addInput(txHash, 0)
|
|
|
|
txb.sign(0, keyPair)
|
|
|
|
|
|
|
|
assert.throws(function () {
|
|
|
|
txb.setLockTime(65535)
|
|
|
|
}, /No, this would invalidate signatures/)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('sign', function () {
|
|
|
|
fixtures.invalid.sign.forEach(function (f) {
|
2015-07-28 08:42:57 +02:00
|
|
|
it('throws on ' + f.exception + (f.description ? ' (' + f.description + ')' : ''), function () {
|
2016-12-14 06:47:08 +01:00
|
|
|
var txb = construct(f, true)
|
2015-01-06 01:00:49 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
f.inputs.forEach(function (input, index) {
|
|
|
|
input.signs.forEach(function (sign) {
|
2015-08-07 09:01:31 +02:00
|
|
|
var keyPairNetwork = NETWORKS[sign.network || f.network]
|
2017-01-06 03:47:41 +01:00
|
|
|
var keyPair2 = ECPair.fromWIF(sign.keyPair, keyPairNetwork)
|
2015-02-04 10:22:11 +01:00
|
|
|
var redeemScript
|
2015-01-06 01:00:49 +01:00
|
|
|
|
2015-02-04 10:22:11 +01:00
|
|
|
if (sign.redeemScript) {
|
2015-08-20 05:37:19 +02:00
|
|
|
redeemScript = bscript.fromASM(sign.redeemScript)
|
2015-02-04 10:22:11 +01:00
|
|
|
}
|
2015-01-06 01:00:49 +01:00
|
|
|
|
2015-02-04 10:22:11 +01:00
|
|
|
if (!sign.throws) {
|
2017-01-06 03:47:41 +01:00
|
|
|
txb.sign(index, keyPair2, redeemScript, sign.hashType, sign.value)
|
2015-01-06 01:00:49 +01:00
|
|
|
} else {
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2017-01-06 03:47:41 +01:00
|
|
|
txb.sign(index, keyPair2, redeemScript, sign.hashType, sign.value)
|
2015-01-06 01:00:49 +01:00
|
|
|
}, new RegExp(f.exception))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('build', function () {
|
|
|
|
fixtures.valid.build.forEach(function (f) {
|
|
|
|
it('builds "' + f.description + '"', function () {
|
2016-09-27 16:46:14 +02:00
|
|
|
var txb = construct(f)
|
2014-06-16 08:05:31 +02:00
|
|
|
var tx = txb.build()
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(tx.toHex(), f.txHex)
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-09-27 17:05:01 +02:00
|
|
|
// TODO: remove duplicate test code
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.build.forEach(function (f) {
|
|
|
|
describe('for ' + (f.description || f.exception), function () {
|
2016-09-27 16:16:51 +02:00
|
|
|
it('throws ' + f.exception, function () {
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2016-09-27 17:05:01 +02:00
|
|
|
var txb
|
|
|
|
if (f.txHex) {
|
|
|
|
txb = TransactionBuilder.fromTransaction(Transaction.fromHex(f.txHex))
|
|
|
|
} else {
|
|
|
|
txb = construct(f)
|
|
|
|
}
|
|
|
|
|
2015-01-28 07:31:06 +01:00
|
|
|
txb.build()
|
|
|
|
}, new RegExp(f.exception))
|
2014-07-16 14:24:10 +02:00
|
|
|
})
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2016-09-27 17:05:01 +02:00
|
|
|
// if throws on incomplete too, enforce that
|
|
|
|
if (f.incomplete) {
|
|
|
|
it('throws ' + f.exception, function () {
|
|
|
|
assert.throws(function () {
|
|
|
|
var txb
|
|
|
|
if (f.txHex) {
|
|
|
|
txb = TransactionBuilder.fromTransaction(Transaction.fromHex(f.txHex))
|
|
|
|
} else {
|
|
|
|
txb = construct(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
txb.buildIncomplete()
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
it('does not throw if buildIncomplete', function () {
|
|
|
|
var txb
|
|
|
|
if (f.txHex) {
|
|
|
|
txb = TransactionBuilder.fromTransaction(Transaction.fromHex(f.txHex))
|
|
|
|
} else {
|
|
|
|
txb = construct(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
txb.buildIncomplete()
|
|
|
|
})
|
|
|
|
}
|
2014-07-16 14:24:10 +02:00
|
|
|
})
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|
2017-07-20 23:30:14 +02:00
|
|
|
|
|
|
|
it('for incomplete with 0 signatures', function () {
|
|
|
|
var randomTxData = '0100000000010100010000000000000000000000000000000000000000000000000000000000000000000000ffffffff01e8030000000000001976a9144c9c3dfac4207d5d8cb89df5722cb3d712385e3f88ac02483045022100aa5d8aa40a90f23ce2c3d11bc845ca4a12acd99cbea37de6b9f6d86edebba8cb022022dedc2aa0a255f74d04c0b76ece2d7c691f9dd11a64a8ac49f62a99c3a05f9d01232103596d3451025c19dbbdeb932d6bf8bfb4ad499b95b6f88db8899efac102e5fc71ac00000000'
|
|
|
|
var randomAddress = '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH'
|
|
|
|
|
|
|
|
var randomTx = Transaction.fromHex(randomTxData)
|
|
|
|
var tx = new TransactionBuilder()
|
|
|
|
tx.addInput(randomTx, 0)
|
2017-08-07 15:09:37 +02:00
|
|
|
tx.addOutput(randomAddress, 1000)
|
2017-07-20 23:30:14 +02:00
|
|
|
tx = tx.buildIncomplete()
|
|
|
|
assert(tx)
|
|
|
|
})
|
2017-08-01 22:12:34 +02:00
|
|
|
|
|
|
|
it('for incomplete P2SH with 0 signatures', function () {
|
|
|
|
var inp = Buffer.from('010000000173120703f67318aef51f7251272a6816d3f7523bb25e34b136d80be959391c100000000000ffffffff0100c817a80400000017a91471a8ec07ff69c6c4fee489184c462a9b1b9237488700000000', 'hex') // arbitrary P2SH input
|
|
|
|
var inpTx = Transaction.fromBuffer(inp)
|
|
|
|
|
|
|
|
var txb = new TransactionBuilder(NETWORKS.testnet)
|
|
|
|
txb.addInput(inpTx, 0)
|
2017-08-07 15:09:37 +02:00
|
|
|
txb.addOutput('2NAkqp5xffoomp5RLBcakuGpZ12GU4twdz4', 1e8) // arbitrary output
|
2017-08-01 22:12:34 +02:00
|
|
|
|
|
|
|
txb.buildIncomplete()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('for incomplete P2WPKH with 0 signatures', function () {
|
|
|
|
var inp = Buffer.from('010000000173120703f67318aef51f7251272a6816d3f7523bb25e34b136d80be959391c100000000000ffffffff0100c817a8040000001600141a15805e1f4040c9f68ccc887fca2e63547d794b00000000', 'hex')
|
|
|
|
var inpTx = Transaction.fromBuffer(inp)
|
|
|
|
|
|
|
|
var txb = new TransactionBuilder(NETWORKS.testnet)
|
|
|
|
txb.addInput(inpTx, 0)
|
2017-08-07 15:09:37 +02:00
|
|
|
txb.addOutput('2NAkqp5xffoomp5RLBcakuGpZ12GU4twdz4', 1e8) // arbitrary output
|
2017-08-01 22:12:34 +02:00
|
|
|
|
|
|
|
txb.buildIncomplete()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('for incomplete P2WSH with 0 signatures', function () {
|
|
|
|
var inpTx = Transaction.fromBuffer(Buffer.from('010000000173120703f67318aef51f7251272a6816d3f7523bb25e34b136d80be959391c100000000000ffffffff0100c817a80400000022002072df76fcc0b231b94bdf7d8c25d7eef4716597818d211e19ade7813bff7a250200000000', 'hex'))
|
|
|
|
|
|
|
|
var txb = new TransactionBuilder(NETWORKS.testnet)
|
|
|
|
txb.addInput(inpTx, 0)
|
2017-08-07 15:09:37 +02:00
|
|
|
txb.addOutput('2NAkqp5xffoomp5RLBcakuGpZ12GU4twdz4', 1e8) // arbitrary output
|
2017-08-01 22:12:34 +02:00
|
|
|
|
|
|
|
txb.buildIncomplete()
|
|
|
|
})
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|
2014-07-28 06:28:44 +02:00
|
|
|
|
2015-03-03 11:50:46 +01:00
|
|
|
describe('multisig', function () {
|
|
|
|
fixtures.valid.multisig.forEach(function (f) {
|
|
|
|
it(f.description, function () {
|
2016-12-14 06:47:08 +01:00
|
|
|
var txb = construct(f, true)
|
2015-03-03 11:50:46 +01:00
|
|
|
var tx
|
2015-08-07 08:41:24 +02:00
|
|
|
var network = NETWORKS[f.network]
|
2015-03-03 11:50:46 +01:00
|
|
|
|
2015-03-11 01:51:01 +01:00
|
|
|
f.inputs.forEach(function (input, i) {
|
2015-08-20 05:37:19 +02:00
|
|
|
var redeemScript = bscript.fromASM(input.redeemScript)
|
2015-03-03 11:50:46 +01:00
|
|
|
|
2015-03-11 01:51:01 +01:00
|
|
|
input.signs.forEach(function (sign) {
|
|
|
|
// rebuild the transaction each-time after the first
|
|
|
|
if (tx) {
|
|
|
|
// do we filter OP_0's beforehand?
|
|
|
|
if (sign.filterOP_0) {
|
|
|
|
var scriptSig = tx.ins[i].script
|
|
|
|
|
|
|
|
// ignore OP_0 on the front, ignore redeemScript
|
2015-08-20 05:37:19 +02:00
|
|
|
var signatures = bscript.decompile(scriptSig).slice(1, -1).filter(function (x) { return x !== ops.OP_0 })
|
2015-03-11 01:51:01 +01:00
|
|
|
|
|
|
|
// rebuild/replace the scriptSig without them
|
2016-11-02 02:30:37 +01:00
|
|
|
var replacement = bscript.scriptHash.input.encode(bscript.multisig.input.encode(signatures), redeemScript)
|
2015-08-20 05:37:19 +02:00
|
|
|
assert.strictEqual(bscript.toASM(replacement), sign.scriptSigFiltered)
|
2015-03-11 01:32:27 +01:00
|
|
|
|
2015-03-11 01:51:01 +01:00
|
|
|
tx.ins[i].script = replacement
|
|
|
|
}
|
|
|
|
// now import it
|
2015-08-07 08:41:24 +02:00
|
|
|
txb = TransactionBuilder.fromTransaction(tx, network)
|
2015-03-11 01:51:01 +01:00
|
|
|
}
|
2015-03-03 11:50:46 +01:00
|
|
|
|
2017-01-06 03:47:41 +01:00
|
|
|
var keyPair2 = ECPair.fromWIF(sign.keyPair, network)
|
|
|
|
txb.sign(i, keyPair2, redeemScript, sign.hashType)
|
2015-03-03 11:50:46 +01:00
|
|
|
|
2015-03-11 01:51:01 +01:00
|
|
|
// update the tx
|
|
|
|
tx = txb.buildIncomplete()
|
2015-03-11 01:32:27 +01:00
|
|
|
// now verify the serialized scriptSig is as expected
|
2015-08-20 05:37:19 +02:00
|
|
|
assert.strictEqual(bscript.toASM(tx.ins[i].script), sign.scriptSig)
|
2015-03-03 11:50:46 +01:00
|
|
|
})
|
2015-03-11 01:51:01 +01:00
|
|
|
})
|
2015-03-03 11:50:46 +01:00
|
|
|
|
|
|
|
tx = txb.build()
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(tx.toHex(), f.txHex)
|
2015-03-03 11:50:46 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-07-13 03:27:59 +02:00
|
|
|
describe('various edge case', function () {
|
2016-09-27 16:46:14 +02:00
|
|
|
var network = NETWORKS.testnet
|
|
|
|
|
2015-06-23 08:09:14 +02:00
|
|
|
it('should handle badly pre-filled OP_0s', function () {
|
2016-09-27 16:46:14 +02:00
|
|
|
// OP_0 is used where a signature is missing
|
|
|
|
var redeemScripSig = bscript.fromASM('OP_0 OP_0 3045022100daf0f4f3339d9fbab42b098045c1e4958ee3b308f4ae17be80b63808558d0adb02202f07e3d1f79dc8da285ae0d7f68083d769c11f5621ebd9691d6b48c0d4283d7d01 52410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a4104f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e67253ae')
|
|
|
|
var redeemScript = bscript.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG')
|
2015-08-07 08:55:13 +02:00
|
|
|
|
2016-09-27 16:46:14 +02:00
|
|
|
var tx = new Transaction()
|
2017-04-19 09:39:16 +02:00
|
|
|
tx.addInput(Buffer.from('cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f07149', 'hex'), 0, undefined, redeemScripSig)
|
|
|
|
tx.addOutput(Buffer.from('76a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac', 'hex'), 1000)
|
2015-05-28 22:36:41 +02:00
|
|
|
|
2016-09-27 16:46:14 +02:00
|
|
|
// now import the Transaction
|
|
|
|
var txb = TransactionBuilder.fromTransaction(tx, NETWORKS.testnet)
|
2015-05-28 22:36:41 +02:00
|
|
|
|
2017-01-06 03:47:41 +01:00
|
|
|
var keyPair2 = ECPair.fromWIF('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe', network)
|
|
|
|
txb.sign(0, keyPair2, redeemScript)
|
2015-05-28 22:36:41 +02:00
|
|
|
|
2016-09-27 16:46:14 +02:00
|
|
|
var tx2 = txb.build()
|
|
|
|
assert.equal(tx2.getId(), 'eab59618a564e361adef6d918bd792903c3d41bcf1220137364fb847880467f9')
|
|
|
|
assert.equal(bscript.toASM(tx2.ins[0].script), 'OP_0 3045022100daf0f4f3339d9fbab42b098045c1e4958ee3b308f4ae17be80b63808558d0adb02202f07e3d1f79dc8da285ae0d7f68083d769c11f5621ebd9691d6b48c0d4283d7d01 3045022100a346c61738304eac5e7702188764d19cdf68f4466196729db096d6c87ce18cdd022018c0e8ad03054b0e7e235cda6bedecf35881d7aa7d94ff425a8ace7220f38af001 52410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a4104f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e67253ae')
|
2015-05-28 22:36:41 +02:00
|
|
|
})
|
2017-07-13 03:27:59 +02:00
|
|
|
|
|
|
|
it('should not classify blank scripts as nonstandard', function () {
|
|
|
|
var tx = new TransactionBuilder()
|
|
|
|
tx.addInput('aa94ab02c182214f090e99a0d57021caffd0f195a81c24602b1028b130b63e31', 0)
|
|
|
|
|
|
|
|
var incomplete = tx.buildIncomplete().toHex()
|
|
|
|
var keyPair = ECPair.fromWIF('L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy')
|
|
|
|
|
|
|
|
// sign, as expected
|
|
|
|
tx.addOutput('1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK', 15000)
|
|
|
|
tx.sign(0, keyPair)
|
|
|
|
var txId = tx.build().getId()
|
|
|
|
assert.equal(txId, '54f097315acbaedb92a95455da3368eb45981cdae5ffbc387a9afc872c0f29b3')
|
|
|
|
|
|
|
|
// and, repeat
|
|
|
|
tx = TransactionBuilder.fromTransaction(Transaction.fromHex(incomplete))
|
|
|
|
tx.addOutput('1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK', 15000)
|
|
|
|
tx.sign(0, keyPair)
|
|
|
|
var txId2 = tx.build().getId()
|
|
|
|
assert.equal(txId, txId2)
|
|
|
|
})
|
2015-05-28 22:36:41 +02:00
|
|
|
})
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|