Skip to content

Commit e6d38e7

Browse files
authored
Merge pull request #52 from BitGo/BG-10832-fix-xlm-eth-erc
BG-10832 fixed xlm and eth signing issues
2 parents be5e6b3 + b55574f commit e6d38e7

File tree

6 files changed

+317
-211
lines changed

6 files changed

+317
-211
lines changed

app/sign.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const BN = require('bignumber.js');
66
const prompt = require('prompt-sync')();
77
const utils = require('./utils');
88
const bip39 = require('bip39');
9+
const bitgojs = require('bitgo');
10+
let bitgo;
911

1012
const utxoNetworks = {
1113
btc: utxoLib.networks.bitcoin,
@@ -158,8 +160,8 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
158160
return txBuilder.build().toHex();
159161
};
160162

161-
const handleHalfSignEth = function(recoveryRequest, key, skipConfirm) {
162-
return utils.halfSignEthTransaction(recoveryRequest.coin, recoveryRequest.txPrebuild, key);
163+
const handleHalfSignEth = function(recoveryRequest, key, skipConfirm, basecoin) {
164+
return utils.halfSignEthTransaction(basecoin, recoveryRequest, key);
163165
}
164166

165167
const handleSignEthereum = function(recoveryRequest, key, skipConfirm) {
@@ -244,13 +246,13 @@ const handleSignXlm = function(recoveryRequest, key, skipConfirm) {
244246
throw new Error('Recovery transaction is trying to perform multiple operations - aborting');
245247
}
246248

247-
if (transaction.operations[0].type !== 'payment') {
248-
throw new Error('Recovery transaction is not a payment transaction - aborting');
249+
if (transaction.operations[0].type !== 'payment' && transaction.operations[0].type !== 'createAccount') {
250+
throw new Error('Recovery transaction is not a payment or createAccount transaction - aborting');
249251
}
250252

251253
const outputs = [{
252254
address: transaction.operations[0].destination,
253-
amount: transaction.operations[0].amount
255+
amount: transaction.operations[0].amount || transaction.operations[0].startingBalance
254256
}];
255257

256258
confirmRecovery(recoveryRequest.backupKey, outputs, customMessage, skipConfirm);
@@ -363,7 +365,14 @@ const handleSign = function(args) {
363365
const file = args.file;
364366

365367
const recoveryRequest = JSON.parse(fs.readFileSync(file, { encoding: 'utf8' }));
366-
const coin = recoveryRequest.coin;
368+
let coin = recoveryRequest.coin;
369+
370+
if (coin.startsWith('t')) {
371+
bitgo = new bitgojs.BitGo({ env: 'test' });
372+
} else {
373+
console.log('prod');
374+
bitgo = new bitgojs.BitGo({ env: 'prod' });
375+
}
367376

368377
if(!args.key) {
369378
console.log("\nEnter your private key for signing.\nEnter an xprv or 24 words.\nIf entering 24 words, separate each word with only a comma and no spaces.\n");
@@ -374,27 +383,27 @@ const handleSign = function(args) {
374383

375384
let txHex, halfSignedInfo;
376385

377-
switch (coin) {
378-
case 'eth': case 'teth':
386+
// If a tokenContractAddress was provided, use that. Otherwise use the named coin
387+
const basecoin = recoveryRequest.tokenContractAddress ? bitgo.coin(recoveryRequest.tokenContractAddress) : bitgo.coin(coin);
388+
389+
switch (basecoin.getFamily()) {
390+
case 'eth':
379391
if (recoveryRequest.txPrebuild) {
380-
halfSignedInfo = handleHalfSignEth(recoveryRequest, key, args.confirm);
392+
halfSignedInfo = handleHalfSignEth(recoveryRequest, key, args.confirm, basecoin);
381393
} else {
382-
txHex = handleSignEthereum(recoveryRequest, key, args.confirm);
394+
if (coin.getChain() === 'eth' || coin.getChain() === 'teth') {
395+
txHex = handleSignEthereum(recoveryRequest, key, args.confirm);
396+
} else {
397+
txHex = handleSignErc20(recoveryRequest, key, args.confirm, basecoin);
398+
}
383399
}
384400
break;
385-
case 'xrp': case 'txrp':
401+
case 'xrp':
386402
txHex = handleSignXrp(recoveryRequest, key, args.confirm);
387403
break;
388-
case 'xlm': case'txlm':
404+
case 'xlm':
389405
txHex = handleSignXlm(recoveryRequest, key, args.confirm);
390406
break;
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-
}
397-
break;
398407
default:
399408
txHex = handleSignUtxo(recoveryRequest, key, args.confirm);
400409
break;
@@ -408,7 +417,7 @@ const handleSign = function(args) {
408417
console.log(`Writing signed transaction to file: ${filename}`);
409418

410419
let finalRecovery;
411-
420+
412421
if (txHex) {
413422
finalRecovery = _.pick(recoveryRequest, ['backupKey', 'coin', 'recoveryAmount']);
414423
finalRecovery.txHex = txHex;

app/utils.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ 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-
const bitgojs = require('bitgo');
10-
const bitgo = new bitgojs.BitGo({});
119
const rippleParse = require('ripple-binary-codec');
1210
const rippleKeypairs = require('ripple-keypairs');
1311

@@ -172,8 +170,8 @@ exports.signXrpWithPrivateKey = function(txHex, privateKey, options) {
172170
};
173171
};
174172

175-
exports.halfSignEthTransaction = function(coin, txPrebuild, key) {
176-
const basecoin = bitgo.coin(coin);
173+
exports.halfSignEthTransaction = function(basecoin, recoveryRequest, key) {
174+
const txPrebuild = recoveryRequest.txPrebuild;
177175
const obj = {
178176
prv: key,
179177
gasLimit: txPrebuild.gasLimit,
@@ -189,5 +187,9 @@ exports.halfSignEthTransaction = function(coin, txPrebuild, key) {
189187
delete outFile.txInfo.recipients;
190188
outFile.txInfo.clientSignature = outFile.txInfo.signature;
191189
delete outFile.txInfo.signature;
190+
outFile.coin = recoveryRequest.coin;
191+
if (recoveryRequest.tokenContractAddress) {
192+
outFile.tokenContractAddress = recoveryRequest.tokenContractAddress;
193+
}
192194
return outFile;
193195
}

0 commit comments

Comments
 (0)