Browse Source

handle unknown proposal types

activeAddress
Gregg Zigler 10 years ago
parent
commit
10644f7edc
  1. 44
      lib/model/txproposal.js
  2. 74
      test/models/txproposal.js

44
lib/model/txproposal.js

@ -13,26 +13,36 @@ function TxProposal() {
}; };
TxProposal.TYPE_SIMPLE = 'simple'; TxProposal.TYPE_SIMPLE = 'simple';
TxProposal.TYPE_MULTIPLEOUTPUTS = 'multiple-outputs'; TxProposal.TYPE_MULTIPLEOUTPUTS = 'multiple_outputs';
TxProposal.prototype._createSimple = function(opts) { TxProposal.prototype._isTypeSupported = function() {
this.toAddress = opts.toAddress; var supported = [TxProposal.TYPE_SIMPLE, TxProposal.TYPE_MULTIPLEOUTPUTS];
this.amount = opts.amount; return !this.type || supported.indexOf(this.type) > -1;
this.outputOrder = _.shuffle(_.range(2));
this.network = Bitcore.Address(this.toAddress).toObject().network;
}; };
TxProposal.prototype._createMultipleOutputs = function(opts) { TxProposal._create = {};
this.outputs = opts.outputs;
this.outputOrder = _.shuffle(_.range(this.outputs.length + 1)); TxProposal._create.simple = function(txp, opts) {
this.network = Bitcore.Address(this.outputs[0].toAddress).toObject().network; txp.toAddress = opts.toAddress;
txp.amount = opts.amount;
txp.outputOrder = _.shuffle(_.range(2));
txp.network = Bitcore.Address(txp.toAddress).toObject().network;
};
TxProposal._create.multiple_outputs = function(txp, opts) {
txp.outputs = opts.outputs;
txp.outputOrder = _.shuffle(_.range(txp.outputs.length + 1));
txp.network = Bitcore.Address(txp.outputs[0].toAddress).toObject().network;
}; };
TxProposal.create = function(opts) { TxProposal.create = function(opts) {
opts = opts || {}; opts = opts || {};
var x = new TxProposal(); var x = new TxProposal();
x.type = opts.type; x.type = opts.type || TxProposal.TYPE_SIMPLE;
if (!x._isTypeSupported()) {
throw new Error('Unsupported transaction proposal type');
};
var now = Date.now(); var now = Date.now();
x.createdOn = Math.floor(now / 1000); x.createdOn = Math.floor(now / 1000);
@ -52,11 +62,7 @@ TxProposal.create = function(opts) {
x.fee = null; x.fee = null;
x.feePerKb = opts.feePerKb; x.feePerKb = opts.feePerKb;
if (x.type == TxProposal.TYPE_MULTIPLEOUTPUTS) { TxProposal._create[x.type](x, opts);
x._createMultipleOutputs(opts);
} else {
x._createSimple(opts);
}
return x; return x;
}; };
@ -64,8 +70,12 @@ TxProposal.create = function(opts) {
TxProposal.fromObj = function(obj) { TxProposal.fromObj = function(obj) {
var x = new TxProposal(); var x = new TxProposal();
x.type = obj.type || TxProposal.TYPE_SIMPLE;
if (!x._isTypeSupported()) {
throw new Error('Unsupported transaction proposal type');
};
x.version = obj.version; x.version = obj.version;
x.type = obj.type;
x.createdOn = obj.createdOn; x.createdOn = obj.createdOn;
x.id = obj.id; x.id = obj.id;
x.walletId = obj.walletId; x.walletId = obj.walletId;

74
test/models/txproposal.js

@ -6,19 +6,53 @@ var sinon = require('sinon');
var should = chai.should(); var should = chai.should();
var TXP = require('../../lib/model/txproposal'); var TXP = require('../../lib/model/txproposal');
var Bitcore = require('bitcore-wallet-utils').Bitcore; var Bitcore = require('bitcore-wallet-utils').Bitcore;
var multiple_outputs = TXP.TYPE_MULTIPLEOUTPUTS;
describe('TXProposal', function() { describe('TXProposal', function() {
describe('#fromObj', function() { describe('#create', function() {
it('should create a TXP', function() { it('should create a TXP', function() {
var txp = TXP.fromObj(aTXP()); var txp = TXP.create(aTxpOpts());
should.exist(txp); should.exist(txp);
should.exist(txp.toAddress);
should.not.exist(txp.outputs);
}); });
it('should create a multiple-outputs TXP', function() { it('should create a multiple-outputs TXP', function() {
var txp = TXP.fromObj(aTXP(multiple_outputs)); var txp = TXP.create(aTxpOpts(TXP.TYPE_MULTIPLEOUTPUTS));
should.exist(txp);
should.not.exist(txp.toAddress);
should.exist(txp.outputs);
});
it('should fail to create a TXP of unknown type', function() {
var txp;
try {
txp = TXP.create(aTxpOpts('bogus'));
} catch(e) {
should.exist(e);
}
should.not.exist(txp);
});
});
describe('#fromObj', function() {
it('should copy a TXP', function() {
var txp = TXP.fromObj(aTXP());
should.exist(txp);
txp.toAddress.should.equal(aTXP().toAddress);
});
it('should copy a multiple-outputs TXP', function() {
var txp = TXP.fromObj(aTXP(TXP.TYPE_MULTIPLEOUTPUTS));
should.exist(txp); should.exist(txp);
txp.outputs.should.deep.equal(aTXP(TXP.TYPE_MULTIPLEOUTPUTS).outputs);
});
it('should fail to copy a TXP of unknown type', function() {
var txp;
try {
txp = TXP.fromObj(aTxpOpts('bogus'));
} catch(e) {
should.exist(e);
}
should.not.exist(txp);
}); });
}); });
@ -40,7 +74,7 @@ describe('TXProposal', function() {
t.getChangeOutput().should.deep.equal(t.outputs[0]); t.getChangeOutput().should.deep.equal(t.outputs[0]);
}); });
it('should create a bitcore TX with multiple outputs', function() { it('should create a bitcore TX with multiple outputs', function() {
var txp = TXP.fromObj(aTXP(multiple_outputs)); var txp = TXP.fromObj(aTXP(TXP.TYPE_MULTIPLEOUTPUTS));
txp.outputOrder = [0, 1, 2]; txp.outputOrder = [0, 1, 2];
var t = txp.getBitcoreTx(); var t = txp.getBitcoreTx();
t.getChangeOutput().should.deep.equal(t.outputs[2]); t.getChangeOutput().should.deep.equal(t.outputs[2]);
@ -98,9 +132,36 @@ var theXPriv = 'xprv9s21ZrQH143K2rMHbXTJmWTuFx6ssqn1vyRoZqPkCXYchBSkp5ey8kMJe84s
var theXPub = 'xpub661MyMwAqRbcFLRkhYzK8eQdoywNHJVsJCMQNDoMks5bZymuMcyDgYfnVQYq2Q9npnVmdTAthYGc3N3uxm5sEdnTpSqBc4YYTAhNnoSxCm9'; var theXPub = 'xpub661MyMwAqRbcFLRkhYzK8eQdoywNHJVsJCMQNDoMks5bZymuMcyDgYfnVQYq2Q9npnVmdTAthYGc3N3uxm5sEdnTpSqBc4YYTAhNnoSxCm9';
var theSignatures = ['3045022100896aeb8db75fec22fddb5facf791927a996eb3aee23ee6deaa15471ea46047de02204c0c33f42a9d3ff93d62738712a8c8a5ecd21b45393fdd144e7b01b5a186f1f9']; var theSignatures = ['3045022100896aeb8db75fec22fddb5facf791927a996eb3aee23ee6deaa15471ea46047de02204c0c33f42a9d3ff93d62738712a8c8a5ecd21b45393fdd144e7b01b5a186f1f9'];
var aTxpOpts = function(type) {
var opts = {
type: type,
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: 50000000,
message: 'some message'
};
if (type == TXP.TYPE_MULTIPLEOUTPUTS) {
opts.outputs = [
{
toAddress: "18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7",
amount: 10000000,
message: "first message"
},
{
toAddress: "18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7",
amount: 20000000,
message: "second message"
},
];
delete opts.toAddress;
delete opts.amount;
}
return opts;
};
var aTXP = function(type) { var aTXP = function(type) {
var txp = { var txp = {
"version": "1.0.0", "version": "1.0.0",
"type": type,
"createdOn": 1423146231, "createdOn": 1423146231,
"id": "75c34f49-1ed6-255f-e9fd-0c71ae75ed1e", "id": "75c34f49-1ed6-255f-e9fd-0c71ae75ed1e",
"walletId": "1", "walletId": "1",
@ -136,8 +197,7 @@ var aTXP = function(type) {
"actions": [], "actions": [],
"outputOrder": [0, 1], "outputOrder": [0, 1],
}; };
if (type == 'multiple-outputs') { if (type == TXP.TYPE_MULTIPLEOUTPUTS) {
txp.type = type;
txp.outputs = [ txp.outputs = [
{ {
toAddress: "18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7", toAddress: "18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7",

Loading…
Cancel
Save