commit
006377d38f
12 changed files with 779 additions and 96 deletions
|
|
@ -25,7 +25,7 @@ angular.module('copayApp.services')
|
|||
body = gettextCatalog.getString('Copayer already in this wallet');
|
||||
break;
|
||||
case 'COPAYER_REGISTERED':
|
||||
body = gettextCatalog.getString('Wallet already registered');
|
||||
body = gettextCatalog.getString('Key already associated with an existing wallet');
|
||||
break;
|
||||
case 'COPAYER_VOTED':
|
||||
body = gettextCatalog.getString('Copayer already voted on this spend proposal');
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
@ -267,6 +268,7 @@ angular.module('copayApp.services')
|
|||
return cb(gettext('Cannot join the same wallet more that once'));
|
||||
}
|
||||
} catch (ex) {
|
||||
$log.debug(ex);
|
||||
return cb(gettext('Bad wallet invitation'));
|
||||
}
|
||||
opts.networkName = walletData.network;
|
||||
|
|
@ -275,7 +277,7 @@ angular.module('copayApp.services')
|
|||
root._seedWallet(opts, function(err, walletClient) {
|
||||
if (err) return cb(err);
|
||||
|
||||
walletClient.joinWallet(opts.secret, opts.myName || 'me', function(err) {
|
||||
walletClient.joinWallet(opts.secret, opts.myName || 'me', {}, function(err) {
|
||||
if (err) return bwsError.cb(err, gettext('Could not join wallet'), cb);
|
||||
|
||||
root.profile.credentials.push(JSON.parse(walletClient.export()));
|
||||
|
|
@ -551,8 +553,9 @@ angular.module('copayApp.services')
|
|||
$log.info('Requesting Ledger Chrome app to sign the transaction');
|
||||
|
||||
ledger.signTx(txp, 0, function(result) {
|
||||
$log.debug('Ledger response',result);
|
||||
if (!result.success)
|
||||
return cb(result);
|
||||
return cb(result.message || result.error);
|
||||
|
||||
txp.signatures = lodash.map(result.signatures, function(s) {
|
||||
return s.substring(0, s.length - 2);
|
||||
|
|
@ -561,16 +564,40 @@ angular.module('copayApp.services')
|
|||
});
|
||||
};
|
||||
|
||||
|
||||
root._signWithTrezor = function(txp, cb) {
|
||||
var fc = root.focusedClient;
|
||||
$log.info('Requesting Trezor to sign the transaction');
|
||||
|
||||
console.log('[profileService.js.570] xPub:', fc.credentials.xPubKey); //TODO
|
||||
var xPubKeys = lodash.pluck(fc.credentials.publicKeyRing,'xPubKey');
|
||||
console.log('[profileService.js.571:xPubKeys:]',xPubKeys); //TODO
|
||||
|
||||
trezor.signTx(xPubKeys, txp, 0, function(result) {
|
||||
$log.debug('Trezor response',result);
|
||||
if (!result.success)
|
||||
return cb(result.error || result);
|
||||
|
||||
txp.signatures = result.signatures;
|
||||
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();
|
||||
|
|
|
|||
237
src/js/services/trezor.js
Normal file
237
src/js/services/trezor.js
Normal file
|
|
@ -0,0 +1,237 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.services')
|
||||
.factory('trezor', function($log, $timeout, bwcService, gettext, lodash, bitcore) {
|
||||
var root = {};
|
||||
|
||||
var SETTLE_TIME = 3000;
|
||||
|
||||
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;
|
||||
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 = [44 | 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 = [44 | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]), parseInt(pathArr[2])];
|
||||
|
||||
tmpOutputs.push({
|
||||
address_n: n,
|
||||
amount: change,
|
||||
script_type: 'PAYTOADDRESS'
|
||||
});
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// P2SH 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 = [44 | 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 = [44 | 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,
|
||||
};
|
||||
}));
|
||||
|
||||
// 6D
|
||||
// 6C
|
||||
// Addr: 3HFkHufeSaqJtqby8G9RiajaL6HdQDypRT
|
||||
//
|
||||
//
|
||||
//(sin reverse)
|
||||
// 6C
|
||||
// 6D
|
||||
// Addr: 3KCPRDXpmovs9nFvJHJjjsyoBDXXUZ2Frg
|
||||
// "asm" : "2 03e53b2f69e1705b253029aae2591fbd0e799ed8071c8588a545b2d472dd12df88 0379797abc21d6f82c7f0aba78fd3888d8ae75ec56a10509b20feedbeac20285d9 2 OP_CHECKMULTISIG",
|
||||
//
|
||||
|
||||
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(result) {
|
||||
callback(result);
|
||||
});
|
||||
};
|
||||
|
||||
root._getPath = function(account) {
|
||||
return "44'/0'/" + account + "'";
|
||||
}
|
||||
|
||||
return root;
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue