Wallet/src/js/controllers/join.js

86 lines
2.5 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('joinController',
2015-07-17 15:53:50 +02:00
function($scope, $rootScope, $timeout, go, isMobile, notification, profileService, isCordova, isChromeApp, $modal, gettext, lodash, ledger) {
2015-03-06 12:00:10 -03:00
var self = this;
2015-07-17 15:53:50 +02:00
this.isChromeApp = function() {
return isChromeApp;
};
2015-03-06 12:00:10 -03: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
};
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;
}
self.loading = true;
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-07-17 15:53:50 +02:00
}
2015-09-04 12:25:54 -03:00
2015-09-03 01:49:48 -03:00
var setSeed = form.setSeed.$modelValue;
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;
} else {
opts.passphrase = form.createPassphrase.$modelValue;
}
2015-09-05 00:11:14 -03:00
if (setSeed && !opts.mnemonic && !opts.extendedPrivateKey) {
2015-09-03 01:49:48 -03:00
this.error = gettext('Please enter the wallet seed');
return;
}
2015-07-17 15:53:50 +02:00
if (form.hwLedger.$modelValue) {
self.ledger = true;
// TODO account / network
ledger.getInfoForNewWallet(0, opts.networkName, function(err, lopts) {
2015-07-17 15:53:50 +02:00
self.ledger = 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 {
self._join(opts);
}
};
this._join = function(opts) {
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-04-27 02:38:48 -03:00
return
2015-03-06 12:00:10 -03:00
}
$timeout(function() {
var fc = profileService.focusedClient;
2015-09-05 00:11:14 -03:00
if ( fc.isComplete() && (opts.mnemonic || opts.externalSource || opts.extendedPrivateKey)) {
$rootScope.$emit('Local/WalletImported', fc.credentials.walletId);
} else {
go.walletHome();
}
}, 2000);
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
});