Merge pull request #338 from isocolsky/fix/model_version

Refactor version handling in models
This commit is contained in:
Matias Alejo Garcia 2015-08-27 10:58:09 -03:00
commit a6362524f5
10 changed files with 34 additions and 40 deletions

View File

@ -2,15 +2,14 @@
var Bitcore = require('bitcore-wallet-utils').Bitcore;
function Address() {
this.version = '1.0.0';
};
function Address() {};
Address.create = function(opts) {
opts = opts || {};
var x = new Address();
x.version = '1.0.0';
x.createdOn = Math.floor(Date.now() / 1000);
x.address = opts.address;
x.walletId = opts.walletId;
@ -24,6 +23,7 @@ Address.create = function(opts) {
Address.fromObj = function(obj) {
var x = new Address();
x.version = obj.version;
x.createdOn = obj.createdOn;
x.address = obj.address;
x.walletId = obj.walletId;

View File

@ -2,15 +2,14 @@ var _ = require('lodash');
var SHARED_INDEX = 0x80000000 - 1;
function AddressManager() {
this.version = '1.0.0';
};
function AddressManager() {};
AddressManager.create = function(opts) {
opts = opts || {};
var x = new AddressManager();
x.version = '1.0.0';
x.receiveAddressIndex = 0;
x.changeAddressIndex = 0;
x.copayerIndex = (opts && _.isNumber(opts.copayerIndex)) ? opts.copayerIndex : SHARED_INDEX;
@ -22,6 +21,7 @@ AddressManager.create = function(opts) {
AddressManager.fromObj = function(obj) {
var x = new AddressManager();
x.version = obj.version;
x.receiveAddressIndex = obj.receiveAddressIndex;
x.changeAddressIndex = obj.changeAddressIndex;
x.copayerIndex = obj.copayerIndex;

View File

@ -12,13 +12,7 @@ var WalletUtils = require('bitcore-wallet-utils');
var Bitcore = WalletUtils.Bitcore;
var HDPublicKey = Bitcore.HDPublicKey;
function Copayer() {
this.version = '2';
};
Copayer.getVersion = function() {
return parseInt(this.version);
};
function Copayer() {};
Copayer.create = function(opts) {
opts = opts || {};
@ -29,16 +23,14 @@ Copayer.create = function(opts) {
opts.copayerIndex = opts.copayerIndex || 0;
var x = new Copayer();
x.version = 2;
x.createdOn = Math.floor(Date.now() / 1000);
x.xPubKey = opts.xPubKey;
x.id = WalletUtils.xPubToCopayerId(x.xPubKey);
x.name = opts.name;
x.requestPubKey = opts.requestPubKey;
x.signature = opts.signature;
x.requestPubKeys = [{
key: opts.requestPubKey,
signature: opts.signature,
@ -56,15 +48,15 @@ Copayer.create = function(opts) {
Copayer.fromObj = function(obj) {
var x = new Copayer();
x.version = obj.version;
x.createdOn = obj.createdOn;
x.id = obj.id;
x.name = obj.name;
x.xPubKey = obj.xPubKey;
x.requestPubKey = obj.requestPubKey;
x.signature = obj.signature;
if (this.getVersion() == 1) {
if (parseInt(x.version) == 1) {
x.requestPubKeys = [{
key: x.requestPubKey,
signature: x.signature,

View File

@ -3,15 +3,14 @@
var _ = require('lodash');
var Uuid = require('uuid');
function Email() {
this.version = '1.0.1';
};
function Email() {};
Email.create = function(opts) {
opts = opts || {};
var x = new Email();
x.version = 2;
var now = Date.now();
x.createdOn = Math.floor(now / 1000);
x.id = _.padLeft(now, 14, '0') + Uuid.v4();
@ -33,6 +32,7 @@ Email.create = function(opts) {
Email.fromObj = function(obj) {
var x = new Email();
x.version = obj.version;
x.createdOn = obj.createdOn;
x.id = obj.id;
x.walletId = obj.walletId;
@ -40,8 +40,9 @@ Email.fromObj = function(obj) {
x.from = obj.from;
x.to = obj.to;
x.subject = obj.subject;
if (obj.version == '1.0.0') {
if (parseInt(x.version) == 1) {
x.bodyPlain = obj.body;
x.version = 2;
} else {
x.bodyPlain = obj.bodyPlain;
}

View File

@ -23,15 +23,14 @@ var Uuid = require('uuid');
* to notify the user
*
*/
function Notification() {
this.version = '1.0.0';
};
function Notification() {};
Notification.create = function(opts) {
opts = opts || {};
var x = new Notification();
x.version = '1.0.0';
var now = Date.now();
x.createdOn = Math.floor(now / 1000);
x.id = _.padLeft(now, 14, '0') + _.padLeft(opts.ticker || 0, 4, '0');
@ -46,6 +45,7 @@ Notification.create = function(opts) {
Notification.fromObj = function(obj) {
var x = new Notification();
x.version = obj.version;
x.createdOn = obj.createdOn;
x.id = obj.id;
x.type = obj.type,

View File

@ -1,14 +1,13 @@
'use strict';
function Preferences() {
this.version = '1.0.0';
};
function Preferences() {};
Preferences.create = function(opts) {
opts = opts || {};
var x = new Preferences();
x.version = '1.0.0';
x.createdOn = Math.floor(Date.now() / 1000);
x.walletId = opts.walletId;
x.copayerId = opts.copayerId;
@ -21,6 +20,7 @@ Preferences.create = function(opts) {
Preferences.fromObj = function(obj) {
var x = new Preferences();
x.version = obj.version;
x.createdOn = obj.createdOn;
x.walletId = obj.walletId;
x.copayerId = obj.copayerId;

View File

@ -8,9 +8,7 @@ var Address = Bitcore.Address;
var TxProposalAction = require('./txproposalaction');
function TxProposal() {
this.version = '2.0.0';
};
function TxProposal() {};
TxProposal.Types = {
SIMPLE: 'simple',
@ -49,6 +47,7 @@ TxProposal.create = function(opts) {
var x = new TxProposal();
x.version = '2.0.0';
x.type = opts.type || TxProposal.Types.SIMPLE;
var now = Date.now();
@ -83,12 +82,12 @@ TxProposal.create = function(opts) {
TxProposal.fromObj = function(obj) {
var x = new TxProposal();
if (obj.version == '1.0.0') {
x.version = obj.version;
if (obj.version === '1.0.0') {
x.type = TxProposal.Types.SIMPLE;
} else {
x.type = obj.type;
}
x.version = obj.version;
x.createdOn = obj.createdOn;
x.id = obj.id;
x.walletId = obj.walletId;

View File

@ -1,14 +1,13 @@
'use strict';
function TxProposalAction() {
this.version = '1.0.0';
};
function TxProposalAction() {};
TxProposalAction.create = function(opts) {
opts = opts || {};
var x = new TxProposalAction();
x.version = '1.0.0';
x.createdOn = Math.floor(Date.now() / 1000);
x.copayerId = opts.copayerId;
x.type = opts.type;
@ -22,6 +21,7 @@ TxProposalAction.create = function(opts) {
TxProposalAction.fromObj = function(obj) {
var x = new TxProposalAction();
x.version = obj.version;
x.createdOn = obj.createdOn;
x.copayerId = obj.copayerId;
x.type = obj.type;

View File

@ -10,15 +10,14 @@ var Copayer = require('./copayer');
var AddressManager = require('./addressmanager');
var WalletUtils = require('bitcore-wallet-utils');
function Wallet() {
this.version = '1.0.0';
};
function Wallet() {};
Wallet.create = function(opts) {
opts = opts || {};
var x = new Wallet();
x.version = '1.0.0';
x.createdOn = Math.floor(Date.now() / 1000);
x.id = opts.id || Uuid.v4();
x.name = opts.name;
@ -39,6 +38,7 @@ Wallet.create = function(opts) {
Wallet.fromObj = function(obj) {
var x = new Wallet();
x.version = obj.version;
x.createdOn = obj.createdOn;
x.id = obj.id;
x.name = obj.name;

View File

@ -20,6 +20,8 @@ describe('Copayer', function() {
it('create an address', function() {
var w = Wallet.fromObj(testWallet);
var c = Copayer.fromObj(testWallet.copayers[2]);
should.exist(c.requestPubKeys);
c.requestPubKeys.length.should.equal(1);
var a1 = c.createAddress(w, true);
a1.address.should.equal('3AXmDe2FkWY9g5LpRaTs1U7pXKtkNm3NBf');
a1.path.should.equal('m/2/1/0');