verify checksum on actions that store the profile

This commit is contained in:
Ivan Socolsky 2014-12-30 15:49:19 -03:00
commit f2cda099f9
2 changed files with 84 additions and 73 deletions

View file

@ -77,7 +77,7 @@ Identity.getKeyForEmail = function(email) {
}; };
Identity.prototype.getChecksumForStorage = function() { Identity.prototype.getChecksumForStorage = function() {
return JSON.stringify(this.walletIds); return JSON.stringify(_.sortBy(this.walletIds));
}; };
Identity.prototype.getId = function() { Identity.prototype.getId = function() {
@ -163,8 +163,8 @@ Identity.prototype.verifyChecksum = function (cb) {
} catch (e) { } catch (e) {
return cb(e); return cb(e);
} }
return cb(null, self.getChecksumForStorage() == self.getChecksumForStorage.call(iden)); return cb(null, self.getChecksumForStorage() == self.getChecksumForStorage.call(iden));
});
}; };
@ -206,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');
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);
}); });
});
}; };
@ -366,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');
self.store({
noWallets: true noWallets: true
}, function() {}); }, function() {});
});
} }
Identity.prototype.exportWithWalletInfo = function(opts) { Identity.prototype.exportWithWalletInfo = function(opts) {
@ -619,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');
opts = opts || {}; opts = opts || {};
opts.networkName = opts.networkName || 'testnet'; opts.networkName = opts.networkName || 'testnet';
@ -634,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,
@ -645,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');
@ -657,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())) {
@ -685,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);
} }