Wallet/src/js/controllers/import.js

338 lines
9.5 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('importController',
2016-05-30 16:32:28 -03:00
function($scope, $rootScope, $ionicScrollDelegate, $timeout, $log, profileService, configService, notification, go, sjcl, gettext, lodash, ledger, trezor, derivationPathHelper, platformInfo) {
var isChromeApp = platformInfo.isChromeApp;
var isDevel = platformInfo.isDevel;
2015-03-06 12:00:10 -03:00
var self = this;
var reader = new FileReader();
2015-10-16 12:35:18 -03:00
var defaults = configService.getDefaults();
$scope.bwsurl = defaults.bws.url;
$scope.derivationPath = derivationPathHelper.default;
$scope.account = 1;
2015-03-06 12:00:10 -03:00
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-11-04 15:45:33 -03:00
var updateSeedSourceSelect = function() {
self.seedOptions = [];
2015-11-05 16:00:38 -03:00
if (isChromeApp) {
self.seedOptions.push({
id: 'ledger',
label: 'Ledger Hardware Wallet',
2015-11-05 16:00:38 -03:00
});
}
if (isChromeApp || isDevel) {
self.seedOptions.push({
id: 'trezor',
label: 'Trezor Hardware Wallet',
2015-11-05 16:00:38 -03:00
});
$scope.seedSource = self.seedOptions[0];
}
2015-11-04 15:45:33 -03:00
};
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 spending password');
2015-03-06 12:00:10 -03:00
$log.warn(e);
};
if (err) {
self.error = err;
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
2015-08-24 17:09:59 -03:00
$timeout(function() {
$rootScope.$apply();
});
return;
}
2015-03-06 12:00:10 -03:00
self.loading = true;
2015-10-20 12:03:08 -03:00
opts.compressed = null;
opts.password = null;
2015-03-06 12:00:10 -03:00
$timeout(function() {
2015-10-20 12:03:08 -03:00
profileService.importWallet(str2, opts, function(err, walletId) {
2015-03-06 12:00:10 -03:00
self.loading = false;
if (err) {
self.error = err;
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
2015-08-24 17:09:59 -03:00
} else {
2015-10-20 12:46:46 -03:00
$rootScope.$emit('Local/WalletImported', walletId);
notification.success(gettext('Success'), gettext('Your wallet has been imported correctly'));
2016-02-22 19:32:24 -03:00
go.walletHome();
2015-03-06 12:00:10 -03:00
}
});
}, 100);
};
2015-10-20 12:03:08 -03:00
var _importExtendedPrivateKey = function(xPrivKey, opts) {
2015-09-05 00:11:14 -03:00
self.loading = true;
$timeout(function() {
2015-10-20 12:03:08 -03:00
profileService.importExtendedPrivateKey(xPrivKey, opts, function(err, walletId) {
2015-09-05 00:11:14 -03:00
self.loading = false;
if (err) {
self.error = err;
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
2015-09-05 00:11:14 -03:00
return $timeout(function() {
$scope.$apply();
});
}
2015-10-20 12:46:46 -03:00
$rootScope.$emit('Local/WalletImported', walletId);
notification.success(gettext('Success'), gettext('Your wallet has been imported correctly'));
2016-02-29 18:41:10 -03:00
go.walletHome();
2015-09-05 00:11:14 -03:00
});
}, 100);
};
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() {
2015-09-05 00:11:14 -03:00
profileService.importMnemonic(words, opts, function(err, walletId) {
2015-08-24 17:09:59 -03:00
self.loading = false;
if (err) {
self.error = err;
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
2015-08-24 17:09:59 -03:00
return $timeout(function() {
$scope.$apply();
});
}
2015-10-20 12:46:46 -03:00
$rootScope.$emit('Local/WalletImported', walletId);
notification.success(gettext('Success'), gettext('Your wallet has been imported correctly'));
2016-02-29 18:41:10 -03:00
go.walletHome();
2015-08-24 17:09:59 -03:00
});
}, 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-10-20 12:03:08 -03:00
var opts = {};
opts.bwsurl = $scope.bwsurl;
_importBlob(evt.target.result, opts);
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');
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
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');
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
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-10-20 12:03:08 -03:00
var opts = {};
opts.bwsurl = $scope.bwsurl;
_importBlob(backupText, opts);
2015-08-24 17:09:59 -03:00
}
};
this.importMnemonic = function(form) {
if (form.$invalid) {
this.error = gettext('There is an error in the form');
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
2015-08-24 17:09:59 -03:00
$timeout(function() {
$scope.$apply();
});
return;
}
var opts = {};
2015-10-20 12:03:08 -03:00
if ($scope.bwsurl)
opts.bwsurl = $scope.bwsurl;
2015-08-24 17:09:59 -03:00
2016-05-09 12:03:25 -03:00
var pathData = derivationPathHelper.parse($scope.derivationPath);
if (!pathData) {
this.error = gettext('Invalid derivation path');
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
2016-05-09 12:03:25 -03:00
return;
}
opts.account = pathData.account;
opts.networkName = pathData.networkName;
opts.derivationStrategy = pathData.derivationStrategy;
2015-08-24 17:09:59 -03:00
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) {
this.error = gettext('Please enter the recovery phrase');
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
2015-10-16 12:35:18 -03:00
} else if (words.indexOf('xprv') == 0 || words.indexOf('tprv') == 0) {
2015-10-20 12:03:08 -03:00
return _importExtendedPrivateKey(words, opts);
2015-09-02 15:56:00 -03:00
} else {
var wordList = words.split(/[\u3000\s]+/);
2015-09-05 01:02:01 -03:00
2016-05-30 16:32:28 -03:00
if ((wordList.length % 3) != 0) {
this.error = gettext('Wrong number of recovery words:') + wordList.length;
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
}
2015-09-02 15:56:00 -03:00
}
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
2016-05-09 12:03:25 -03:00
var passphrase = form.passphrase.$modelValue;
2015-08-24 17:09:59 -03:00
opts.passphrase = form.passphrase.$modelValue || null;
2015-09-03 01:49:48 -03:00
_importMnemonic(words, opts);
2015-03-06 12:00:10 -03:00
};
2015-11-04 15:45:33 -03:00
this.importTrezor = function(account, isMultisig) {
var self = this;
2015-11-04 00:53:26 -03:00
trezor.getInfoForNewWallet(isMultisig, account, function(err, lopts) {
self.hwWallet = false;
if (err) {
self.error = err;
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
$scope.$apply();
return;
}
2015-10-20 12:46:46 -03:00
lopts.externalSource = 'trezor';
2015-10-20 12:46:46 -03:00
lopts.bwsurl = $scope.bwsurl;
self.loading = true;
$log.debug('Import opts', lopts);
2015-10-20 12:46:46 -03:00
profileService.importExtendedPublicKey(lopts, function(err, walletId) {
self.loading = false;
if (err) {
self.error = err;
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
return $timeout(function() {
$scope.$apply();
});
}
2015-10-20 12:46:46 -03:00
$rootScope.$emit('Local/WalletImported', walletId);
notification.success(gettext('Success'), gettext('Your wallet has been imported correctly'));
go.walletHome();
});
}, 100);
};
2015-11-04 15:45:33 -03:00
this.importHW = function(form) {
2016-05-30 16:32:28 -03:00
if (form.$invalid || $scope.account < 0) {
this.error = gettext('There is an error in the form');
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
$timeout(function() {
$scope.$apply();
});
return;
}
this.error = '';
2015-11-04 15:45:33 -03:00
2016-05-30 16:32:28 -03:00
var account = +$scope.account;
if (self.seedSourceId == 'trezor') {
2016-05-30 16:32:28 -03:00
if (account < 1) {
this.error = gettext('Invalid account number');
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
return;
}
account = account - 1;
}
2015-11-04 15:45:33 -03:00
var isMultisig = form.isMultisig.$modelValue;
switch (self.seedSourceId) {
case ('ledger'):
self.hwWallet = 'Ledger';
self.importLedger(account);
break;
case ('trezor'):
self.hwWallet = 'Trezor';
self.importTrezor(account, isMultisig);
break;
default:
throw ('Error: bad source id');
};
};
this.setSeedSource = function() {
if (!$scope.seedSource) return;
self.seedSourceId = $scope.seedSource.id;
$timeout(function() {
$rootScope.$apply();
});
};
this.importLedger = function(account) {
var self = this;
2015-11-04 00:53:26 -03:00
ledger.getInfoForNewWallet(true, account, function(err, lopts) {
self.hwWallet = false;
if (err) {
self.error = err;
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
$scope.$apply();
return;
}
2015-10-20 12:46:46 -03:00
lopts.externalSource = 'ledger';
2015-10-20 12:46:46 -03:00
lopts.bwsurl = $scope.bwsurl;
self.loading = true;
$log.debug('Import opts', lopts);
2015-10-20 12:46:46 -03:00
profileService.importExtendedPublicKey(lopts, function(err, walletId) {
self.loading = false;
if (err) {
self.error = err;
2016-05-30 16:32:28 -03:00
$ionicScrollDelegate.scrollTop();
return $timeout(function() {
$scope.$apply();
});
}
2015-10-20 12:46:46 -03:00
$rootScope.$emit('Local/WalletImported', walletId);
notification.success(gettext('Success'), gettext('Your wallet has been imported correctly'));
2016-02-22 19:32:24 -03:00
go.walletHome();
});
}, 100);
};
2015-11-04 15:45:33 -03:00
updateSeedSourceSelect();
self.setSeedSource('new');
2015-03-06 12:00:10 -03:00
});