diff --git a/js/controllers/import.js b/js/controllers/import.js index 190c37c80..de127507c 100644 --- a/js/controllers/import.js +++ b/js/controllers/import.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('copayApp.controllers').controller('ImportController', - function($scope, $rootScope, $location, controllerUtils, Passphrase, notification, isMobile) { + function($scope, $rootScope, $location, controllerUtils, Passphrase, notification, isMobile, Compatibility) { $rootScope.title = 'Import a backup'; $scope.importStatus = 'Importing wallet - Reading backup...'; @@ -94,7 +94,11 @@ angular.module('copayApp.controllers').controller('ImportController', reader.readAsBinaryString(backupFile); } else { - _importBackup(backupText); + try { + _importBackup(backupText); + } catch(e) { + Compatibility.preDotEightImportWalletToStorage(backupText, $scope.password, $scope.skipPublicKeyRing, $scope.skipTxProposals); + } } }; }); diff --git a/js/models/Compatibility.js b/js/models/Compatibility.js new file mode 100644 index 000000000..d8222d615 --- /dev/null +++ b/js/models/Compatibility.js @@ -0,0 +1,94 @@ +'use strict'; +var Identity = require('Identity'), + Passphrase = require('Passphrase'), + Wallet = require('Wallet'), +// walletFactory = new WalletFactory(), + passphrase = new Passphrase(); + +function Compatibility(){ +// - preDotEightListWallets() +// - preDotEightImportWalletToStorage(walletId, passphrase, profile) (edited) +} + +Compatibility.prototype.preDotEightListWallets = function () {}; + + +Compatibility.prototype.preDotEightImportWalletToStorage = function(encryptedObj, password, skipPublicKeyRing, skipTxProposals) { + passphrase.getBase64Async(password, function(passphrase) { +// updateStatus('Importing wallet - Setting things up...'); + var w, errMsg; + + var skipFields = []; + if (skipPublicKeyRing) + skipFields.push('publicKeyRing'); + + if (skipTxProposals) + skipFields.push('txProposals'); + + // try to import encrypted wallet with passphrase + try { + w = walletFactory.import(encryptedObj, passphrase, skipFields); + } catch (e) { + errMsg = e.message; + } + + if (!w) { +// $scope.loading = false; +// notification.error('Error', errMsg || 'Wrong password'); + $rootScope.$digest(); + return; + } + + // if wallet was never used, we're done + if (!w.isReady()) { + $rootScope.wallet = w; +// controllerUtils.startNetwork($rootScope.wallet, $scope); + return; + } + + // if it was used, we need to scan for indices + w.updateIndexes(function(err) { +// updateStatus('Importing wallet - We are almost there...'); + if (err) { +// $scope.loading = false; +// notification.error('Error', 'Error updating indexes: ' + err); + } + $rootScope.wallet = w; +// controllerUtils.startNetwork($rootScope.wallet, $scope); + }); + }); +}; + +Compatibility.prototype.fromEncryptedObj = function(base64, passphrase, skipFields) { + this.storage.setPassphrase(passphrase); + var walletObj = this.storage.import(base64); + if (!walletObj) return false; + return this.fromObj(walletObj, skipFields); +}; + +Compatibility.prototype.fromObj = function(inObj, skipFields) { + var networkName = this.obtainNetworkName(inObj); + preconditions.checkState(networkName); + preconditions.checkArgument(inObj); + + var obj = JSON.parse(JSON.stringify(inObj)); + + // not stored options + obj.opts = obj.opts || {}; + obj.opts.reconnectDelay = this.walletDefaults.reconnectDelay; + + skipFields = skipFields || []; + skipFields.forEach(function(k) { + if (obj[k]) { + delete obj[k]; + } else + throw new Error('unknown field:' + k); + }); + + var w = Wallet.fromObj(obj, this.storage, this.networks[networkName], this.blockchains[networkName]); + if (!w) return false; + this._checkVersion(w.version); + return w; +}; + +module.exports = Compatibility; diff --git a/util/build.js b/util/build.js index 86638335e..e20961b98 100644 --- a/util/build.js +++ b/util/build.js @@ -69,6 +69,9 @@ var createBundle = function(opts) { b.require('./js/models/Insight', { expose: '../js/models/Insight' }); + b.require('./js/models/Compatibility', { + expose: '../js/models/Compatibility' + }); b.require('./js/models/PrivateKey', { expose: '../js/models/PrivateKey' });