2015-03-06 12:00:10 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
angular.module('copayApp.controllers').controller('createController',
|
2016-09-01 10:56:13 -03:00
|
|
|
function($scope, $rootScope, $timeout, $log, lodash, $state, $ionicScrollDelegate, profileService, configService, gettext, gettextCatalog, ledger, trezor, platformInfo, derivationPathHelper, ongoingProcess, walletService, storageService, popupService) {
|
2016-05-31 14:55:08 -03:00
|
|
|
|
|
|
|
|
var isChromeApp = platformInfo.isChromeApp;
|
|
|
|
|
var isCordova = platformInfo.isCordova;
|
|
|
|
|
var isDevel = platformInfo.isDevel;
|
|
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-08 11:28:41 -03:00
|
|
|
$scope.init = function(tc) {
|
2016-09-05 17:14:41 -03:00
|
|
|
$scope.formData = {};
|
|
|
|
|
var defaults = configService.getDefaults();
|
|
|
|
|
$scope.formData.account = 1;
|
|
|
|
|
$scope.formData.bwsurl = defaults.bws.url;
|
|
|
|
|
$scope.TCValues = lodash.range(2, defaults.limits.totalCopayers + 1);
|
|
|
|
|
$scope.formData.totalCopayers = defaults.wallet.totalCopayers;
|
|
|
|
|
$scope.formData.derivationPath = derivationPathHelper.default;
|
2016-09-08 11:28:41 -03:00
|
|
|
$scope.setTotalCopayers(tc);
|
|
|
|
|
updateRCSelect(tc);
|
|
|
|
|
updateSeedSourceSelect(tc);
|
2016-09-05 17:14:41 -03:00
|
|
|
};
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2016-08-31 18:43:58 -03:00
|
|
|
$scope.showAdvChange = function() {
|
|
|
|
|
$ionicScrollDelegate.resize();
|
|
|
|
|
};
|
|
|
|
|
|
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-08-10 15:03:08 -03:00
|
|
|
label: gettext('Random'),
|
2015-11-04 00:53:26 -03:00
|
|
|
}, {
|
|
|
|
|
id: 'set',
|
2016-05-09 20:23:20 +02:00
|
|
|
label: gettext('Specify Recovery Phrase...'),
|
2015-11-04 00:53:26 -03:00
|
|
|
}];
|
2016-09-05 17:14:41 -03:00
|
|
|
|
|
|
|
|
$scope.seedSource = seedOptions[0];
|
2015-11-04 00:53:26 -03:00
|
|
|
|
2015-11-05 16:00:38 -03:00
|
|
|
if (n > 1 && isChromeApp)
|
2016-09-05 17:14:41 -03:00
|
|
|
seedOptions.push({
|
2015-11-04 00:53:26 -03:00
|
|
|
id: 'ledger',
|
2016-08-10 15:03:08 -03:00
|
|
|
label: 'Ledger Hardware Wallet',
|
2015-11-04 00:53:26 -03:00
|
|
|
});
|
|
|
|
|
|
2015-11-05 16:00:38 -03:00
|
|
|
if (isChromeApp || isDevel) {
|
2016-09-05 17:14:41 -03:00
|
|
|
seedOptions.push({
|
2015-11-05 16:00:38 -03:00
|
|
|
id: 'trezor',
|
2016-08-10 15:03:08 -03:00
|
|
|
label: 'Trezor Hardware Wallet',
|
2015-11-05 16:00:38 -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;
|
2015-05-20 04:05:13 -03:00
|
|
|
updateRCSelect(tc);
|
2015-11-04 00:53:26 -03:00
|
|
|
updateSeedSourceSelect(tc);
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-05 17:14:41 -03:00
|
|
|
$scope.create = function(form) {
|
2015-03-06 12:00:10 -03:00
|
|
|
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-11-05 19:46:32 -03:00
|
|
|
|
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,
|
|
|
|
|
networkName: $scope.formData.testnetEnabled ? 'testnet' : 'livenet',
|
|
|
|
|
bwsurl: $scope.formData.bwsurl,
|
|
|
|
|
singleAddress: $scope.formData.singleAddressEnabled,
|
|
|
|
|
walletPrivKey: $scope.formData._walletPrivKey, // Only for testing
|
2015-03-06 12:00:10 -03:00
|
|
|
};
|
2016-09-05 17:14:41 -03:00
|
|
|
|
|
|
|
|
var setSeed = $scope.seedSource.id == 'set';
|
2015-09-05 00:11:14 -03:00
|
|
|
if (setSeed) {
|
2015-11-05 19:46:32 -03:00
|
|
|
|
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;
|
2015-11-05 19:46:32 -03:00
|
|
|
|
2016-09-05 17:14:41 -03:00
|
|
|
var pathData = derivationPathHelper.parse($scope.formData.derivationPath);
|
2015-11-05 19:46:32 -03:00
|
|
|
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 {
|
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
|
|
|
|
2016-09-05 17:14:41 -03:00
|
|
|
if ($scope.seedSource.id == 'ledger' || $scope.seedSource.id == 'trezor') {
|
|
|
|
|
var account = $scope.formData.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 01:54:54 -03:00
|
|
|
return;
|
|
|
|
|
}
|
2015-11-05 19:46:32 -03:00
|
|
|
|
2016-09-05 17:14:41 -03:00
|
|
|
if ($scope.seedSource.id == 'trezor')
|
2015-11-05 19:46:32 -03:00
|
|
|
account = account - 1;
|
|
|
|
|
|
|
|
|
|
opts.account = account;
|
2016-09-05 17:14:41 -03:00
|
|
|
ongoingProcess.set('connecting' + $scope.seedSource.id, true);
|
2016-06-13 15:25:40 -03:00
|
|
|
|
2016-09-05 17:14:41 -03:00
|
|
|
var src = $scope.seedSource.id == 'ledger' ? ledger : trezor;
|
2015-09-26 08:26:31 -03:00
|
|
|
|
2015-11-04 00:53:26 -03:00
|
|
|
src.getInfoForNewWallet(opts.n > 1, account, function(err, lopts) {
|
2016-09-05 17:14:41 -03:00
|
|
|
ongoingProcess.set('connecting' + $scope.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-05-20 11:50:55 -03:00
|
|
|
|
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);
|
2015-09-14 09:46:45 -03:00
|
|
|
return;
|
2015-10-16 11:11:49 -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-05 17:14:41 -03:00
|
|
|
if ($scope.seedSource.id == 'set') {
|
2016-08-30 17:07:49 -03:00
|
|
|
profileService.setBackupFlag(client.credentials.walletId);
|
2016-07-06 15:25:44 -03:00
|
|
|
}
|
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-03-06 12:00:10 -03:00
|
|
|
});
|