WIP trezor 48
This commit is contained in:
parent
47752fbe1e
commit
d1c1638b14
7 changed files with 141 additions and 104 deletions
38
src/js/services/hwWallet.js
Normal file
38
src/js/services/hwWallet.js
Normal 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;
|
||||
});
|
||||
|
|
@ -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 = [];
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue