diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 5806063..ff2041a 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -1,6 +1,7 @@ var assert = require('assert') var bcrypto = require('./crypto') var bufferutils = require('./bufferutils') +var networks = require('./networks') var ops = require('./opcodes') var scripts = require('./scripts') @@ -83,10 +84,11 @@ function extractInput (txIn) { } } -function TransactionBuilder () { +function TransactionBuilder (network) { this.prevTxMap = {} this.prevOutScripts = {} this.prevOutTypes = {} + this.network = network || networks.bitcoin this.inputs = [] this.tx = new Transaction() @@ -188,7 +190,7 @@ TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) { // Attempt to get a valid address if it's a base58 address string if (typeof scriptPubKey === 'string') { - scriptPubKey = Address.toOutputScript(scriptPubKey) + scriptPubKey = Address.toOutputScript(scriptPubKey, this.network) } return this.tx.addOutput(scriptPubKey, value) diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 09dcb9a..ba1ea6c 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -142,6 +142,12 @@ describe('TransactionBuilder', function () { assert.strictEqual(txout.value, 1000) }) + it('throws if address is of the wrong network', function () { + assert.throws(function () { + txb.addOutput('2NGHjvjw83pcVFgMcA7QvSMh2c246rxLVz9', 1000) + }, /2NGHjvjw83pcVFgMcA7QvSMh2c246rxLVz9 has no matching Script/) + }) + it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function () { txb.addInput(prevTxHash, 0) txb.addOutput(privScript, 2000)