2015-03-06 12:00:10 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
angular.module('copayApp.controllers').controller('importController',
|
2015-11-05 19:46:32 -03:00
|
|
|
function($scope, $rootScope, $location, $timeout, $log, profileService, configService, notification, go, sjcl, gettext, lodash, ledger, trezor, isChromeApp, isDevel, derivationPathHelper) {
|
2015-03-06 12:00:10 -03:00
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
var reader = new FileReader();
|
2015-10-16 12:35:18 -03:00
|
|
|
var defaults = configService.getDefaults();
|
|
|
|
|
$scope.bwsurl = defaults.bws.url;
|
2015-11-05 19:46:32 -03:00
|
|
|
$scope.derivationPath = derivationPathHelper.default;
|
|
|
|
|
$scope.account = 1;
|
2015-03-06 12:00:10 -03:00
|
|
|
|
|
|
|
|
window.ignoreMobilePause = true;
|
|
|
|
|
$scope.$on('$destroy', function() {
|
2015-08-24 17:09:59 -03:00
|
|
|
$timeout(function() {
|
2015-03-06 12:00:10 -03:00
|
|
|
window.ignoreMobilePause = false;
|
|
|
|
|
}, 100);
|
|
|
|
|
});
|
|
|
|
|
|
2015-11-04 15:45:33 -03:00
|
|
|
var updateSeedSourceSelect = function() {
|
|
|
|
|
self.seedOptions = [];
|
|
|
|
|
|
2015-11-05 16:00:38 -03:00
|
|
|
if (isChromeApp) {
|
|
|
|
|
self.seedOptions.push({
|
|
|
|
|
id: 'ledger',
|
2015-11-11 14:11:35 -03:00
|
|
|
label: 'Ledger Hardware Wallet',
|
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
|
|
|
});
|
|
|
|
|
$scope.seedSource = self.seedOptions[0];
|
|
|
|
|
}
|
2015-11-04 15:45:33 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-08-24 17:09:59 -03:00
|
|
|
this.setType = function(type) {
|
|
|
|
|
$scope.type = type;
|
2015-09-02 15:56:00 -03:00
|
|
|
this.error = null;
|
2015-08-24 17:09:59 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var _importBlob = function(str, opts) {
|
2015-04-30 13:03:30 -03:00
|
|
|
var str2, err;
|
2015-03-06 12:00:10 -03:00
|
|
|
try {
|
2015-08-24 17:09:59 -03:00
|
|
|
str2 = sjcl.decrypt(self.password, str);
|
2015-03-06 12:00:10 -03:00
|
|
|
} catch (e) {
|
2015-04-30 13:03:30 -03:00
|
|
|
err = gettext('Could not decrypt file, check your password');
|
2015-03-06 12:00:10 -03:00
|
|
|
$log.warn(e);
|
|
|
|
|
};
|
|
|
|
|
|
2015-04-30 13:03:30 -03:00
|
|
|
if (err) {
|
|
|
|
|
self.error = err;
|
2015-08-24 17:09:59 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
});
|
2015-04-30 13:03:30 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
self.loading = true;
|
2015-10-20 12:03:08 -03:00
|
|
|
opts.compressed = null;
|
|
|
|
|
opts.password = null;
|
2015-03-06 12:00:10 -03:00
|
|
|
|
|
|
|
|
$timeout(function() {
|
2015-10-20 12:03:08 -03:00
|
|
|
profileService.importWallet(str2, opts, function(err, walletId) {
|
2015-03-06 12:00:10 -03:00
|
|
|
self.loading = false;
|
|
|
|
|
if (err) {
|
|
|
|
|
self.error = err;
|
2015-08-24 17:09:59 -03:00
|
|
|
} else {
|
2015-10-20 12:46:46 -03:00
|
|
|
$rootScope.$emit('Local/WalletImported', walletId);
|
|
|
|
|
notification.success(gettext('Success'), gettext('Your wallet has been imported correctly'));
|
2016-02-22 19:32:24 -03:00
|
|
|
go.walletHome();
|
2015-03-06 12:00:10 -03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, 100);
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-20 12:03:08 -03:00
|
|
|
var _importExtendedPrivateKey = function(xPrivKey, opts) {
|
2016-05-03 10:42:18 -03:00
|
|
|
console.log('[import.js.84:xPrivKey:]',xPrivKey); //TODO
|
2015-09-05 00:11:14 -03:00
|
|
|
self.loading = true;
|
|
|
|
|
|
|
|
|
|
$timeout(function() {
|
2015-10-20 12:03:08 -03:00
|
|
|
profileService.importExtendedPrivateKey(xPrivKey, opts, function(err, walletId) {
|
2015-09-05 00:11:14 -03:00
|
|
|
self.loading = false;
|
|
|
|
|
if (err) {
|
|
|
|
|
self.error = err;
|
|
|
|
|
return $timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-10-20 12:46:46 -03:00
|
|
|
$rootScope.$emit('Local/WalletImported', walletId);
|
|
|
|
|
notification.success(gettext('Success'), gettext('Your wallet has been imported correctly'));
|
2016-02-29 18:41:10 -03:00
|
|
|
go.walletHome();
|
2015-09-05 00:11:14 -03:00
|
|
|
});
|
|
|
|
|
}, 100);
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-03 01:49:48 -03:00
|
|
|
var _importMnemonic = function(words, opts) {
|
2015-08-24 17:09:59 -03:00
|
|
|
self.loading = true;
|
|
|
|
|
|
|
|
|
|
$timeout(function() {
|
2015-09-05 00:11:14 -03:00
|
|
|
profileService.importMnemonic(words, opts, function(err, walletId) {
|
2015-08-24 17:09:59 -03:00
|
|
|
self.loading = false;
|
|
|
|
|
if (err) {
|
|
|
|
|
self.error = err;
|
|
|
|
|
return $timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-10-20 12:46:46 -03:00
|
|
|
$rootScope.$emit('Local/WalletImported', walletId);
|
|
|
|
|
notification.success(gettext('Success'), gettext('Your wallet has been imported correctly'));
|
2016-02-29 18:41:10 -03:00
|
|
|
go.walletHome();
|
2015-08-24 17:09:59 -03:00
|
|
|
});
|
|
|
|
|
}, 100);
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
$scope.getFile = function() {
|
|
|
|
|
// If we use onloadend, we need to check the readyState.
|
|
|
|
|
reader.onloadend = function(evt) {
|
|
|
|
|
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
|
2015-10-20 12:03:08 -03:00
|
|
|
var opts = {};
|
|
|
|
|
opts.bwsurl = $scope.bwsurl;
|
|
|
|
|
_importBlob(evt.target.result, opts);
|
2015-03-06 12:00:10 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-24 17:09:59 -03:00
|
|
|
this.importBlob = function(form) {
|
2015-03-06 12:00:10 -03:00
|
|
|
if (form.$invalid) {
|
2015-04-29 19:19:10 -03:00
|
|
|
this.error = gettext('There is an error in the form');
|
2015-08-24 17:09:59 -03:00
|
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
2015-03-06 12:00:10 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var backupFile = $scope.file;
|
|
|
|
|
var backupText = form.backupText.$modelValue;
|
|
|
|
|
var password = form.password.$modelValue;
|
|
|
|
|
|
|
|
|
|
if (!backupFile && !backupText) {
|
2015-04-29 19:19:10 -03:00
|
|
|
this.error = gettext('Please, select your backup file');
|
2015-08-24 17:09:59 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (backupFile) {
|
|
|
|
|
reader.readAsBinaryString(backupFile);
|
|
|
|
|
} else {
|
2015-10-20 12:03:08 -03:00
|
|
|
var opts = {};
|
|
|
|
|
opts.bwsurl = $scope.bwsurl;
|
|
|
|
|
_importBlob(backupText, opts);
|
2015-08-24 17:09:59 -03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.importMnemonic = function(form) {
|
|
|
|
|
if (form.$invalid) {
|
|
|
|
|
this.error = gettext('There is an error in the form');
|
|
|
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var opts = {};
|
2015-10-20 12:03:08 -03:00
|
|
|
if ($scope.bwsurl)
|
|
|
|
|
opts.bwsurl = $scope.bwsurl;
|
2015-08-24 17:09:59 -03:00
|
|
|
|
|
|
|
|
var passphrase = form.passphrase.$modelValue;
|
|
|
|
|
var words = form.words.$modelValue;
|
2015-09-02 15:56:00 -03:00
|
|
|
this.error = null;
|
2015-08-24 17:09:59 -03:00
|
|
|
|
2015-09-02 15:56:00 -03:00
|
|
|
if (!words) {
|
2015-09-04 22:15:50 -03:00
|
|
|
this.error = gettext('Please enter the seed words');
|
2015-10-16 12:35:18 -03:00
|
|
|
} else if (words.indexOf('xprv') == 0 || words.indexOf('tprv') == 0) {
|
2015-10-20 12:03:08 -03:00
|
|
|
return _importExtendedPrivateKey(words, opts);
|
2015-09-02 15:56:00 -03:00
|
|
|
} else {
|
2015-09-15 12:07:34 -03:00
|
|
|
var wordList = words.split(/[\u3000\s]+/);
|
2015-09-05 01:02:01 -03:00
|
|
|
|
2015-09-15 13:08:34 -03:00
|
|
|
if ((wordList.length % 3) != 0)
|
2015-09-04 22:15:50 -03:00
|
|
|
this.error = gettext('Wrong number of seed words:') + wordList.length;
|
2015-09-02 15:56:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.error) {
|
2015-08-24 17:09:59 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
return;
|
2015-03-06 12:00:10 -03:00
|
|
|
}
|
2015-08-24 17:09:59 -03:00
|
|
|
|
|
|
|
|
opts.passphrase = form.passphrase.$modelValue || null;
|
2015-11-05 19:46:32 -03:00
|
|
|
|
|
|
|
|
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-08-24 17:09:59 -03:00
|
|
|
|
2015-09-03 01:49:48 -03:00
|
|
|
_importMnemonic(words, opts);
|
2015-03-06 12:00:10 -03:00
|
|
|
};
|
2015-09-04 21:18:20 -03:00
|
|
|
|
2015-11-04 15:45:33 -03:00
|
|
|
this.importTrezor = function(account, isMultisig) {
|
2015-09-29 12:45:06 -03:00
|
|
|
var self = this;
|
2015-11-04 00:53:26 -03:00
|
|
|
trezor.getInfoForNewWallet(isMultisig, account, function(err, lopts) {
|
2015-09-29 12:45:06 -03:00
|
|
|
self.hwWallet = false;
|
|
|
|
|
if (err) {
|
|
|
|
|
self.error = err;
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-10-20 12:46:46 -03:00
|
|
|
|
2015-09-29 12:45:06 -03:00
|
|
|
lopts.externalSource = 'trezor';
|
2015-10-20 12:46:46 -03:00
|
|
|
lopts.bwsurl = $scope.bwsurl;
|
2015-09-29 12:45:06 -03:00
|
|
|
self.loading = true;
|
|
|
|
|
$log.debug('Import opts', lopts);
|
2015-10-20 12:46:46 -03:00
|
|
|
|
2015-09-29 12:45:06 -03:00
|
|
|
profileService.importExtendedPublicKey(lopts, function(err, walletId) {
|
|
|
|
|
self.loading = false;
|
|
|
|
|
if (err) {
|
|
|
|
|
self.error = err;
|
|
|
|
|
return $timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-10-20 12:46:46 -03:00
|
|
|
$rootScope.$emit('Local/WalletImported', walletId);
|
|
|
|
|
notification.success(gettext('Success'), gettext('Your wallet has been imported correctly'));
|
|
|
|
|
go.walletHome();
|
2015-09-29 12:45:06 -03:00
|
|
|
});
|
|
|
|
|
}, 100);
|
|
|
|
|
};
|
|
|
|
|
|
2015-11-04 15:45:33 -03:00
|
|
|
this.importHW = function(form) {
|
2015-11-05 19:46:32 -03:00
|
|
|
if (form.$invalid || $scope.account < 0 ) {
|
2015-09-04 21:18:20 -03:00
|
|
|
this.error = gettext('There is an error in the form');
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-11-05 19:46:32 -03:00
|
|
|
this.error = '';
|
2015-11-04 15:45:33 -03:00
|
|
|
|
2015-11-05 19:46:32 -03:00
|
|
|
var account = + $scope.account;
|
|
|
|
|
|
|
|
|
|
if (self.seedSourceId == 'trezor') {
|
|
|
|
|
if ( account < 1) {
|
|
|
|
|
this.error = gettext('Invalid account number');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
account = account - 1;
|
|
|
|
|
}
|
2015-11-04 15:45:33 -03:00
|
|
|
var isMultisig = form.isMultisig.$modelValue;
|
|
|
|
|
|
|
|
|
|
switch (self.seedSourceId) {
|
|
|
|
|
case ('ledger'):
|
|
|
|
|
self.hwWallet = 'Ledger';
|
|
|
|
|
self.importLedger(account);
|
|
|
|
|
break;
|
|
|
|
|
case ('trezor'):
|
|
|
|
|
self.hwWallet = 'Trezor';
|
|
|
|
|
self.importTrezor(account, isMultisig);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw ('Error: bad source id');
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.setSeedSource = function() {
|
|
|
|
|
if (!$scope.seedSource) return;
|
|
|
|
|
self.seedSourceId = $scope.seedSource.id;
|
|
|
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$rootScope.$apply();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.importLedger = function(account) {
|
|
|
|
|
var self = this;
|
2015-11-04 00:53:26 -03:00
|
|
|
ledger.getInfoForNewWallet(true, account, function(err, lopts) {
|
2015-09-29 12:45:06 -03:00
|
|
|
self.hwWallet = false;
|
2015-09-04 21:18:20 -03:00
|
|
|
if (err) {
|
|
|
|
|
self.error = err;
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-10-20 12:46:46 -03:00
|
|
|
|
2015-09-04 21:18:20 -03:00
|
|
|
lopts.externalSource = 'ledger';
|
2015-10-20 12:46:46 -03:00
|
|
|
lopts.bwsurl = $scope.bwsurl;
|
2015-09-04 21:18:20 -03:00
|
|
|
self.loading = true;
|
|
|
|
|
$log.debug('Import opts', lopts);
|
2015-10-20 12:46:46 -03:00
|
|
|
|
2015-09-04 21:18:20 -03:00
|
|
|
profileService.importExtendedPublicKey(lopts, function(err, walletId) {
|
|
|
|
|
self.loading = false;
|
|
|
|
|
if (err) {
|
|
|
|
|
self.error = err;
|
|
|
|
|
return $timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-10-20 12:46:46 -03:00
|
|
|
$rootScope.$emit('Local/WalletImported', walletId);
|
|
|
|
|
notification.success(gettext('Success'), gettext('Your wallet has been imported correctly'));
|
2016-02-22 19:32:24 -03:00
|
|
|
go.walletHome();
|
2015-09-04 21:18:20 -03:00
|
|
|
});
|
|
|
|
|
}, 100);
|
|
|
|
|
};
|
|
|
|
|
|
2015-11-04 15:45:33 -03:00
|
|
|
updateSeedSourceSelect();
|
|
|
|
|
self.setSeedSource('new');
|
2015-03-06 12:00:10 -03:00
|
|
|
});
|