2015-03-06 12:00:10 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
angular.module('copayApp.controllers').controller('createController',
|
2016-05-30 16:32:28 -03:00
|
|
|
function($scope, $ionicScrollDelegate, $rootScope, $timeout, $log, lodash, go, profileService, configService, gettext, ledger, trezor, platformInfo, derivationPathHelper) {
|
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
|
|
|
var self = this;
|
|
|
|
|
var defaults = configService.getDefaults();
|
2016-05-31 14:55:08 -03:00
|
|
|
this.isWindowsPhoneApp = platformInfo.isWP && isCordova;
|
2015-11-05 19:46:32 -03:00
|
|
|
$scope.account = 1;
|
2015-09-05 00:11:14 -03:00
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-16 11:11:49 -03:00
|
|
|
var defaults = configService.getDefaults();
|
|
|
|
|
$scope.bwsurl = defaults.bws.url;
|
2015-11-05 19:46:32 -03:00
|
|
|
$scope.derivationPath = derivationPathHelper.default;
|
2015-10-16 11:11:49 -03:00
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
// ng-repeat defined number of times instead of repeating over array?
|
|
|
|
|
this.getNumber = function(num) {
|
|
|
|
|
return new Array(num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var updateRCSelect = function(n) {
|
2015-05-20 04:05:13 -03:00
|
|
|
$scope.totalCopayers = n;
|
2015-03-06 12:00:10 -03:00
|
|
|
var maxReq = COPAYER_PAIR_LIMITS[n];
|
|
|
|
|
self.RCValues = lodash.range(1, maxReq + 1);
|
|
|
|
|
$scope.requiredCopayers = Math.min(parseInt(n / 2 + 1), maxReq);
|
|
|
|
|
};
|
|
|
|
|
|
2015-11-04 00:53:26 -03:00
|
|
|
var updateSeedSourceSelect = function(n) {
|
|
|
|
|
|
|
|
|
|
self.seedOptions = [{
|
|
|
|
|
id: 'new',
|
2016-05-09 20:23:20 +02:00
|
|
|
label: gettext('New Random Recovery Phrase'),
|
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
|
|
|
}];
|
|
|
|
|
$scope.seedSource = self.seedOptions[0];
|
|
|
|
|
|
2015-11-05 16:00:38 -03:00
|
|
|
if (n > 1 && isChromeApp)
|
2015-11-04 00:53:26 -03:00
|
|
|
self.seedOptions.push({
|
|
|
|
|
id: 'ledger',
|
2015-11-11 14:11:35 -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) {
|
|
|
|
|
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 00:53:26 -03:00
|
|
|
};
|
|
|
|
|
|
2015-05-20 04:05:13 -03:00
|
|
|
this.TCValues = lodash.range(2, defaults.limits.totalCopayers + 1);
|
2015-03-06 12:00:10 -03:00
|
|
|
$scope.totalCopayers = defaults.wallet.totalCopayers;
|
|
|
|
|
|
2015-05-20 04:05:13 -03:00
|
|
|
this.setTotalCopayers = function(tc) {
|
|
|
|
|
updateRCSelect(tc);
|
2015-11-04 00:53:26 -03:00
|
|
|
updateSeedSourceSelect(tc);
|
2015-11-04 01:54:54 -03:00
|
|
|
self.seedSourceId = $scope.seedSource.id;
|
2015-11-04 00:53:26 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.setSeedSource = function(src) {
|
|
|
|
|
self.seedSourceId = $scope.seedSource.id;
|
2015-11-04 01:54:54 -03:00
|
|
|
|
2015-11-04 00:53:26 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
});
|
2015-05-20 04:05:13 -03:00
|
|
|
};
|
|
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
this.create = function(form) {
|
|
|
|
|
if (form && form.$invalid) {
|
2015-04-29 19:19:10 -03:00
|
|
|
this.error = gettext('Please enter the required fields');
|
2016-05-30 16:32:28 -03:00
|
|
|
$ionicScrollDelegate.scrollTop();
|
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 = {
|
|
|
|
|
m: $scope.requiredCopayers,
|
|
|
|
|
n: $scope.totalCopayers,
|
2016-02-08 13:04:14 -05:00
|
|
|
name: $scope.walletName,
|
|
|
|
|
myName: $scope.totalCopayers > 1 ? $scope.myName : null,
|
2016-05-19 18:18:05 -03:00
|
|
|
networkName: $scope.testnetEnabled ? 'testnet' : 'livenet',
|
2015-11-04 17:50:05 -03:00
|
|
|
bwsurl: $scope.bwsurl,
|
2016-06-07 10:37:06 -03:00
|
|
|
singleAddress: $scope.singleAddressEnabled,
|
2016-05-20 11:50:55 -03:00
|
|
|
walletPrivKey: $scope._walletPrivKey, // Only for testing
|
2015-03-06 12:00:10 -03:00
|
|
|
};
|
2015-11-05 19:46:32 -03:00
|
|
|
var setSeed = self.seedSourceId == 'set';
|
2015-09-05 00:11:14 -03:00
|
|
|
if (setSeed) {
|
2015-11-05 19:46:32 -03:00
|
|
|
|
2016-02-08 13:04:14 -05:00
|
|
|
var words = $scope.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-02-08 13:04:14 -05:00
|
|
|
opts.passphrase = $scope.passphrase;
|
2015-11-05 19:46:32 -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();
|
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-02-08 13:04:14 -05:00
|
|
|
opts.passphrase = $scope.createPassphrase;
|
2015-09-03 01:49:48 -03:00
|
|
|
}
|
|
|
|
|
|
2015-09-05 00:11:14 -03:00
|
|
|
if (setSeed && !opts.mnemonic && !opts.extendedPrivateKey) {
|
2016-05-09 20:23:20 +02:00
|
|
|
this.error = gettext('Please enter the wallet recovery phrase');
|
2016-05-30 16:32:28 -03:00
|
|
|
$ionicScrollDelegate.scrollTop();
|
2015-09-03 01:49:48 -03:00
|
|
|
return;
|
|
|
|
|
}
|
2015-09-05 00:11:14 -03:00
|
|
|
|
2015-11-04 01:54:54 -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) {
|
|
|
|
|
this.error = gettext('Invalid account number');
|
2016-05-30 16:32:28 -03:00
|
|
|
$ionicScrollDelegate.scrollTop();
|
2015-11-04 01:54:54 -03:00
|
|
|
return;
|
|
|
|
|
}
|
2015-11-05 19:46:32 -03:00
|
|
|
|
2016-05-19 18:18:05 -03:00
|
|
|
if (self.seedSourceId == 'trezor')
|
2015-11-05 19:46:32 -03:00
|
|
|
account = account - 1;
|
|
|
|
|
|
|
|
|
|
opts.account = account;
|
2015-11-04 01:54:54 -03:00
|
|
|
self.hwWallet = self.seedSourceId == 'ledger' ? 'Ledger' : 'Trezor';
|
|
|
|
|
var src = self.seedSourceId == '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) {
|
2015-09-26 08:26:31 -03:00
|
|
|
self.hwWallet = false;
|
2015-09-04 10:17:59 -03:00
|
|
|
if (err) {
|
|
|
|
|
self.error = err;
|
2016-05-30 16:32:28 -03:00
|
|
|
$ionicScrollDelegate.scrollTop();
|
2015-07-17 15:53:50 +02:00
|
|
|
$scope.$apply();
|
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);
|
|
|
|
|
self._create(opts);
|
2015-07-17 15:53:50 +02:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
self._create(opts);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-05 00:11:14 -03:00
|
|
|
this._create = function(opts) {
|
2015-09-04 10:17:59 -03:00
|
|
|
self.loading = true;
|
2015-03-06 12:00:10 -03:00
|
|
|
$timeout(function() {
|
2016-05-20 11:50:55 -03:00
|
|
|
|
2016-06-06 12:21:15 -03:00
|
|
|
profileService.createWallet(opts, function(err) {
|
2015-03-06 12:00:10 -03:00
|
|
|
self.loading = false;
|
|
|
|
|
if (err) {
|
2015-09-04 10:17:59 -03:00
|
|
|
$log.warn(err);
|
2015-04-30 13:03:30 -03:00
|
|
|
self.error = err;
|
2016-05-30 16:32:28 -03:00
|
|
|
$ionicScrollDelegate.scrollTop();
|
2015-08-12 08:57:44 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
});
|
2015-09-14 09:46:45 -03:00
|
|
|
return;
|
2015-10-16 11:11:49 -03:00
|
|
|
}
|
2016-02-22 19:32:24 -03:00
|
|
|
go.walletHome();
|
2015-10-16 11:11:49 -03:00
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
});
|
|
|
|
|
}, 100);
|
2015-07-17 15:53:50 +02:00
|
|
|
}
|
2015-09-05 00:11:14 -03:00
|
|
|
|
2015-04-14 16:20:12 -03:00
|
|
|
this.formFocus = function(what) {
|
|
|
|
|
if (!this.isWindowsPhoneApp) return
|
|
|
|
|
|
|
|
|
|
if (what && what == 'my-name') {
|
|
|
|
|
this.hideWalletName = true;
|
2015-05-20 15:16:39 -03:00
|
|
|
this.hideTabs = true;
|
2015-09-05 00:11:14 -03:00
|
|
|
} else if (what && what == 'wallet-name') {
|
2015-07-17 15:53:50 +02:00
|
|
|
this.hideTabs = true;
|
2015-09-05 00:11:14 -03:00
|
|
|
} else {
|
2015-04-29 19:19:10 -03:00
|
|
|
this.hideWalletName = false;
|
2015-05-20 15:16:39 -03:00
|
|
|
this.hideTabs = false;
|
2015-04-14 16:20:12 -03:00
|
|
|
}
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$rootScope.$digest();
|
|
|
|
|
}, 1);
|
|
|
|
|
};
|
2015-03-06 12:00:10 -03:00
|
|
|
|
|
|
|
|
$scope.$on("$destroy", function() {
|
|
|
|
|
$rootScope.hideWalletNavigation = false;
|
|
|
|
|
});
|
2015-11-04 15:45:33 -03:00
|
|
|
|
2015-11-04 00:53:26 -03:00
|
|
|
updateSeedSourceSelect(1);
|
2015-11-04 15:45:33 -03:00
|
|
|
self.setSeedSource('new');
|
2015-03-06 12:00:10 -03:00
|
|
|
});
|