separate common properties from those that vary by proposal type

This commit is contained in:
Gregg Zigler 2015-06-17 13:33:43 -07:00
parent 6f43888176
commit 453a7187b5
2 changed files with 25 additions and 17 deletions

View File

@ -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;
};

View File

@ -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]);