From 453a7187b5526549287b50c0ae0232b772b8b4f9 Mon Sep 17 00:00:00 2001 From: Gregg Zigler Date: Wed, 17 Jun 2015 13:33:43 -0700 Subject: [PATCH] separate common properties from those that vary by proposal type --- lib/model/txproposal.js | 37 ++++++++++++++++++++++--------------- test/models/txproposal.js | 5 +++-- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/lib/model/txproposal.js b/lib/model/txproposal.js index 508fddf..8f4378d 100644 --- a/lib/model/txproposal.js +++ b/lib/model/txproposal.js @@ -12,6 +12,22 @@ function TxProposal() { this.version = '1.0.1'; }; +TxProposal.TYPE_SIMPLE = 'simple'; +TxProposal.TYPE_MULTIPLEOUTPUTS = 'multiple-outputs'; + +TxProposal.prototype._createSimple = function(opts) { + this.toAddress = opts.toAddress; + this.amount = opts.amount; + this.outputOrder = _.shuffle(_.range(2)); + this.network = Bitcore.Address(this.toAddress).toObject().network; +}; + +TxProposal.prototype._createMultipleOutputs = function(opts) { + this.outputs = opts.outputs; + this.outputOrder = _.shuffle(_.range(this.outputs.length + 1)); + this.network = Bitcore.Address(this.outputs[0].toAddress).toObject().network; +}; + TxProposal.create = function(opts) { opts = opts || {}; @@ -23,17 +39,6 @@ TxProposal.create = function(opts) { x.id = _.padLeft(now, 14, '0') + Uuid.v4(); x.walletId = opts.walletId; x.creatorId = opts.creatorId; - if (x.type == 'multiple-outputs' && opts.outputs) { - x.outputs = opts.outputs; - } else if (x.type == 'multiple-outputs') { - x.outputs = [{ - toAddress: opts.toAddress, - amount: opts.amount - }]; - } else { - x.toAddress = opts.toAddress; - x.amount = opts.amount; - } x.message = opts.message; x.payProUrl = opts.payProUrl; x.proposalSignature = opts.proposalSignature; @@ -44,13 +49,15 @@ TxProposal.create = function(opts) { x.requiredRejections = opts.requiredRejections; x.status = 'pending'; x.actions = []; - var outputCount = x.outputs ? x.outputs.length + 1 : 2; - x.outputOrder = _.shuffle(_.range(outputCount)); x.fee = null; - var toAddress = x.outputs ? x.outputs[0].toAddress : x.toAddress; - x.network = Bitcore.Address(toAddress).toObject().network; x.feePerKb = opts.feePerKb; + if (x.type == TxProposal.TYPE_MULTIPLEOUTPUTS) { + x._createMultipleOutputs(opts); + } else { + x._createSimple(opts); + } + return x; }; diff --git a/test/models/txproposal.js b/test/models/txproposal.js index a152217..6180486 100644 --- a/test/models/txproposal.js +++ b/test/models/txproposal.js @@ -6,6 +6,7 @@ var sinon = require('sinon'); var should = chai.should(); var TXP = require('../../lib/model/txproposal'); var Bitcore = require('bitcore-wallet-utils').Bitcore; +var multiple_outputs = TXP.TYPE_MULTIPLEOUTPUTS; describe('TXProposal', function() { @@ -16,7 +17,7 @@ describe('TXProposal', function() { should.exist(txp); }); it('should create a multiple-outputs TXP', function() { - var txp = TXP.fromObj(aTXP('multiple-outputs')); + var txp = TXP.fromObj(aTXP(multiple_outputs)); should.exist(txp); }); }); @@ -39,7 +40,7 @@ describe('TXProposal', function() { t.getChangeOutput().should.deep.equal(t.outputs[0]); }); it('should create a bitcore TX with multiple outputs', function() { - var txp = TXP.fromObj(aTXP('multiple-outputs')); + var txp = TXP.fromObj(aTXP(multiple_outputs)); txp.outputOrder = [0, 1, 2]; var t = txp.getBitcoreTx(); t.getChangeOutput().should.deep.equal(t.outputs[2]);