210 lines
6.1 KiB
JavaScript
210 lines
6.1 KiB
JavaScript
'use strict';
|
|
|
|
angular.module('copayApp.services')
|
|
.factory('trezor', function($log, $timeout, gettext, lodash, bitcore, hwWallet) {
|
|
var root = {};
|
|
|
|
var SETTLE_TIME = 3000;
|
|
root.callbacks = {};
|
|
|
|
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.getXPubKey = function(path, callback) {
|
|
$log.debug('TREZOR deriving xPub path:', path);
|
|
TrezorConnect.getXPubKey(path, callback);
|
|
};
|
|
|
|
|
|
root.getInfoForNewWallet = function(isMultisig, account, callback) {
|
|
account = account - 1;
|
|
var opts = {};
|
|
root.getEntropySource(isMultisig, account, function(err, data) {
|
|
if (err) return callback(err);
|
|
opts.entropySource = data;
|
|
$log.debug('Waiting TREZOR to settle...');
|
|
$timeout(function() {
|
|
|
|
root.getXPubKey(hwWallet.getAddressPath(isMultisig, account), function(data) {
|
|
if (!data.success)
|
|
return callback(hwWallet._err(data));
|
|
|
|
opts.extendedPublicKey = data.xpubkey;
|
|
opts.externalSource = 'trezor';
|
|
opts.account = account;
|
|
return callback(null, opts);
|
|
});
|
|
}, SETTLE_TIME);
|
|
});
|
|
};
|
|
|
|
root._orderPubKeys = function(xPub, np) {
|
|
var xPubKeys = lodash.clone(xPub);
|
|
var path = lodash.clone(np);
|
|
path.unshift('m');
|
|
path = path.join('/');
|
|
|
|
var keys = lodash.map(xPubKeys, function(x) {
|
|
var pub = (new bitcore.HDPublicKey(x)).derive(path).publicKey;
|
|
return {
|
|
xpub: x,
|
|
pub: pub.toString('hex'),
|
|
};
|
|
});
|
|
|
|
var sorted = lodash.sortBy(keys, function(x) {
|
|
return x.pub;
|
|
});
|
|
|
|
return lodash.pluck(sorted, 'xpub');
|
|
};
|
|
|
|
root.signTx = function(xPubKeys, txp, account, callback) {
|
|
|
|
var inputs = [],
|
|
outputs = [];
|
|
var tmpOutputs = [];
|
|
|
|
if (txp.type != 'simple')
|
|
return callback('Only TXPs type SIMPLE are supported in TREZOR');
|
|
|
|
var toScriptType = 'PAYTOADDRESS';
|
|
if (txp.toAddress.charAt(0) == '2' || txp.toAddress.charAt(0) == '3')
|
|
toScriptType = 'PAYTOSCRIPTHASH';
|
|
|
|
|
|
// Add to
|
|
tmpOutputs.push({
|
|
address: txp.toAddress,
|
|
amount: txp.amount,
|
|
script_type: toScriptType,
|
|
});
|
|
|
|
|
|
|
|
if (txp.addressType == 'P2PKH') {
|
|
|
|
var inAmount = 0;
|
|
inputs = lodash.map(txp.inputs, function(i) {
|
|
var pathArr = i.path.split('/');
|
|
var n = [hwWallet.UNISIG_ROOTPATH | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
|
|
inAmount += i.satoshis;
|
|
return {
|
|
address_n: n,
|
|
prev_index: i.vout,
|
|
prev_hash: i.txid,
|
|
};
|
|
});
|
|
|
|
var change = inAmount - txp.fee - txp.amount;
|
|
if (change > 0) {
|
|
var pathArr = txp.changeAddress.path.split('/');
|
|
var n = [hwWallet.UNISIG_ROOTPATH | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
|
|
|
|
tmpOutputs.push({
|
|
address_n: n,
|
|
amount: change,
|
|
script_type: 'PAYTOADDRESS'
|
|
});
|
|
}
|
|
|
|
} else {
|
|
|
|
// P2SH Wallet, multisig wallet
|
|
var inAmount = 0;
|
|
|
|
var sigs = xPubKeys.map(function(v) {
|
|
return '';
|
|
});
|
|
|
|
|
|
inputs = lodash.map(txp.inputs, function(i) {
|
|
var pathArr = i.path.split('/');
|
|
var n = [hwWallet.MULTISIG_ROOTPATH | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
|
|
var np = n.slice(3);
|
|
|
|
inAmount += i.satoshis;
|
|
|
|
var orderedPubKeys = root._orderPubKeys(xPubKeys, np);
|
|
var pubkeys = lodash(orderedPubKeys.map(function(v) {
|
|
return {
|
|
node: v,
|
|
address_n: np,
|
|
};
|
|
}));
|
|
|
|
return {
|
|
address_n: n,
|
|
prev_index: i.vout,
|
|
prev_hash: i.txid,
|
|
script_type: 'SPENDMULTISIG',
|
|
multisig: {
|
|
pubkeys: pubkeys,
|
|
signatures: sigs,
|
|
m: txp.requiredSignatures,
|
|
}
|
|
};
|
|
});
|
|
|
|
var change = inAmount - txp.fee - txp.amount;
|
|
if (change > 0) {
|
|
var pathArr = txp.changeAddress.path.split('/');
|
|
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);
|
|
var pubkeys = lodash(orderedPubKeys.map(function(v) {
|
|
return {
|
|
node: v,
|
|
address_n: np,
|
|
};
|
|
}));
|
|
|
|
tmpOutputs.push({
|
|
address_n: n,
|
|
amount: change,
|
|
script_type: 'PAYTOMULTISIG',
|
|
multisig: {
|
|
pubkeys: pubkeys,
|
|
signatures: sigs,
|
|
m: txp.requiredSignatures,
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
// Shuffle outputs for improved privacy
|
|
if (tmpOutputs.length > 1) {
|
|
outputs = new Array(tmpOutputs.length);
|
|
lodash.each(txp.outputOrder, function(order) {
|
|
outputs[order] = tmpOutputs.shift();
|
|
});
|
|
|
|
if (tmpOutputs.length)
|
|
return cb("Error creating transaction: tmpOutput order");
|
|
} else {
|
|
outputs = tmpOutputs;
|
|
}
|
|
|
|
// Prevents: Uncaught DataCloneError: Failed to execute 'postMessage' on 'Window': An object could not be cloned.
|
|
inputs = JSON.parse(JSON.stringify(inputs));
|
|
outputs = JSON.parse(JSON.stringify(outputs));
|
|
|
|
$log.debug('Signing with TREZOR', inputs, outputs);
|
|
TrezorConnect.signTx(inputs, outputs, function(res) {
|
|
if (!res.success)
|
|
return callback(hwWallet._err(res));
|
|
|
|
callback(null, res);
|
|
});
|
|
};
|
|
|
|
return root;
|
|
});
|