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;
}
$location.path('/');
setTimeout(function() {
$timeout(function() {
notification.error('Success', 'Profile successfully deleted');
}, 1);
});
});
};
@ -73,10 +73,10 @@ angular.module('copayApp.controllers').controller('ProfileController', function(
identityService.deleteWallet($scope.item, function(err) {
if (err) {
$scope.loading = null;
$scope.error = err.message;
$scope.error = err.message || err;
copay.logger.warn(err);
}
else {
$timeout(function () { $scope.$digest(); });
} else {
$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');
};
Identity.prototype.getChecksumForStorage = function() {
return JSON.stringify(_.sortBy(this.walletIds));
};
Identity.prototype.getId = function() {
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
* @returns {Wallet}
@ -186,18 +206,22 @@ Identity.prototype.deleteWallet = function(walletId, cb) {
var self = this;
var w = this.getWalletById(walletId);
w.close();
delete this.wallets[walletId];
delete this.focusedTimestamps[walletId];
this.walletIds = _.without(this.walletIds, walletId);
this.storage.removeItem(Wallet.getStorageKey(walletId), function(err) {
self.verifyChecksum(function (err, match) {
if (err) return cb(err);
self.emitAndKeepAlive('walletDeleted', walletId);
self.store(null, cb);
if (!match) return cb('The profile is out of sync. Please re-login to get the latest changes.');
var w = self.getWalletById(walletId);
w.close();
delete self.wallets[walletId];
delete self.focusedTimestamps[walletId];
self.walletIds = _.without(self.walletIds, walletId);
self.storage.removeItem(Wallet.getStorageKey(walletId), function(err) {
if (err) return cb(err);
self.emitAndKeepAlive('walletDeleted', walletId);
self.store(null, cb);
});
});
};
@ -346,18 +370,19 @@ Identity.prototype.exportEncryptedWithWalletInfo = function(opts) {
return crypto.encrypt(this.password, this.exportWithWalletInfo(opts));
};
Identity.prototype.setBackupNeeded = function() {
this.backupNeeded = true;
this.store({
noWallets: true
}, function() {});
}
Identity.prototype.setBackupNeeded = function(backupNeeded) {
var self = this;
Identity.prototype.setBackupDone = function() {
this.backupNeeded = false;
this.store({
noWallets: true
}, function() {});
self.backupNeeded = !!backupNeeded;
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
}, function() {});
});
}
Identity.prototype.exportWithWalletInfo = function(opts) {
@ -598,71 +623,77 @@ Identity.prototype.bindWallet = function(w) {
*/
Identity.prototype.createWallet = function(opts, cb) {
preconditions.checkArgument(cb);
opts = opts || {};
opts.networkName = opts.networkName || 'testnet';
log.debug('### CREATING NEW WALLET.' + (opts.id ? ' USING ID: ' + opts.id : ' NEW ID') + (opts.privateKey ? ' USING PrivateKey: ' + opts.privateKey.getId() : ' NEW PrivateKey'));
var privOpts = {
networkName: opts.networkName,
};
if (opts.privateKeyHex && opts.privateKeyHex.length > 1) {
privOpts.extendedPrivateKeyString = opts.privateKeyHex;
}
opts.privateKey = opts.privateKey || new PrivateKey(privOpts);
var requiredCopayers = opts.requiredCopayers || this.walletDefaults.requiredCopayers;
var totalCopayers = opts.totalCopayers || this.walletDefaults.totalCopayers;
opts.lockTimeoutMin = this.walletDefaults.idleDurationMin;
opts.publicKeyRing = opts.publicKeyRing || new PublicKeyRing({
networkName: opts.networkName,
requiredCopayers: requiredCopayers,
totalCopayers: totalCopayers,
});
opts.publicKeyRing.addCopayer(
opts.privateKey.deriveBIP45Branch().extendedPublicKeyString(),
opts.nickname || this.getName()
);
log.debug('\t### PublicKeyRing Initialized');
opts.txProposals = opts.txProposals || new TxProposals({
networkName: opts.networkName,
});
var walletClass = opts.walletClass || Wallet;
log.debug('\t### TxProposals Initialized');
opts.networkOpts = this.networkOpts;
opts.blockchainOpts = this.blockchainOpts;
opts.spendUnconfirmed = opts.spendUnconfirmed || this.walletDefaults.spendUnconfirmed;
opts.reconnectDelay = opts.reconnectDelay || this.walletDefaults.reconnectDelay;
opts.requiredCopayers = requiredCopayers;
opts.totalCopayers = totalCopayers;
opts.version = opts.version || this.version;
var self = this;
var w = new walletClass(opts);
if (self.getWalletById(w.getId())) {
return cb('walletAlreadyExists');
}
self.addWallet(w);
self.updateFocusedTimestamp(w.getId());
self.bindWallet(w);
self.storeWallet(w, function(err) {
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.backupNeeded = true;
self.store({
noWallets: true,
}, function(err) {
return cb(err, w);
opts = opts || {};
opts.networkName = opts.networkName || 'testnet';
log.debug('### CREATING NEW WALLET.' + (opts.id ? ' USING ID: ' + opts.id : ' NEW ID') + (opts.privateKey ? ' USING PrivateKey: ' + opts.privateKey.getId() : ' NEW PrivateKey'));
var privOpts = {
networkName: opts.networkName,
};
if (opts.privateKeyHex && opts.privateKeyHex.length > 1) {
privOpts.extendedPrivateKeyString = opts.privateKeyHex;
}
opts.privateKey = opts.privateKey || new PrivateKey(privOpts);
var requiredCopayers = opts.requiredCopayers || self.walletDefaults.requiredCopayers;
var totalCopayers = opts.totalCopayers || self.walletDefaults.totalCopayers;
opts.lockTimeoutMin = self.walletDefaults.idleDurationMin;
opts.publicKeyRing = opts.publicKeyRing || new PublicKeyRing({
networkName: opts.networkName,
requiredCopayers: requiredCopayers,
totalCopayers: totalCopayers,
});
opts.publicKeyRing.addCopayer(
opts.privateKey.deriveBIP45Branch().extendedPublicKeyString(),
opts.nickname || self.getName()
);
log.debug('\t### PublicKeyRing Initialized');
opts.txProposals = opts.txProposals || new TxProposals({
networkName: opts.networkName,
});
var walletClass = opts.walletClass || Wallet;
log.debug('\t### TxProposals Initialized');
opts.networkOpts = self.networkOpts;
opts.blockchainOpts = self.blockchainOpts;
opts.spendUnconfirmed = opts.spendUnconfirmed || self.walletDefaults.spendUnconfirmed;
opts.reconnectDelay = opts.reconnectDelay || self.walletDefaults.reconnectDelay;
opts.requiredCopayers = requiredCopayers;
opts.totalCopayers = totalCopayers;
opts.version = opts.version || self.version;
var w = new walletClass(opts);
if (self.getWalletById(w.getId())) {
return cb('walletAlreadyExists');
}
self.addWallet(w);
self.updateFocusedTimestamp(w.getId());
self.bindWallet(w);
self.storeWallet(w, function(err) {
if (err) return cb(err);
self.backupNeeded = true;
self.store({
noWallets: true,
}, function(err) {
return cb(err, w);
});
});
});
};

View file

@ -72,7 +72,7 @@ BackupService.prototype.walletDownload = function(wallet) {
};
BackupService.prototype.profileEncrypted = function(iden) {
iden.setBackupDone();
iden.setBackupNeeded(false);
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) {
var priv = 'tprv8ZgxMBicQKsPdEqHcA7RjJTayxA3gSSqeRTttS1JjVbgmNDZdSk9EHZK5pc52GY5xFmwcakmUeKWUDzGoMLGAhrfr5b3MovMUZUTPqisL2m';
args.storage.getItem = sinon.stub().yields(null, JSON.stringify(iden));
args.storage.setItem = sinon.stub();
args.storage.setItem.onFirstCall().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) {
args.storage.getItem = sinon.stub().yields(null, JSON.stringify(iden));
args.storage.setItem = sinon.stub();
args.storage.setItem.onCall(0).callsArg(2);
args.storage.setItem.onCall(1).callsArg(2);
@ -351,7 +353,8 @@ describe('Identity model', function() {
walletClass: walletClass,
}, function(err, w1) {
should.exist(w1);
args.storage.getItem = sinon.stub().yields(null, JSON.stringify(iden));
iden.createWallet({
walletClass: walletClass,
}, function(err, w2) {
@ -622,6 +625,7 @@ describe('Identity model', function() {
it('should delete wallet', function(done) {
iden.addWallet(w);
iden.getWalletById('32').getName().should.equal('treintaydos');
iden.storage.getItem = sinon.stub().yields(null, JSON.stringify(iden));
iden.deleteWallet('32', function(err) {
should.not.exist(iden.getWalletById('32'));
iden.walletIds.should.deep.equal([]);