Skip to content

Commit 98fe259

Browse files
committed
chore: adds tron
1 parent 66382cd commit 98fe259

8 files changed

Lines changed: 115 additions & 1 deletion

File tree

packages/keyring-api/src/api/account.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ export enum SolAccountType {
3030
DataAccount = 'solana:data-account',
3131
}
3232

33+
/**
34+
* Supported Tron account types.
35+
*/
36+
export enum TrxAccountType {
37+
Eoa = 'tron:eoa',
38+
}
39+
3340
/**
3441
* A generic account type. It can be used to represent any account type that is
3542
* not covered by the other account types. It only applies to non-EVM chains.
@@ -49,6 +56,7 @@ export type KeyringAccountType =
4956
| `${BtcAccountType.P2wpkh}`
5057
| `${BtcAccountType.P2tr}`
5158
| `${SolAccountType.DataAccount}`
59+
| `${TrxAccountType.Eoa}`
5260
| `${AnyAccountType.Account}`;
5361

5462
/**
@@ -75,6 +83,7 @@ export const KeyringAccountStruct = object({
7583
`${BtcAccountType.P2wpkh}`,
7684
`${BtcAccountType.P2tr}`,
7785
`${SolAccountType.DataAccount}`,
86+
`${TrxAccountType.Eoa}`,
7887
`${AnyAccountType.Account}`,
7988
]),
8089

packages/keyring-api/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from './api';
22
export * from './btc';
33
export * from './sol';
44
export * from './eth';
5+
export * from './trx';
56
export type * from './contexts';
67
export * from './rpc';
78
export * from './events';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// istanbul ignore file
2+
3+
/**
4+
* Scopes for TRON account type. See {@link KeyringAccount.scopes}.
5+
*/
6+
export enum TrxScope {
7+
Mainnet = 'tron:728126428',
8+
Nile = 'tron:3448148188',
9+
Shasta = 'tron:2494104990',
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './constants';
2+
export * from './types';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { Extends } from '@metamask/keyring-utils';
2+
import { expectTrue } from '@metamask/keyring-utils';
3+
4+
import type { TrxEoaAccount } from './types';
5+
import type { KeyringAccount } from '../api';
6+
7+
// `TrxEoaAccount` extends `KeyringAccount`
8+
expectTrue<Extends<TrxEoaAccount, KeyringAccount>>();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { TrxAddressStruct } from './types';
2+
3+
describe('types', () => {
4+
describe('TrxAddressStruct', () => {
5+
it.each([
6+
'TJ1111111111111111111111111111111111111111',
7+
'TJ2222222222222222222222222222222222222222',
8+
])('is valid address: %s', (address) => {
9+
expect(() => TrxAddressStruct.assert(address)).not.toThrow();
10+
});
11+
12+
it.each([
13+
// Invalid lengths, too long (45 chars)
14+
'TJ11111111111111111111111111111111111111111',
15+
// Too short (31 chars)
16+
'TJ111111111111111111111111111111111111111',
17+
// Empty or invalid input
18+
'',
19+
// Eth style address
20+
'0x1234',
21+
'not-an-address',
22+
])('rejects invalid address: %s', (address) => {
23+
expect(() => TrxAddressStruct.assert(address)).toThrow(
24+
`Expected a value of type \`TrxAddress\`, but received: \`"${address}"\``,
25+
);
26+
});
27+
});
28+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { object } from '@metamask/keyring-utils';
2+
import type { Infer } from '@metamask/superstruct';
3+
import { array, enums, literal, nonempty, record } from '@metamask/superstruct';
4+
import { definePattern } from '@metamask/utils';
5+
6+
import {
7+
CaipChainIdStruct,
8+
KeyringAccountStruct,
9+
SolAccountType,
10+
TrxAccountType,
11+
} from '../api';
12+
13+
/**
14+
* TRON addresses are Base58-encoded strings that are exactly 34 characters long
15+
* and start with the letter 'T'.
16+
*/
17+
export const TrxAddressStruct = definePattern(
18+
'TrxAddress',
19+
/^T[1-9A-HJ-NP-Za-km-z]{33}$/iu,
20+
);
21+
22+
/**
23+
* Supported TRON methods.
24+
*/
25+
export declare enum TrxMethod {
26+
SignMessageV2 = "signMessageV2",
27+
VerifyMessageV2 = "verifyMessageV2"
28+
}
29+
30+
export const TrxEoaAccountStruct = object({
31+
...KeyringAccountStruct.schema,
32+
/**
33+
* Account address.
34+
*/
35+
address: TrxAddressStruct,
36+
/**
37+
* Account type.
38+
*/
39+
type: literal(`${TrxAccountType.Eoa}`),
40+
/**
41+
* Account supported scopes (CAIP-2 chain IDs).
42+
*/
43+
scopes: nonempty(array(CaipChainIdStruct)),
44+
/**
45+
* Account supported methods.
46+
*/
47+
methods: array(enums(Object.values(TrxMethod))),
48+
});
49+
50+
export type TrxEoaAccount = Infer<typeof TrxEoaAccountStruct>;

packages/keyring-snap-bridge/src/account.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
KeyringAccountStruct,
1313
SolAccountType,
1414
SolDataAccountStruct,
15+
TrxAccountType,
16+
TrxEoaAccountStruct,
1517
} from '@metamask/keyring-api';
1618
import { assert, omit, type Infer } from '@metamask/superstruct';
1719

@@ -66,6 +68,10 @@ export function assertKeyringAccount<
6668
assert(account, EthEoaAccountStruct);
6769
return account;
6870
}
71+
case TrxAccountType.Eoa: {
72+
assert(account, TrxEoaAccountStruct);
73+
return account;
74+
}
6975
case AnyAccountType.Account: {
7076
assert(account, KeyringAccountStruct);
7177
return account;
@@ -95,5 +101,5 @@ export function transformAccount(
95101
: accountToTransform;
96102

97103
// We still assert that the converted account is valid according to their account's type.
98-
return assertKeyringAccount(account);
104+
return assertKeyringAccount(account as { type: KeyringAccountType });
99105
}

0 commit comments

Comments
 (0)