Wallet/src/js/controllers/import.js

203 lines
5.4 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('importController',
function($scope, $rootScope, $location, $timeout, $log, profileService, notification, go, isMobile, isCordova, sjcl, gettext, lodash, ledger) {
2015-03-06 12:00:10 -03:00
var self = this;
this.isSafari = isMobile.Safari();
this.isCordova = isCordova;
this.externalIndexValues = lodash.range(0, ledger.MAX_SLOT);
$scope.externalIndex = 0;
2015-03-06 12:00:10 -03:00
var reader = new FileReader();
window.ignoreMobilePause = true;
$scope.$on('$destroy', function() {
2015-08-24 17:09:59 -03:00
$timeout(function() {
2015-03-06 12:00:10 -03:00
window.ignoreMobilePause = false;
}, 100);
});
2015-08-24 17:09:59 -03:00
this.setType = function(type) {
$scope.type = type;
2015-09-02 15:56:00 -03:00
this.error = null;
2015-08-24 17:09:59 -03:00
$timeout(function() {
$rootScope.$apply();
});
};
var _importBlob = function(str, opts) {
var str2, err;
2015-03-06 12:00:10 -03:00
try {
2015-08-24 17:09:59 -03:00
str2 = sjcl.decrypt(self.password, str);
2015-03-06 12:00:10 -03:00
} catch (e) {
err = gettext('Could not decrypt file, check your password');
2015-03-06 12:00:10 -03:00
$log.warn(e);
};
if (err) {
self.error = err;
2015-08-24 17:09:59 -03:00
$timeout(function() {
$rootScope.$apply();
});
return;
}
2015-03-06 12:00:10 -03:00
self.loading = true;
$timeout(function() {
profileService.importWallet(str2, {
compressed: null,
password: null
}, function(err, walletId) {
self.loading = false;
if (err) {
self.error = err;
2015-08-24 17:09:59 -03:00
} else {
2015-04-18 07:23:11 -03:00
$rootScope.$emit('Local/WalletImported', walletId);
2015-03-06 12:00:10 -03:00
go.walletHome();
notification.success(gettext('Success'), gettext('Your wallet has been imported correctly'));
2015-03-06 12:00:10 -03:00
}
});
}, 100);
};
2015-08-24 17:09:59 -03:00
2015-09-03 01:49:48 -03:00
var _importMnemonic = function(words, opts) {
2015-08-24 17:09:59 -03:00
self.loading = true;
$timeout(function() {
profileService.importWalletMnemonic(words, opts, function(err, walletId) {
2015-08-24 17:09:59 -03:00
self.loading = false;
if (err) {
self.error = err;
return $timeout(function() {
$scope.$apply();
});
}
$rootScope.$emit('Local/WalletImported', walletId);
notification.success(gettext('Success'), gettext('Your wallet has been imported correctly'));
go.walletHome();
});
}, 100);
};
2015-03-06 12:00:10 -03:00
$scope.getFile = function() {
// If we use onloadend, we need to check the readyState.
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
2015-08-24 17:09:59 -03:00
_importBlob(evt.target.result);
2015-03-06 12:00:10 -03:00
}
}
};
2015-08-24 17:09:59 -03:00
this.importBlob = function(form) {
2015-03-06 12:00:10 -03:00
if (form.$invalid) {
this.error = gettext('There is an error in the form');
2015-08-24 17:09:59 -03:00
$timeout(function() {
$scope.$apply();
});
2015-03-06 12:00:10 -03:00
return;
}
var backupFile = $scope.file;
var backupText = form.backupText.$modelValue;
var password = form.password.$modelValue;
if (!backupFile && !backupText) {
this.error = gettext('Please, select your backup file');
2015-08-24 17:09:59 -03:00
$timeout(function() {
$scope.$apply();
});
2015-03-06 12:00:10 -03:00
return;
}
if (backupFile) {
reader.readAsBinaryString(backupFile);
} else {
2015-08-24 17:09:59 -03:00
_importBlob(backupText);
}
};
this.importMnemonic = function(form) {
if (form.$invalid) {
this.error = gettext('There is an error in the form');
$timeout(function() {
$scope.$apply();
});
return;
}
var opts = {};
var passphrase = form.passphrase.$modelValue;
var words = form.words.$modelValue;
2015-09-02 15:56:00 -03:00
this.error = null;
2015-08-24 17:09:59 -03:00
2015-09-02 15:56:00 -03:00
if (!words) {
2015-09-04 22:15:50 -03:00
this.error = gettext('Please enter the seed words');
2015-09-02 15:56:00 -03:00
} else {
var wordList = words.split(/ /).filter(function(v) {
return v.length > 0;
});
2015-09-04 22:15:50 -03:00
if ((wordList.length % 3) != 0)
this.error = gettext('Wrong number of seed words:') + wordList.length;
else
2015-09-02 15:56:00 -03:00
words = wordList.join(' ');
}
if (this.error) {
2015-08-24 17:09:59 -03:00
$timeout(function() {
$scope.$apply();
});
return;
2015-03-06 12:00:10 -03:00
}
2015-08-24 17:09:59 -03:00
2015-09-02 15:56:00 -03:00
2015-08-24 17:09:59 -03:00
opts.passphrase = form.passphrase.$modelValue || null;
2015-09-02 15:56:00 -03:00
opts.networkName = form.isTestnet.$modelValue ? 'testnet' : 'livenet';
2015-08-24 17:09:59 -03:00
2015-09-03 01:49:48 -03:00
_importMnemonic(words, opts);
2015-03-06 12:00:10 -03:00
};
this.importLedger = function(form) {
var self = this;
if (form.$invalid) {
this.error = gettext('There is an error in the form');
$timeout(function() {
$scope.$apply();
});
return;
}
self.ledger = true;
ledger.getInfoForNewWallet($scope.externalIndex, function(err, lopts) {
self.ledger = false;
if (err) {
self.error = err;
$scope.$apply();
return;
}
lopts.externalIndex = $scope.externalIndex;
lopts.externalSource = 'ledger';
self.loading = true;
$log.debug('Import opts', lopts);
profileService.importExtendedPublicKey(lopts, function(err, walletId) {
self.loading = false;
if (err) {
self.error = err;
return $timeout(function() {
$scope.$apply();
});
}
$rootScope.$emit('Local/WalletImported', walletId);
notification.success(gettext('Success'), gettext('Your wallet has been imported correctly'));
go.walletHome();
});
}, 100);
};
2015-03-06 12:00:10 -03:00
});