WIP trezor support, wallet creation working

This commit is contained in:
Matias Alejo Garcia 2015-09-26 08:26:31 -03:00
commit 9ce342bba7
8 changed files with 542 additions and 24 deletions

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.services')
.factory('profileService', function profileServiceFactory($rootScope, $location, $timeout, $filter, $log, lodash, storageService, bwcService, configService, notificationService, isChromeApp, isCordova, gettext, gettextCatalog, nodeWebkit, bwsError, uxLanguage, ledger, bitcore) {
.factory('profileService', function profileServiceFactory($rootScope, $location, $timeout, $filter, $log, lodash, storageService, bwcService, configService, notificationService, isChromeApp, isCordova, gettext, gettextCatalog, nodeWebkit, bwsError, uxLanguage, ledger, bitcore, trezor) {
var root = {};
@ -236,6 +236,7 @@ angular.module('copayApp.services')
root._seedWallet(opts, function(err, walletClient) {
if (err) return cb(err);
console.log('[profileService.js.239:walletClient:]',walletClient); //TODO
walletClient.createWallet(opts.name, opts.myName || 'me', opts.m, opts.n, {
network: opts.networkName
}, function(err, secret) {
@ -561,16 +562,38 @@ angular.module('copayApp.services')
});
};
root._signWithTrezor = function(txp, cb) {
var fc = root.focusedClient;
$log.info('Requesting Trezor to sign the transaction');
trezor.signTx(txp, 0, function(result) {
console.log('[profileService.js.570:result:]',result); //TODO
if (!result.success)
return cb(result);
txp.signatures = lodash.map(result.signatures, function(s) {
return s.substring(0, s.length - 2);
});
return fc.signTxProposal(txp, cb);
});
};
root.signTxProposal = function(txp, cb) {
var fc = root.focusedClient;
if (fc.isPrivKeyExternal()) {
if (fc.getPrivKeyExternalSourceName() != 'ledger') {
var msg = 'Unsupported External Key:' + fc.getPrivKeyExternalSourceName();
$log.error(msg);
return cb(msg);
switch (fc.getPrivKeyExternalSourceName()) {
case 'ledger':
return root._signWithLedger(txp, cb);
case 'trezor':
return root._signWithTrezor(txp, cb);
default:
var msg = 'Unsupported External Key:' + fc.getPrivKeyExternalSourceName();
$log.error(msg);
return cb(msg);
}
return root._signWithLedger(txp, cb);
} else {
return fc.signTxProposal(txp, function(err, signedTxp) {
root.lockFC();

114
src/js/services/trezor.js Normal file
View file

@ -0,0 +1,114 @@
'use strict';
angular.module('copayApp.services')
.factory('trezor', function($log, $timeout, bwcService, gettext, lodash) {
var root = {};
root.ENTROPY_INDEX_PATH = "0xb11e/";
root.callbacks = {};
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.getXPubKeyForAddresses = function(account, callback) {
return root.getXPubKey(root._getPath(account), callback);
};
root.getXPubKey = function(path, callback) {
$log.debug('TREZOR deriving xPub path:', path);
TrezorConnect.getXPubKey(path, callback);
};
root.getInfoForNewWallet = function(account, callback) {
var opts = {};
root.getEntropySource(account, function(data) {
if (!data.success) {
$log.warn(data.message);
return callback(data.message);
}
opts.entropySource = data.entropySource;
$log.debug('Waiting TREZOR to settle...');
$timeout(function() {
root.getXPubKeyForAddresses(account, function(data) {
if (!data.success) {
$log.warn(data.message);
return callback(data);
}
opts.extendedPublicKey = data.xpubkey;
opts.externalSource = 'trezor';
opts.externalIndex = account;
console.log('[trezor.js.51:opts:]', opts); //TODO
return callback(null, opts);
});
}, 5000);
});
};
root.signTx = function(txp, account, callback) {
if (txp.type != 'simple')
return callback('Only simple TXs are supported in TREZOR');
console.log('[trezor.js.90:txp:]', txp); //TODO
if (txp.addressType == 'P2PKH') {
var tx = bwcService.getUtils().buildTx(txp);
// spend one change output
var inputs = lodash.map(txp.inputs, function(i){
var pathArr = i.path.split('/');
console.log('[trezor.js.72:pathArr:]',pathArr); //TODO
var n = [44 | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]) , parseInt(pathArr[2])];
return {
address_n: n,
prev_index: i.vout,
prev_hash: i.txid,
};
});
console.log('[trezor.js.68:inputs:]',inputs); //TODO
var pathArr = txp.changeAddress.path.split('/');
console.log('[trezor.js.82:pathArr:]',pathArr); //TODO
var n = [44 | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]) , parseInt(pathArr[2])];
// send to 1 address output and one change output
var outputs = [{
address_n: n,
amount: txp.amount - txp.fee,
script_type: 'PAYTOADDRESS'
}, {
address: txp.toAddress,
amount: txp.amount,
script_type: 'PAYTOADDRESS'
}];
console.log('[trezor.js.84:outputs:]',outputs); //TODO
TrezorConnect.signTx(inputs, outputs, function(result) {
console.log('[trezor.js.78:result:]', result); //TODO
callback(result);
});
} else {
var msg = 'P2SH wallets are not supported with TREZOR';
$log.error(msg);
return callback(msg);
}
}
root._getPath = function(account) {
return "44'/0'/" + account + "'";
}
return root;
});