Skip to content

Commit 0ce2ef8

Browse files
committed
feat: add tron type data encode support, dart impl for tronWeb typeData.ts
1 parent d3d58e8 commit 0ce2ef8

File tree

3 files changed

+762
-0
lines changed

3 files changed

+762
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'package:on_chain/tron/tron.dart';
2+
3+
void main() {
4+
final domain = TronTypedDataDomain(
5+
name: 'Permit2',
6+
chainId: BigInt.parse('728126428'),
7+
verifyingContract: 'TDJNTBi51CnnpCYYgi6GitoT4CJWrqim2G',
8+
);
9+
10+
// 定义类型结构
11+
final types = {
12+
'PermitDetails': [
13+
const TronTypedDataField(name: 'token', type: 'address'),
14+
const TronTypedDataField(name: 'amount', type: 'uint160'),
15+
const TronTypedDataField(name: 'expiration', type: 'uint48'),
16+
const TronTypedDataField(name: 'nonce', type: 'uint48'),
17+
],
18+
'PermitSingle': [
19+
const TronTypedDataField(name: 'details', type: 'PermitDetails'),
20+
const TronTypedDataField(name: 'spender', type: 'address'),
21+
const TronTypedDataField(name: 'sigDeadline', type: 'uint256'),
22+
],
23+
};
24+
25+
final value = {
26+
'details': {
27+
'token': '0xa614f803b6fd780986a42c78ec9c7f77e6ded13c',
28+
'amount': '1100000',
29+
'expiration': 0,
30+
'nonce': 1,
31+
},
32+
'spender': '0xbde814ebd17a0b25c39ee16a8b2ff48d1628e503',
33+
'sigDeadline': 1753172632,
34+
};
35+
36+
try {
37+
final encoder = TronTypedDataEncoder(types);
38+
print('Primary Type: ${encoder.primaryType}');
39+
40+
final domainHash = TronTypedDataEncoder.hashDomain(domain);
41+
print('Domain Hash: $domainHash');
42+
43+
final structHash = encoder.hash(value);
44+
print('Struct Hash: $structHash');
45+
46+
final finalHash = TronTypedDataEncoder.hashTypedData(domain, types, value);
47+
print('Final TIP-712 Hash: $finalHash');
48+
// expected finalHash == 9cd34058512e83f5f87643fa4388c2eda64079e4e532acfd5a184f3adf943279
49+
} catch (e) {
50+
print('Error: $e');
51+
}
52+
}

0 commit comments

Comments
 (0)