diff --git a/src/transaction.js b/src/transaction.js index f4d78ce..4d5bd91 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -112,7 +112,8 @@ Transaction.prototype.addOutput = function (address, value, network) { this.outs.push(new TransactionOut({ value: value, - script: Script.createOutputScript(address, network) + script: Script.createOutputScript(address, network), + network: network })) } @@ -485,7 +486,10 @@ var TransactionOut = function (data) { : data.address ? Script.createOutputScript(data.address) : new Script() - if (this.script.buffer.length > 0) this.address = this.script.getToAddress(); + var network = data.network || Network.bitcoin + if (this.script.buffer.length > 0) { + this.address = this.script.getToAddress(network) + } this.value = Array.isArray(data.value) ? convert.bytesToNum(data.value) diff --git a/test/transaction.js b/test/transaction.js index 616135c..e672411 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -173,6 +173,15 @@ describe('Transaction', function() { verifyTransactionOut() }) + it('supports alternative networks', function(){ + var addr = 'mkHJaNR7uuwRG1JrmTZsV4MszaTKjCBvCR' + + tx.addOutput(addr, 40000, network.testnet) + verifyTransactionOut() + + assert.equal(tx.outs[0].address.toString(), addr) + }) + function verifyTransactionOut(){ assert.equal(tx.outs.length, 1) diff --git a/test/wallet.js b/test/wallet.js index 9426b07..f83c160 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -467,7 +467,10 @@ describe('Wallet', function() { var to = 'mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue' var wallet = new Wallet(seed, {network: 'testnet'}) var tx = wallet.createTx(to, value) + assert.equal(tx.outs.length, 1) + assert.equal(tx.outs[0].address.toString(), to) + assert.equal(tx.outs[0].value, value) }) })