Browse Source

move types to single object, remove default type assignment from proposal

activeAddress
Gregg Zigler 10 years ago
parent
commit
ab33debdd1
  1. 12
      lib/model/txproposal.js
  2. 2
      package.json
  3. 12
      test/models/txproposal.js

12
lib/model/txproposal.js

@ -12,12 +12,10 @@ function TxProposal() {
this.version = '1.0.1'; this.version = '1.0.1';
}; };
TxProposal.TYPE_SIMPLE = 'simple'; TxProposal.Types = { SIMPLE: 'simple', MULTIPLEOUTPUTS: 'multiple_outputs' };
TxProposal.TYPE_MULTIPLEOUTPUTS = 'multiple_outputs';
TxProposal.prototype._isTypeSupported = function() { TxProposal.prototype._isTypeSupported = function() {
var supported = [TxProposal.TYPE_SIMPLE, TxProposal.TYPE_MULTIPLEOUTPUTS]; return !this.type || _.contains(_.values(TxProposal.Types), this.type);
return !this.type || supported.indexOf(this.type) > -1;
}; };
TxProposal._create = {}; TxProposal._create = {};
@ -39,7 +37,7 @@ TxProposal.create = function(opts) {
opts = opts || {}; opts = opts || {};
var x = new TxProposal(); var x = new TxProposal();
x.type = opts.type || TxProposal.TYPE_SIMPLE; x.type = opts.type;
if (!x._isTypeSupported()) { if (!x._isTypeSupported()) {
throw new Error('Unsupported transaction proposal type'); throw new Error('Unsupported transaction proposal type');
}; };
@ -62,7 +60,7 @@ TxProposal.create = function(opts) {
x.fee = null; x.fee = null;
x.feePerKb = opts.feePerKb; x.feePerKb = opts.feePerKb;
TxProposal._create[x.type](x, opts); TxProposal._create[x.type || TxProposal.Types.SIMPLE](x, opts);
return x; return x;
}; };
@ -70,7 +68,7 @@ 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; x.type = obj.type;
if (!x._isTypeSupported()) { if (!x._isTypeSupported()) {
throw new Error('Unsupported transaction proposal type'); throw new Error('Unsupported transaction proposal type');
}; };

2
package.json

@ -20,7 +20,7 @@
"dependencies": { "dependencies": {
"async": "^0.9.0", "async": "^0.9.0",
"bitcore": "^0.12.9", "bitcore": "^0.12.9",
"bitcore-wallet-utils": "0.0.16", "bitcore-wallet-utils": "0.0.17",
"body-parser": "^1.11.0", "body-parser": "^1.11.0",
"coveralls": "^2.11.2", "coveralls": "^2.11.2",
"email-validator": "^1.0.1", "email-validator": "^1.0.1",

12
test/models/txproposal.js

@ -18,7 +18,7 @@ describe('TXProposal', function() {
should.not.exist(txp.outputs); should.not.exist(txp.outputs);
}); });
it('should create a multiple-outputs TXP', function() { it('should create a multiple-outputs TXP', function() {
var txp = TXP.create(aTxpOpts(TXP.TYPE_MULTIPLEOUTPUTS)); var txp = TXP.create(aTxpOpts(TXP.Types.MULTIPLEOUTPUTS));
should.exist(txp); should.exist(txp);
should.not.exist(txp.toAddress); should.not.exist(txp.toAddress);
should.exist(txp.outputs); should.exist(txp.outputs);
@ -41,9 +41,9 @@ describe('TXProposal', function() {
txp.toAddress.should.equal(aTXP().toAddress); txp.toAddress.should.equal(aTXP().toAddress);
}); });
it('should copy a multiple-outputs TXP', function() { it('should copy a multiple-outputs TXP', function() {
var txp = TXP.fromObj(aTXP(TXP.TYPE_MULTIPLEOUTPUTS)); var txp = TXP.fromObj(aTXP(TXP.Types.MULTIPLEOUTPUTS));
should.exist(txp); should.exist(txp);
txp.outputs.should.deep.equal(aTXP(TXP.TYPE_MULTIPLEOUTPUTS).outputs); txp.outputs.should.deep.equal(aTXP(TXP.Types.MULTIPLEOUTPUTS).outputs);
}); });
it('should fail to copy a TXP of unknown type', function() { it('should fail to copy a TXP of unknown type', function() {
var txp; var txp;
@ -74,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(TXP.TYPE_MULTIPLEOUTPUTS)); var txp = TXP.fromObj(aTXP(TXP.Types.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]);
@ -139,7 +139,7 @@ var aTxpOpts = function(type) {
amount: 50000000, amount: 50000000,
message: 'some message' message: 'some message'
}; };
if (type == TXP.TYPE_MULTIPLEOUTPUTS) { if (type == TXP.Types.MULTIPLEOUTPUTS) {
opts.outputs = [ opts.outputs = [
{ {
toAddress: "18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7", toAddress: "18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7",
@ -197,7 +197,7 @@ var aTXP = function(type) {
"actions": [], "actions": [],
"outputOrder": [0, 1], "outputOrder": [0, 1],
}; };
if (type == TXP.TYPE_MULTIPLEOUTPUTS) { if (type == TXP.Types.MULTIPLEOUTPUTS) {
txp.outputs = [ txp.outputs = [
{ {
toAddress: "18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7", toAddress: "18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7",

Loading…
Cancel
Save