Browse Source

Merge pull request #338 from isocolsky/fix/model_version

Refactor version handling in models
activeAddress
Matias Alejo Garcia 10 years ago
parent
commit
a6362524f5
  1. 6
      lib/model/address.js
  2. 6
      lib/model/addressmanager.js
  3. 18
      lib/model/copayer.js
  4. 9
      lib/model/email.js
  5. 6
      lib/model/notification.js
  6. 6
      lib/model/preferences.js
  7. 9
      lib/model/txproposal.js
  8. 6
      lib/model/txproposalaction.js
  9. 6
      lib/model/wallet.js
  10. 2
      test/models/copayer.js

6
lib/model/address.js

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

6
lib/model/addressmanager.js

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

18
lib/model/copayer.js

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

9
lib/model/email.js

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

6
lib/model/notification.js

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

6
lib/model/preferences.js

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

9
lib/model/txproposal.js

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

6
lib/model/txproposalaction.js

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

6
lib/model/wallet.js

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

2
test/models/copayer.js

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

Loading…
Cancel
Save