Wallet/src/js/controllers/create.js
2016-08-30 16:37:05 -03:00

221 lines
6.2 KiB
JavaScript

'use strict';
angular.module('copayApp.controllers').controller('createController',
function($scope, $rootScope, $timeout, $log, lodash, $state, profileService, configService, gettext, ledger, trezor, platformInfo, derivationPathHelper, ongoingProcess, walletService, storageService) {
var isChromeApp = platformInfo.isChromeApp;
var isCordova = platformInfo.isCordova;
var isDevel = platformInfo.isDevel;
var self = this;
var defaults = configService.getDefaults();
this.isWindowsPhoneApp = platformInfo.isWP && isCordova;
$scope.account = 1;
/* 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,
};
var defaults = configService.getDefaults();
$scope.bwsurl = defaults.bws.url;
$scope.derivationPath = derivationPathHelper.default;
// ng-repeat defined number of times instead of repeating over array?
this.getNumber = function(num) {
return new Array(num);
}
var updateRCSelect = function(n) {
$scope.totalCopayers = n;
var maxReq = COPAYER_PAIR_LIMITS[n];
self.RCValues = lodash.range(1, maxReq + 1);
$scope.requiredCopayers = Math.min(parseInt(n / 2 + 1), maxReq);
};
var updateSeedSourceSelect = function(n) {
self.seedOptions = [{
id: 'new',
label: gettext('Random'),
}, {
id: 'set',
label: gettext('Specify Recovery Phrase...'),
}];
$scope.seedSource = self.seedOptions[0];
if (n > 1 && isChromeApp)
self.seedOptions.push({
id: 'ledger',
label: 'Ledger Hardware Wallet',
});
if (isChromeApp || isDevel) {
self.seedOptions.push({
id: 'trezor',
label: 'Trezor Hardware Wallet',
});
}
};
this.TCValues = lodash.range(2, defaults.limits.totalCopayers + 1);
$scope.totalCopayers = defaults.wallet.totalCopayers;
this.setTotalCopayers = function(tc) {
updateRCSelect(tc);
updateSeedSourceSelect(tc);
self.seedSourceId = $scope.seedSource.id;
};
this.setSeedSource = function(src) {
self.seedSourceId = $scope.seedSource.id;
$timeout(function() {
$rootScope.$apply();
});
};
this.create = function(form) {
if (form && form.$invalid) {
this.error = gettext('Please enter the required fields');
return;
}
var opts = {
m: $scope.requiredCopayers,
n: $scope.totalCopayers,
name: $scope.walletName,
myName: $scope.totalCopayers > 1 ? $scope.myName : null,
networkName: $scope.testnetEnabled ? 'testnet' : 'livenet',
bwsurl: $scope.bwsurl,
singleAddress: $scope.singleAddressEnabled,
walletPrivKey: $scope._walletPrivKey, // Only for testing
};
var setSeed = self.seedSourceId == 'set';
if (setSeed) {
var words = $scope.privateKey || '';
if (words.indexOf(' ') == -1 && words.indexOf('prv') == 1 && words.length > 108) {
opts.extendedPrivateKey = words;
} else {
opts.mnemonic = words;
}
opts.passphrase = $scope.passphrase;
var pathData = derivationPathHelper.parse($scope.derivationPath);
if (!pathData) {
this.error = gettext('Invalid derivation path');
return;
}
opts.account = pathData.account;
opts.networkName = pathData.networkName;
opts.derivationStrategy = pathData.derivationStrategy;
} else {
opts.passphrase = $scope.createPassphrase;
}
if (setSeed && !opts.mnemonic && !opts.extendedPrivateKey) {
this.error = gettext('Please enter the wallet recovery phrase');
return;
}
if (self.seedSourceId == 'ledger' || self.seedSourceId == 'trezor') {
var account = $scope.account;
if (!account || account < 1) {
this.error = gettext('Invalid account number');
return;
}
if (self.seedSourceId == 'trezor')
account = account - 1;
opts.account = account;
ongoingProcess.set('connecting' + self.seedSourceId, true);
var src = self.seedSourceId == 'ledger' ? ledger : trezor;
src.getInfoForNewWallet(opts.n > 1, account, function(err, lopts) {
ongoingProcess.set('connecting' + self.seedSourceId, false);
if (err) {
self.error = err;
$scope.$apply();
return;
}
opts = lodash.assign(lopts, opts);
self._create(opts);
});
} else {
self._create(opts);
}
};
this._create = function(opts) {
ongoingProcess.set('creatingWallet', true);
$timeout(function() {
profileService.createWallet(opts, function(err, client) {
ongoingProcess.set('creatingWallet', false);
if (err) {
$log.warn(err);
self.error = err;
$timeout(function() {
$rootScope.$apply();
});
return;
}
walletService.updateRemotePreferences(client, {}, function() {
$log.debug('Remote preferences saved for:' + client.credentials.walletId)
});
if (self.seedSourceId == 'set') {
$timeout(function() {
$log.debug('Backup done');
storageService.setBackupFlag(wallet.credentials.walletId, function(err) {
$log.debug('Backup stored');
});
}, 1);
}
$state.go('tabs.home')
});
}, 100);
}
this.formFocus = function(what) {
if (!this.isWindowsPhoneApp) return
if (what && what == 'my-name') {
this.hideWalletName = true;
this.hideTabs = true;
} else if (what && what == 'wallet-name') {
this.hideTabs = true;
} else {
this.hideWalletName = false;
this.hideTabs = false;
}
$timeout(function() {
$rootScope.$digest();
}, 1);
};
$scope.$on("$destroy", function() {
$rootScope.hideWalletNavigation = false;
});
updateSeedSourceSelect(1);
self.setSeedSource();
});