From a9bc42019a352bf10c3711fe41c01cd54d3d16bb Mon Sep 17 00:00:00 2001 From: William Cotton Date: Fri, 25 Apr 2014 12:39:30 -0700 Subject: [PATCH 1/4] fixed issues with testnet and createTx --- src/wallet.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/wallet.js b/src/wallet.js index 563f931..5cad501 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -2,6 +2,7 @@ var convert = require('./convert') var Transaction = require('./transaction').Transaction var HDNode = require('./hdwallet.js') var rng = require('secure-random') +var Network = require('./network') function Wallet(seed, options) { if (!(this instanceof Wallet)) { return new Wallet(seed, options); } @@ -173,7 +174,7 @@ function Wallet(seed, options) { checkDust(value) var tx = new Transaction() - tx.addOutput(to, value) + tx.addOutput(to, value, Network[network]) var utxo = getCandidateOutputs(value) var totalInValue = 0 @@ -189,7 +190,7 @@ function Wallet(seed, options) { var change = totalInValue - value - fee if(change > 0 && !isDust(change)) { - tx.addOutput(getChangeAddress(), change) + tx.addOutput(getChangeAddress(), change, Network[network]) } break } @@ -245,7 +246,7 @@ function Wallet(seed, options) { function estimateFeePadChangeOutput(tx){ var tmpTx = tx.clone() - tmpTx.addOutput(getChangeAddress(), 0) + tmpTx.addOutput(getChangeAddress(), 0, Network[network]) return tmpTx.estimateFee() } @@ -265,7 +266,7 @@ function Wallet(seed, options) { tx.ins.forEach(function(inp,i) { var output = me.outputs[inp.outpoint.hash + ':' + inp.outpoint.index] if (output) { - tx.sign(i, me.getPrivateKeyForAddress(output.address)) + tx.sign(i, me.getPrivateKeyForAddress(output.address), false, Network[network]) } }) return tx From 590bb8e1c3487ce57643a268655f0f57d915ab1f Mon Sep 17 00:00:00 2001 From: William Cotton Date: Fri, 25 Apr 2014 12:49:51 -0700 Subject: [PATCH 2/4] custom changeAddress for wallet.createTx() --- src/wallet.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wallet.js b/src/wallet.js index 5cad501..a4aa0a4 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -170,7 +170,7 @@ function Wallet(seed, options) { }) } - this.createTx = function(to, value, fixedFee) { + this.createTx = function(to, value, fixedFee, changeAddress) { checkDust(value) var tx = new Transaction() @@ -190,7 +190,7 @@ function Wallet(seed, options) { var change = totalInValue - value - fee if(change > 0 && !isDust(change)) { - tx.addOutput(getChangeAddress(), change, Network[network]) + tx.addOutput(changeAddress || getChangeAddress(), change, Network[network]) } break } From dfbfdafc94962236428a46d486f081f63a1f59a5 Mon Sep 17 00:00:00 2001 From: William Cotton Date: Fri, 25 Apr 2014 13:43:26 -0700 Subject: [PATCH 3/4] added tests for createTx testnet and createTx custom changeAddress --- test/wallet.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/wallet.js b/test/wallet.js index 565e3c8..f5a3be7 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -459,6 +459,25 @@ describe('Wallet', function() { }) }) + describe('testnet', function(){ + it('should create transaction', function(){ + var to = 'mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue' + var wallet = new Wallet(seed, {network: 'testnet'}) + var tx = wallet.createTx(to, value) + assert.equal(tx.outs.length, 1) + }) + }) + + describe('changeAddress', function(){ + it('should allow custom changeAddress', function(){ + var to = "mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue" + var changeAddress = 'mfrFjnKZUvTcvdAK2fUX5D8v1Epu5H8JCk' + var wallet = new Wallet(seed, {network: 'testnet'}) + var tx = wallet.createTx(to, value, false, changeAddress) + assert.equal(tx.outs.length, 1) + }) + }) + describe('transaction outputs', function(){ it('includes the specified address and amount', function(){ var tx = wallet.createTx(to, value) From 8f698d19f75e205ebb4045cc1be937b5dffd7f35 Mon Sep 17 00:00:00 2001 From: William Cotton Date: Fri, 25 Apr 2014 16:30:36 -0700 Subject: [PATCH 4/4] custom changeAddress has full integration test --- test/wallet.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/test/wallet.js b/test/wallet.js index f5a3be7..64b700d 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -470,11 +470,19 @@ describe('Wallet', function() { describe('changeAddress', function(){ it('should allow custom changeAddress', function(){ + var wallet = new Wallet(seed, {network: 'testnet'}) + var address = wallet.generateAddress() + utxo = { + "hash":"b3c5fde139dc0a3bba2729bfd5b9e16f5894131dc3dc46a91151da3053e7e3a5", + "outputIndex": 0, + "address" : address, + "value": 100000 + } var to = "mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue" var changeAddress = 'mfrFjnKZUvTcvdAK2fUX5D8v1Epu5H8JCk' - var wallet = new Wallet(seed, {network: 'testnet'}) - var tx = wallet.createTx(to, value, false, changeAddress) - assert.equal(tx.outs.length, 1) + wallet.setUnspentOutputs([utxo]) + var tx = wallet.createTx(to, 10000, 1000, changeAddress) + assert.equal(tx.outs.length, 2) }) })