WIP trezor 48

This commit is contained in:
Matias Alejo Garcia 2015-11-04 00:53:26 -03:00
commit d1c1638b14
7 changed files with 141 additions and 104 deletions

View file

@ -99,8 +99,17 @@
</a>
</div>
<div ng-hide="hideAdv" class="row">
<div class="large-12 columns">
<div class="large-12 medium-12 columns">
<label><span translate>Wallet Seed</span>
<select class="m10t" ng-model="seedSource"
ng-options="seed as seed.label for seed in create.seedOptions"
ng-change="create.setSeedSource()">
</select>
</label>
</div>
<div class="large-12 columns">
<label ng-show="index.isChromeApp && totalCopayers > 1 " for="hw-ledger" class="oh">
<span translate>Use Ledger hardware wallet</span>
<switch id="hw-ledger" name="hwLedger" ng-model="hwLedger" ng-change="isTestnet=false" class="green right m5t m10b"></switch>
@ -124,26 +133,21 @@
<switch id="network-name" name="isTestnet" ng-model="isTestnet" class="green right m5t m10b"></switch>
</label>
<label ng-show="!hwLedger && !hwTrezor" for="seed" class="oh">
<span translate>Specify your wallet seed</span>
<switch id="seed" name="setSeed" ng-model="setSeed" class="green right m5t m10b"></switch>
</label>
<label for="createPassphrase" class="oh" ng-hide="setSeed || hwLedger || hwTrezor" ><span translate>Add a Seed Passphrase</span> <small translate>Add an optional passphrase to secure the seed</small>
<label for="createPassphrase" class="oh" ng-show="create.seedSourceId=='new' " ><span translate>Add a Seed Passphrase</span> <small translate>Add an optional passphrase to secure the seed</small>
<div class="input">
<input type="text" class="form-control"
name="createPassphrase" ng-model="createPassphrase">
</div>
</label>
<label for="ext-master" class="m10t" ng-show="setSeed">
<label for="ext-master" class="m10t" ng-show="create.seedSourceId=='set'">
<span translate>Wallet Seed</span>
<small translate>Enter the seed words (BIP39)</small>
<input id="ext-master"
type="text"
name="privateKey" ng-model="privateKey">
</label>
<label for="passphrase" class="oh" ng-show="setSeed"><span translate>Seed Passphrase</span> <small translate>The seed could require a passphrase to be imported</small>
<label for="passphrase" class="oh" ng-show="create.seedSourceId=='set'"><span translate>Seed Passphrase</span> <small translate>The seed could require a passphrase to be imported</small>
<div class="input">
<input type="text" class="form-control" name="passphrase" ng-model="passphrase">
</div>

View file

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('createController',
function($scope, $rootScope, $location, $timeout, $log, lodash, go, profileService, configService, isCordova, gettext, ledger, trezor, isMobile) {
function($scope, $rootScope, $location, $timeout, $log, lodash, go, profileService, configService, isCordova, gettext, ledger, trezor, isMobile, isChromeApp) {
var self = this;
var defaults = configService.getDefaults();
@ -38,11 +38,48 @@ angular.module('copayApp.controllers').controller('createController',
$scope.requiredCopayers = Math.min(parseInt(n / 2 + 1), maxReq);
};
var updateSeedSourceSelect = function(n) {
self.seedOptions = [{
id: 'new',
label: gettext('New Random Seed'),
}, {
id: 'set',
label: gettext('Specify Seed...'),
}];
$scope.seedSource = self.seedOptions[0];
// TODO
// if (!isChromeApp) return;
if (n > 1)
self.seedOptions.push({
id: 'ledger',
label: gettext('Ledger Hardware Wallet'),
});
self.seedOptions.push({
id: 'trezor',
label: gettext('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);
};
this.setSeedSource = function(src) {
self.seedSourceId = $scope.seedSource.id;
console.log('[create.js.78:seedSourceId:]',self.seedSourceId); //TODO
$timeout(function() {
$rootScope.$apply();
});
};
this.create = function(form) {
@ -81,9 +118,8 @@ angular.module('copayApp.controllers').controller('createController',
var src = form.hwLedger.$modelValue ? ledger : trezor;
// TODO : account
var account = 0;
src.getInfoForNewWallet(account, function(err, lopts) {
var account = form.account.$modelValue;
src.getInfoForNewWallet(opts.n > 1, account, function(err, lopts) {
self.hwWallet = false;
if (err) {
self.error = err;
@ -141,4 +177,5 @@ angular.module('copayApp.controllers').controller('createController',
$scope.$on("$destroy", function() {
$rootScope.hideWalletNavigation = false;
});
updateSeedSourceSelect(1);
});

View file

@ -188,8 +188,10 @@ angular.module('copayApp.controllers').controller('importController',
return;
}
self.hwWallet = 'Trezor';
// TODO account
trezor.getInfoForNewWallet(0, function(err, lopts) {
var account = form.account.$modelValue;
var isMultisig = form.isMultisig.$modelValue;
trezor.getInfoForNewWallet(isMultisig, account, function(err, lopts) {
self.hwWallet = false;
if (err) {
self.error = err;
@ -227,8 +229,8 @@ angular.module('copayApp.controllers').controller('importController',
return;
}
self.hwWallet = 'Ledger';
// TODO account
ledger.getInfoForNewWallet(0, function(err, lopts) {
var account = form.account.$modelValue;
ledger.getInfoForNewWallet(true, account, function(err, lopts) {
self.hwWallet = false;
if (err) {
self.error = err;

View file

@ -47,9 +47,9 @@ angular.module('copayApp.controllers').controller('joinController',
if (form.hwLedger.$modelValue || form.hwTrezor.$modelValue) {
self.hwWallet = form.hwLedger.$modelValue ? 'Ledger' : 'TREZOR';
var src = form.hwLedger.$modelValue ? ledger : trezor;
var account = form.account.$modelValue;
var account = 0;
src.getInfoForNewWallet(account, function(err, lopts) {
src.getInfoForNewWallet(true, account, function(err, lopts) {
self.hwWallet = false;
if (err) {
self.error = err;

View file

@ -0,0 +1,38 @@
'use strict';
angular.module('copayApp.services')
.factory('hwWallet', function($log) {
var root = {};
// Ledger magic number to get xPub without user confirmation
root.ENTROPY_INDEX_PATH = "0xb11e/";
root.UNISIG_ROOTPATH = 44;
root.MULTISIG_ROOTPATH = 48;
root.LIVENET_PATH = 0;
root._err = function(data) {
var msg = 'HW WALLET Error: ' + (data.error || data.message || 'unknown');
$log.warn(msg);
return msg;
};
root.getAddressPath = function(isMultisig, account) {
var rootPath = isMultisig ? root.MULTISIG_ROOTPATH : root.UNISIG_ROOTPATH;
return rootPath + "'/" + root.LIVENET_PATH + "'/" + account + "'";
}
root.getEntropyPath = function(isMultisig, account) {
var rootPath = isMultisig ? root.MULTISIG_ROOTPATH : root.UNISIG_ROOTPATH;
var path = hwWallet.ENTROPY_INDEX_PATH + rootPath + "'/"
account + "'";
return path;
};
root.pubKeyToEntropySource = function(xPubKey) {
var b = bwcService.getBitcore();
var x = b.HDPublicKey(xPubKey);
return x.publicKey.toString();
};
return root;
});

View file

@ -7,6 +7,8 @@ angular.module('copayApp.services')
// Ledger magic number to get xPub without user confirmation
root.ENTROPY_INDEX_PATH = "0xb11e/";
root.UNISIG_ROOTPATH = 44;
root.MULTISIG_ROOTPATH = 48;
root.callbacks = {};
@ -16,28 +18,17 @@ angular.module('copayApp.services')
});
}
root.getEntropySource = function(account, callback) {
var path = root.ENTROPY_INDEX_PATH + account + "'";
var xpub = root.getXPubKey(path, function(data) {
if (!data.success) {
$log.warn(data.message);
return callback(data);
}
var b = bwcService.getBitcore();
var x = b.HDPublicKey(data.xpubkey);
data.entropySource = x.publicKey.toString();
return callback(data);
root.getEntropySource = function(isMultisig, account, callback) {
root.getXPubKey(hwWallet.getEntropyPath(isMultisig, account), function(data) {
if (!data.success)
return callback(hwWallet._err(data));
return callback(null, hwWallet.pubKeyToEntropySource(data.xpubkey));
});
};
root.getXPubKeyForAddresses = function(account, callback) {
return root.getXPubKey(root._getPath(account), callback);
};
root.getXPubKey = function(path, callback) {
$log.debug('Ledger deriving xPub path:', path);
root.callbacks["get_xpubkey"] = callback;
root._messageAfterSession({
@ -47,15 +38,13 @@ angular.module('copayApp.services')
};
root.getInfoForNewWallet = function(account, callback) {
root.getInfoForNewWallet = function(isMultisig, account, callback) {
var opts = {};
root.getEntropySource(account, function(data) {
if (!data.success) {
$log.warn(data.message);
return callback(data.message);
}
opts.entropySource = data.entropySource;
root.getXPubKeyForAddresses(account, function(data) {
root.getEntropySource(isMultisig, account, function(entropySource) {
if (err) return callback(err);
opts.entropySource = entropySource;
root.getXPubKey(hwWallet.getAddressPath(isMultisig, account), function(data) {
if (!data.success) {
$log.warn(data.message);
return callback(data);
@ -68,14 +57,15 @@ angular.module('copayApp.services')
});
};
root._signP2SH = function(txp, account, callback) {
root._signP2SH = function(txp, account, isMultisig, callback) {
root.callbacks["sign_p2sh"] = callback;
var redeemScripts = [];
var paths = [];
var tx = bwcService.getUtils().buildTx(txp);
for (var i = 0; i < tx.inputs.length; i++) {
redeemScripts.push(new ByteString(tx.inputs[i].redeemScript.toBuffer().toString('hex'), GP.HEX).toString());
paths.push(root._getPath(account) + txp.inputs[i].path.substring(1));
paths.push(hwWallet.getAddressPath(isMultisig, account) + txp.inputs[i].path.substring(1));
}
var splitTransaction = root._splitTransaction(new ByteString(tx.toString(), GP.HEX));
var inputs = [];
@ -98,12 +88,15 @@ angular.module('copayApp.services')
};
root.signTx = function(txp, account, callback) {
// TODO Compat
var isMultisig = true;
if (txp.addressType == 'P2PKH') {
var msg = 'P2PKH wallets are not supported with ledger';
$log.error(msg);
return callback(msg);
} else {
root._signP2SH(txp, account, callback);
root._signP2SH(txp, account, isMultisig, callback);
}
}
@ -154,10 +147,6 @@ angular.module('copayApp.services')
}
}
root._getPath = function(account) {
return "44'/0'/" + account + "'";
}
root._splitTransaction = function(transaction) {
var result = {};
var inputs = [];

View file

@ -1,39 +1,21 @@
'use strict';
angular.module('copayApp.services')
.factory('trezor', function($log, $timeout, bwcService, gettext, lodash, bitcore) {
.factory('trezor', function($log, $timeout, bwcService, gettext, lodash, bitcore, hwWallet) {
var root = {};
var SETTLE_TIME = 3000;
root.ENTROPY_INDEX_PATH = "0xb11e/";
root.callbacks = {};
root._err = function(data) {
var msg = 'TREZOR Error: ' + (data.error || data.message || 'unknown');
$log.warn(msg);
return msg;
};
root.getEntropySource = function(account, callback) {
var path = root.ENTROPY_INDEX_PATH + account + "'";
var xpub = root.getXPubKey(path, function(data) {
if (!data.success) {
return callback(root._err(data));
}
var b = bwcService.getBitcore();
var x = b.HDPublicKey(data.xpubkey);
data.entropySource = x.publicKey.toString();
return callback(null, data);
root.getEntropySource = function(isMultisig, account, callback) {
root.getXPubKey(hwWallet.getEntropyPath(isMultisig, account), function(data) {
if (!data.success)
return callback(hwWallet._err(data));
return callback(null, hwWallet.pubKeyToEntropySource(data.xpubkey));
});
};
root.getXPubKeyForAddresses = function(account, callback) {
return root.getXPubKey(root._getPath(account), callback);
};
root.getXPubKey = function(path, callback) {
$log.debug('TREZOR deriving xPub path:', path);
@ -41,16 +23,17 @@ angular.module('copayApp.services')
};
root.getInfoForNewWallet = function(account, callback) {
root.getInfoForNewWallet = function(isMultisig, account, callback) {
var opts = {};
root.getEntropySource(account, function(err, data) {
root.getEntropySource(isMultisig, account, function(err, data) {
if (err) return callback(err);
opts.entropySource = data.entropySource;
$log.debug('Waiting TREZOR to settle...');
$timeout(function() {
root.getXPubKeyForAddresses(account, function(data) {
if (!data.success)
return callback(root._err(data));
root.getXPubKey(hwWallet.getAddressPath(isMultisig, account), function(data) {
if (!data.success)
return callback(hwWallet._err(data));
opts.extendedPublicKey = data.xpubkey;
opts.externalSource = 'trezor';
@ -110,7 +93,7 @@ angular.module('copayApp.services')
var inAmount = 0;
inputs = lodash.map(txp.inputs, function(i) {
var pathArr = i.path.split('/');
var n = [44 | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
var n = [hwWallet.UNISIG_ROOTPATH | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
inAmount += i.satoshis;
return {
address_n: n,
@ -122,7 +105,7 @@ angular.module('copayApp.services')
var change = inAmount - txp.fee - txp.amount;
if (change > 0) {
var pathArr = txp.changeAddress.path.split('/');
var n = [44 | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
var n = [hwWallet.UNISIG_ROOTPATH | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
tmpOutputs.push({
address_n: n,
@ -133,7 +116,7 @@ angular.module('copayApp.services')
} else {
// P2SH Wallet
// P2SH Wallet, multisig wallet
var inAmount = 0;
var sigs = xPubKeys.map(function(v) {
@ -143,7 +126,7 @@ angular.module('copayApp.services')
inputs = lodash.map(txp.inputs, function(i) {
var pathArr = i.path.split('/');
var n = [44 | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
var n = [hwWallet.MULTISIG_ROOTPATH | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
var np = n.slice(3);
inAmount += i.satoshis;
@ -172,7 +155,7 @@ angular.module('copayApp.services')
var change = inAmount - txp.fee - txp.amount;
if (change > 0) {
var pathArr = txp.changeAddress.path.split('/');
var n = [44 | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
var n = [hwWallet.MULTISIG_ROOTPATH | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
var np = n.slice(3);
var orderedPubKeys = root._orderPubKeys(xPubKeys, np);
@ -183,18 +166,6 @@ angular.module('copayApp.services')
};
}));
// 6D
// 6C
// Addr: 3HFkHufeSaqJtqby8G9RiajaL6HdQDypRT
//
//
//(sin reverse)
// 6C
// 6D
// Addr: 3KCPRDXpmovs9nFvJHJjjsyoBDXXUZ2Frg
// "asm" : "2 03e53b2f69e1705b253029aae2591fbd0e799ed8071c8588a545b2d472dd12df88 0379797abc21d6f82c7f0aba78fd3888d8ae75ec56a10509b20feedbeac20285d9 2 OP_CHECKMULTISIG",
//
tmpOutputs.push({
address_n: n,
amount: change,
@ -227,16 +198,12 @@ angular.module('copayApp.services')
$log.debug('Signing with TREZOR', inputs, outputs);
TrezorConnect.signTx(inputs, outputs, function(result) {
if (!data.success)
return callback(root._err(data));
if (!data.success)
return callback(hwWallet._err(data));
callback(null, result);
});
};
root._getPath = function(account) {
return "44'/0'/" + account + "'";
}
return root;
});