refactor remaining objects

This commit is contained in:
Ivan Socolsky 2015-02-17 21:20:08 -03:00
parent 4c7f8d3a53
commit 52f6529952
9 changed files with 100 additions and 69 deletions

View File

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

View File

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

View File

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

View File

@ -1,5 +1,3 @@
var Uuid = require('uuid');
/*
@ -20,25 +18,33 @@ var Uuid = require('uuid');
* { amount: 'xxx', address: 'xxx'}
* { txProposalId: 'xxx', copayerId: 'xxx' }
*
* Data is meant to provide only the needed information
* to notify the user
* Data is meant to provide only the needed information
* to notify the user
*
*/
function Notification() {
this.version = '1.0.0';
};
function Notification(opts) {
Notification.create = function(opts) {
opts = opts || {};
var x = new Notification();
var now = Date.now();
this.createdOn = Math.floor(now / 1000);
this.id = ('00000000000000' + now).slice(-14) + ('0000' + opts.ticker||0).slice(-4) ;
this.type = opts.type || 'general';
this.data = opts.data;
x.createdOn = Math.floor(now / 1000);
x.id = ('00000000000000' + now).slice(-14) + ('0000' + opts.ticker || 0).slice(-4);
x.type = opts.type || 'general';
x.data = opts.data;
return x;
};
Notification.fromObj = function(obj) {
var x= new Notification();
var x = new Notification();
x.createdOn = obj.createdOn;
x.id = obj.id;
x.type = obj.type,
x.data = obj.data;

View File

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

View File

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

View File

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

View File

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

View File

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