move types to single object, remove default type assignment from proposal
This commit is contained in:
parent
07a381bdb3
commit
ab33debdd1
|
@ -12,12 +12,10 @@ function TxProposal() {
|
|||
this.version = '1.0.1';
|
||||
};
|
||||
|
||||
TxProposal.TYPE_SIMPLE = 'simple';
|
||||
TxProposal.TYPE_MULTIPLEOUTPUTS = 'multiple_outputs';
|
||||
TxProposal.Types = { SIMPLE: 'simple', MULTIPLEOUTPUTS: 'multiple_outputs' };
|
||||
|
||||
TxProposal.prototype._isTypeSupported = function() {
|
||||
var supported = [TxProposal.TYPE_SIMPLE, TxProposal.TYPE_MULTIPLEOUTPUTS];
|
||||
return !this.type || supported.indexOf(this.type) > -1;
|
||||
return !this.type || _.contains(_.values(TxProposal.Types), this.type);
|
||||
};
|
||||
|
||||
TxProposal._create = {};
|
||||
|
@ -39,7 +37,7 @@ TxProposal.create = function(opts) {
|
|||
opts = opts || {};
|
||||
|
||||
var x = new TxProposal();
|
||||
x.type = opts.type || TxProposal.TYPE_SIMPLE;
|
||||
x.type = opts.type;
|
||||
if (!x._isTypeSupported()) {
|
||||
throw new Error('Unsupported transaction proposal type');
|
||||
};
|
||||
|
@ -62,7 +60,7 @@ TxProposal.create = function(opts) {
|
|||
x.fee = null;
|
||||
x.feePerKb = opts.feePerKb;
|
||||
|
||||
TxProposal._create[x.type](x, opts);
|
||||
TxProposal._create[x.type || TxProposal.Types.SIMPLE](x, opts);
|
||||
|
||||
return x;
|
||||
};
|
||||
|
@ -70,7 +68,7 @@ TxProposal.create = function(opts) {
|
|||
TxProposal.fromObj = function(obj) {
|
||||
var x = new TxProposal();
|
||||
|
||||
x.type = obj.type || TxProposal.TYPE_SIMPLE;
|
||||
x.type = obj.type;
|
||||
if (!x._isTypeSupported()) {
|
||||
throw new Error('Unsupported transaction proposal type');
|
||||
};
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
"dependencies": {
|
||||
"async": "^0.9.0",
|
||||
"bitcore": "^0.12.9",
|
||||
"bitcore-wallet-utils": "0.0.16",
|
||||
"bitcore-wallet-utils": "0.0.17",
|
||||
"body-parser": "^1.11.0",
|
||||
"coveralls": "^2.11.2",
|
||||
"email-validator": "^1.0.1",
|
||||
|
|
|
@ -18,7 +18,7 @@ describe('TXProposal', function() {
|
|||
should.not.exist(txp.outputs);
|
||||
});
|
||||
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.not.exist(txp.toAddress);
|
||||
should.exist(txp.outputs);
|
||||
|
@ -41,9 +41,9 @@ describe('TXProposal', function() {
|
|||
txp.toAddress.should.equal(aTXP().toAddress);
|
||||
});
|
||||
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);
|
||||
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() {
|
||||
var txp;
|
||||
|
@ -74,7 +74,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(TXP.TYPE_MULTIPLEOUTPUTS));
|
||||
var txp = TXP.fromObj(aTXP(TXP.Types.MULTIPLEOUTPUTS));
|
||||
txp.outputOrder = [0, 1, 2];
|
||||
var t = txp.getBitcoreTx();
|
||||
t.getChangeOutput().should.deep.equal(t.outputs[2]);
|
||||
|
@ -139,7 +139,7 @@ var aTxpOpts = function(type) {
|
|||
amount: 50000000,
|
||||
message: 'some message'
|
||||
};
|
||||
if (type == TXP.TYPE_MULTIPLEOUTPUTS) {
|
||||
if (type == TXP.Types.MULTIPLEOUTPUTS) {
|
||||
opts.outputs = [
|
||||
{
|
||||
toAddress: "18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7",
|
||||
|
@ -197,7 +197,7 @@ var aTXP = function(type) {
|
|||
"actions": [],
|
||||
"outputOrder": [0, 1],
|
||||
};
|
||||
if (type == TXP.TYPE_MULTIPLEOUTPUTS) {
|
||||
if (type == TXP.Types.MULTIPLEOUTPUTS) {
|
||||
txp.outputs = [
|
||||
{
|
||||
toAddress: "18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7",
|
||||
|
|
Loading…
Reference in New Issue