Ledger hardware wallet support

This commit is contained in:
Eric Larchevêque 2015-07-17 15:53:50 +02:00 committed by Matias Alejo Garcia
commit d3f77b37ad
9 changed files with 693 additions and 23 deletions

View file

@ -1,12 +1,12 @@
'use strict';
angular.module('copayApp.controllers').controller('createController',
function($scope, $rootScope, $location, $timeout, $log, lodash, go, profileService, configService, isMobile, isCordova, gettext) {
function($scope, $rootScope, $location, $timeout, $log, lodash, go, profileService, configService, isMobile, isCordova, gettext, isChromeApp, ledger) {
var self = this;
var defaults = configService.getDefaults();
this.isWindowsPhoneApp = isMobile.Windows() && isCordova;
/* For compressed keys, m*73 + n*34 <= 496 */
var COPAYER_PAIR_LIMITS = {
1: 1,
@ -35,6 +35,8 @@ angular.module('copayApp.controllers').controller('createController',
$scope.requiredCopayers = Math.min(parseInt(n / 2 + 1), maxReq);
};
this.externatIndexValues = lodash.range(0,20);
$scope.externalIndex = 0;
this.TCValues = lodash.range(2, defaults.limits.totalCopayers + 1);
$scope.totalCopayers = defaults.wallet.totalCopayers;
@ -42,6 +44,10 @@ angular.module('copayApp.controllers').controller('createController',
updateRCSelect(tc);
};
this.isChromeApp = function() {
return isChromeApp;
};
this.create = function(form) {
if (form && form.$invalid) {
this.error = gettext('Please enter the required fields');
@ -53,14 +59,40 @@ angular.module('copayApp.controllers').controller('createController',
name: form.walletName.$modelValue,
extendedPrivateKey: form.privateKey.$modelValue,
myName: $scope.totalCopayers > 1 ? form.myName.$modelValue : null,
networkName: form.isTestnet.$modelValue ? 'testnet' : 'livenet'
networkName: form.isTestnet.$modelValue ? 'testnet' : 'livenet',
};
self.loading = true;
if (form.hwLedger.$modelValue) {
self.ledger = true;
ledger.getXPubKey($scope.externalIndex, function(data) {
self.ledger = false;
$scope.$apply();
if (data.success) {
opts.extendedPublicKey = data.xpubkey;
opts.externalSource = 'ledger';
opts.externalIndex = $scope.externalIndex;
self._create(opts);
} else {
self.loading = false;
$log.debug(data.message);
self.error = data.message;
$scope.$apply();
}
});
} else {
self._create(opts);
}
};
this._create = function (opts) {
$timeout(function() {
profileService.createWallet(opts, function(err, secret) {
self.loading = false;
if (err) {
if (err == "Error creating wallet" && opts.extendedPublicKey) {
err = "This xpub index is already used by another wallet. Please select another index."
}
$log.debug(err);
self.error = err;
$timeout(function() {
@ -72,7 +104,7 @@ angular.module('copayApp.controllers').controller('createController',
}
});
}, 100);
};
}
this.formFocus = function(what) {
if (!this.isWindowsPhoneApp) return
@ -82,7 +114,7 @@ angular.module('copayApp.controllers').controller('createController',
this.hideTabs = true;
}
else if (what && what == 'wallet-name'){
this.hideTabs = true;
this.hideTabs = true;
}
else {
this.hideWalletName = false;

View file

@ -1,9 +1,15 @@
'use strict';
angular.module('copayApp.controllers').controller('joinController',
function($scope, $rootScope, $timeout, go, isMobile, notification, profileService, isCordova, $modal, gettext) {
function($scope, $rootScope, $timeout, go, isMobile, notification, profileService, isCordova, isChromeApp, $modal, gettext, lodash, ledger) {
var self = this;
this.externatIndexValues = lodash.range(0,20);
$scope.externalIndex = 0;
this.isChromeApp = function() {
return isChromeApp;
};
//TODO : make one function - this was copied from topbar.js
var cordovaOpenScanner = function() {
@ -145,12 +151,37 @@ angular.module('copayApp.controllers').controller('joinController',
}
self.loading = true;
var opts = {
secret: form.secret.$modelValue,
extendedPrivateKey: form.privateKey.$modelValue,
myName: form.myName.$modelValue
}
if (form.hwLedger.$modelValue) {
self.ledger = true;
ledger.getXPubKey($scope.externalIndex, function(data) {
self.ledger = false;
$scope.$apply();
if (data.success) {
opts.extendedPublicKey = data.xpubkey;
opts.externalSource = 'ledger';
opts.externalIndex = $scope.externalIndex;
self._join(opts);
} else {
self.loading = false;
$log.debug(data.message);
self.error = data.message;
$scope.$apply();
}
});
} else {
self._join(opts);
}
};
this._join = function(opts) {
$timeout(function() {
profileService.joinWallet({
secret: form.secret.$modelValue,
extendedPrivateKey: form.privateKey.$modelValue,
myName: form.myName.$modelValue
}, function(err) {
profileService.joinWallet(opts, function(err) {
if (err) {
self.loading = false;
self.error = err;
@ -162,5 +193,5 @@ angular.module('copayApp.controllers').controller('joinController',
}, 2000);
});
}, 100);
}
};
});

View file

@ -13,8 +13,11 @@ angular.module('copayApp.controllers').controller('preferencesController',
$scope.glideraEnabled = config.glidera.enabled;
$scope.glideraTestnet = config.glidera.testnet;
var fc = profileService.focusedClient;
if (fc)
if (fc) {
$scope.encrypt = fc.hasPrivKeyEncrypted();
this.externalSource = fc.getPrivKeyExternalSourceName() == 'ledger' ? "Ledger" : null;
this.externalIndex = fc.getExternalIndex();
}
var unwatchSpendUnconfirmed = $scope.$watch('spendUnconfirmed', function(newVal, oldVal) {
if (newVal == oldVal) return;

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('walletHomeController', function($scope, $rootScope, $timeout, $filter, $modal, $log, notification, txStatus, isCordova, profileService, lodash, configService, rateService, storageService, bitcore, isChromeApp, gettext, gettextCatalog, nodeWebkit, addressService, feeService, bwsError, txFormatService) {
angular.module('copayApp.controllers').controller('walletHomeController', function($scope, $rootScope, $timeout, $filter, $modal, $log, notification, txStatus, isCordova, profileService, lodash, configService, rateService, storageService, bitcore, isChromeApp, gettext, gettextCatalog, nodeWebkit, addressService, ledger, feeService, bwsError, utilService) {
var self = this;
$rootScope.hideMenuBar = false;
@ -258,6 +258,33 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
return;
};
if (fc.isPrivKeyExternal()) {
if (fc.getPrivKeyExternalSourceName() == 'ledger') {
$log.debug('Requesting Ledger Chrome app to sign the transaction');
self.setOngoingProcess(gettext('Requesting Ledger Wallet to sign'));
$scope.loading = true;
$scope.error = null;
ledger.signTx(txp, fc.getExternalIndex(), function(result) {
if (result.success) {
txp.signatures = [];
for (var i=0; i<result.signatures.length; i++) {
txp.signatures.push(result.signatures[i].substring(0, result.signatures[i].length - 2));
}
$scope._doSign(txp);
} else {
$scope.loading = false;
$scope.error = result.message;
self.setOngoingProcess();
$scope.$digest();
}
});
}
} else {
$scope._doSign(txp);
}
};
$scope._doSign = function(txp) {
self.setOngoingProcess(gettext('Signing payment'));
$scope.loading = true;
$scope.error = null;
@ -804,6 +831,30 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
this.signAndBroadcast = function(txp, cb) {
var fc = profileService.focusedClient;
if (fc.isPrivKeyExternal()) {
if (fc.getPrivKeyExternalSourceName() == 'ledger') {
$log.debug('Requesting Ledger Chrome app to sign the transaction');
self.setOngoingProcess(gettext('Requesting Ledger Wallet to sign'));
ledger.signTx(txp, fc.getExternalIndex(), function(result) {
if (result.success) {
txp.signatures = [];
for (var i=0; i<result.signatures.length; i++) {
txp.signatures.push(result.signatures[i].substring(0, result.signatures[i].length - 2));
}
self._doSignAndBroadcast(txp, cb);
} else {
return cb(result);
}
});
}
} else {
self._doSignAndBroadcast(txp, cb);
}
};
this._doSignAndBroadcast = function(txp, cb) {
var fc = profileService.focusedClient;
self.setOngoingProcess(gettext('Signing transaction'));
fc.signTxProposal(txp, function(err, signedTx) {
profileService.lockFC();