Merge pull request #5505 from ajp8164/bug/sync-storage-upgrade
Synchronize all storage upgrade.
This commit is contained in:
commit
901f8e9f57
2 changed files with 96 additions and 15 deletions
|
|
@ -44,7 +44,7 @@ angular.module('copayApp.controllers').controller('bitpayCardIntroController', f
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
appIdentityService.getIdentity(bitpayService.getEnvironment(), function(err, appIdentity) {
|
appIdentityService.getIdentity(bitpayService.getEnvironment().network, function(err, appIdentity) {
|
||||||
if (err) popupService.showAlert(null, err);
|
if (err) popupService.showAlert(null, err);
|
||||||
else $log.info('App identity: OK');
|
else $log.info('App identity: OK');
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,8 @@ angular.module('copayApp.services')
|
||||||
var _upgraders = {
|
var _upgraders = {
|
||||||
'00_bitpayDebitCards' : _upgrade_bitpayDebitCards, // 2016-11: Upgrade bitpayDebitCards-x to bitpayAccounts-x
|
'00_bitpayDebitCards' : _upgrade_bitpayDebitCards, // 2016-11: Upgrade bitpayDebitCards-x to bitpayAccounts-x
|
||||||
'01_bitpayCardCredentials' : _upgrade_bitpayCardCredentials, // 2016-11: Upgrade bitpayCardCredentials-x to appIdentity-x
|
'01_bitpayCardCredentials' : _upgrade_bitpayCardCredentials, // 2016-11: Upgrade bitpayCardCredentials-x to appIdentity-x
|
||||||
'02_bitpayAccounts' : _upgrade_bitpayAccounts // 2016-12: Upgrade tpayAccounts-x to bitpayAccounts-v2-x
|
'02_bitpayAccounts' : _upgrade_bitpayAccounts, // 2016-12: Upgrade bitpayAccounts-x to bitpayAccounts-v2-x
|
||||||
|
'03_bitpayAccounts-v2' : _validate_bitpayAccounts_v2 // 2017-01: Validate keys on bitpayAccounts-v2-x, remove if not valid
|
||||||
};
|
};
|
||||||
|
|
||||||
function _upgrade_bitpayDebitCards(key, network, cb) {
|
function _upgrade_bitpayDebitCards(key, network, cb) {
|
||||||
|
|
@ -156,12 +157,13 @@ angular.module('copayApp.services')
|
||||||
}
|
}
|
||||||
data = data || {};
|
data = data || {};
|
||||||
var upgraded = '';
|
var upgraded = '';
|
||||||
Object.keys(data).forEach(function(key) {
|
_asyncEach(Object.keys(data), function(key, callback) {
|
||||||
// Keys are account emails
|
// Keys are account emails
|
||||||
if (!data[key]['bitpayApi-' + network]) {
|
if (!data[key]['bitpayApi-' + network]) {
|
||||||
// Needs upgrade
|
// Needs upgrade
|
||||||
upgraded += ' ' + key;
|
upgraded += ' ' + key;
|
||||||
var acctData = {
|
var acctData = {
|
||||||
|
acct: data[key],
|
||||||
token: data[key]['bitpayDebitCards-' + network].token,
|
token: data[key]['bitpayDebitCards-' + network].token,
|
||||||
email: key
|
email: key
|
||||||
};
|
};
|
||||||
|
|
@ -170,17 +172,73 @@ angular.module('copayApp.services')
|
||||||
|
|
||||||
_02_setBitpayDebitCards(network, data[key]['bitpayDebitCards-' + network], function(err) {
|
_02_setBitpayDebitCards(network, data[key]['bitpayDebitCards-' + network], function(err) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
callback();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}, function() {
|
||||||
|
// done
|
||||||
|
// Remove obsolete key.
|
||||||
|
storage.remove('bitpayAccounts-' + network, function() {
|
||||||
|
if (upgraded.length > 0) {
|
||||||
|
cb(null, 'upgraded to \'bitpayAccounts-v2-' + network + '\':' + upgraded);
|
||||||
|
} else {
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
// Remove obsolete key.
|
});
|
||||||
storage.remove('bitpayAccounts-' + network, function() {
|
};
|
||||||
if (upgraded.length > 0) {
|
|
||||||
cb(null, 'upgraded to \'bitpayAccounts-v2-' + network + '\':' + upgraded);
|
function _validate_bitpayAccounts_v2(key, network, cb) {
|
||||||
|
key += '-' + network;
|
||||||
|
storage.get(key, function(err, data) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
if (lodash.isString(data)) {
|
||||||
|
data = JSON.parse(data);
|
||||||
|
}
|
||||||
|
data = data || {};
|
||||||
|
var verified = '';
|
||||||
|
var toRemove = [];
|
||||||
|
_asyncEach(Object.keys(data), function(key, callback) {
|
||||||
|
// Verify account API data
|
||||||
|
if (!data[key]['bitpayApi-' + network] ||
|
||||||
|
!data[key]['bitpayApi-' + network].token) {
|
||||||
|
// Invalid entry - one or more keys are missing
|
||||||
|
toRemove.push(key);
|
||||||
|
return callback();
|
||||||
|
}
|
||||||
|
// Verify debit cards
|
||||||
|
if (Array.isArray(data[key]['bitpayDebitCards-' + network])) {
|
||||||
|
for (var i = 0; i < data[key]['bitpayDebitCards-' + network].length; i++) {
|
||||||
|
if (!data[key]['bitpayDebitCards-' + network][i].token ||
|
||||||
|
!data[key]['bitpayDebitCards-' + network][i].eid ||
|
||||||
|
!data[key]['bitpayDebitCards-' + network][i].id ||
|
||||||
|
!data[key]['bitpayDebitCards-' + network][i].lastFourDigits) {
|
||||||
|
// Invalid entry - one or more keys are missing
|
||||||
|
toRemove.push(key);
|
||||||
|
return callback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
verified += ' ' + key;
|
||||||
|
return callback();
|
||||||
|
}, function() {
|
||||||
|
// done, remove invalid account entrys
|
||||||
|
if (toRemove.length > 0) {
|
||||||
|
var removed = '';
|
||||||
|
for (var i = 0; i < toRemove.length; i++) {
|
||||||
|
removed += ' ' + toRemove[i];
|
||||||
|
delete data[toRemove[i]];
|
||||||
|
}
|
||||||
|
storage.set('bitpayAccounts-v2-' + network, JSON.stringify(data), function(err) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
cb(null, 'removed invalid account records, please re-pair cards for these accounts:' + removed + '; ' +
|
||||||
|
'the following accounts validated OK: ' + (verified.length > 0 ? verified : 'none'));
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
cb();
|
cb(null, (verified.length > 0 ? 'accounts OK: ' + verified : ''));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
@ -217,15 +275,15 @@ angular.module('copayApp.services')
|
||||||
data = JSON.parse(data);
|
data = JSON.parse(data);
|
||||||
}
|
}
|
||||||
data = data || {};
|
data = data || {};
|
||||||
if (lodash.isEmpty(data) || !data.email) return cb('No account to set');
|
if (lodash.isEmpty(data) || !data.email || !data.acct) return cb('No account to set');
|
||||||
storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) {
|
storage.get('bitpayAccounts-v2-' + network, function(err, bitpayAccounts) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
if (lodash.isString(bitpayAccounts)) {
|
if (lodash.isString(bitpayAccounts)) {
|
||||||
bitpayAccounts = JSON.parse(bitpayAccounts);
|
bitpayAccounts = JSON.parse(bitpayAccounts);
|
||||||
}
|
}
|
||||||
bitpayAccounts = bitpayAccounts || {};
|
bitpayAccounts = bitpayAccounts || {};
|
||||||
bitpayAccounts[data.email] = bitpayAccounts[data.email] || {};
|
bitpayAccounts[data.email] = data.acct;
|
||||||
bitpayAccounts[data.email]['bitpayApi-' + network] = bitpayAccounts[data.email]['bitpayApi-' + network] || {};
|
bitpayAccounts[data.email]['bitpayApi-' + network] = {};
|
||||||
bitpayAccounts[data.email]['bitpayApi-' + network].token = data.token;
|
bitpayAccounts[data.email]['bitpayApi-' + network].token = data.token;
|
||||||
storage.set('bitpayAccounts-v2-' + network, JSON.stringify(bitpayAccounts), cb);
|
storage.set('bitpayAccounts-v2-' + network, JSON.stringify(bitpayAccounts), cb);
|
||||||
});
|
});
|
||||||
|
|
@ -267,12 +325,14 @@ angular.module('copayApp.services')
|
||||||
$log.info('Storage upgraded for \'' + key + '\': ' + msg);
|
$log.info('Storage upgraded for \'' + key + '\': ' + msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// IMPORTANT: This function is designed to block execution until it completes.
|
||||||
|
// Ideally storage should not be used until it has been verified.
|
||||||
function _upgrade(cb) {
|
function _upgrade(cb) {
|
||||||
var errorCount = 0;
|
var errorCount = 0;
|
||||||
var errorMessage = undefined;
|
var errorMessage = undefined;
|
||||||
var keys = Object.keys(_upgraders).sort();
|
var keys = Object.keys(_upgraders).sort();
|
||||||
var networks = ['livenet', 'testnet'];
|
var networks = ['livenet', 'testnet'];
|
||||||
keys.forEach(function(key) {
|
_asyncEach(keys, function(key, callback) {
|
||||||
networks.forEach(function(network) {
|
networks.forEach(function(network) {
|
||||||
var storagekey = key.split('_')[1];
|
var storagekey = key.split('_')[1];
|
||||||
_upgraders[key](storagekey, network, function(err, msg) {
|
_upgraders[key](storagekey, network, function(err, msg) {
|
||||||
|
|
@ -282,10 +342,31 @@ angular.module('copayApp.services')
|
||||||
errorMessage = errorCount + ' storage upgrade failures';
|
errorMessage = errorCount + ' storage upgrade failures';
|
||||||
}
|
}
|
||||||
if (msg) _handleUpgradeSuccess(storagekey + '-' + network, msg);
|
if (msg) _handleUpgradeSuccess(storagekey + '-' + network, msg);
|
||||||
|
callback();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}, function() {
|
||||||
|
//done
|
||||||
|
cb(errorMessage);
|
||||||
});
|
});
|
||||||
cb(errorMessage);
|
};
|
||||||
|
|
||||||
|
function _asyncEach(iterableList, callback, done) {
|
||||||
|
var i = -1;
|
||||||
|
var length = iterableList.length;
|
||||||
|
|
||||||
|
function loop() {
|
||||||
|
i++;
|
||||||
|
if (i === length) {
|
||||||
|
done();
|
||||||
|
return;
|
||||||
|
} else if (i < length) {
|
||||||
|
callback(iterableList[i], loop);
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loop();
|
||||||
};
|
};
|
||||||
|
|
||||||
root.tryToMigrate = function(cb) {
|
root.tryToMigrate = function(cb) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue