|
@ -1,8 +1,8 @@ |
|
|
const { describe, it, beforeEach } = require('mocha') |
|
|
const { describe, it, beforeEach } = require('mocha') |
|
|
const assert = require('assert') |
|
|
const assert = require('assert') |
|
|
const baddress = require('../src/address') |
|
|
const baddress = require('../src/address') |
|
|
const bcrypto = require('../src/crypto') |
|
|
|
|
|
const bscript = require('../src/script') |
|
|
const bscript = require('../src/script') |
|
|
|
|
|
const payments = require('../src/payments') |
|
|
|
|
|
|
|
|
const ECPair = require('../src/ecpair') |
|
|
const ECPair = require('../src/ecpair') |
|
|
const Transaction = require('../src/transaction') |
|
|
const Transaction = require('../src/transaction') |
|
@ -11,11 +11,6 @@ const NETWORKS = require('../src/networks') |
|
|
|
|
|
|
|
|
const fixtures = require('./fixtures/transaction_builder') |
|
|
const fixtures = require('./fixtures/transaction_builder') |
|
|
|
|
|
|
|
|
// TODO: remove
|
|
|
|
|
|
function getAddress (node) { |
|
|
|
|
|
return baddress.toBase58Check(bcrypto.hash160(node.publicKey), NETWORKS.bitcoin.pubKeyHash) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function constructSign (f, txb) { |
|
|
function constructSign (f, txb) { |
|
|
const network = NETWORKS[f.network] |
|
|
const network = NETWORKS[f.network] |
|
|
const stages = f.stages && f.stages.concat() |
|
|
const stages = f.stages && f.stages.concat() |
|
@ -236,6 +231,7 @@ describe('TransactionBuilder', function () { |
|
|
|
|
|
|
|
|
it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function () { |
|
|
it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function () { |
|
|
txb.addInput(txHash, 0) |
|
|
txb.addInput(txHash, 0) |
|
|
|
|
|
txb.addOutput(scripts[0], 1000) |
|
|
txb.sign(0, keyPair) |
|
|
txb.sign(0, keyPair) |
|
|
|
|
|
|
|
|
assert.throws(function () { |
|
|
assert.throws(function () { |
|
@ -251,7 +247,7 @@ describe('TransactionBuilder', function () { |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it('accepts an address string and value', function () { |
|
|
it('accepts an address string and value', function () { |
|
|
const address = getAddress(keyPair) |
|
|
const { address } = payments.p2pkh({ pubkey: keyPair.publicKey }) |
|
|
const vout = txb.addOutput(address, 1000) |
|
|
const vout = txb.addOutput(address, 1000) |
|
|
assert.strictEqual(vout, 0) |
|
|
assert.strictEqual(vout, 0) |
|
|
|
|
|
|
|
@ -318,6 +314,7 @@ describe('TransactionBuilder', function () { |
|
|
it('throws if if there exist any scriptSigs', function () { |
|
|
it('throws if if there exist any scriptSigs', function () { |
|
|
const txb = new TransactionBuilder() |
|
|
const txb = new TransactionBuilder() |
|
|
txb.addInput(txHash, 0) |
|
|
txb.addInput(txHash, 0) |
|
|
|
|
|
txb.addOutput(scripts[0], 100) |
|
|
txb.sign(0, keyPair) |
|
|
txb.sign(0, keyPair) |
|
|
|
|
|
|
|
|
assert.throws(function () { |
|
|
assert.throws(function () { |
|
@ -345,6 +342,7 @@ describe('TransactionBuilder', function () { |
|
|
it('throws ' + f.exception + (f.description ? ' (' + f.description + ')' : ''), function () { |
|
|
it('throws ' + f.exception + (f.description ? ' (' + f.description + ')' : ''), function () { |
|
|
const txb = construct(f, true) |
|
|
const txb = construct(f, true) |
|
|
|
|
|
|
|
|
|
|
|
let threw = false |
|
|
f.inputs.forEach(function (input, index) { |
|
|
f.inputs.forEach(function (input, index) { |
|
|
input.signs.forEach(function (sign) { |
|
|
input.signs.forEach(function (sign) { |
|
|
const keyPairNetwork = NETWORKS[sign.network || f.network] |
|
|
const keyPairNetwork = NETWORKS[sign.network || f.network] |
|
@ -360,15 +358,18 @@ describe('TransactionBuilder', function () { |
|
|
witnessScript = bscript.fromASM(sign.witnessScript) |
|
|
witnessScript = bscript.fromASM(sign.witnessScript) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!sign.throws) { |
|
|
if (sign.throws) { |
|
|
txb.sign(index, keyPair2, redeemScript, sign.hashType, sign.value, witnessScript) |
|
|
|
|
|
} else { |
|
|
|
|
|
assert.throws(function () { |
|
|
assert.throws(function () { |
|
|
txb.sign(index, keyPair2, redeemScript, sign.hashType, sign.value, witnessScript) |
|
|
txb.sign(index, keyPair2, redeemScript, sign.hashType, sign.value, witnessScript) |
|
|
}, new RegExp(f.exception)) |
|
|
}, new RegExp(f.exception)) |
|
|
|
|
|
threw = true |
|
|
|
|
|
} else { |
|
|
|
|
|
txb.sign(index, keyPair2, redeemScript, sign.hashType, sign.value, witnessScript) |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
assert.equal(threw, true) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|