Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/sdk-coin-trx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"dependencies": {
"@bitgo/sdk-core": "^36.33.2",
"@bitgo/sdk-lib-mpc": "^10.9.0",
"@bitgo/secp256k1": "^1.10.0",
"@bitgo/statics": "^58.29.0",
"@stablelib/hex": "^1.0.0",
Expand Down
8 changes: 8 additions & 0 deletions modules/sdk-coin-trx/src/lib/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,14 @@ export class Transaction extends BaseTransaction {
return this._inputs;
}

/** @inheritDoc */
get signablePayload(): Buffer {
if (!this._transaction) {
throw new ParseTransactionError('Empty transaction');
}
return Buffer.from(this._transaction.raw_data_hex, 'hex');
}

/** @inheritdoc */
canSign(key: BaseKey): boolean {
// Tron transaction do not contain the owners account address so it is not possible to check the
Expand Down
22 changes: 21 additions & 1 deletion modules/sdk-coin-trx/src/lib/transactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
BuildTransactionError,
InvalidTransactionError,
ParseTransactionError,
PublicKey as BasePublicKey,
SigningError,
ExtendTransactionError,
InvalidParameterValueError,
Expand Down Expand Up @@ -36,6 +37,8 @@ export class TransactionBuilder extends BaseTransactionBuilder {
protected _refBlockHash: string;
protected _expiration: number;
protected _timestamp: number;
protected _signature?: Buffer;
protected _signaturePublicKey?: string;

/**
* Public constructor.
Expand Down Expand Up @@ -162,12 +165,29 @@ export class TransactionBuilder extends BaseTransactionBuilder {
return new Transaction(this._coinConfig, signedTransaction);
}

/** @inheritDoc */
addSignature(publicKey: BasePublicKey, signature: Buffer): void {
this._signature = signature;
this._signaturePublicKey = publicKey.pub;
}

/** @inheritdoc */
protected async buildImplementation(): Promise<Transaction> {
// This is a no-op since Tron transactions are built from
if (!this.transaction.id) {
throw new BuildTransactionError('A valid transaction must have an id');
}

if (this._signature) {
const txJson = this.transaction.toJson();
const signatures = txJson.signature ? [...txJson.signature] : [];
signatures.push(this._signature.toString('hex'));
const updatedReceipt: TransactionReceipt = {
...txJson,
signature: signatures,
};
return new Transaction(this._coinConfig, updatedReceipt);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check whether we need to assign it to this.transaction or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we don't need to assign it to this.transaction. The base class build() method (in baseTransactionBuilder.ts) does not assign the result of buildImplementation() back to this.transaction , instead it simply returns it to the caller.

}

return Promise.resolve(this.transaction);
}

Expand Down
7 changes: 6 additions & 1 deletion modules/sdk-coin-trx/src/lib/wrappedBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BigNumber from 'bignumber.js';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { BaseKey, BaseTransaction, InvalidTransactionError } from '@bitgo/sdk-core';
import { BaseKey, BaseTransaction, InvalidTransactionError, PublicKey as BasePublicKey } from '@bitgo/sdk-core';
import { Transaction } from './transaction';
import { Address } from './address';
import { TransactionBuilder } from './transactionBuilder';
Expand Down Expand Up @@ -137,6 +137,11 @@ export class WrappedBuilder extends TransactionBuilder {
this._builder.sign(key);
}

/** @inheritDoc */
addSignature(publicKey: BasePublicKey, signature: Buffer): void {
this._builder.addSignature(publicKey, signature);
}

/** @inheritdoc */
async build(): Promise<BaseTransaction> {
return this._builder.build();
Expand Down
44 changes: 41 additions & 3 deletions modules/sdk-coin-trx/src/trx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @prettier
*/
import * as secp256k1 from 'secp256k1';
import { randomBytes } from 'crypto';
import { createHash, Hash, randomBytes } from 'crypto';
import { CoinFamily, BaseCoin as StaticsBaseCoin } from '@bitgo/statics';
import { bip32 } from '@bitgo/secp256k1';
import * as request from 'superagent';
Expand All @@ -16,6 +16,7 @@ import {
KeyPair,
KeyIndices,
MethodNotImplementedError,
MPCAlgorithm,
ParsedTransaction,
ParseTransactionOptions,
SignedTransaction,
Expand All @@ -31,7 +32,10 @@ import {
multisigTypes,
AuditDecryptedKeyParams,
AddressCoinSpecific,
verifyMPCWalletAddress,
isTssVerifyAddressOptions,
} from '@bitgo/sdk-core';
import { auditEcdsaPrivateKey } from '@bitgo/sdk-lib-mpc';
import { Interface, Utils, WrappedBuilder, KeyPair as TronKeyPair } from './lib';
import { ValueFields, TransactionReceipt } from './lib/iface';
import { getBuilder } from './lib/builder';
Expand Down Expand Up @@ -189,6 +193,21 @@ export class Trx extends BaseCoin {
return multisigTypes.onchain;
}

/** @inheritDoc */
supportsTss(): boolean {
return true;
}

/** @inheritDoc */
getMPCAlgorithm(): MPCAlgorithm {
return 'ecdsa';
}

/** @inheritDoc */
getHashFunction(): Hash {
return createHash('sha256');
}

static createInstance(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>): BaseCoin {
return new Trx(bitgo, staticsCoin);
}
Expand Down Expand Up @@ -269,6 +288,14 @@ export class Trx extends BaseCoin {
async isWalletAddress(params: VerifyAddressOptions): Promise<boolean> {
const { address, keychains } = params;

if (isTssVerifyAddressOptions(params)) {
return verifyMPCWalletAddress(
{ ...params, keyCurve: 'secp256k1' },
this.isValidAddress.bind(this),
(pubKey: string) => new TronKeyPair({ pub: pubKey }).getAddress()
);
}

if (!isTrxVerifyAddressOptions(params)) {
throw new Error('Invalid or missing index for address verification');
}
Expand Down Expand Up @@ -404,6 +431,13 @@ export class Trx extends BaseCoin {
return true;
}

/** @inheritDoc */
async getSignablePayload(serializedTx: string): Promise<Buffer> {
const txBuilder = getBuilder(this.getChain()).from(serializedTx);
const rebuiltTransaction = await txBuilder.build();
return rebuiltTransaction.signablePayload;
}

/**
* Derive a user key using the chain path of the address
* @param key
Expand Down Expand Up @@ -1071,7 +1105,11 @@ export class Trx extends BaseCoin {
}

/** @inheritDoc */
auditDecryptedKey(params: AuditDecryptedKeyParams) {
throw new MethodNotImplementedError();
auditDecryptedKey({ multiSigType, publicKey, prv }: AuditDecryptedKeyParams) {
if (multiSigType === 'tss') {
auditEcdsaPrivateKey(prv as string, publicKey as string);
} else {
throw new MethodNotImplementedError();
}
}
}
4 changes: 4 additions & 0 deletions modules/statics/src/coinFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ export const TRX_FEATURES = [
CoinFeature.MULTISIG_COLD,
CoinFeature.MULTISIG,
CoinFeature.STAKING,
CoinFeature.TSS,
CoinFeature.TSS_COLD,
CoinFeature.MPCV2,
CoinFeature.SHA256_WITH_ECDSA_TSS,
];
export const COSMOS_SIDECHAIN_FEATURES = [
...ACCOUNT_COIN_DEFAULT_FEATURES,
Expand Down
4 changes: 2 additions & 2 deletions modules/statics/test/unit/fixtures/expectedColdFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const expectedColdFeatures = {
'tsoneium',
'flr',
'tflr',
'trx',
'ttrx',
],
justMultiSig: [
'algo',
Expand Down Expand Up @@ -54,9 +56,7 @@ export const expectedColdFeatures = {
'thbar',
'tltc',
'trbtc',
'trx',
'tstx',
'ttrx',
'txlm',
'txrp',
'txtz',
Expand Down