Browse Source

refactor remaining objects

activeAddress
Ivan Socolsky 10 years ago
parent
commit
52f6529952
  1. 21
      lib/model/address.js
  2. 28
      lib/model/addressmanager.js
  3. 5
      lib/model/copayer.js
  4. 22
      lib/model/notification.js
  5. 42
      lib/model/txproposal.js
  6. 22
      lib/model/txproposalaction.js
  7. 9
      lib/model/wallet.js
  8. 4
      lib/server.js
  9. 6
      test/addressmanager.js

21
lib/model/address.js

@ -2,16 +2,23 @@
var Bitcore = require('bitcore'); var Bitcore = require('bitcore');
function Address(opts) { function Address() {
this.version = '1.0.0';
};
Address.create = function(opts) {
opts = opts || {}; opts = opts || {};
this.createdOn = Math.floor(Date.now() / 1000); var x = new Address();
this.address = opts.address;
this.path = opts.path; x.createdOn = Math.floor(Date.now() / 1000);
this.publicKeys = opts.publicKeys; x.address = opts.address;
x.path = opts.path;
x.publicKeys = opts.publicKeys;
return x;
}; };
Address.fromObj = function (obj) { Address.fromObj = function(obj) {
var x = new Address(); var x = new Address();
x.createdOn = obj.createdOn; x.createdOn = obj.createdOn;
@ -28,7 +35,7 @@ Address.fromObj = function (obj) {
* @param {number} threshold - amount of required signatures to spend the output * @param {number} threshold - amount of required signatures to spend the output
* @return {Script} * @return {Script}
*/ */
Address.prototype.getScriptPubKey = function (threshold) { Address.prototype.getScriptPubKey = function(threshold) {
return Bitcore.Script.buildMultisigOut(this.publicKeys, threshold).toScriptHashOut(); return Bitcore.Script.buildMultisigOut(this.publicKeys, threshold).toScriptHashOut();
}; };

28
lib/model/addressmanager.js

@ -1,14 +1,24 @@
var _ = require('lodash'); var _ = require('lodash');
var HDPath = require('../hdpath'); var HDPath = require('../hdpath');
function AddressManager(opts) { function AddressManager() {
this.receiveAddressIndex = 0; this.version = '1.0.0';
this.changeAddressIndex = 0;
this.copayerIndex = (opts && _.isNumber(opts.copayerIndex)) ? opts.copayerIndex : HDPath.SHARED_INDEX;
}; };
AddressManager.create = function(opts) {
opts = opts || {};
AddressManager.fromObj = function (obj) { var x = new AddressManager();
x.receiveAddressIndex = 0;
x.changeAddressIndex = 0;
x.copayerIndex = (opts && _.isNumber(opts.copayerIndex)) ? opts.copayerIndex : HDPath.SHARED_INDEX;
return x;
};
AddressManager.fromObj = function(obj) {
var x = new AddressManager(); var x = new AddressManager();
x.receiveAddressIndex = obj.receiveAddressIndex; x.receiveAddressIndex = obj.receiveAddressIndex;
@ -18,7 +28,7 @@ AddressManager.fromObj = function (obj) {
return x; return x;
}; };
AddressManager.prototype._incrementIndex = function (isChange) { AddressManager.prototype._incrementIndex = function(isChange) {
if (isChange) { if (isChange) {
this.changeAddressIndex++; this.changeAddressIndex++;
} else { } else {
@ -26,11 +36,11 @@ AddressManager.prototype._incrementIndex = function (isChange) {
} }
}; };
AddressManager.prototype.getCurrentAddressPath = function (isChange) { AddressManager.prototype.getCurrentAddressPath = function(isChange) {
return HDPath.Branch(isChange ? this.changeAddressIndex : this.receiveAddressIndex, isChange, this.copayerIndex); return HDPath.Branch(isChange ? this.changeAddressIndex : this.receiveAddressIndex, isChange, this.copayerIndex);
}; };
AddressManager.prototype.getNewAddressPath = function (isChange) { AddressManager.prototype.getNewAddressPath = function(isChange) {
var ret = this.getCurrentAddressPath(isChange); var ret = this.getCurrentAddressPath(isChange);
this._incrementIndex(isChange); this._incrementIndex(isChange);
return ret; return ret;

5
lib/model/copayer.js

@ -11,11 +11,10 @@ var AddressManager = require('./addressmanager');
var Utils = require('../walletutils'); var Utils = require('../walletutils');
var VERSION = '1.0.0';
var MESSAGE_SIGNING_PATH = "m/1/0"; var MESSAGE_SIGNING_PATH = "m/1/0";
function Copayer() { function Copayer() {
this.version = VERSION; this.version = '1.0.0';
}; };
Copayer.create = function(opts) { Copayer.create = function(opts) {
@ -32,7 +31,7 @@ Copayer.create = function(opts) {
x.name = opts.name; x.name = opts.name;
x.xPubKeySignature = opts.xPubKeySignature; // So third parties can check independently x.xPubKeySignature = opts.xPubKeySignature; // So third parties can check independently
x.signingPubKey = x.getSigningPubKey(); x.signingPubKey = x.getSigningPubKey();
x.addressManager = new AddressManager({ x.addressManager = AddressManager.create({
copayerIndex: opts.copayerIndex copayerIndex: opts.copayerIndex
}); });

22
lib/model/notification.js

@ -1,5 +1,3 @@
var Uuid = require('uuid'); var Uuid = require('uuid');
/* /*
@ -24,21 +22,29 @@ var Uuid = require('uuid');
* to notify the user * to notify the user
* *
*/ */
function Notification() {
this.version = '1.0.0';
};
function Notification(opts) { Notification.create = function(opts) {
opts = opts || {}; opts = opts || {};
var x = new Notification();
var now = Date.now(); var now = Date.now();
this.createdOn = Math.floor(now / 1000); x.createdOn = Math.floor(now / 1000);
this.id = ('00000000000000' + now).slice(-14) + ('0000' + opts.ticker||0).slice(-4) ; x.id = ('00000000000000' + now).slice(-14) + ('0000' + opts.ticker || 0).slice(-4);
this.type = opts.type || 'general'; x.type = opts.type || 'general';
this.data = opts.data; x.data = opts.data;
return x;
}; };
Notification.fromObj = function(obj) { Notification.fromObj = function(obj) {
var x= new Notification(); var x = new Notification();
x.createdOn = obj.createdOn; x.createdOn = obj.createdOn;
x.id = obj.id;
x.type = obj.type, x.type = obj.type,
x.data = obj.data; x.data = obj.data;

42
lib/model/txproposal.js

@ -7,28 +7,32 @@ var Address = Bitcore.Address;
var TxProposalAction = require('./txproposalaction'); var TxProposalAction = require('./txproposalaction');
var VERSION = '1.0.0'; function TxProposal() {
this.version = '1.0.0';
};
function TxProposal(opts) { TxProposal.create = function(opts) {
opts = opts || {}; opts = opts || {};
this.version = VERSION; var x = new TxProposal();
var now = Date.now(); var now = Date.now();
this.createdOn = Math.floor(now / 1000); x.createdOn = Math.floor(now / 1000);
this.id = ('00000000000000' + now).slice(-14) + Uuid.v4(); x.id = ('00000000000000' + now).slice(-14) + Uuid.v4();
this.creatorId = opts.creatorId; x.creatorId = opts.creatorId;
this.toAddress = opts.toAddress; x.toAddress = opts.toAddress;
this.amount = opts.amount; x.amount = opts.amount;
this.message = opts.message; x.message = opts.message;
this.proposalSignature = opts.proposalSignature; x.proposalSignature = opts.proposalSignature;
this.changeAddress = opts.changeAddress; x.changeAddress = opts.changeAddress;
this.inputs = opts.inputs; x.inputs = [];
this.inputPaths = opts.inputPaths; x.inputPaths = [];
this.requiredSignatures = opts.requiredSignatures; x.requiredSignatures = opts.requiredSignatures;
this.requiredRejections = opts.requiredRejections; x.requiredRejections = opts.requiredRejections;
this.status = 'pending'; x.status = 'pending';
this.actions = {}; x.actions = {};
return x;
}; };
TxProposal.fromObj = function(obj) { TxProposal.fromObj = function(obj) {
@ -51,7 +55,7 @@ TxProposal.fromObj = function(obj) {
x.inputPaths = obj.inputPaths; x.inputPaths = obj.inputPaths;
x.actions = obj.actions; x.actions = obj.actions;
_.each(x.actions, function(action, copayerId) { _.each(x.actions, function(action, copayerId) {
x.actions[copayerId] = new TxProposalAction(action); x.actions[copayerId] = TxProposalAction.fromObj(action);
}); });
return x; return x;
@ -136,7 +140,7 @@ TxProposal.prototype.getActionBy = function(copayerId) {
}; };
TxProposal.prototype.addAction = function(copayerId, type, comment, signatures, xpub) { TxProposal.prototype.addAction = function(copayerId, type, comment, signatures, xpub) {
var action = new TxProposalAction({ var action = TxProposalAction.create({
copayerId: copayerId, copayerId: copayerId,
type: type, type: type,
signatures: signatures, signatures: signatures,

22
lib/model/txproposalaction.js

@ -1,14 +1,22 @@
'use strict'; 'use strict';
function TxProposalAction(opts) { function TxProposalAction() {
this.version = '1.0.0';
};
TxProposalAction.create = function(opts) {
opts = opts || {}; opts = opts || {};
this.createdOn = Math.floor(Date.now() / 1000); var x = new TxProposalAction();
this.copayerId = opts.copayerId;
this.type = opts.type || (opts.signatures ? 'accept' : 'reject'); x.createdOn = Math.floor(Date.now() / 1000);
this.signatures = opts.signatures; x.copayerId = opts.copayerId;
this.xpub = opts.xpub; x.type = opts.type || (opts.signatures ? 'accept' : 'reject');
this.comment = opts.comment; x.signatures = opts.signatures;
x.xpub = opts.xpub;
x.comment = opts.comment;
return x;
}; };
TxProposalAction.fromObj = function(obj) { TxProposalAction.fromObj = function(obj) {

9
lib/model/wallet.js

@ -10,11 +10,8 @@ var Copayer = require('./copayer');
var AddressManager = require('./addressmanager'); var AddressManager = require('./addressmanager');
var WalletUtils = require('../walletutils'); var WalletUtils = require('../walletutils');
var VERSION = '1.0.0';
function Wallet() { function Wallet() {
this.version = VERSION; this.version = '1.0.0';
}; };
Wallet.create = function(opts) { Wallet.create = function(opts) {
@ -33,7 +30,7 @@ Wallet.create = function(opts) {
x.copayers = []; x.copayers = [];
x.pubKey = opts.pubKey; x.pubKey = opts.pubKey;
x.network = opts.network; x.network = opts.network;
x.addressManager = new AddressManager(); x.addressManager = AddressManager.create();
return x; return x;
}; };
@ -125,7 +122,7 @@ Wallet.prototype.createAddress = function(isChange) {
$.checkState(this.isComplete()); $.checkState(this.isComplete());
var path = this.addressManager.getNewAddressPath(isChange); var path = this.addressManager.getNewAddressPath(isChange);
return new Address(WalletUtils.deriveAddress(this.publicKeyRing, path, this.m, this.network)); return Address.create(WalletUtils.deriveAddress(this.publicKeyRing, path, this.m, this.network));
}; };

4
lib/server.js

@ -169,7 +169,7 @@ CopayServer.prototype._notify = function(type, data) {
var walletId = self.walletId || data.walletId; var walletId = self.walletId || data.walletId;
$.checkState(walletId); $.checkState(walletId);
var n = new Notification({ var n = Notification.create({
type: type, type: type,
data: data, data: data,
ticker: this.notifyTicker++, ticker: this.notifyTicker++,
@ -496,7 +496,7 @@ CopayServer.prototype.createTx = function(opts, cb) {
locked: true locked: true
}); });
var txp = new TxProposal({ var txp = TxProposal.create({
creatorId: self.copayerId, creatorId: self.copayerId,
toAddress: opts.toAddress, toAddress: opts.toAddress,
amount: opts.amount, amount: opts.amount,

6
test/addressmanager.js

@ -10,7 +10,7 @@ var AddressManager = require('../lib/model/addressmanager');
describe('AddressManager', function() { describe('AddressManager', function() {
describe('#getCurrentAddressPath', function() { describe('#getCurrentAddressPath', function() {
it('should return a valid BIP32 path for given index', function() { it('should return a valid BIP32 path for given index', function() {
var am = new AddressManager({ var am = AddressManager.create({
copayerIndex: 4 copayerIndex: 4
}); });
am.getCurrentAddressPath(false).should.equal('m/4/0/0'); am.getCurrentAddressPath(false).should.equal('m/4/0/0');
@ -19,14 +19,14 @@ describe('AddressManager', function() {
}); });
describe('#getCurrentAddressPath', function() { describe('#getCurrentAddressPath', function() {
it('should return a valid BIP32 path for defaut Index', function() { it('should return a valid BIP32 path for defaut Index', function() {
var am = new AddressManager(); var am = AddressManager.create();
am.getCurrentAddressPath(false).should.equal('m/2147483647/0/0'); am.getCurrentAddressPath(false).should.equal('m/2147483647/0/0');
am.getCurrentAddressPath(true).should.equal('m/2147483647/1/0'); am.getCurrentAddressPath(true).should.equal('m/2147483647/1/0');
}); });
}); });
describe('#getNewAddressPath', function() { describe('#getNewAddressPath', function() {
it('should return a new valid BIP32 path for given index', function() { it('should return a new valid BIP32 path for given index', function() {
var am = new AddressManager({ var am = AddressManager.create({
copayerIndex: 2 copayerIndex: 2
}); });
am.getNewAddressPath(false).should.equal('m/2/0/0'); am.getNewAddressPath(false).should.equal('m/2/0/0');

Loading…
Cancel
Save