Wallet/src/js/controllers/join.js

142 lines
3.9 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('joinController',
function($scope, $rootScope, $timeout, go, notification, profileService, configService, isCordova, storageService, applicationService, $modal, gettext, lodash, ledger, trezor, isChromeApp, isDevel,derivationPathHelper) {
2015-03-06 12:00:10 -03:00
var self = this;
2015-10-16 13:00:41 -03:00
var defaults = configService.getDefaults();
$scope.bwsurl = defaults.bws.url;
$scope.derivationPath = derivationPathHelper.default;
2015-11-11 16:23:56 -03:00
$scope.account = 1;
2015-07-17 15:53:50 +02:00
this.onQrCodeScanned = function(data) {
$scope.secret = data;
$scope.joinForm.secret.$setViewValue(data);
$scope.joinForm.secret.$render();
2015-03-06 12:00:10 -03:00
};
2015-11-04 15:45:33 -03:00
var updateSeedSourceSelect = function() {
self.seedOptions = [{
id: 'new',
label: gettext('New Random Recovery Phrase'),
2015-11-04 15:45:33 -03:00
}, {
id: 'set',
label: gettext('Specify Recovery Phrase...'),
2015-11-04 15:45:33 -03:00
}];
$scope.seedSource = self.seedOptions[0];
2015-11-05 16:00:38 -03:00
if (isChromeApp) {
self.seedOptions.push({
id: 'ledger',
label: 'Ledger Hardware Wallet',
2015-11-05 16:00:38 -03:00
});
}
if (isChromeApp || isDevel) {
self.seedOptions.push({
id: 'trezor',
label: 'Trezor Hardware Wallet',
2015-11-05 16:00:38 -03:00
});
}
2015-11-04 15:45:33 -03:00
};
this.setSeedSource = function(src) {
self.seedSourceId = $scope.seedSource.id;
$timeout(function() {
$rootScope.$apply();
});
};
2015-03-06 12:00:10 -03:00
this.join = function(form) {
if (form && form.$invalid) {
self.error = gettext('Please enter the required fields');
2015-03-06 12:00:10 -03:00
return;
}
2015-07-17 15:53:50 +02:00
var opts = {
secret: form.secret.$modelValue,
2015-09-04 12:25:54 -03:00
myName: form.myName.$modelValue,
2015-11-04 18:08:22 -03:00
bwsurl: $scope.bwsurl,
2015-07-17 15:53:50 +02:00
}
2015-09-04 12:25:54 -03:00
2015-11-04 15:45:33 -03:00
var setSeed = self.seedSourceId =='set';
2015-10-16 13:00:41 -03:00
if (setSeed) {
2015-09-05 00:11:14 -03:00
var words = form.privateKey.$modelValue;
if (words.indexOf(' ') == -1 && words.indexOf('prv') == 1 && words.length > 108) {
opts.extendedPrivateKey = words;
} else {
opts.mnemonic = words;
}
2015-09-03 01:49:48 -03:00
opts.passphrase = form.passphrase.$modelValue;
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;
2015-09-03 01:49:48 -03:00
} else {
opts.passphrase = form.createPassphrase.$modelValue;
}
2015-09-05 00:11:14 -03:00
if (setSeed && !opts.mnemonic && !opts.extendedPrivateKey) {
this.error = gettext('Please enter the wallet recovery phrase');
2015-09-03 01:49:48 -03:00
return;
}
2015-10-16 13:00:41 -03:00
2015-11-04 15:45:33 -03:00
if (self.seedSourceId == 'ledger' || self.seedSourceId == 'trezor') {
var account = $scope.account;
if (!account || account < 1) {
this.error = gettext('Invalid account number');
2015-11-04 15:45:33 -03:00
return;
}
if ( self.seedSourceId == 'trezor')
account = account - 1;
2015-11-04 18:08:22 -03:00
opts.account = account;
2015-11-04 15:45:33 -03:00
self.hwWallet = self.seedSourceId == 'ledger' ? 'Ledger' : 'Trezor';
var src = self.seedSourceId == 'ledger' ? ledger : trezor;
2015-11-04 00:53:26 -03:00
src.getInfoForNewWallet(true, account, function(err, lopts) {
self.hwWallet = false;
2015-09-04 12:25:54 -03:00
if (err) {
self.error = err;
2015-07-17 15:53:50 +02:00
$scope.$apply();
2015-09-04 12:25:54 -03:00
return;
2015-07-17 15:53:50 +02:00
}
2015-09-04 12:25:54 -03:00
opts = lodash.assign(lopts, opts);
self._join(opts);
2015-07-17 15:53:50 +02:00
});
} else {
2015-07-17 15:53:50 +02:00
self._join(opts);
}
};
this._join = function(opts) {
2015-11-11 16:23:56 -03:00
self.loading = true;
2015-03-06 12:00:10 -03:00
$timeout(function() {
2015-07-17 15:53:50 +02:00
profileService.joinWallet(opts, function(err) {
2015-03-06 12:00:10 -03:00
if (err) {
2015-04-27 02:38:48 -03:00
self.loading = false;
self.error = err;
2015-04-24 03:37:18 -03:00
$rootScope.$apply();
2015-10-19 17:26:15 -03:00
return;
2015-03-06 12:00:10 -03:00
}
2016-02-22 19:32:24 -03:00
go.walletHome();
2015-03-06 12:00:10 -03:00
});
}, 100);
2015-07-17 15:53:50 +02:00
};
2015-11-04 15:45:33 -03:00
updateSeedSourceSelect();
self.setSeedSource('new');
2015-03-06 12:00:10 -03:00
});