Skip to content

Commit be5e6b3

Browse files
authored
Merge pull request #49 from BitGo/BG-9640-add-eth-specific-signing
BG-9640 added tools to half-sign eth
2 parents 421ade1 + 074ce9f commit be5e6b3

File tree

12 files changed

+1011
-15
lines changed

12 files changed

+1011
-15
lines changed

app/sign.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
158158
return txBuilder.build().toHex();
159159
};
160160

161+
const handleHalfSignEth = function(recoveryRequest, key, skipConfirm) {
162+
return utils.halfSignEthTransaction(recoveryRequest.coin, recoveryRequest.txPrebuild, key);
163+
}
164+
161165
const handleSignEthereum = function(recoveryRequest, key, skipConfirm) {
162166
const EthTx = require('ethereumjs-tx');
163167

@@ -330,6 +334,11 @@ const parseKey = function(rawkey, coin, path) {
330334

331335
}
332336
// if it doesn't have commas, we expect it is an xprv or xlmsecret properly formatted
337+
if(path) {
338+
let node = utxoLib.HDNode.fromPrivateKeyBuffer(Buffer.from(rawkey, 'hex'));
339+
node = node.derivePath(path);
340+
return node.toBase58();
341+
}
333342
return rawkey;
334343
}
335344

@@ -363,36 +372,56 @@ const handleSign = function(args) {
363372

364373
const key = parseKey(args.key, coin, args.path);
365374

366-
let txHex;
375+
let txHex, halfSignedInfo;
367376

368377
switch (coin) {
369378
case 'eth': case 'teth':
370-
txHex = handleSignEthereum(recoveryRequest, key, args.confirm);
379+
if (recoveryRequest.txPrebuild) {
380+
halfSignedInfo = handleHalfSignEth(recoveryRequest, key, args.confirm);
381+
} else {
382+
txHex = handleSignEthereum(recoveryRequest, key, args.confirm);
383+
}
371384
break;
372385
case 'xrp': case 'txrp':
373386
txHex = handleSignXrp(recoveryRequest, key, args.confirm);
374387
break;
375388
case 'xlm': case'txlm':
376389
txHex = handleSignXlm(recoveryRequest, key, args.confirm);
377390
break;
378-
case 'erc20':
379-
txHex = handleSignErc20(recoveryRequest, key, args.confirm);
391+
case 'erc20': case 'terc20': case 'terc': case 'erc':
392+
if (recoveryRequest.txPrebuild) {
393+
halfSignedInfo = handleHalfSignEth(recoveryRequest, key, args.confirm);
394+
} else {
395+
txHex = handleSignErc20(recoveryRequest, key, args.confirm);
396+
}
380397
break;
381398
default:
382399
txHex = handleSignUtxo(recoveryRequest, key, args.confirm);
383400
break;
384401
}
385402

386-
console.log(`Signed transaction hex: ${txHex}`);
403+
if(txHex) {
404+
console.log(`Signed transaction hex: ${txHex}`);
405+
}
387406

388407
const filename = file.replace(/\.[^/.]+$/, '') + '.signed.json';
389408
console.log(`Writing signed transaction to file: ${filename}`);
390409

391-
const finalRecovery = _.pick(recoveryRequest, ['backupKey', 'coin', 'recoveryAmount']);
392-
finalRecovery.txHex = txHex;
410+
let finalRecovery;
411+
412+
if (txHex) {
413+
finalRecovery = _.pick(recoveryRequest, ['backupKey', 'coin', 'recoveryAmount']);
414+
finalRecovery.txHex = txHex;
415+
} else {
416+
finalRecovery = halfSignedInfo;
417+
}
393418

394-
fs.writeFileSync(filename, JSON.stringify(finalRecovery, null, 2));
395-
console.log('Done');
419+
const fileStr = JSON.stringify(finalRecovery, null, 2);
420+
if(!args.noWrite) {
421+
fs.writeFileSync(filename, fileStr);
422+
console.log('Done');
423+
}
424+
return finalRecovery;
396425
};
397426

398427
module.exports = { handleSign, handleSignUtxo, handleSignEthereum, handleSignXrp, handleSignXlm, handleSignErc20, parseKey };

app/utils.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const prova = require('prova-lib');
66
const utxoLib = require('bitgo-utxo-lib');
77
const stellar = require('stellar-base');
88
const stellarHd = require('stellar-hd-wallet');
9-
9+
const bitgojs = require('bitgo');
10+
const bitgo = new bitgojs.BitGo({});
1011
const rippleParse = require('ripple-binary-codec');
1112
const rippleKeypairs = require('ripple-keypairs');
1213

@@ -170,3 +171,23 @@ exports.signXrpWithPrivateKey = function(txHex, privateKey, options) {
170171
id: exports.computeBinaryTransactionHash(serialized)
171172
};
172173
};
174+
175+
exports.halfSignEthTransaction = function(coin, txPrebuild, key) {
176+
const basecoin = bitgo.coin(coin);
177+
const obj = {
178+
prv: key,
179+
gasLimit: txPrebuild.gasLimit,
180+
gasPrice: txPrebuild.gasPrice,
181+
expireTime: txPrebuild.expireTime,
182+
txPrebuild
183+
};
184+
const signedtx = basecoin.signTransaction(obj);
185+
const outFile = {
186+
txInfo: signedtx.halfSigned
187+
}
188+
outFile.txInfo.recipient = outFile.txInfo.recipients[0];
189+
delete outFile.txInfo.recipients;
190+
outFile.txInfo.clientSignature = outFile.txInfo.signature;
191+
delete outFile.txInfo.signature;
192+
return outFile;
193+
}

config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module.exports = {
66
tbtc: 'xpub',
77
eth: 'xpub',
88
teth: 'xpub',
9+
erc: 'xpub',
10+
terc: 'xpub',
911
ltc: 'xpub',
1012
tltc: 'xpub',
1113
bch: 'xpub',

0 commit comments

Comments
 (0)