Wallet/src/js/controllers/join.js

216 lines
7.3 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('joinController',
2017-04-17 10:55:16 -04:00
function($scope, $rootScope, $timeout, $state, $ionicHistory, $ionicScrollDelegate, profileService, configService, storageService, applicationService, gettextCatalog, lodash, ledger, trezor, intelTEE, derivationPathHelper, ongoingProcess, walletService, $log, $stateParams, popupService, appConfigService) {
2015-03-06 12:00:10 -03:00
2017-05-22 21:05:32 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
var defaults = configService.getDefaults();
2017-08-30 16:38:42 -03:00
var config = configService.getSync();
$scope.formData = {};
2017-05-22 21:05:32 -03:00
$scope.formData.bwsurl = defaults.bws.url;
$scope.formData.derivationPath = derivationPathHelper.default;
$scope.formData.account = 1;
$scope.formData.secret = null;
2017-08-28 12:57:32 -03:00
$scope.formData.coin = 'btc';
2017-08-30 16:38:42 -03:00
if (config.cashSupport.enabled) $scope.enableCash = true;
resetPasswordFields();
updateSeedSourceSelect();
2017-05-22 21:05:32 -03:00
});
2015-07-17 15:53:50 +02:00
$scope.showAdvChange = function() {
$scope.showAdv = !$scope.showAdv;
$scope.encrypt = null;
$scope.resizeView();
};
$scope.checkPassword = function(pw1, pw2) {
if (pw1 && pw1.length > 0) {
if (pw2 && pw2.length > 0) {
if (pw1 == pw2) $scope.result = 'correct';
else {
$scope.formData.passwordSaved = null;
$scope.result = 'incorrect';
}
} else
$scope.result = null;
} else
$scope.result = null;
};
$scope.resizeView = function() {
$timeout(function() {
$ionicScrollDelegate.resize();
}, 10);
resetPasswordFields();
};
function resetPasswordFields() {
2017-05-22 21:28:41 -03:00
$scope.formData.passphrase = $scope.formData.createPassphrase = $scope.formData.passwordSaved = $scope.formData.repeatPassword = $scope.result = null;
$timeout(function() {
$scope.$apply();
});
};
2016-08-25 11:18:10 -03:00
$scope.onQrCodeScannedJoin = function(data) {
2017-05-22 21:05:32 -03:00
$scope.formData.secret = data;
2017-06-07 12:23:07 -03:00
$scope.$apply();
2015-03-06 12:00:10 -03:00
};
2016-08-25 11:18:10 -03:00
if ($stateParams.url) {
var data = $stateParams.url;
data = data.replace('copay:', '');
$scope.onQrCodeScannedJoin(data);
2016-08-25 11:18:10 -03:00
}
function updateSeedSourceSelect() {
$scope.seedOptions = [{
2015-11-04 15:45:33 -03:00
id: 'new',
2016-12-13 14:21:57 -03:00
label: gettextCatalog.getString('Random'),
2015-11-04 15:45:33 -03:00
}, {
id: 'set',
2016-12-13 14:21:57 -03:00
label: gettextCatalog.getString('Specify Recovery Phrase...'),
2015-11-04 15:45:33 -03:00
}];
2017-05-29 15:34:44 -03:00
$scope.formData.seedSource = $scope.seedOptions[0];
2016-12-06 15:33:00 -03:00
/*
Disable Hardware Wallets
2015-11-04 15:45:33 -03:00
2016-12-14 11:15:22 -03:00
*/
2015-11-05 16:00:38 -03:00
2016-12-27 15:19:53 -03:00
if (appConfigService.name == 'copay') {
if (walletService.externalSource.ledger.supported) {
$scope.seedOptions.push({
2016-12-05 17:33:46 -05:00
id: walletService.externalSource.ledger.id,
2017-04-17 10:55:16 -04:00
label: walletService.externalSource.ledger.longName
2016-12-14 11:15:22 -03:00
});
}
if (walletService.externalSource.trezor.supported) {
$scope.seedOptions.push({
2016-12-05 17:33:46 -05:00
id: walletService.externalSource.trezor.id,
2017-04-17 10:55:16 -04:00
label: walletService.externalSource.trezor.longName
});
}
if (walletService.externalSource.intelTEE.supported) {
$scope.seedOptions.push({
2017-04-17 10:55:16 -04:00
id: walletService.externalSource.intelTEE.id,
label: walletService.externalSource.intelTEE.longName
2016-12-14 11:15:22 -03:00
});
}
2015-11-05 16:00:38 -03:00
}
2015-11-04 15:45:33 -03:00
};
$scope.join = function() {
2015-07-17 15:53:50 +02:00
var opts = {
2017-05-22 21:05:32 -03:00
secret: $scope.formData.secret,
myName: $scope.formData.myName,
2017-08-28 12:57:32 -03:00
bwsurl: $scope.formData.bwsurl,
coin: $scope.formData.coin
2015-07-17 15:53:50 +02:00
}
2015-09-04 12:25:54 -03:00
2017-05-29 15:34:44 -03:00
var setSeed = $scope.formData.seedSource.id == 'set';
2015-10-16 13:00:41 -03:00
if (setSeed) {
2017-05-22 21:05:32 -03:00
var words = $scope.formData.privateKey;
2015-09-05 00:11:14 -03:00
if (words.indexOf(' ') == -1 && words.indexOf('prv') == 1 && words.length > 108) {
opts.extendedPrivateKey = words;
} else {
opts.mnemonic = words;
}
2017-05-22 21:05:32 -03:00
opts.passphrase = $scope.formData.passphrase;
2017-05-22 21:05:32 -03:00
var pathData = derivationPathHelper.parse($scope.formData.derivationPath);
if (!pathData) {
2016-09-01 10:56:13 -03:00
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Invalid derivation path'));
return;
}
opts.account = pathData.account;
opts.networkName = pathData.networkName;
opts.derivationStrategy = pathData.derivationStrategy;
2015-09-03 01:49:48 -03:00
} else {
2017-05-22 21:05:32 -03:00
opts.passphrase = $scope.formData.createPassphrase;
2015-09-03 01:49:48 -03:00
}
2016-06-15 13:07:02 -03:00
opts.walletPrivKey = $scope._walletPrivKey; // Only for testing
2015-09-05 00:11:14 -03:00
if (setSeed && !opts.mnemonic && !opts.extendedPrivateKey) {
2016-09-01 10:56:13 -03:00
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Please enter the wallet recovery phrase'));
2015-09-03 01:49:48 -03:00
return;
}
2015-10-16 13:00:41 -03:00
2017-05-29 15:34:44 -03:00
if ($scope.formData.seedSource.id == walletService.externalSource.ledger.id || $scope.formData.seedSource.id == walletService.externalSource.trezor.id || $scope.formData.seedSource.id == walletService.externalSource.intelTEE.id) {
var account = $scope.formData.account;
if (!account || account < 1) {
2016-09-01 10:56:13 -03:00
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Invalid account number'));
2015-11-04 15:45:33 -03:00
return;
}
2017-05-29 15:34:44 -03:00
if ($scope.formData.seedSource.id == walletService.externalSource.trezor.id || $scope.formData.seedSource.id == walletService.externalSource.intelTEE.id)
account = account - 1;
2016-05-30 16:32:28 -03:00
opts.account = account;
2017-04-07 10:54:30 -04:00
opts.isMultisig = true;
2017-05-29 15:34:44 -03:00
ongoingProcess.set('connecting' + $scope.formData.seedSource.id, true);
2017-04-17 10:55:16 -04:00
var src;
2017-05-29 15:34:44 -03:00
switch ($scope.formData.seedSource.id) {
2017-04-17 10:55:16 -04:00
case walletService.externalSource.ledger.id:
src = ledger;
break;
case walletService.externalSource.trezor.id:
src = trezor;
break;
case walletService.externalSource.intelTEE.id:
src = intelTEE;
break;
default:
popupService.showAlert(gettextCatalog.getString('Error'), 'Invalid seed source id');
2017-04-17 10:55:16 -04:00
return;
}
// TODO: cannot currently join an intelTEE testnet wallet (need to detect from the secret)
2017-04-07 17:12:02 -04:00
src.getInfoForNewWallet(true, account, 'livenet', function(err, lopts) {
2017-05-29 15:34:44 -03:00
ongoingProcess.set('connecting' + $scope.formData.seedSource.id, false);
2015-09-04 12:25:54 -03:00
if (err) {
2016-09-01 10:56:13 -03:00
popupService.showAlert(gettextCatalog.getString('Error'), err);
2015-09-04 12:25:54 -03:00
return;
2015-07-17 15:53:50 +02:00
}
2015-09-04 12:25:54 -03:00
opts = lodash.assign(lopts, opts);
_join(opts);
2015-07-17 15:53:50 +02:00
});
} else {
_join(opts);
2015-07-17 15:53:50 +02:00
}
};
function _join(opts) {
2016-06-13 15:25:40 -03:00
ongoingProcess.set('joiningWallet', true);
2015-03-06 12:00:10 -03:00
$timeout(function() {
2016-08-18 00:27:23 -03:00
profileService.joinWallet(opts, function(err, client) {
2016-06-30 15:14:16 -03:00
ongoingProcess.set('joiningWallet', false);
2015-03-06 12:00:10 -03:00
if (err) {
2016-09-01 10:56:13 -03:00
popupService.showAlert(gettextCatalog.getString('Error'), err);
2015-10-19 17:26:15 -03:00
return;
2015-03-06 12:00:10 -03:00
}
2016-08-15 16:07:30 -03:00
2017-01-16 16:00:09 -03:00
walletService.updateRemotePreferences(client);
2016-09-23 12:42:33 -03:00
$ionicHistory.removeBackView();
if (!client.isComplete()) {
$ionicHistory.nextViewOptions({
disableAnimate: true
});
$state.go('tabs.home');
$timeout(function() {
$state.transitionTo('tabs.copayers', {
walletId: client.credentials.walletId
});
});
} else $state.go('tabs.home');
2015-03-06 12:00:10 -03:00
});
});
2015-07-17 15:53:50 +02:00
};
2015-03-06 12:00:10 -03:00
});