mirror of https://github.com/BTCPrivate/copay.git
Merge pull request #40 from maraoz/refactor/arch2
adding wallet factory
This commit is contained in:
commit
7e5a6c7081
|
@ -9,7 +9,6 @@ var Address = bitcore.Address;
|
||||||
var Script = bitcore.Script;
|
var Script = bitcore.Script;
|
||||||
var coinUtil = bitcore.util;
|
var coinUtil = bitcore.util;
|
||||||
var Transaction = bitcore.Transaction;
|
var Transaction = bitcore.Transaction;
|
||||||
var buffertools = bitcore.buffertools;
|
|
||||||
|
|
||||||
var Storage = imports.Storage || require('../storage/Base.js');
|
var Storage = imports.Storage || require('../storage/Base.js');
|
||||||
var storage = Storage.default();
|
var storage = Storage.default();
|
||||||
|
@ -24,8 +23,6 @@ function PublicKeyRing(opts) {
|
||||||
this.requiredCopayers = opts.requiredCopayers || 3;
|
this.requiredCopayers = opts.requiredCopayers || 3;
|
||||||
this.totalCopayers = opts.totalCopayers || 5;
|
this.totalCopayers = opts.totalCopayers || 5;
|
||||||
|
|
||||||
this.id = opts.id || PublicKeyRing.getRandomId();
|
|
||||||
|
|
||||||
this.copayersBIP32 = [];
|
this.copayersBIP32 = [];
|
||||||
|
|
||||||
this.changeAddressIndex=0;
|
this.changeAddressIndex=0;
|
||||||
|
@ -49,11 +46,6 @@ PublicKeyRing.ChangeBranch = function (index) {
|
||||||
return 'm/1/'+index;
|
return 'm/1/'+index;
|
||||||
};
|
};
|
||||||
|
|
||||||
PublicKeyRing.getRandomId = function () {
|
|
||||||
var r = buffertools.toHex(coinUtil.generateNonce());
|
|
||||||
return r;
|
|
||||||
};
|
|
||||||
|
|
||||||
PublicKeyRing.decrypt = function (passphrase, encPayload) {
|
PublicKeyRing.decrypt = function (passphrase, encPayload) {
|
||||||
console.log('[wallet.js.35] TODO READ: passphrase IGNORED');
|
console.log('[wallet.js.35] TODO READ: passphrase IGNORED');
|
||||||
return encPayload;
|
return encPayload;
|
||||||
|
|
|
@ -1,13 +1,15 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var imports = require('soop').imports();
|
var imports = require('soop').imports();
|
||||||
|
|
||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
var http = require('http');
|
var coinUtil = bitcore.util;
|
||||||
|
var buffertools = bitcore.buffertools;
|
||||||
|
var http = require('http');
|
||||||
|
|
||||||
var Storage = imports.Storage;
|
var Storage = imports.Storage;
|
||||||
var Network = imports.Network;
|
var Network = imports.Network;
|
||||||
var Blockchain = imports.Blockchain;
|
var Blockchain = imports.Blockchain;
|
||||||
|
|
||||||
var copay = copay || require('../../../copay');
|
var copay = copay || require('../../../copay');
|
||||||
|
|
||||||
|
@ -18,31 +20,33 @@ function Wallet(config) {
|
||||||
|
|
||||||
|
|
||||||
Wallet.prototype._startInterface = function(config) {
|
Wallet.prototype._startInterface = function(config) {
|
||||||
this.storage = new Storage(config.storage);
|
this.storage = new Storage(config.storage);
|
||||||
this.network = new Network(config.network);
|
this.network = new Network(config.network);
|
||||||
this.blockchain = new Blockchain(config.blockchain);
|
this.blockchain = new Blockchain(config.blockchain);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Wallet.prototype._createNew = function(config, opts) {
|
Wallet.prototype._createNew = function(config, opts) {
|
||||||
|
|
||||||
console.log('### CREATING NEW WALLET.'
|
this.id = opts.id || Wallet.getRandomId();
|
||||||
+ (opts.walletId ? ' USING ID: ' +opts.walletId : ' NEW ID') );
|
console.log('### CREATING NEW WALLET.' + (opts.id ? ' USING ID: ' + opts.id : ' NEW ID'));
|
||||||
|
|
||||||
this.privateKey = new copay.PrivateKey({networkName: config.networkName});
|
this.privateKey = new copay.PrivateKey({
|
||||||
|
networkName: config.networkName
|
||||||
|
});
|
||||||
console.log('\t### PrivateKey Initialized');
|
console.log('\t### PrivateKey Initialized');
|
||||||
|
|
||||||
this.publicKeyRing = opts.publicKeyRing || new copay.PublicKeyRing({
|
this.publicKeyRing = opts.publicKeyRing || new copay.PublicKeyRing({
|
||||||
id: opts.walletId,
|
id: this.id,
|
||||||
requiredCopayers: opts.requiredCopayers || config.wallet.requiredCopayers,
|
requiredCopayers: opts.requiredCopayers || config.wallet.requiredCopayers,
|
||||||
totalCopayers: opts.totalCopayers || config.wallet.totalCopayers,
|
totalCopayers: opts.totalCopayers || config.wallet.totalCopayers,
|
||||||
networkName: config.networkName,
|
networkName: config.networkName,
|
||||||
});
|
});
|
||||||
this.publicKeyRing.addCopayer(this.privateKey.getBIP32().extendedPublicKeyString());
|
this.publicKeyRing.addCopayer(this.privateKey.getBIP32().extendedPublicKeyString());
|
||||||
console.log('\t### PublicKeyRing Initialized WalletID: ' + this.publicKeyRing.id);
|
console.log('\t### PublicKeyRing Initialized WalletID: ' + this.publicKeyRing.id);
|
||||||
|
|
||||||
this.txProposals = opts.txProposals || new copay.TxProposals({
|
this.txProposals = opts.txProposals || new copay.TxProposals({
|
||||||
walletId: this.publicKeyRing.id,
|
walletId: this.id,
|
||||||
publicKeyRing: this.publicKeyRing,
|
publicKeyRing: this.publicKeyRing,
|
||||||
networkName: config.networkName,
|
networkName: config.networkName,
|
||||||
});
|
});
|
||||||
|
@ -51,14 +55,14 @@ Wallet.prototype._createNew = function(config, opts) {
|
||||||
|
|
||||||
|
|
||||||
Wallet.prototype._load = function(config, walletId) {
|
Wallet.prototype._load = function(config, walletId) {
|
||||||
this.id = walletId;
|
this.id = walletId;
|
||||||
this.publicKeyRing = new copay.PublicKeyRing.fromObj(
|
this.publicKeyRing = new copay.PublicKeyRing.fromObj(
|
||||||
this.storage.get(this.id, 'publicKeyRing')
|
this.storage.get(this.id, 'publicKeyRing')
|
||||||
);
|
);
|
||||||
this.txProposals = new copay.TxProposals.fromObj(
|
this.txProposals = new copay.TxProposals.fromObj(
|
||||||
this.storage.get(this.id, 'txProposals')
|
this.storage.get(this.id, 'txProposals')
|
||||||
);
|
);
|
||||||
this.privateKey = new copay.PrivateKey.fromObj(
|
this.privateKey = new copay.PrivateKey.fromObj(
|
||||||
this.storage.get(this.id, 'privateKey')
|
this.storage.get(this.id, 'privateKey')
|
||||||
); //TODO secure
|
); //TODO secure
|
||||||
|
|
||||||
|
@ -76,7 +80,7 @@ Wallet.prototype._load = function(config, walletId) {
|
||||||
// CONSTRUCTORS
|
// CONSTRUCTORS
|
||||||
Wallet.read = function(config, walletId) {
|
Wallet.read = function(config, walletId) {
|
||||||
var w = new Wallet(config);
|
var w = new Wallet(config);
|
||||||
w.load(walletId);
|
w._load(walletId);
|
||||||
|
|
||||||
return w;
|
return w;
|
||||||
};
|
};
|
||||||
|
@ -87,8 +91,56 @@ Wallet.create = function(config, opts) {
|
||||||
|
|
||||||
return w;
|
return w;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Wallet.getRandomId = function() {
|
||||||
|
var r = buffertools.toHex(coinUtil.generateNonce());
|
||||||
|
return r;
|
||||||
|
};
|
||||||
|
|
||||||
|
Wallet.prototype.store = function() {
|
||||||
|
// TODO store each variable
|
||||||
|
};
|
||||||
|
|
||||||
|
var WalletFactory = function() {
|
||||||
|
this.storage = Storage.
|
||||||
|
default ();
|
||||||
|
};
|
||||||
|
|
||||||
|
WalletFactory.prototype.create = function(config, opts) {
|
||||||
|
var w = new Wallet.create(config, opts);
|
||||||
|
w.store();
|
||||||
|
this._addWalletId(w.id);
|
||||||
|
return w;
|
||||||
|
};
|
||||||
|
|
||||||
|
WalletFactory.prototype.get = function(config, walletId) {
|
||||||
|
return Wallet.read(config, walletId);
|
||||||
|
};
|
||||||
|
|
||||||
|
WalletFactory.prototype.remove = function(walletId) {
|
||||||
|
// TODO remove wallet contents, not only the id (Wallet.remove?)
|
||||||
|
this._delWalletId(walletId);
|
||||||
|
};
|
||||||
|
|
||||||
|
WalletFactory.prototype._addWalletId = function(walletId) {
|
||||||
|
var ids = this._getWalletIds();
|
||||||
|
if (ids.indexOf(walletId) == -1) return;
|
||||||
|
storage.set('walletIds', (ids ? ids + ',' : '') + walletId);
|
||||||
|
};
|
||||||
|
|
||||||
|
WalletFactory.prototype._delWalletId = function(walletId) {
|
||||||
|
var ids = this._getWalletIds();
|
||||||
|
var index = ids.indexOf(walletId);
|
||||||
|
if (index == -1) return;
|
||||||
|
ids.splice(index, 1); // removes walletId
|
||||||
|
this.storage.set('walletIds', ids.join(','));
|
||||||
|
};
|
||||||
|
|
||||||
|
WalletFactory.prototype._getWalletIds = function() {
|
||||||
|
var ids = this.storage.get('walletIds');
|
||||||
|
return ids ? ids.split(',') : [];
|
||||||
|
};
|
||||||
|
|
||||||
|
Wallet.factory = new WalletFactory();
|
||||||
|
|
||||||
module.exports = require('soop')(Wallet);
|
module.exports = require('soop')(Wallet);
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var imports = require('soop').imports();
|
||||||
|
|
||||||
|
function Wallet(opts) {
|
||||||
|
opts = opts || {};
|
||||||
|
this.host = 'localhost';
|
||||||
|
this.port = '3001';
|
||||||
|
}
|
||||||
|
|
||||||
|
WalletFactory = function() {
|
||||||
|
this.storage = copay.Storage.default();
|
||||||
|
};
|
||||||
|
|
||||||
|
WalletFactory.prototype.create = function(config, opts) {
|
||||||
|
var w = new Wallet(config, opts);
|
||||||
|
w.store();
|
||||||
|
this._addWalletId(w.id);
|
||||||
|
};
|
||||||
|
|
||||||
|
WalletFactory.prototype.get = function(walletId) {
|
||||||
|
return Wallet.read(walletId);
|
||||||
|
};
|
||||||
|
|
||||||
|
WalletFactory.prototype.remove = function(walletId) {
|
||||||
|
// TODO remove wallet contents, not only the id (Wallet.remove?)
|
||||||
|
this._delWalletId(walletId);
|
||||||
|
};
|
||||||
|
|
||||||
|
WalletFactory.prototype._addWalletId = function(walletId) {
|
||||||
|
var ids = this._getWalletIds();
|
||||||
|
if (ids.indexOf(walletId) == -1) return;
|
||||||
|
localStorage.setItem('walletIds', (ids ? ids + ',' : '') + walletId);
|
||||||
|
};
|
||||||
|
|
||||||
|
WalletFactory.prototype._delWalletId = function(walletId) {
|
||||||
|
var ids = this._getWalletIds();
|
||||||
|
var index = ids.indexOf(walletId);
|
||||||
|
if (index == -1) return;
|
||||||
|
ids.splice(index, 1); // removes walletId
|
||||||
|
this.storage.set('walletIds', ids.join(','));
|
||||||
|
};
|
||||||
|
|
||||||
|
WalletFactory.prototype._getWalletIds = function() {
|
||||||
|
var ids = this.storage.get('walletIds');
|
||||||
|
return ids ? ids.split(',') : [];
|
||||||
|
};
|
|
@ -3,69 +3,31 @@
|
||||||
angular.module('copay.storage')
|
angular.module('copay.storage')
|
||||||
.factory('Storage', function($rootScope) {
|
.factory('Storage', function($rootScope) {
|
||||||
|
|
||||||
|
return {
|
||||||
var _key = function(walletId, key) {
|
getGlobal: function(key) {
|
||||||
return walletId + '::' + key;
|
return JSON.parse(localStorage.getItem(key));
|
||||||
};
|
|
||||||
|
|
||||||
var _pushKey = function(walletId, key) {
|
|
||||||
var keys = localStorage.getItem(walletId);
|
|
||||||
localStorage.setItem(walletId, (keys?keys+',':'') +key);
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
getGlobal: function( key) {
|
|
||||||
return JSON.parse(localStorage.getItem(key));
|
|
||||||
},
|
},
|
||||||
setGlobal: function( key, data) {
|
setGlobal: function(key, data) {
|
||||||
localStorage.setItem(key, JSON.stringify(data));
|
localStorage.setItem(key, JSON.stringify(data));
|
||||||
},
|
},
|
||||||
get: function(walletId, key) {
|
get: function(walletId, key) {
|
||||||
if (!walletId) return;
|
if (!walletId) return;
|
||||||
return JSON.parse(localStorage.getItem(_key(walletId,key)));
|
return JSON.parse(localStorage.getItem(_key(walletId, key)));
|
||||||
},
|
},
|
||||||
set: function(walletId, key, data) {
|
set: function(walletId, key, data) {
|
||||||
if (!walletId) return;
|
if (!walletId) return;
|
||||||
var k = _key(walletId,key);
|
var k = _key(walletId, key);
|
||||||
localStorage.setItem(k, JSON.stringify(data));
|
localStorage.setItem(k, JSON.stringify(data));
|
||||||
_pushKey(walletId, k);
|
_pushKey(walletId, k);
|
||||||
},
|
},
|
||||||
remove: function(walletId, key) {
|
remove: function(walletId, key) {
|
||||||
localStorage.removeItem(_key(walletId,key));
|
localStorage.removeItem(_key(walletId, key));
|
||||||
},
|
},
|
||||||
clearAll: function(walletId){
|
clearAll: function(walletId) {
|
||||||
var keys = localStorage.getItem(walletId);
|
var keys = localStorage.getItem(walletId);
|
||||||
keys.split(',').forEach(function(k){
|
keys.split(',').forEach(function(k) {
|
||||||
localStorage.removeItem(key);
|
localStorage.removeItem(key);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
addWalletId: function(walletId) {
|
|
||||||
var ids = localStorage.getItem('walletIds');
|
|
||||||
if (ids) {
|
|
||||||
var list = ids.split(',');
|
|
||||||
var l = list.length;
|
|
||||||
for(var i=0; i<l; i++)
|
|
||||||
if (walletId === list[i])
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
localStorage.setItem('walletIds', (ids?ids+',':'') + walletId);
|
|
||||||
},
|
|
||||||
delWalletId: function(walletId) {
|
|
||||||
var ids = localStorage.getItem('walletIds');
|
|
||||||
if (ids) {
|
|
||||||
var is = ids.split(',');
|
|
||||||
var newIds = [];
|
|
||||||
is.forEach(function(i) {
|
|
||||||
if (i != walletId) {
|
|
||||||
newIds.push(i);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
localStorage.setItem('walletIds', newIds.join(',') );
|
|
||||||
}
|
|
||||||
},
|
|
||||||
getWalletIds: function() {
|
|
||||||
var ids = localStorage.getItem('walletIds');
|
|
||||||
return ids ? ids.split(',') : [];
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,25 +1,47 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var chai = chai || require('chai');
|
var chai = chai || require('chai');
|
||||||
var should = chai.should();
|
var should = chai.should();
|
||||||
var bitcore = bitcore || require('bitcore');
|
var copay = copay || require('../copay');
|
||||||
var copay = copay || require('../copay');
|
var Wallet = require('soop').load('../js/models/core/Wallet', {
|
||||||
var Wallet = copay.Wallet || require('./js/models/core/Wallet');
|
Storage: require('./FakeStorage'),
|
||||||
|
Network: copay.WebRTC,
|
||||||
|
Blockchain: copay.Insight
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(Wallet);
|
||||||
|
|
||||||
var config = {
|
|
||||||
wallet: {
|
|
||||||
requiredCopayers: 3,
|
|
||||||
totalCopayers: 5,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
describe('Wallet model', function() {
|
describe('Wallet model', function() {
|
||||||
|
var config = {
|
||||||
|
wallet: {
|
||||||
|
requiredCopayers: 3,
|
||||||
|
totalCopayers: 5,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var opts = {};
|
||||||
|
|
||||||
it('should create an instance', function () {
|
|
||||||
|
it.skip('should create an instance', function () {
|
||||||
var opts = {};
|
var opts = {};
|
||||||
var w = new Wallet(config, opts);
|
var w = Wallet.create(config, opts);
|
||||||
should.exist(w);
|
should.exist(w);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe('factory', function() {
|
||||||
|
it('should create the factory', function() {
|
||||||
|
should.exist(Wallet.factory);
|
||||||
|
});
|
||||||
|
it('should be able to create wallets', function() {
|
||||||
|
var w = Wallet.factory.create(config, opts);
|
||||||
|
should.exist(w);
|
||||||
|
});
|
||||||
|
it.skip('should be able to get wallets', function() {
|
||||||
|
var w = Wallet.factory.create(config, opts);
|
||||||
|
var v = Wallet.factory.get(config, w.id);
|
||||||
|
should.exist(w);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue