From 152ab7aa4460b63d070d84c22a58641ad516d9cd Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 8 Sep 2014 15:50:20 -0300 Subject: [PATCH] Refactored Wallet#createTx --- js/controllers/send.js | 2 +- js/models/core/Wallet.js | 35 ++++++++++------------------------- test/test.PayPro.js | 12 ++++++------ test/test.Wallet.js | 24 ++++++++++++------------ 4 files changed, 29 insertions(+), 44 deletions(-) diff --git a/js/controllers/send.js b/js/controllers/send.js index a3112a093..552d556c8 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -116,7 +116,7 @@ angular.module('copayApp.controllers').controller('SendController', var w = $rootScope.wallet; - function done(ntxid, merchantData) { + function done(err, ntxid, merchantData) { // If user is granted the privilege of choosing // their own amount, add it to the tx. if (merchantData && +merchantData.total === 0) { diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 6ab8e9def..a8441e8aa 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -878,7 +878,6 @@ Wallet.prototype.sendAllTxProposals = function(recipients) { */ Wallet.prototype.sendTxProposal = function(ntxid, recipients) { preconditions.checkArgument(ntxid); - log.debug('### SENDING txProposal ' + ntxid + ' TO:', recipients || 'All', this.txProposals); this.send(recipients, { type: 'txProposal', @@ -2035,24 +2034,6 @@ Wallet.prototype.removeTxWithSpentInputs = function(cb) { Wallet.prototype.createTx = function(toAddress, amountSatStr, comment, opts, cb) { var self = this; - if (_.isFunction(amountSatStr)) { - var cb = amountSatStr; - var merchant = toAddress; - return this.createPaymentTx({ - uri: merchant - }, cb); - } - - if (_.isFunction(comment)) { - var cb = comment; - var merchant = toAddress; - var comment = amountSatStr; - return this.createPaymentTx({ - uri: merchant, - memo: comment - }, cb); - } - if (_.isFunction(opts)) { cb = opts; opts = {}; @@ -2064,14 +2045,18 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, comment, opts, cb) } this.getUnspent(function(err, safeUnspent) { + if (err) return cb(err); + var ntxid = self.createTxSync(toAddress, amountSatStr, comment, safeUnspent, opts); - if (ntxid) { - self.sendIndexes(); - self.sendTxProposal(ntxid); - self.store(); - self.emit('txProposalsUpdated'); + if (!ntxid) { + return cb(new Error('Error creating TX')); } - return cb(ntxid); + + self.sendIndexes(); + self.sendTxProposal(ntxid); + self.store(); + self.emit('txProposalsUpdated'); + return cb(null, ntxid); }); }; diff --git a/test/test.PayPro.js b/test/test.PayPro.js index 0528864f3..fbbeaf7ef 100644 --- a/test/test.PayPro.js +++ b/test/test.PayPro.js @@ -739,7 +739,7 @@ describe('PayPro (in Wallet) model', function() { uri = address.split(/\s+/)[1]; } - w.createTx(uri, commentText, function(ntxid, merchantData) { + w.createPaymentTx({ uri: uri, memo: commentText }, function(ntxid, merchantData) { if (w.isShared()) { should.exist(ntxid); should.exist(merchantData); @@ -760,7 +760,7 @@ describe('PayPro (in Wallet) model', function() { should.exist(w); var address = 'bitcoin:2NBzZdFBoQymDgfzH2Pmnthser1E71MmU47?amount=0.00003&r=' + server.uri + '/request'; var commentText = 'Hello, server. I\'d like to make a payment.'; - w.createTx(address, commentText, function(ntxid, merchantData) { + w.createPaymentTx({ uri: address, memo: commentText }, function(ntxid, merchantData) { if (w.isShared()) { should.exist(ntxid); should.exist(merchantData); @@ -780,7 +780,7 @@ describe('PayPro (in Wallet) model', function() { should.exist(w); var address = 'bitcoin:2NBzZdFBoQymDgfzH2Pmnthser1E71MmU47?amount=0.00003&r=' + server.uri + '/request'; var commentText = 'Hello, server. I\'d like to make a payment.'; - w.createTx(address, commentText, function(ntxid, merchantData) { + w.createPaymentTx({ uri: address, memo: commentText }, function(ntxid, merchantData) { should.exist(ntxid); should.exist(merchantData); @@ -816,7 +816,7 @@ describe('PayPro (in Wallet) model', function() { should.exist(w); var address = 'bitcoin:2NBzZdFBoQymDgfzH2Pmnthser1E71MmU47?amount=0.00003&r=' + server.uri + '/request'; var commentText = 'Hello, server. I\'d like to make a payment.'; - w.createTx(address, commentText, function(ntxid, merchantData) { + w.createPaymentTx({ uri: address, memo: commentText }, function(ntxid, merchantData) { should.exist(ntxid); should.exist(merchantData); @@ -843,7 +843,7 @@ describe('PayPro (in Wallet) model', function() { should.exist(w); var address = 'bitcoin:2NBzZdFBoQymDgfzH2Pmnthser1E71MmU47?amount=0.00003&r=' + server.uri + '/request'; var commentText = 'Hello, server. I\'d like to make a payment.'; - w.createTx(address, commentText, function(ntxid, merchantData) { + w.createPaymentTx({ uri: address, memo: commentText }, function(ntxid, merchantData) { should.exist(ntxid); should.exist(merchantData); @@ -869,7 +869,7 @@ describe('PayPro (in Wallet) model', function() { should.exist(w); var address = 'bitcoin:2NBzZdFBoQymDgfzH2Pmnthser1E71MmU47?amount=0.00003&r=' + server.uri + '/request'; var commentText = 'Hello, server. I\'d like to make a payment.'; - w.createTx(address, commentText, function(ntxid, merchantData) { + w.createPaymentTx({ uri: address, memo: commentText }, function(ntxid, merchantData) { should.exist(ntxid); should.exist(merchantData); diff --git a/test/test.Wallet.js b/test/test.Wallet.js index 4d80895a9..be685ebf5 100644 --- a/test/test.Wallet.js +++ b/test/test.Wallet.js @@ -676,7 +676,7 @@ describe('Wallet model', function() { var w = cachedCreateW2(); var utxo = createUTXO(w); w.blockchain.fixUnspent(utxo); - w.createTx(toAddress, amountSatStr, null, function(ntxid) { + w.createTx(toAddress, amountSatStr, null, function(err, ntxid) { ntxid.length.should.equal(64); done(); }); @@ -690,7 +690,7 @@ describe('Wallet model', function() { var w = createW2([k2]); var utxo = createUTXO(w); w.blockchain.fixUnspent(utxo); - w.createTx(toAddress, amountSatStr, null, function(ntxid) { + w.createTx(toAddress, amountSatStr, null, function(err, ntxid) { w.on('txProposalsUpdated', function() { w.getTxProposals()[0].signedByUs.should.equal(true); w.getTxProposals()[0].rejectedByUs.should.equal(false); @@ -706,7 +706,7 @@ describe('Wallet model', function() { var w = cachedCreateW2(); var utxo = createUTXO(w); w.blockchain.fixUnspent(utxo); - w.createTx(toAddress, amountSatStr, null, function(ntxid) { + w.createTx(toAddress, amountSatStr, null, function(err, ntxid) { (function() { w.reject(ntxid); }).should.throw('reject a signed'); @@ -718,7 +718,7 @@ describe('Wallet model', function() { var oldK = w.privateKey; var utxo = createUTXO(w); w.blockchain.fixUnspent(utxo); - w.createTx(toAddress, amountSatStr, null, function(ntxid) { + w.createTx(toAddress, amountSatStr, null, function(err, ntxid) { var s = sinon.stub(w, 'getMyCopayerId').returns('213'); Object.keys(w.txProposals.get(ntxid).rejectedBy).length.should.equal(0); w.reject(ntxid); @@ -732,7 +732,7 @@ describe('Wallet model', function() { var w = createW2(null, 1); var utxo = createUTXO(w); w.blockchain.fixUnspent(utxo); - w.createTx(toAddress, amountSatStr, null, function(ntxid) { + w.createTx(toAddress, amountSatStr, null, function(err, ntxid) { w.sendTx(ntxid, function(txid) { txid.length.should.equal(64); done(); @@ -743,7 +743,7 @@ describe('Wallet model', function() { var w = createW2(null, 1); var utxo = createUTXO(w); w.blockchain.fixUnspent(utxo); - w.createTx(toAddress, amountSatStr, null, function(ntxid) { + w.createTx(toAddress, amountSatStr, null, function(err, ntxid) { var txp = w.txProposals.get(ntxid); // Assign fake builder txp.builder = new Builder(); @@ -758,7 +758,7 @@ describe('Wallet model', function() { var w = createW2(null, 1); var utxo = createUTXO(w); w.blockchain.fixUnspent(utxo); - w.createTx(toAddress, amountSatStr, null, function(ntxid) { + w.createTx(toAddress, amountSatStr, null, function(err, ntxid) { sinon.stub(w.blockchain, 'broadcast').yields({statusCode: 303}); var spyCheckSentTx = sinon.spy(w, '_checkSentTx'); w.sendTx(ntxid, function () {}); @@ -770,7 +770,7 @@ describe('Wallet model', function() { var w = cachedCreateW2(); var utxo = createUTXO(w); w.blockchain.fixUnspent(utxo); - w.createTx(toAddress, amountSatStr, null, function(ntxid) { + w.createTx(toAddress, amountSatStr, null, function(err, ntxid) { w.sendTxProposal.bind(w).should.throw('Illegal Argument.'); (function() { w.sendTxProposal(ntxid); @@ -783,7 +783,7 @@ describe('Wallet model', function() { var w = cachedCreateW2(); var utxo = createUTXO(w); w.blockchain.fixUnspent(utxo); - w.createTx(toAddress, amountSatStr, null, function(ntxid) { + w.createTx(toAddress, amountSatStr, null, function(err, ntxid) { w.sendAllTxProposals.bind(w).should.not.throw(); (function() { w.sendAllTxProposals(); @@ -815,7 +815,7 @@ describe('Wallet model', function() { var utxo = createUTXO(w); chai.expect(w.getTxProposals().length).to.equal(0); w.blockchain.fixUnspent(utxo); - w.createTx(toAddress, amountSatStr, null, function(ntxid) { + w.createTx(toAddress, amountSatStr, null, function(err, ntxid) { w.sendTxProposal(ntxid); chai.expect(w.getTxProposals().length).to.equal(1); @@ -840,7 +840,7 @@ describe('Wallet model', function() { utxo[1].vout = 1; chai.expect(w.getTxProposals().length).to.equal(0); w.blockchain.fixUnspent(utxo); - w.createTx(toAddress, '100000', null, function(ntxid) { + w.createTx(toAddress, '100000', null, function(err, ntxid) { w.sendTxProposal(ntxid); chai.expect(w.getTxProposals().length).to.equal(1); @@ -862,7 +862,7 @@ describe('Wallet model', function() { var utxo = createUTXO(w); chai.expect(w.getTxProposals().length).to.equal(0); w.blockchain.fixUnspent(utxo); - w.createTx(toAddress, amountSatStr, null, function(ntxid) { + w.createTx(toAddress, amountSatStr, null, function(err, ntxid) { w.sendTxProposal(ntxid); chai.expect(w.getTxProposals().length).to.equal(1);