Wallet/src/js/controllers/create.js

255 lines
8.6 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('createController',
function($scope, $rootScope, $timeout, $log, lodash, $state, $ionicScrollDelegate, $ionicHistory, profileService, configService, gettextCatalog, ledger, trezor, intelTEE, derivationPathHelper, ongoingProcess, walletService, storageService, popupService, appConfigService, pushNotificationsService) {
2015-03-06 12:00:10 -03:00
/* For compressed keys, m*73 + n*34 <= 496 */
var COPAYER_PAIR_LIMITS = {
1: 1,
2: 2,
3: 3,
4: 4,
5: 4,
6: 4,
7: 3,
8: 3,
9: 2,
10: 2,
11: 1,
12: 1,
};
2017-05-22 21:28:41 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
2016-09-05 17:14:41 -03:00
$scope.formData = {};
var defaults = configService.getDefaults();
2017-08-30 16:38:42 -03:00
var config = configService.getSync();
2017-05-22 21:28:41 -03:00
var tc = $state.current.name == 'tabs.add.create-personal' ? 1 : defaults.wallet.totalCopayers;
2016-09-05 17:14:41 -03:00
$scope.formData.account = 1;
$scope.formData.bwsurl = defaults.bws.url;
$scope.TCValues = lodash.range(2, defaults.limits.totalCopayers + 1);
$scope.formData.derivationPath = derivationPathHelper.default;
$scope.formData.coin = 'btc';
2017-08-30 16:38:42 -03:00
if (config.cashSupport.enabled) $scope.enableCash = true;
2016-09-08 11:28:41 -03:00
$scope.setTotalCopayers(tc);
updateRCSelect(tc);
resetPasswordFields();
2017-05-22 21:28:41 -03:00
});
2015-03-06 12:00:10 -03:00
2016-08-31 18:43:58 -03: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-31 18:43:58 -03:00
};
2016-09-05 17:14:41 -03:00
function updateRCSelect(n) {
$scope.formData.totalCopayers = n;
2015-03-06 12:00:10 -03:00
var maxReq = COPAYER_PAIR_LIMITS[n];
2016-09-05 17:14:41 -03:00
$scope.RCValues = lodash.range(1, maxReq + 1);
$scope.formData.requiredCopayers = Math.min(parseInt(n / 2 + 1), maxReq);
2015-03-06 12:00:10 -03:00
};
2016-09-05 17:14:41 -03:00
function updateSeedSourceSelect(n) {
var seedOptions = [{
2015-11-04 00:53:26 -03:00
id: 'new',
2016-12-13 14:21:57 -03:00
label: gettextCatalog.getString('Random'),
2017-04-07 17:12:02 -04:00
supportsTestnet: true
}, {
2015-11-04 00:53:26 -03:00
id: 'set',
2016-12-13 14:21:57 -03:00
label: gettextCatalog.getString('Specify Recovery Phrase...'),
2017-04-07 17:12:02 -04:00
supportsTestnet: false
2015-11-04 00:53:26 -03:00
}];
2016-09-05 17:14:41 -03:00
2017-05-29 15:04:22 -03:00
$scope.formData.seedSource = seedOptions[0];
2015-11-04 00:53:26 -03:00
2016-12-06 15:33:00 -03:00
/*
2016-12-14 11:15:22 -03:00
Disable Hardware Wallets for BitPay distribution
2016-12-06 15:33:00 -03:00
*/
2016-12-14 11:15:22 -03:00
2016-12-27 15:19:53 -03:00
if (appConfigService.name == 'copay') {
if (n > 1 && walletService.externalSource.ledger.supported)
2016-12-14 11:15:22 -03:00
seedOptions.push({
2016-12-05 17:33:46 -05:00
id: walletService.externalSource.ledger.id,
label: walletService.externalSource.ledger.longName,
2017-04-07 17:12:02 -04:00
supportsTestnet: walletService.externalSource.ledger.supportsTestnet
2016-12-14 11:15:22 -03:00
});
2016-12-05 17:33:46 -05:00
if (walletService.externalSource.trezor.supported) {
2016-12-14 11:15:22 -03:00
seedOptions.push({
2016-12-05 17:33:46 -05:00
id: walletService.externalSource.trezor.id,
label: walletService.externalSource.trezor.longName,
2017-04-07 17:12:02 -04:00
supportsTestnet: walletService.externalSource.trezor.supportsTestnet
2016-12-14 11:15:22 -03:00
});
}
if (walletService.externalSource.intelTEE.supported) {
seedOptions.push({
id: walletService.externalSource.intelTEE.id,
label: walletService.externalSource.intelTEE.longName,
2017-04-07 17:12:02 -04:00
supportsTestnet: walletService.externalSource.intelTEE.supportsTestnet
});
}
2016-12-14 11:15:22 -03:00
}
2016-09-05 17:14:41 -03:00
$scope.seedOptions = seedOptions;
2015-11-04 00:53:26 -03:00
};
2016-09-05 17:14:41 -03:00
$scope.setTotalCopayers = function(tc) {
$scope.formData.totalCopayers = tc;
updateRCSelect(tc);
2015-11-04 00:53:26 -03:00
updateSeedSourceSelect(tc);
};
2017-05-22 21:05:32 -03:00
$scope.create = function() {
2015-03-06 12:00:10 -03:00
var opts = {
2016-09-05 17:14:41 -03:00
name: $scope.formData.walletName,
m: $scope.formData.requiredCopayers,
n: $scope.formData.totalCopayers,
myName: $scope.formData.totalCopayers > 1 ? $scope.formData.myName : null,
2017-08-31 18:13:15 -03:00
networkName: $scope.formData.testnetEnabled && $scope.formData.coin != 'bch' ? 'testnet' : 'livenet',
2016-09-05 17:14:41 -03:00
bwsurl: $scope.formData.bwsurl,
singleAddress: $scope.formData.singleAddressEnabled,
walletPrivKey: $scope.formData._walletPrivKey, // Only for testing
coin: $scope.formData.coin
2015-03-06 12:00:10 -03:00
};
2016-09-05 17:14:41 -03:00
2017-05-29 15:04:22 -03:00
var setSeed = $scope.formData.seedSource.id == 'set';
2015-09-05 00:11:14 -03:00
if (setSeed) {
2016-09-05 17:14:41 -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;
}
2016-09-05 17:14:41 -03:00
opts.passphrase = $scope.formData.passphrase;
2016-09-05 17:14:41 -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 {
2016-09-05 17:14:41 -03:00
opts.passphrase = $scope.formData.createPassphrase;
2015-09-03 01:49:48 -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-09-05 00:11:14 -03:00
2017-05-29 15:04:22 -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) {
2016-09-05 17:14:41 -03:00
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 01:54:54 -03:00
return;
}
2017-05-29 15:04:22 -03:00
if ($scope.formData.seedSource.id == walletService.externalSource.trezor.id || $scope.formData.seedSource.id == walletService.externalSource.intelTEE.id)
account = account - 1;
opts.account = account;
2017-05-29 15:04:22 -03:00
ongoingProcess.set('connecting ' + $scope.formData.seedSource.id, true);
2016-06-13 15:25:40 -03:00
2016-12-05 17:33:46 -05:00
var src;
2017-05-29 15:04:22 -03:00
switch ($scope.formData.seedSource.id) {
2016-12-05 17:33:46 -05:00
case walletService.externalSource.ledger.id:
src = ledger;
2016-12-05 17:33:46 -05:00
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');
2016-12-05 17:33:46 -05:00
return;
}
2017-04-07 17:12:02 -04:00
src.getInfoForNewWallet(opts.n > 1, account, opts.networkName, function(err, lopts) {
2017-05-29 15:04:22 -03:00
ongoingProcess.set('connecting ' + $scope.formData.seedSource.id, false);
2015-09-04 10:17:59 -03:00
if (err) {
2016-09-01 10:56:13 -03:00
popupService.showAlert(gettextCatalog.getString('Error'), err);
2015-09-04 10:17:59 -03:00
return;
2015-07-17 15:53:50 +02:00
}
2015-09-04 10:17:59 -03:00
opts = lodash.assign(lopts, opts);
2016-09-05 17:14:41 -03:00
_create(opts);
2015-07-17 15:53:50 +02:00
});
} else {
2016-09-05 17:14:41 -03:00
_create(opts);
2015-07-17 15:53:50 +02:00
}
};
2016-09-05 17:14:41 -03:00
function _create(opts) {
2016-06-13 15:25:40 -03:00
ongoingProcess.set('creatingWallet', true);
2015-03-06 12:00:10 -03:00
$timeout(function() {
2016-08-18 00:27:23 -03:00
profileService.createWallet(opts, function(err, client) {
2016-06-13 15:25:40 -03:00
ongoingProcess.set('creatingWallet', false);
2015-03-06 12:00:10 -03:00
if (err) {
2015-09-04 10:17:59 -03:00
$log.warn(err);
2016-09-01 10:56:13 -03:00
popupService.showAlert(gettextCatalog.getString('Error'), err);
return;
}
2016-08-15 16:07:30 -03:00
2017-01-16 16:00:09 -03:00
walletService.updateRemotePreferences(client);
pushNotificationsService.updateSubscription(client);
2016-08-15 16:07:30 -03:00
2017-05-29 15:04:22 -03:00
if ($scope.formData.seedSource.id == 'set') {
2016-08-30 17:07:49 -03:00
profileService.setBackupFlag(client.credentials.walletId);
}
2016-09-23 12:42:33 -03:00
$ionicHistory.removeBackView();
2016-09-22 16:04:05 -03:00
if (!client.isComplete()) {
$ionicHistory.nextViewOptions({
2016-09-22 16:50:06 -03:00
disableAnimate: true
2016-09-22 16:04:05 -03:00
});
2016-09-22 16:50:06 -03:00
$state.go('tabs.home');
$timeout(function() {
$state.transitionTo('tabs.copayers', {
walletId: client.credentials.walletId
});
}, 100);
} else $state.go('tabs.home');
2015-03-06 12:00:10 -03:00
});
2017-03-30 21:37:12 +02:00
}, 300);
2015-07-17 15:53:50 +02:00
}
2015-03-06 12:00:10 -03:00
});