Skip to content

Commit f22f270

Browse files
committed
feat: add sdk changes for mpc tss support for TRON
TICKET: CHALO-33
1 parent e1378ac commit f22f270

File tree

7 files changed

+78
-6
lines changed

7 files changed

+78
-6
lines changed

modules/sdk-coin-trx/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
},
4848
"dependencies": {
4949
"@bitgo/sdk-core": "^36.33.2",
50+
"@bitgo/sdk-lib-mpc": "^10.9.0",
5051
"@bitgo/secp256k1": "^1.10.0",
5152
"@bitgo/statics": "^58.29.0",
5253
"@stablelib/hex": "^1.0.0",

modules/sdk-coin-trx/src/lib/transaction.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,24 @@ export class Transaction extends BaseTransaction {
318318
return this._inputs;
319319
}
320320

321+
/** @inheritDoc */
322+
get signablePayload(): Buffer {
323+
if (!this._transaction) {
324+
throw new ParseTransactionError('Empty transaction');
325+
}
326+
return Buffer.from(this._transaction.raw_data_hex, 'hex');
327+
}
328+
329+
addSignature(signature: Buffer): void {
330+
if (!this._transaction) {
331+
throw new ParseTransactionError('Empty transaction');
332+
}
333+
if (!this._transaction.signature) {
334+
this._transaction.signature = [];
335+
}
336+
this._transaction.signature.push(signature.toString('hex'));
337+
}
338+
321339
/** @inheritdoc */
322340
canSign(key: BaseKey): boolean {
323341
// Tron transaction do not contain the owners account address so it is not possible to check the

modules/sdk-coin-trx/src/lib/transactionBuilder.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
BuildTransactionError,
99
InvalidTransactionError,
1010
ParseTransactionError,
11+
PublicKey as BasePublicKey,
1112
SigningError,
1213
ExtendTransactionError,
1314
InvalidParameterValueError,
@@ -162,6 +163,11 @@ export class TransactionBuilder extends BaseTransactionBuilder {
162163
return new Transaction(this._coinConfig, signedTransaction);
163164
}
164165

166+
/** @inheritDoc */
167+
addSignature(publicKey: BasePublicKey, signature: Buffer): void {
168+
this.transaction.addSignature(signature);
169+
}
170+
165171
/** @inheritdoc */
166172
protected async buildImplementation(): Promise<Transaction> {
167173
// This is a no-op since Tron transactions are built from

modules/sdk-coin-trx/src/lib/wrappedBuilder.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import BigNumber from 'bignumber.js';
22
import { BaseCoin as CoinConfig } from '@bitgo/statics';
3-
import { BaseKey, BaseTransaction, InvalidTransactionError } from '@bitgo/sdk-core';
3+
import { BaseKey, BaseTransaction, InvalidTransactionError, PublicKey as BasePublicKey } from '@bitgo/sdk-core';
44
import { Transaction } from './transaction';
55
import { Address } from './address';
66
import { TransactionBuilder } from './transactionBuilder';
@@ -137,6 +137,11 @@ export class WrappedBuilder extends TransactionBuilder {
137137
this._builder.sign(key);
138138
}
139139

140+
/** @inheritDoc */
141+
addSignature(publicKey: BasePublicKey, signature: Buffer): void {
142+
this._builder.addSignature(publicKey, signature);
143+
}
144+
140145
/** @inheritdoc */
141146
async build(): Promise<BaseTransaction> {
142147
return this._builder.build();

modules/sdk-coin-trx/src/trx.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @prettier
33
*/
44
import * as secp256k1 from 'secp256k1';
5-
import { randomBytes } from 'crypto';
5+
import { createHash, Hash, randomBytes } from 'crypto';
66
import { CoinFamily, BaseCoin as StaticsBaseCoin } from '@bitgo/statics';
77
import { bip32 } from '@bitgo/secp256k1';
88
import * as request from 'superagent';
@@ -16,6 +16,7 @@ import {
1616
KeyPair,
1717
KeyIndices,
1818
MethodNotImplementedError,
19+
MPCAlgorithm,
1920
ParsedTransaction,
2021
ParseTransactionOptions,
2122
SignedTransaction,
@@ -31,7 +32,10 @@ import {
3132
multisigTypes,
3233
AuditDecryptedKeyParams,
3334
AddressCoinSpecific,
35+
verifyMPCWalletAddress,
36+
isTssVerifyAddressOptions,
3437
} from '@bitgo/sdk-core';
38+
import { auditEcdsaPrivateKey } from '@bitgo/sdk-lib-mpc';
3539
import { Interface, Utils, WrappedBuilder, KeyPair as TronKeyPair } from './lib';
3640
import { ValueFields, TransactionReceipt } from './lib/iface';
3741
import { getBuilder } from './lib/builder';
@@ -189,6 +193,21 @@ export class Trx extends BaseCoin {
189193
return multisigTypes.onchain;
190194
}
191195

196+
/** @inheritDoc */
197+
supportsTss(): boolean {
198+
return true;
199+
}
200+
201+
/** @inheritDoc */
202+
getMPCAlgorithm(): MPCAlgorithm {
203+
return 'ecdsa';
204+
}
205+
206+
/** @inheritDoc */
207+
getHashFunction(): Hash {
208+
return createHash('sha256');
209+
}
210+
192211
static createInstance(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>): BaseCoin {
193212
return new Trx(bitgo, staticsCoin);
194213
}
@@ -269,6 +288,14 @@ export class Trx extends BaseCoin {
269288
async isWalletAddress(params: VerifyAddressOptions): Promise<boolean> {
270289
const { address, keychains } = params;
271290

291+
if (isTssVerifyAddressOptions(params)) {
292+
return verifyMPCWalletAddress(
293+
{ ...params, keyCurve: 'secp256k1' },
294+
this.isValidAddress.bind(this),
295+
(pubKey: string) => new TronKeyPair({ pub: pubKey }).getAddress()
296+
);
297+
}
298+
272299
if (!isTrxVerifyAddressOptions(params)) {
273300
throw new Error('Invalid or missing index for address verification');
274301
}
@@ -404,6 +431,13 @@ export class Trx extends BaseCoin {
404431
return true;
405432
}
406433

434+
/** @inheritDoc */
435+
async getSignablePayload(serializedTx: string): Promise<Buffer> {
436+
const txBuilder = getBuilder(this.getChain()).from(serializedTx);
437+
const rebuiltTransaction = await txBuilder.build();
438+
return rebuiltTransaction.signablePayload;
439+
}
440+
407441
/**
408442
* Derive a user key using the chain path of the address
409443
* @param key
@@ -1071,7 +1105,11 @@ export class Trx extends BaseCoin {
10711105
}
10721106

10731107
/** @inheritDoc */
1074-
auditDecryptedKey(params: AuditDecryptedKeyParams) {
1075-
throw new MethodNotImplementedError();
1108+
auditDecryptedKey({ multiSigType, publicKey, prv }: AuditDecryptedKeyParams) {
1109+
if (multiSigType === 'tss') {
1110+
auditEcdsaPrivateKey(prv as string, publicKey as string);
1111+
} else {
1112+
throw new MethodNotImplementedError();
1113+
}
10761114
}
10771115
}

modules/statics/src/coinFeatures.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ export const TRX_FEATURES = [
433433
CoinFeature.MULTISIG_COLD,
434434
CoinFeature.MULTISIG,
435435
CoinFeature.STAKING,
436+
CoinFeature.TSS,
437+
CoinFeature.TSS_COLD,
438+
CoinFeature.MPCV2,
439+
CoinFeature.SHA256_WITH_ECDSA_TSS,
436440
];
437441
export const COSMOS_SIDECHAIN_FEATURES = [
438442
...ACCOUNT_COIN_DEFAULT_FEATURES,

modules/statics/test/unit/fixtures/expectedColdFeatures.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export const expectedColdFeatures = {
1313
'tsoneium',
1414
'flr',
1515
'tflr',
16+
'trx',
17+
'ttrx',
1618
],
1719
justMultiSig: [
1820
'algo',
@@ -54,9 +56,7 @@ export const expectedColdFeatures = {
5456
'thbar',
5557
'tltc',
5658
'trbtc',
57-
'trx',
5859
'tstx',
59-
'ttrx',
6060
'txlm',
6161
'txrp',
6262
'txtz',

0 commit comments

Comments
 (0)