Skip to content

Commit fadf3b7

Browse files
authored
Merge pull request #59 from BitGo/TRON-58-add-krs-support-for-tron
[TRON-58] Add KRS Support for TRON
2 parents dba9fee + f888c9f commit fadf3b7

File tree

6 files changed

+1408
-1396
lines changed

6 files changed

+1408
-1396
lines changed

app/sign.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const utxoLib = require('bitgo-utxo-lib');
2+
const accountLib = require('@bitgo/account-lib');
3+
const statics = require('@bitgo/statics');
24
const prova = require('prova-lib');
35
const fs = require('fs');
46
const _ = require('lodash');
@@ -28,6 +30,7 @@ const coinDecimals = {
2830
btc: 8,
2931
eth: 18,
3032
eos: 4,
33+
trx: 6,
3134
xrp: 6,
3235
bch: 8,
3336
bsv: 8,
@@ -38,6 +41,7 @@ const coinDecimals = {
3841
tbtc: 8,
3942
teth: 18,
4043
teos: 4,
44+
ttrx: 6,
4145
txrp: 6,
4246
tltc: 8,
4347
txlm: 7,
@@ -204,6 +208,34 @@ const handleSignEthereum = function(recoveryRequest, key, skipConfirm) {
204208
return transaction.serialize().toString('hex');
205209
};
206210

211+
const handleSignTrx = function(recoveryRequest, key, skipConfirm) {
212+
const coin = recoveryRequest.coin;
213+
214+
const txHex = getTransactionHexFromRequest(recoveryRequest);
215+
const builder = new accountLib.TransactionBuilder({ coinName: coin });
216+
builder.from(txHex);
217+
218+
const customMessage = recoveryRequest.custom ? recoveryRequest.custom.message : 'None';
219+
220+
const outputs = builder.build().destinations.map(d => {
221+
return {
222+
address: d.address,
223+
amount: d.value.toString(10)
224+
};
225+
});
226+
227+
confirmRecovery(recoveryRequest.backupKey, outputs, customMessage, skipConfirm);
228+
229+
if (!key) {
230+
console.log('Please enter the xprv of the wallet for signing: ');
231+
key = prompt();
232+
}
233+
234+
const backupKeyNode = getHDNodeAndVerify(key, recoveryRequest.backupKey);
235+
builder.sign({ key: backupKeyNode.keyPair.getPrivateKeyBuffer() });
236+
return JSON.stringify(builder.build().toJson());
237+
};
238+
207239
const handleSignEos = function(recoveryRequest, key, skipConfirm) {
208240
const EosJs = require('eosjs');
209241
const ecc = require('eosjs-ecc');
@@ -418,12 +450,11 @@ const handleSign = function(args) {
418450
const file = args.file;
419451

420452
const recoveryRequest = JSON.parse(fs.readFileSync(file, { encoding: 'utf8' }));
421-
let coin = recoveryRequest.coin;
453+
const coin = statics.coins.get(recoveryRequest.coin);
422454

423-
if (coin.startsWith('t')) {
455+
if (coin.network.type === 'testnet') {
424456
bitgo = new bitgojs.BitGo({ env: 'test' });
425457
} else {
426-
console.log('prod');
427458
bitgo = new bitgojs.BitGo({ env: 'prod' });
428459
}
429460

@@ -432,19 +463,19 @@ const handleSign = function(args) {
432463
args.key = prompt("Key: ");
433464
}
434465

435-
const key = parseKey(args.key, coin, args.path);
466+
const key = parseKey(args.key, coin.name, args.path);
436467

437468
let txHex, halfSignedInfo;
438469

439470
// If a tokenContractAddress was provided, use that. Otherwise use the named coin
440-
const basecoin = recoveryRequest.tokenContractAddress ? bitgo.coin(recoveryRequest.tokenContractAddress) : bitgo.coin(coin);
471+
const basecoin = recoveryRequest.tokenContractAddress ? bitgo.coin(recoveryRequest.tokenContractAddress) : bitgo.coin(coin.name);
441472

442473
switch (basecoin.getFamily()) {
443474
case 'eth':
444475
if (recoveryRequest.txPrebuild) {
445476
halfSignedInfo = handleHalfSignEth(recoveryRequest, key, args.confirm, basecoin);
446477
} else {
447-
if (coin.getChain() === 'eth' || coin.getChain() === 'teth') {
478+
if (coin.family === 'eth' && !coin.isToken) {
448479
txHex = handleSignEthereum(recoveryRequest, key, args.confirm);
449480
} else {
450481
txHex = handleSignErc20(recoveryRequest, key, args.confirm, basecoin);
@@ -454,6 +485,9 @@ const handleSign = function(args) {
454485
case 'eos':
455486
txHex = handleSignEos(recoveryRequest, key, args.confirm);
456487
break;
488+
case 'trx':
489+
txHex = handleSignTrx(recoveryRequest, key, args.confirm);
490+
break;
457491
case 'xrp':
458492
txHex = handleSignXrp(recoveryRequest, key, args.confirm);
459493
break;
@@ -489,4 +523,4 @@ const handleSign = function(args) {
489523
return finalRecovery;
490524
};
491525

492-
module.exports = { handleSign, handleSignUtxo, handleSignEthereum, handleSignXrp, handleSignXlm, handleSignErc20, handleSignEos, parseKey };
526+
module.exports = { handleSign, handleSignUtxo, handleSignEthereum, handleSignXrp, handleSignXlm, handleSignErc20, handleSignEos, handleSignTrx, parseKey };

config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ module.exports = {
2222
txrp: 'xpub',
2323
dash: 'xpub',
2424
tdash: 'xpub',
25+
trx: 'xpub',
26+
ttrx: 'xpub',
2527
xlm: 'xlm',
26-
txlm: 'xlm'
28+
txlm: 'xlm',
2729
},
2830
"host": "0.0.0.0",
2931
"port": 6833,

0 commit comments

Comments
 (0)