Merge pull request #2253 from isocolsky/checksum

Verify if profile is in sync before saving changes
This commit is contained in:
Matias Alejo Garcia 2014-12-30 18:05:10 -03:00
commit d0d7397746
4 changed files with 112 additions and 77 deletions

View file

@ -52,9 +52,9 @@ angular.module('copayApp.controllers').controller('ProfileController', function(
return; return;
} }
$location.path('/'); $location.path('/');
setTimeout(function() { $timeout(function() {
notification.error('Success', 'Profile successfully deleted'); notification.error('Success', 'Profile successfully deleted');
}, 1); });
}); });
}; };
@ -73,10 +73,10 @@ angular.module('copayApp.controllers').controller('ProfileController', function(
identityService.deleteWallet($scope.item, function(err) { identityService.deleteWallet($scope.item, function(err) {
if (err) { if (err) {
$scope.loading = null; $scope.loading = null;
$scope.error = err.message; $scope.error = err.message || err;
copay.logger.warn(err); copay.logger.warn(err);
} $timeout(function () { $scope.$digest(); });
else { } else {
$modalInstance.close($scope.item.name || $scope.item.id); $modalInstance.close($scope.item.name || $scope.item.id);
} }
}); });

View file

@ -76,6 +76,10 @@ Identity.getKeyForEmail = function(email) {
return Identity.getStoragePrefix() + bitcore.util.sha256ripe160(email).toString('hex'); return Identity.getStoragePrefix() + bitcore.util.sha256ripe160(email).toString('hex');
}; };
Identity.prototype.getChecksumForStorage = function() {
return JSON.stringify(_.sortBy(this.walletIds));
};
Identity.prototype.getId = function() { Identity.prototype.getId = function() {
return Identity.getKeyForEmail(this.email); return Identity.getKeyForEmail(this.email);
}; };
@ -148,6 +152,22 @@ Identity.open = function(opts, cb) {
}); });
}; };
Identity.prototype.verifyChecksum = function (cb) {
var self = this;
self.storage.getItem(Identity.getKeyForEmail(self.email), function(err, data, headers) {
var iden;
if (err) return cb(err);
try {
iden = JSON.parse(data);
} catch (e) {
return cb(e);
}
return cb(null, self.getChecksumForStorage() == self.getChecksumForStorage.call(iden));
});
};
/** /**
* @param {string} walletId * @param {string} walletId
* @returns {Wallet} * @returns {Wallet}
@ -186,19 +206,23 @@ Identity.prototype.deleteWallet = function(walletId, cb) {
var self = this; var self = this;
self.verifyChecksum(function (err, match) {
if (err) return cb(err);
if (!match) return cb('The profile is out of sync. Please re-login to get the latest changes.');
var w = this.getWalletById(walletId); var w = self.getWalletById(walletId);
w.close(); w.close();
delete this.wallets[walletId]; delete self.wallets[walletId];
delete this.focusedTimestamps[walletId]; delete self.focusedTimestamps[walletId];
this.walletIds = _.without(this.walletIds, walletId); self.walletIds = _.without(self.walletIds, walletId);
this.storage.removeItem(Wallet.getStorageKey(walletId), function(err) { self.storage.removeItem(Wallet.getStorageKey(walletId), function(err) {
if (err) return cb(err); if (err) return cb(err);
self.emitAndKeepAlive('walletDeleted', walletId); self.emitAndKeepAlive('walletDeleted', walletId);
self.store(null, cb); self.store(null, cb);
}); });
});
}; };
@ -346,18 +370,19 @@ Identity.prototype.exportEncryptedWithWalletInfo = function(opts) {
return crypto.encrypt(this.password, this.exportWithWalletInfo(opts)); return crypto.encrypt(this.password, this.exportWithWalletInfo(opts));
}; };
Identity.prototype.setBackupNeeded = function() { Identity.prototype.setBackupNeeded = function(backupNeeded) {
this.backupNeeded = true; var self = this;
this.store({
noWallets: true
}, function() {});
}
Identity.prototype.setBackupDone = function() { self.backupNeeded = !!backupNeeded;
this.backupNeeded = false;
this.store({ self.verifyChecksum(function (err, match) {
if (err) return cb(err);
if (!match) return cb('The profile is out of sync. Please re-login to get the latest changes.');
self.store({
noWallets: true noWallets: true
}, function() {}); }, function() {});
});
} }
Identity.prototype.exportWithWalletInfo = function(opts) { Identity.prototype.exportWithWalletInfo = function(opts) {
@ -599,6 +624,12 @@ Identity.prototype.bindWallet = function(w) {
Identity.prototype.createWallet = function(opts, cb) { Identity.prototype.createWallet = function(opts, cb) {
preconditions.checkArgument(cb); preconditions.checkArgument(cb);
var self = this;
self.verifyChecksum(function (err, match) {
if (err) return cb(err);
if (!match) return cb('The profile is out of sync. Please re-login to get the latest changes.');
opts = opts || {}; opts = opts || {};
opts.networkName = opts.networkName || 'testnet'; opts.networkName = opts.networkName || 'testnet';
@ -614,9 +645,9 @@ Identity.prototype.createWallet = function(opts, cb) {
opts.privateKey = opts.privateKey || new PrivateKey(privOpts); opts.privateKey = opts.privateKey || new PrivateKey(privOpts);
var requiredCopayers = opts.requiredCopayers || this.walletDefaults.requiredCopayers; var requiredCopayers = opts.requiredCopayers || self.walletDefaults.requiredCopayers;
var totalCopayers = opts.totalCopayers || this.walletDefaults.totalCopayers; var totalCopayers = opts.totalCopayers || self.walletDefaults.totalCopayers;
opts.lockTimeoutMin = this.walletDefaults.idleDurationMin; opts.lockTimeoutMin = self.walletDefaults.idleDurationMin;
opts.publicKeyRing = opts.publicKeyRing || new PublicKeyRing({ opts.publicKeyRing = opts.publicKeyRing || new PublicKeyRing({
networkName: opts.networkName, networkName: opts.networkName,
@ -625,7 +656,7 @@ Identity.prototype.createWallet = function(opts, cb) {
}); });
opts.publicKeyRing.addCopayer( opts.publicKeyRing.addCopayer(
opts.privateKey.deriveBIP45Branch().extendedPublicKeyString(), opts.privateKey.deriveBIP45Branch().extendedPublicKeyString(),
opts.nickname || this.getName() opts.nickname || self.getName()
); );
log.debug('\t### PublicKeyRing Initialized'); log.debug('\t### PublicKeyRing Initialized');
@ -637,16 +668,15 @@ Identity.prototype.createWallet = function(opts, cb) {
log.debug('\t### TxProposals Initialized'); log.debug('\t### TxProposals Initialized');
opts.networkOpts = this.networkOpts; opts.networkOpts = self.networkOpts;
opts.blockchainOpts = this.blockchainOpts; opts.blockchainOpts = self.blockchainOpts;
opts.spendUnconfirmed = opts.spendUnconfirmed || this.walletDefaults.spendUnconfirmed; opts.spendUnconfirmed = opts.spendUnconfirmed || self.walletDefaults.spendUnconfirmed;
opts.reconnectDelay = opts.reconnectDelay || this.walletDefaults.reconnectDelay; opts.reconnectDelay = opts.reconnectDelay || self.walletDefaults.reconnectDelay;
opts.requiredCopayers = requiredCopayers; opts.requiredCopayers = requiredCopayers;
opts.totalCopayers = totalCopayers; opts.totalCopayers = totalCopayers;
opts.version = opts.version || this.version; opts.version = opts.version || self.version;
var self = this;
var w = new walletClass(opts); var w = new walletClass(opts);
if (self.getWalletById(w.getId())) { if (self.getWalletById(w.getId())) {
@ -665,6 +695,7 @@ Identity.prototype.createWallet = function(opts, cb) {
return cb(err, w); return cb(err, w);
}); });
}); });
});
}; };
/** /**

View file

@ -72,7 +72,7 @@ BackupService.prototype.walletDownload = function(wallet) {
}; };
BackupService.prototype.profileEncrypted = function(iden) { BackupService.prototype.profileEncrypted = function(iden) {
iden.setBackupDone(); iden.setBackupNeeded(false);
return iden.exportEncryptedWithWalletInfo(iden.password); return iden.exportEncryptedWithWalletInfo(iden.password);
} }

View file

@ -327,6 +327,7 @@ describe('Identity model', function() {
it('should be able to create wallets with given pk', function(done) { it('should be able to create wallets with given pk', function(done) {
var priv = 'tprv8ZgxMBicQKsPdEqHcA7RjJTayxA3gSSqeRTttS1JjVbgmNDZdSk9EHZK5pc52GY5xFmwcakmUeKWUDzGoMLGAhrfr5b3MovMUZUTPqisL2m'; var priv = 'tprv8ZgxMBicQKsPdEqHcA7RjJTayxA3gSSqeRTttS1JjVbgmNDZdSk9EHZK5pc52GY5xFmwcakmUeKWUDzGoMLGAhrfr5b3MovMUZUTPqisL2m';
args.storage.getItem = sinon.stub().yields(null, JSON.stringify(iden));
args.storage.setItem = sinon.stub(); args.storage.setItem = sinon.stub();
args.storage.setItem.onFirstCall().callsArg(2); args.storage.setItem.onFirstCall().callsArg(2);
args.storage.setItem.onSecondCall().callsArg(2); args.storage.setItem.onSecondCall().callsArg(2);
@ -342,6 +343,7 @@ describe('Identity model', function() {
}); });
it('should be able to create wallets with random pk', function(done) { it('should be able to create wallets with random pk', function(done) {
args.storage.getItem = sinon.stub().yields(null, JSON.stringify(iden));
args.storage.setItem = sinon.stub(); args.storage.setItem = sinon.stub();
args.storage.setItem.onCall(0).callsArg(2); args.storage.setItem.onCall(0).callsArg(2);
args.storage.setItem.onCall(1).callsArg(2); args.storage.setItem.onCall(1).callsArg(2);
@ -352,6 +354,7 @@ describe('Identity model', function() {
}, function(err, w1) { }, function(err, w1) {
should.exist(w1); should.exist(w1);
args.storage.getItem = sinon.stub().yields(null, JSON.stringify(iden));
iden.createWallet({ iden.createWallet({
walletClass: walletClass, walletClass: walletClass,
}, function(err, w2) { }, function(err, w2) {
@ -622,6 +625,7 @@ describe('Identity model', function() {
it('should delete wallet', function(done) { it('should delete wallet', function(done) {
iden.addWallet(w); iden.addWallet(w);
iden.getWalletById('32').getName().should.equal('treintaydos'); iden.getWalletById('32').getName().should.equal('treintaydos');
iden.storage.getItem = sinon.stub().yields(null, JSON.stringify(iden));
iden.deleteWallet('32', function(err) { iden.deleteWallet('32', function(err) {
should.not.exist(iden.getWalletById('32')); should.not.exist(iden.getWalletById('32'));
iden.walletIds.should.deep.equal([]); iden.walletIds.should.deep.equal([]);