Skip to content

Commit 586db74

Browse files
committed
feat: add tron scope types
1 parent 8277266 commit 586db74

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ export { getMultichainClient, getDefaultTransport, getExternallyConnectableTrans
3535
export type * from './types/transport';
3636
export type * from './types/session';
3737
export type * from './types/multichainApi';
38+
export type * from './types/scopes';
3839
export * from './types/errors';

src/types/scopes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Bip122Rpc } from './bip122.types';
22
import type { Eip155Rpc } from './eip155.types';
33
import type { SolanaRpc } from './solana.types';
4+
import type { TronRpc } from './tron.types';
45

56
export type RpcApi = Record<
67
string,
@@ -34,4 +35,5 @@ export type DefaultRpcApi = {
3435
eip155: Eip155Rpc;
3536
solana: SolanaRpc;
3637
bip122: Bip122Rpc;
38+
tron: TronRpc;
3739
};

src/types/scopes/tron.types.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import type { RpcMethod } from '.';
2+
import type { CaipChainId } from '../session';
3+
4+
/**
5+
* A TronWeb transaction in serialized base64 format.
6+
* @see https://github.com/tronprotocol/tronweb/blob/master/src/types/Transaction.ts
7+
*
8+
* To serialize a TronWeb transaction object to this format:
9+
* ```typescript
10+
* import TronWeb from 'tronweb';
11+
*
12+
* const tronWeb = new TronWeb({
13+
* fullHost: 'https://api.trongrid.io'
14+
* });
15+
*
16+
* // Create your transaction
17+
* const transaction = await tronWeb.transactionBuilder.sendTrx(
18+
* 'TGehVcNhud84JDCGrNHKVz9jEAVKUpbuiv',
19+
* 1000000,
20+
* 'TJRabPrwbZy45sbavfcjinPJC18kjpRTv8'
21+
* );
22+
*
23+
* // Serialize to base64
24+
* const txPb = tronWeb.utils.transaction.txJsonToPb(transaction);
25+
* const base64Transaction = Buffer.from(txPb.serializeBinary()).toString('base64');
26+
* ```
27+
*
28+
* To deserialize back to a transaction object:
29+
* ```typescript
30+
* const txBytes = Buffer.from(base64Transaction, 'base64');
31+
* const txPb = tronWeb.utils.transaction.txPbToTxID(txBytes);
32+
* const transaction = tronWeb.utils.transaction.txPbToJson(txPb);
33+
* ```
34+
*/
35+
export type Base64Transaction = string;
36+
37+
/**
38+
* A Tron address in Base58Check format, starting with 'T'.
39+
* @example "TJRabPrwbZy45sbavfcjinPJC18kjpRTv8"
40+
*/
41+
export type TronAddress = `T${string}`;
42+
43+
/**
44+
* A signature.
45+
*/
46+
export type Signature = `${string}`;
47+
48+
/**
49+
* Signs a plain text message.
50+
* The signature can be used to verify ownership of the account.
51+
*
52+
* @param chainId - CAIP-2 chain ID identifying the Tron network (e.g., "tron:0x2b6653dc")
53+
* @param address - The Tron address that will sign the message
54+
* @param message - The message string to be signed
55+
* @returns An object containing the hexadecimal signature of the message
56+
*/
57+
export type SignMessageMethod = RpcMethod<
58+
{
59+
chainId: CaipChainId;
60+
address: TronAddress;
61+
message: string;
62+
},
63+
{ signature: Signature }
64+
>;
65+
66+
/**
67+
* Signs a Tron transaction.
68+
* The transaction must be provided as a base64-encoded serialized transaction string.
69+
*
70+
* @param chainId - CAIP-2 chain ID identifying the Tron network (e.g., "tron:0x2b6653dc")
71+
* @param address - The Tron address that will sign the transaction
72+
* @param transaction - The Tron transaction in serialized base64 format
73+
* @returns An object containing the hexadecimal signature of the transaction
74+
*/
75+
export type SignTransactionMethod = RpcMethod<
76+
{
77+
chainId: CaipChainId;
78+
address: TronAddress;
79+
transaction: Base64Transaction;
80+
},
81+
{ signature: Signature }
82+
>;
83+
84+
export type TronRpc = {
85+
methods: {
86+
signMessage: SignMessageMethod;
87+
signTransaction: SignTransactionMethod;
88+
};
89+
events: [];
90+
};

tests/index.test-d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ expectType<`0x${string}`>(
4646
}),
4747
);
4848

49+
// Basic tron signMessage call with correct scope and parameters
50+
expectType<{ signature: string }>(
51+
await client.invokeMethod({
52+
scope: 'tron:728126428',
53+
request: {
54+
method: 'signMessage',
55+
params: {
56+
chainId: 'tron:728126428',
57+
address: 'TJRabPrwbZy45sbavfcjinPJC18kjpRTv8',
58+
message: 'hello world',
59+
},
60+
},
61+
}),
62+
);
63+
4964
// ==========================================
5065
// Test error cases for invalid inputs
5166
// ==========================================

0 commit comments

Comments
 (0)