Wallet/src/js/services/trezor.js

210 lines
6.1 KiB
JavaScript
Raw Normal View History

'use strict';
angular.module('copayApp.services')
2015-11-04 01:54:54 -03:00
.factory('trezor', function($log, $timeout, gettext, lodash, bitcore, hwWallet) {
var root = {};
2015-10-02 12:01:57 -03:00
var SETTLE_TIME = 3000;
root.callbacks = {};
2015-11-04 00:53:26 -03:00
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);
};
2015-11-04 00:53:26 -03:00
root.getInfoForNewWallet = function(isMultisig, account, callback) {
2015-11-04 01:54:54 -03:00
account = account - 1;
var opts = {};
2015-11-04 00:53:26 -03:00
root.getEntropySource(isMultisig, account, function(err, data) {
2015-10-05 16:36:33 -03:00
if (err) return callback(err);
2015-11-04 01:54:54 -03:00
opts.entropySource = data;
$log.debug('Waiting TREZOR to settle...');
$timeout(function() {
2015-11-04 00:53:26 -03:00
root.getXPubKey(hwWallet.getAddressPath(isMultisig, account), function(data) {
if (!data.success)
return callback(hwWallet._err(data));
2015-10-05 16:36:33 -03:00
opts.extendedPublicKey = data.xpubkey;
opts.externalSource = 'trezor';
2015-11-04 01:54:54 -03:00
opts.account = account;
return callback(null, opts);
});
2015-10-02 12:01:57 -03:00
}, SETTLE_TIME);
});
};
2015-10-02 17:18:54 -03:00
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) {
2015-09-28 20:25:28 -03:00
var inputs = [],
outputs = [];
var tmpOutputs = [];
if (txp.type != 'simple')
2015-09-28 20:25:28 -03:00
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') {
2015-09-28 20:25:28 -03:00
var inAmount = 0;
inputs = lodash.map(txp.inputs, function(i) {
var pathArr = i.path.split('/');
2015-11-04 00:53:26 -03:00
var n = [hwWallet.UNISIG_ROOTPATH | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
2015-09-28 20:25:28 -03:00
inAmount += i.satoshis;
return {
address_n: n,
prev_index: i.vout,
prev_hash: i.txid,
};
});
2015-09-28 20:25:28 -03:00
var change = inAmount - txp.fee - txp.amount;
if (change > 0) {
var pathArr = txp.changeAddress.path.split('/');
2015-11-04 00:53:26 -03:00
var n = [hwWallet.UNISIG_ROOTPATH | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
2015-09-28 20:25:28 -03:00
tmpOutputs.push({
address_n: n,
amount: change,
script_type: 'PAYTOADDRESS'
});
}
} else {
2015-11-04 00:53:26 -03:00
// 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('/');
2015-11-04 00:53:26 -03:00
var n = [hwWallet.MULTISIG_ROOTPATH | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
2015-10-02 12:01:57 -03:00
var np = n.slice(3);
inAmount += i.satoshis;
2015-10-02 17:18:54 -03:00
var orderedPubKeys = root._orderPubKeys(xPubKeys, np);
var pubkeys = lodash(orderedPubKeys.map(function(v) {
return {
node: v,
2015-10-02 12:01:57 -03:00
address_n: np,
};
2015-10-02 17:18:54 -03:00
}));
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('/');
2015-11-04 00:53:26 -03:00
var n = [hwWallet.MULTISIG_ROOTPATH | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
2015-10-02 12:01:57 -03:00
var np = n.slice(3);
2015-10-02 17:18:54 -03:00
var orderedPubKeys = root._orderPubKeys(xPubKeys, np);
var pubkeys = lodash(orderedPubKeys.map(function(v) {
return {
node: v,
2015-10-02 12:01:57 -03:00
address_n: np,
};
2015-10-02 17:18:54 -03:00
}));
2015-10-02 12:01:57 -03:00
tmpOutputs.push({
address_n: n,
amount: change,
script_type: 'PAYTOMULTISIG',
multisig: {
pubkeys: pubkeys,
signatures: sigs,
m: txp.requiredSignatures,
}
});
}
2015-09-28 20:25:28 -03:00
}
// Shuffle outputs for improved privacy
if (tmpOutputs.length > 1) {
2015-10-02 12:01:57 -03:00
outputs = new Array(tmpOutputs.length);
2015-09-28 20:25:28 -03:00
lodash.each(txp.outputOrder, function(order) {
outputs[order] = tmpOutputs.shift();
});
2015-09-28 20:25:28 -03:00
if (tmpOutputs.length)
2015-09-28 20:25:28 -03:00
return cb("Error creating transaction: tmpOutput order");
} else {
2015-09-28 20:25:28 -03:00
outputs = tmpOutputs;
}
2015-09-28 20:25:28 -03:00
2015-10-02 12:01:57 -03:00
// 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));
2015-09-28 20:25:28 -03:00
$log.debug('Signing with TREZOR', inputs, outputs);
TrezorConnect.signTx(inputs, outputs, function(res) {
if (!res.success)
return callback(hwWallet._err(res));
2015-10-05 16:36:33 -03:00
callback(null, res);
2015-09-28 20:25:28 -03:00
});
};
return root;
});