Fix Conflicts:

js/models/core/WalletFactory.js
This commit is contained in:
Gustavo Cortez 2014-06-18 10:03:22 -03:00
commit 21baa103bd
13 changed files with 346 additions and 137 deletions

View File

@ -219,7 +219,6 @@ small.has-error {
font-weight: bold;
}
.totalAmount {
line-height: 120%;
margin-top:2px;

View File

@ -769,6 +769,9 @@
</a>
</div>
</div>
<div class="row text-center small" style="margin-top:20px">
<div class="button radius warning" ng-really-message="Are you sure to delete this wallet from this computer?" ng-really-click="deleteWallet()">Delete this wallet from this computer</div>
</div>
</div>
</script>

View File

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('BackupController',
function($scope, $rootScope, $location, $window, $timeout, $modal, backupService) {
function($scope, $rootScope, $location, $window, $timeout, $modal, backupService, walletFactory, controllerUtils) {
$scope.title = 'Backup';
$scope.download = function() {
@ -19,6 +19,14 @@ angular.module('copayApp.controllers').controller('BackupController',
});
};
$scope.deleteWallet = function() {
var w = $rootScope.wallet;
w.disconnect();
walletFactory.delete(w.id, function() {
controllerUtils.logout();
});
};
});
var ModalInstanceCtrl = function($scope, $modalInstance) {

View File

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('HeaderController',
function($scope, $rootScope, $location, $notification, $http, walletFactory, controllerUtils) {
function($scope, $rootScope, $location, $notification, $http, controllerUtils) {
$scope.menu = [
{
'title': 'Addresses',

View File

@ -202,4 +202,23 @@ angular.module('copayApp.directives')
});
}
};
});
})
// From https://gist.github.com/asafge/7430497
.directive('ngReallyClick', [
function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
var message = attrs.ngReallyMessage;
if (message && confirm(message)) {
scope.$apply(attrs.ngReallyClick);
}
});
}
}
}
])
;

View File

@ -1,6 +1,6 @@
'use strict';
var imports = require('soop').imports();
var imports = require('soop').imports();
var TxProposals = require('./TxProposals');
var PublicKeyRing = require('./PublicKeyRing');
@ -29,13 +29,13 @@ function WalletFactory(config, version) {
this.network = new this.Network(config.network);
this.blockchain = new this.Blockchain(config.blockchain);
this.networkName = config.networkName;
this.verbose = config.verbose;
this.networkName = config.networkName;
this.verbose = config.verbose;
this.walletDefaults = config.wallet;
this.version = version;
this.version = version;
}
WalletFactory.prototype.log = function(){
WalletFactory.prototype.log = function() {
if (!this.verbose) return;
if (console) {
console.log.apply(console, arguments);
@ -45,18 +45,18 @@ WalletFactory.prototype.log = function(){
WalletFactory.prototype._checkRead = function(walletId) {
var s = this.storage;
var ret =
s.get(walletId, 'publicKeyRing') &&
s.get(walletId, 'txProposals') &&
s.get(walletId, 'opts') &&
s.get(walletId, 'privateKey');
var ret =
s.get(walletId, 'publicKeyRing') &&
s.get(walletId, 'txProposals') &&
s.get(walletId, 'opts') &&
s.get(walletId, 'privateKey');
return !!ret;
};
WalletFactory.prototype.fromObj = function(obj) {
// not stored options
obj.opts.reconnectDelay = this.walletDefaults.reconnectDelay;
obj.opts.reconnectDelay = this.walletDefaults.reconnectDelay;
var w = Wallet.fromObj(obj, this.storage, this.network, this.blockchain);
if (!w) return false;
@ -75,34 +75,36 @@ WalletFactory.prototype.fromEncryptedObj = function(base64, password) {
};
WalletFactory.prototype.read = function(walletId) {
if (! this._checkRead(walletId))
if (!this._checkRead(walletId))
return false;
var obj = {};
var s = this.storage;
obj.id = walletId;
obj.opts = s.get(walletId, 'opts');
obj.publicKeyRing = s.get(walletId, 'publicKeyRing');
obj.txProposals = s.get(walletId, 'txProposals');
obj.privateKey = s.get(walletId, 'privateKey');
obj.addressBook = s.get(walletId, 'addressBook');
obj.opts = s.get(walletId, 'opts');
obj.publicKeyRing = s.get(walletId, 'publicKeyRing');
obj.txProposals = s.get(walletId, 'txProposals');
obj.privateKey = s.get(walletId, 'privateKey');
obj.addressBook = s.get(walletId, 'addressBook');
var w = this.fromObj(obj);
return w;
};
WalletFactory.prototype.create = function(opts) {
opts = opts || {};
this.log('### CREATING NEW WALLET.' +
(opts.id ? ' USING ID: ' + opts.id : ' NEW ID') +
(opts.privateKey ? ' USING PrivateKey: ' + opts.privateKey.getId() : ' NEW PrivateKey')
);
opts = opts || {};
this.log('### CREATING NEW WALLET.' +
(opts.id ? ' USING ID: ' + opts.id : ' NEW ID') +
(opts.privateKey ? ' USING PrivateKey: ' + opts.privateKey.getId() : ' NEW PrivateKey')
);
opts.privateKey = opts.privateKey || new PrivateKey({ networkName: this.networkName });
opts.privateKey = opts.privateKey || new PrivateKey({
networkName: this.networkName
});
var requiredCopayers = opts.requiredCopayers || this.walletDefaults.requiredCopayers;
var totalCopayers = opts.totalCopayers || this.walletDefaults.totalCopayers;
var totalCopayers = opts.totalCopayers || this.walletDefaults.totalCopayers;
opts.publicKeyRing = opts.publicKeyRing || new PublicKeyRing({
networkName: this.networkName,
@ -127,10 +129,10 @@ WalletFactory.prototype.create = function(opts) {
opts.verbose = this.verbose;
opts.spendUnconfirmed = opts.spendUnconfirmed || this.walletDefaults.spendUnconfirmed;
opts.reconnectDelay = opts.reconnectDelay || this.walletDefaults.reconnectDelay;
opts.reconnectDelay = opts.reconnectDelay || this.walletDefaults.reconnectDelay;
opts.requiredCopayers = requiredCopayers;
opts.totalCopayers = totalCopayers;
opts.version = opts.version || this.version;
opts.totalCopayers = totalCopayers;
opts.version = opts.version || this.version;
var w = new Wallet(opts);
w.store();
return w;
@ -140,32 +142,25 @@ WalletFactory.prototype.create = function(opts) {
WalletFactory.prototype._checkVersion = function(inVersion) {
var thisV = this.version.split('.');
var thisV0 = parseInt(thisV[0]);
var inV = inVersion.split('.');
var inV0 = parseInt(inV[0]);
var inV = inVersion.split('.');
var inV0 = parseInt(inV[0]);
//We only check for major version differences
if( thisV0 < inV0 ) {
if (thisV0 < inV0) {
throw new Error('Major difference in software versions' +
'. Received:' + inVersion +
'. Current version:' + this.version +
'. Aborting.');
'. Received:' + inVersion +
'. Current version:' + this.version +
'. Aborting.');
}
};
WalletFactory.prototype._checkNetwork = function(inNetworkName) {
if( this.networkName !== inNetworkName ) {
throw new Error('This Wallet is configured for '
+ inNetworkName
+ ' while currently Copay is configured for: '
+ this.networkName
+ '. Check your settings.'
);
if (this.networkName !== inNetworkName) {
throw new Error('This Wallet is configured for ' + inNetworkName + ' while currently Copay is configured for: ' + this.networkName + '. Check your settings.');
}
};
WalletFactory.prototype.open = function(walletId, opts) {
opts = opts || {};
opts.id = walletId;
@ -182,14 +177,16 @@ WalletFactory.prototype.open = function(walletId, opts) {
WalletFactory.prototype.getWallets = function() {
var ret = this.storage.getWallets();
ret.forEach(function(i) {
i.show = i.name ? ( (i.name + ' <'+i.id+'>') ) : i.id;
i.show = i.name ? ((i.name + ' <' + i.id + '>')) : i.id;
});
return ret;
};
WalletFactory.prototype.remove = function(walletId) {
// TODO remove wallet contents
this.log('TODO: remove wallet contents');
WalletFactory.prototype.delete = function(walletId, cb) {
var s = this.storage;
this.log('## DELETING WALLET ID:' + walletId); //TODO
s.deleteWallet(walletId);
return cb();
};
WalletFactory.prototype.decodeSecret = function(secret) {
@ -205,9 +202,11 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras
var s = self.decodeSecret(secret);
if (!s) return cb('badSecret');
//Create our PrivateK
var privateKey = new PrivateKey({ networkName: this.networkName });
var privateKey = new PrivateKey({
networkName: this.networkName
});
this.log('\t### PrivateKey Initialized');
var opts = {
copayerId: privateKey.getId(),
@ -226,13 +225,13 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras
return cb(connectedOnce ? 'walletFull' : 'joinError');
});
self.network.on('data', function(sender, data) {
if (data.type ==='walletId') {
if (data.networkName !== self.networkName ){
if (data.type === 'walletId') {
if (data.networkName !== self.networkName) {
return cb('badNetwork');
}
data.opts.privateKey = privateKey;
data.opts.nickname = nickname;
data.opts.nickname = nickname;
data.opts.passphrase = passphrase;
data.opts.id = data.walletId;
var w = self.create(data.opts);

View File

@ -135,7 +135,6 @@ Storage.prototype.getWalletIds = function() {
Storage.prototype.getWallets = function() {
var wallets = [];
var uniq = {};
var ids = this.getWalletIds();
for (var i in ids) {
@ -147,6 +146,23 @@ Storage.prototype.getWallets = function() {
return wallets;
};
Storage.prototype.deleteWallet = function(walletId) {
var toDelete = {};
toDelete['nameFor::' + walletId] = 1;
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i);
var split = key.split('::');
if (split.length == 2 && split[0] === walletId) {
toDelete[key] = 1;
}
}
for (var i in toDelete) {
this.removeGlobal(i);
}
};
//obj contains keys to be set
Storage.prototype.setFromObj = function(walletId, obj) {
for (var k in obj) {

View File

@ -1,6 +1,8 @@
'use strict';
var BackupService = function() {};
var BackupService = function($notification) {
this.notifications = $notification;
};
BackupService.prototype.getName = function(wallet) {
return (wallet.name ? (wallet.name+'-') : '') + wallet.id;
@ -11,6 +13,7 @@ BackupService.prototype.download = function(wallet) {
var timestamp = +(new Date());
var walletName = this.getName(wallet);
var filename = walletName + '-' + timestamp + '-keybackup.json.aes';
this.notifications.success('Backup created', 'Encrypted backup file saved.');
var blob = new Blob([ew], {
type: 'text/plain;charset=utf-8'
});
@ -44,4 +47,4 @@ BackupService.prototype.sendEmail = function(email, wallet) {
}
};
angular.module('copayApp.services').value('backupService', new BackupService());
angular.module('copayApp.services').service('backupService', BackupService);

View File

@ -57,6 +57,7 @@
"cli-color": "0.3.2"
},
"dependencies": {
"mocha": "^1.18.2"
"mocha": "^1.18.2",
"mocha-lcov-reporter": "0.0.1"
}
}

View File

@ -1,40 +1,103 @@
var FakeStorage = function() {
this.reset();
};
var FakeStorage = function(){
FakeStorage.prototype.reset = function(password) {
this.storage = {};
};
};
FakeStorage.prototype._setPassphrase = function (password) {
FakeStorage.prototype._setPassphrase = function(password) {
this.storage.passphrase = password;
};
FakeStorage.prototype.setGlobal = function (id, payload) {
FakeStorage.prototype.setGlobal = function(id, payload) {
this.storage[id] = payload;
};
FakeStorage.prototype.getGlobal = function(id) {
return this.storage[id];
}
};
FakeStorage.prototype.set = function (wid, id, payload) {
this.storage[wid + '-' + id] = payload;
FakeStorage.prototype.removeGlobal = function(id) {
delete this.storage[id];
};
FakeStorage.prototype.set = function(wid, id, payload) {
this.storage[wid + '::' + id] = payload;
};
FakeStorage.prototype.get = function(wid, id) {
return this.storage[wid + '-' +id];
}
return this.storage[wid + '::' + id];
};
FakeStorage.prototype.clear = function() {
delete this['storage'];
}
};
FakeStorage.prototype.getWalletIds = function() {
var walletIds = [];
var uniq = {};
for (var ii in this.storage) {
var split = ii.split('::');
if (split.length == 2) {
var walletId = split[0];
if (walletId !== 'nameFor' && typeof uniq[walletId] === 'undefined') {
walletIds.push(walletId);
uniq[walletId] = 1;
}
}
}
return walletIds;
};
FakeStorage.prototype.deleteWallet = function(walletId) {
var toDelete = {};
toDelete['nameFor::' + walletId] = 1;
for (var key in this.storage) {
var split = key.split('::');
if (split.length == 2 && split[0] === walletId) {
toDelete[key] = 1;
}
}
for (var i in toDelete) {
this.removeGlobal(i);
}
};
FakeStorage.prototype.getName = function(walletId) {
return this.getGlobal('nameFor::' + walletId);
};
FakeStorage.prototype.setName = function(walletId, name) {
this.setGlobal('nameFor::' + walletId, name);
};
FakeStorage.prototype.getWallets = function() {
return [];
var wallets = [];
var ids = this.getWalletIds();
for (var i in ids) {
wallets.push({
id: ids[i],
name: this.getName(ids[i]),
});
}
return wallets;
};
FakeStorage.prototype.setFromObj = function(walletId, obj) {
for (var i in obj) {
this.storage[walletId + '-' + i] = obj[i];
};
for (var k in obj) {
this.set(walletId, k, obj[k]);
}
this.setName(walletId, obj.opts.name);
};
module.exports = require('soop')(FakeStorage);

View File

@ -1,21 +1,23 @@
var FakeWallet = function(){
this.balance=10000;
this.safeBalance=1000;
this.balanceByAddr={'1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC': 1000};
var FakeWallet = function() {
this.id = 'testID';
this.balance = 10000;
this.safeBalance = 1000;
this.balanceByAddr = {
'1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC': 1000
};
this.name = 'myTESTwullet';
};
FakeWallet.prototype.set = function(balance, safeBalance, balanceByAddr){
this.balance=balance;
FakeWallet.prototype.set = function(balance, safeBalance, balanceByAddr) {
this.balance = balance;
this.safeBalance = safeBalance;
this.balanceByAddr = balanceByAddr;
};
FakeWallet.prototype.getAddressesInfo=function(){
FakeWallet.prototype.getAddressesInfo = function() {
var ret = [];
for(var ii in this.balanceByAddr){
for (var ii in this.balanceByAddr) {
ret.push({
address: ii,
isChange: false,
@ -25,13 +27,20 @@ FakeWallet.prototype.getAddressesInfo=function(){
};
FakeWallet.prototype.getBalance=function(cb){
FakeWallet.prototype.getBalance = function(cb) {
return cb(null, this.balance, this.balanceByAddr, this.safeBalance);
};
FakeWallet.prototype.setEnc = function(enc) {
this.enc = enc;
};
FakeWallet.prototype.toEncryptedObj = function() {
return 'SUPERENCRYPTEDSICRITSTUFF';
return this.enc;
};
FakeWallet.prototype.disconnect = function() {
this.disconnectCalled = 1;
};
// This mock is meant for karma, module.exports is not necesary.

View File

@ -3,27 +3,17 @@
var chai = chai || require('chai');
var should = chai.should();
var copay = copay || require('../copay');
var sinon = require('sinon');
var FakeNetwork = require('./mocks/FakeNetwork');
var Insight = require('../js/models/blockchain/Insight');
var FakeBlockchain = require('./mocks/FakeBlockchain');
var FakeStorage = require('./mocks/FakeStorage');
try {
var copay = require('copay'); //browser
} catch (e) {
var copay = require('../copay'); //node
}
var WalletFactory = require('../js/models/core/WalletFactory');
var addCopayers = function(w) {
for (var i = 0; i < 4; i++) {
w.publicKeyRing.addCopayer();
}
};
describe('WalletFactory model', function() {
var config = {
Network: FakeNetwork,
Blockchain: Insight,
Blockchain: FakeBlockchain,
Storage: FakeStorage,
wallet: {
requiredCopayers: 3,
@ -38,12 +28,34 @@ describe('WalletFactory model', function() {
},
networkName: 'testnet',
passphrase: 'test',
storageObj: new FakeStorage(),
networkObj: new FakeNetwork(),
blockchainObj: new FakeBlockchain(),
};
it('should create the factory', function() {
var wf = new WalletFactory(config);
should.exist(wf);
beforeEach(function() {
config.storageObj.reset();
});
it('should create the factory', function() {
var wf = new WalletFactory(config, '0.0.1');
should.exist(wf);
wf.networkName.should.equal(config.networkName);
wf.walletDefaults.should.deep.equal(config.wallet);
wf.version.should.equal('0.0.1');
});
it('should log', function() {
var c2 = JSON.parse(JSON.stringify(config));
c2.verbose = 1;
var wf = new WalletFactory(c2, '0.0.1');
var spy = sinon.spy(console, 'log');
wf.log('ok');
sinon.assert.callCount(spy, 1);
spy.getCall(0).args[0].should.equal('ok');
});
it('#_checkRead should return false', function() {
var wf = new WalletFactory(config);
wf._checkRead('dummy').should.equal(false);
@ -75,7 +87,7 @@ describe('WalletFactory model', function() {
should.exist(w.publicKeyRing.getCopayerId);
should.exist(w.txProposals.toObj);
should.exist(w.privateKey.toObj);
JSON.stringify(w.toObj()).should.equal(o);
});
@ -83,7 +95,7 @@ describe('WalletFactory model', function() {
it('BIP32 length problem', function() {
var sconfig = {
Network: FakeNetwork,
Blockchain: Insight,
Blockchain: FakeBlockchain,
Storage: FakeStorage,
"networkName": "testnet",
"network": {
@ -101,7 +113,7 @@ describe('WalletFactory model', function() {
"wallet": {
"requiredCopayers": 2,
"totalCopayers": 3,
"reconnectDelay":100,
"reconnectDelay": 100,
"spendUnconfirmed": 1,
"verbose": 0
},
@ -114,7 +126,10 @@ describe('WalletFactory model', function() {
"port": 3001
},
"verbose": 0,
"themes": ["default"]
"themes": ["default"],
storageObj: new FakeStorage(),
networkObj: new FakeNetwork(),
blockchainObj: new FakeBlockchain(),
};
var wf = new WalletFactory(sconfig, '0.0.1');
var opts = {
@ -125,4 +140,30 @@ describe('WalletFactory model', function() {
});
it('should be able to get current wallets', function() {
var wf = new WalletFactory(config, '0.0.1');
var ws = wf.getWallets();
var w = wf.create({
name: 'test wallet'
});
var ws = wf.getWallets();
ws.length.should.equal(1);
ws[0].name.should.equal('test wallet');
});
it('should be able to delete wallet', function(done) {
var wf = new WalletFactory(config, '0.0.1');
var w = wf.create({
name: 'test wallet'
});
var ws = wf.getWallets();
ws.length.should.equal(1);
wf.delete(ws[0].id, function() {
ws = wf.getWallets();
ws.length.should.equal(0);
done();
});
});
});

View File

@ -1,6 +1,14 @@
//
// test/unit/controllers/controllersSpec.js
//
// Replace saveAs plugin
saveAsLastCall = null;
saveAs = function(o) {
saveAsLastCall = o;
};
describe("Unit: Controllers", function() {
var scope;
@ -9,6 +17,47 @@ describe("Unit: Controllers", function() {
beforeEach(module('copayApp.services'));
beforeEach(module('copayApp.controllers'));
var config = {
requiredCopayers: 3,
totalCopayers: 5,
spendUnconfirmed: 1,
reconnectDelay: 100,
networkName: 'testnet',
};
describe('Backup Controller', function() {
var ctrl;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
$rootScope.wallet = new FakeWallet(config);
ctrl = $controller('BackupController', {
$scope: scope,
$modal: {},
});
}));
it('Should have a Backup controller', function() {
expect(scope.title).equal('Backup');
});
it('Backup controller #download', function() {
scope.wallet.setEnc('1234567');
expect(saveAsLastCall).equal(null);
scope.download();
expect(saveAsLastCall.size).equal(7);
expect(saveAsLastCall.type).equal('text/plain;charset=utf-8');
});
it('Backup controller #delete', function() {
expect(scope.wallet).not.equal(undefined);
scope.deleteWallet();
expect(scope.wallet).equal(undefined);
});
});
describe('Address Controller', function() {
var addressCtrl;
beforeEach(inject(function($controller, $rootScope) {
@ -55,15 +104,15 @@ describe("Unit: Controllers", function() {
beforeEach(inject(function($controller, $injector) {
$httpBackend = $injector.get('$httpBackend');
$httpBackend.when('GET', GH)
.respond( [{
name: "v100.1.6",
zipball_url: "https://api.github.com/repos/bitpay/copay/zipball/v0.0.6",
tarball_url: "https://api.github.com/repos/bitpay/copay/tarball/v0.0.6",
commit: {
sha: "ead7352bf2eca705de58d8b2f46650691f2bc2c7",
url: "https://api.github.com/repos/bitpay/copay/commits/ead7352bf2eca705de58d8b2f46650691f2bc2c7"
}
}]);
.respond([{
name: "v100.1.6",
zipball_url: "https://api.github.com/repos/bitpay/copay/zipball/v0.0.6",
tarball_url: "https://api.github.com/repos/bitpay/copay/tarball/v0.0.6",
commit: {
sha: "ead7352bf2eca705de58d8b2f46650691f2bc2c7",
url: "https://api.github.com/repos/bitpay/copay/commits/ead7352bf2eca705de58d8b2f46650691f2bc2c7"
}
}]);
}));
var rootScope;
@ -86,32 +135,31 @@ describe("Unit: Controllers", function() {
$httpBackend.flush();
});
it('should hit github for version', function() {
$httpBackend.expectGET(GH);
scope.$apply();
$httpBackend.flush();
});
it('should hit github for version', function() {
$httpBackend.expectGET(GH);
scope.$apply();
$httpBackend.flush();
});
it('should check version ', function() {
$httpBackend.expectGET(GH);
scope.$apply();
$httpBackend.flush();
expect(scope.updateVersion.class).equal('error');
expect(scope.updateVersion.version).equal('v100.1.6');
});
it('should check version ', function() {
$httpBackend.expectGET(GH);
scope.$apply();
$httpBackend.flush();
expect(scope.updateVersion.class).equal('error');
expect(scope.updateVersion.version).equal('v100.1.6');
});
it('should check blockChainStatus', function() {
$httpBackend.expectGET(GH);
$httpBackend.flush();
rootScope.insightError=1;
scope.$apply();
expect(rootScope.insightError).equal(1);
scope.$apply();
expect(rootScope.insightError).equal(1);
scope.$apply();
});
it('should check blockChainStatus', function() {
$httpBackend.expectGET(GH);
$httpBackend.flush();
rootScope.insightError = 1;
scope.$apply();
expect(rootScope.insightError).equal(1);
scope.$apply();
expect(rootScope.insightError).equal(1);
scope.$apply();
});
});
});