2015-03-06 12:00:10 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
angular.module('copayApp.controllers').controller('joinController',
|
2016-09-26 09:55:16 -03:00
|
|
|
function($scope, $rootScope, $timeout, $state, $ionicHistory, profileService, configService, storageService, applicationService, gettext, gettextCatalog, lodash, ledger, trezor, platformInfo, derivationPathHelper, ongoingProcess, walletService, $log, $stateParams, popupService) {
|
2016-05-31 14:55:08 -03:00
|
|
|
|
|
|
|
|
var isChromeApp = platformInfo.isChromeApp;
|
|
|
|
|
var isDevel = platformInfo.isDevel;
|
2015-03-06 12:00:10 -03:00
|
|
|
|
|
|
|
|
var self = this;
|
2015-10-16 13:00:41 -03:00
|
|
|
var defaults = configService.getDefaults();
|
|
|
|
|
$scope.bwsurl = defaults.bws.url;
|
2015-11-05 19:46:32 -03:00
|
|
|
$scope.derivationPath = derivationPathHelper.default;
|
2015-11-11 16:23:56 -03:00
|
|
|
$scope.account = 1;
|
2015-07-17 15:53:50 +02:00
|
|
|
|
2016-08-25 11:18:10 -03:00
|
|
|
|
2015-09-28 01:20:16 -03:00
|
|
|
this.onQrCodeScanned = function(data) {
|
|
|
|
|
$scope.secret = data;
|
2016-08-25 11:18:10 -03:00
|
|
|
if ($scope.joinForm) {
|
|
|
|
|
$scope.joinForm.secret.$setViewValue(data);
|
|
|
|
|
$scope.joinForm.secret.$render();
|
|
|
|
|
}
|
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:', '');
|
|
|
|
|
this.onQrCodeScanned(data);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-04 15:45:33 -03:00
|
|
|
var updateSeedSourceSelect = function() {
|
|
|
|
|
self.seedOptions = [{
|
|
|
|
|
id: 'new',
|
2016-08-10 15:03:08 -03:00
|
|
|
label: gettext('Random'),
|
2015-11-04 15:45:33 -03:00
|
|
|
}, {
|
|
|
|
|
id: 'set',
|
2016-05-09 20:23:20 +02:00
|
|
|
label: gettext('Specify Recovery Phrase...'),
|
2015-11-04 15:45:33 -03:00
|
|
|
}];
|
|
|
|
|
$scope.seedSource = self.seedOptions[0];
|
|
|
|
|
|
|
|
|
|
|
2015-11-05 16:00:38 -03:00
|
|
|
if (isChromeApp) {
|
|
|
|
|
self.seedOptions.push({
|
|
|
|
|
id: 'ledger',
|
2015-11-11 14:11:35 -03:00
|
|
|
label: 'Ledger Hardware Wallet',
|
2015-11-05 16:00:38 -03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isChromeApp || isDevel) {
|
|
|
|
|
self.seedOptions.push({
|
|
|
|
|
id: 'trezor',
|
2015-11-11 14:11:35 -03:00
|
|
|
label: 'Trezor Hardware Wallet',
|
2015-11-05 16:00:38 -03:00
|
|
|
});
|
|
|
|
|
}
|
2015-11-04 15:45:33 -03:00
|
|
|
};
|
|
|
|
|
|
2016-06-15 13:07:02 -03:00
|
|
|
this.setSeedSource = function() {
|
2015-11-04 15:45:33 -03:00
|
|
|
self.seedSourceId = $scope.seedSource.id;
|
|
|
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
this.join = function(form) {
|
|
|
|
|
if (form && form.$invalid) {
|
2016-09-01 10:56:13 -03:00
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Please enter the required fields'));
|
2015-03-06 12:00:10 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-17 15:53:50 +02:00
|
|
|
var opts = {
|
|
|
|
|
secret: form.secret.$modelValue,
|
2015-09-04 12:25:54 -03:00
|
|
|
myName: form.myName.$modelValue,
|
2015-11-04 18:08:22 -03:00
|
|
|
bwsurl: $scope.bwsurl,
|
2015-07-17 15:53:50 +02:00
|
|
|
}
|
2015-09-04 12:25:54 -03:00
|
|
|
|
2016-05-30 16:32:28 -03:00
|
|
|
var setSeed = self.seedSourceId == 'set';
|
2015-10-16 13:00:41 -03:00
|
|
|
if (setSeed) {
|
2015-09-05 00:11:14 -03:00
|
|
|
var words = form.privateKey.$modelValue;
|
|
|
|
|
if (words.indexOf(' ') == -1 && words.indexOf('prv') == 1 && words.length > 108) {
|
|
|
|
|
opts.extendedPrivateKey = words;
|
|
|
|
|
} else {
|
|
|
|
|
opts.mnemonic = words;
|
|
|
|
|
}
|
2015-09-03 01:49:48 -03:00
|
|
|
opts.passphrase = form.passphrase.$modelValue;
|
2015-11-05 19:46:32 -03:00
|
|
|
|
|
|
|
|
var pathData = derivationPathHelper.parse($scope.derivationPath);
|
|
|
|
|
if (!pathData) {
|
2016-09-01 10:56:13 -03:00
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Invalid derivation path'));
|
2015-11-05 19:46:32 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
opts.account = pathData.account;
|
|
|
|
|
opts.networkName = pathData.networkName;
|
|
|
|
|
opts.derivationStrategy = pathData.derivationStrategy;
|
2015-09-03 01:49:48 -03:00
|
|
|
} else {
|
|
|
|
|
opts.passphrase = form.createPassphrase.$modelValue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-15 13:07:02 -03:00
|
|
|
opts.walletPrivKey = $scope._walletPrivKey; // Only for testing
|
|
|
|
|
|
2016-05-20 11:50:55 -03:00
|
|
|
|
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
|
|
|
|
2015-11-04 15:45:33 -03:00
|
|
|
if (self.seedSourceId == 'ledger' || self.seedSourceId == 'trezor') {
|
|
|
|
|
var account = $scope.account;
|
2015-11-05 19:46:32 -03:00
|
|
|
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;
|
|
|
|
|
}
|
2015-11-05 19:46:32 -03:00
|
|
|
|
2016-05-30 16:32:28 -03:00
|
|
|
if (self.seedSourceId == 'trezor')
|
2015-11-05 19:46:32 -03:00
|
|
|
account = account - 1;
|
|
|
|
|
|
2016-05-30 16:32:28 -03:00
|
|
|
opts.account = account;
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('connecting' + self.seedSourceId, true);
|
2015-11-04 15:45:33 -03:00
|
|
|
var src = self.seedSourceId == 'ledger' ? ledger : trezor;
|
2015-09-29 12:45:06 -03:00
|
|
|
|
2015-11-04 00:53:26 -03:00
|
|
|
src.getInfoForNewWallet(true, account, function(err, lopts) {
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('connecting' + self.seedSourceId, 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);
|
|
|
|
|
self._join(opts);
|
2015-07-17 15:53:50 +02:00
|
|
|
});
|
|
|
|
|
} else {
|
2016-05-20 11:50:55 -03:00
|
|
|
|
2015-07-17 15:53:50 +02:00
|
|
|
self._join(opts);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this._join = function(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
|
|
|
|
2016-08-18 00:27:23 -03:00
|
|
|
walletService.updateRemotePreferences(client, {}, function() {
|
|
|
|
|
$log.debug('Remote preferences saved for:' + client.credentials.walletId)
|
2016-08-15 16:07:30 -03:00
|
|
|
});
|
2016-09-23 12:42:33 -03:00
|
|
|
$ionicHistory.removeBackView();
|
2016-08-19 13:07:18 -03:00
|
|
|
$state.go('tabs.home');
|
2015-03-06 12:00:10 -03:00
|
|
|
});
|
|
|
|
|
}, 100);
|
2015-07-17 15:53:50 +02:00
|
|
|
};
|
2015-11-04 15:45:33 -03:00
|
|
|
|
|
|
|
|
updateSeedSourceSelect();
|
2016-06-15 13:07:02 -03:00
|
|
|
self.setSeedSource();
|
2015-03-06 12:00:10 -03:00
|
|
|
});
|