2015-03-06 12:00:10 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
angular.module('copayApp.controllers').controller('createController',
|
2015-07-17 15:53:50 +02:00
|
|
|
function($scope, $rootScope, $location, $timeout, $log, lodash, go, profileService, configService, isMobile, isCordova, gettext, isChromeApp, ledger) {
|
2015-03-06 12:00:10 -03:00
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
var defaults = configService.getDefaults();
|
2015-04-14 16:20:12 -03:00
|
|
|
this.isWindowsPhoneApp = isMobile.Windows() && isCordova;
|
2015-07-17 15:53:50 +02: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,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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-07-17 16:38:52 +02:00
|
|
|
this.externalIndexValues = lodash.range(0,20);
|
2015-07-17 15:53:50 +02:00
|
|
|
$scope.externalIndex = 0;
|
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-07-17 15:53:50 +02:00
|
|
|
this.isChromeApp = function() {
|
|
|
|
|
return isChromeApp;
|
|
|
|
|
};
|
|
|
|
|
|
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');
|
2015-03-06 12:00:10 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var opts = {
|
|
|
|
|
m: $scope.requiredCopayers,
|
|
|
|
|
n: $scope.totalCopayers,
|
|
|
|
|
name: form.walletName.$modelValue,
|
2015-08-31 18:10:46 -03:00
|
|
|
mnemonic: form.privateKey.$modelValue,
|
2015-03-06 12:00:10 -03:00
|
|
|
myName: $scope.totalCopayers > 1 ? form.myName.$modelValue : null,
|
2015-07-17 15:53:50 +02:00
|
|
|
networkName: form.isTestnet.$modelValue ? 'testnet' : 'livenet',
|
2015-03-06 12:00:10 -03:00
|
|
|
};
|
|
|
|
|
self.loading = true;
|
|
|
|
|
|
2015-07-17 15:53:50 +02:00
|
|
|
if (form.hwLedger.$modelValue) {
|
|
|
|
|
self.ledger = true;
|
|
|
|
|
ledger.getXPubKey($scope.externalIndex, function(data) {
|
|
|
|
|
self.ledger = false;
|
|
|
|
|
if (data.success) {
|
2015-07-23 10:47:03 -07:00
|
|
|
$scope.$apply();
|
2015-07-17 15:53:50 +02:00
|
|
|
opts.extendedPublicKey = data.xpubkey;
|
|
|
|
|
opts.externalSource = 'ledger';
|
|
|
|
|
opts.externalIndex = $scope.externalIndex;
|
|
|
|
|
self._create(opts);
|
|
|
|
|
} else {
|
|
|
|
|
self.loading = false;
|
|
|
|
|
self.error = data.message;
|
|
|
|
|
$scope.$apply();
|
2015-07-23 10:47:03 -07:00
|
|
|
$log.debug(data.message);
|
2015-07-17 15:53:50 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
self._create(opts);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this._create = function (opts) {
|
2015-03-06 12:00:10 -03:00
|
|
|
$timeout(function() {
|
2015-09-02 17:11:59 -03:00
|
|
|
profileService.createWallet(opts, function(err, secret, walletId) {
|
2015-03-06 12:00:10 -03:00
|
|
|
self.loading = false;
|
|
|
|
|
if (err) {
|
2015-07-17 15:53:50 +02:00
|
|
|
if (err == "Error creating wallet" && opts.extendedPublicKey) {
|
2015-07-17 16:49:56 +02:00
|
|
|
err = gettext("This xpub index is already used by another wallet. Please select another index.");
|
2015-07-17 15:53:50 +02:00
|
|
|
}
|
2015-03-06 12:00:10 -03:00
|
|
|
$log.debug(err);
|
2015-04-30 13:03:30 -03:00
|
|
|
self.error = err;
|
2015-08-12 08:57:44 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
});
|
2015-03-06 12:00:10 -03:00
|
|
|
}
|
|
|
|
|
else {
|
2015-09-02 17:11:59 -03:00
|
|
|
if (opts.mnemonic && opts.n==1) {
|
|
|
|
|
$rootScope.$emit('Local/WalletImported', walletId);
|
|
|
|
|
} else {
|
|
|
|
|
go.walletHome();
|
|
|
|
|
}
|
2015-03-06 12:00:10 -03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, 100);
|
2015-07-17 15:53:50 +02: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;
|
|
|
|
|
}
|
|
|
|
|
else if (what && what == 'wallet-name'){
|
2015-07-17 15:53:50 +02:00
|
|
|
this.hideTabs = true;
|
2015-04-14 16:20:12 -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;
|
|
|
|
|
});
|
|
|
|
|
});
|