Line data Source code
1 : import 'dart:convert';
2 : import 'dart:math';
3 : import 'dart:typed_data';
4 : import 'package:convert/convert.dart';
5 : import 'package:walletkit_dart/src/utils/keccak.dart';
6 : import 'package:walletkit_dart/walletkit_dart.dart';
7 : // import 'package:bip39/bip39.dart' as bip39;
8 : import 'package:bip32/bip32.dart' as bip32;
9 : import 'package:bip39/bip39.dart' as bip39;
10 :
11 : export 'utxo/utils/derivation.dart';
12 :
13 1 : Future<TokenInfo?> getTokenInfo({
14 : required String contractAddress,
15 : required EvmRpcInterface rpc,
16 : }) async {
17 1 : final tokenContract = ERC20Contract(
18 : contractAddress: contractAddress,
19 : rpc: rpc,
20 : );
21 :
22 : try {
23 1 : final result = await Future.wait(
24 1 : [
25 1 : tokenContract.getDecimals(),
26 1 : tokenContract.getName(),
27 1 : tokenContract.getSymbol(),
28 1 : tokenContract.getSupply(),
29 : ],
30 : eagerError: true,
31 : );
32 1 : int decimals = result[0] as int;
33 1 : String name = result[1] as String;
34 1 : String symbol = result[2] as String;
35 1 : BigInt maxSupply = result[3] as BigInt;
36 3 : double maxSupplyDouble = maxSupply.toDouble() / pow(10, decimals);
37 :
38 1 : return TokenInfo(
39 : decimals: decimals,
40 : maxSupply: maxSupplyDouble,
41 : symbol: symbol,
42 : name: name,
43 : contractAddress: contractAddress,
44 : );
45 : } catch (e) {
46 : return null;
47 : }
48 : }
49 :
50 0 : Future<bool> isErc1155({
51 : required String contractAddress,
52 : required EvmRpcInterface rpc,
53 : required String address,
54 : }) async {
55 : bool isErc1155 = false;
56 : try {
57 0 : await rpc.fetchERC1155BalanceOfToken(
58 : address: address,
59 0 : tokenID: BigInt.from(0),
60 : contractAddress: contractAddress,
61 : );
62 : isErc1155 = true;
63 : } catch (e) {
64 : isErc1155 = false;
65 : }
66 : return isErc1155;
67 : }
68 :
69 1 : Uint8List publicKeyToAddress(Uint8List publicKey) {
70 : // 1. Ensure the public key is in the correct format
71 2 : if (publicKey.length == 64) {
72 : // If public key is 64 bytes, prepend 0x04 to indicate uncompressed key
73 3 : publicKey = Uint8List.fromList([4, ...publicKey]);
74 0 : } else if (publicKey.length != 65) {
75 0 : throw ArgumentError(
76 : 'Invalid public key length. Expected 65 bytes (or 64 bytes without prefix).');
77 : }
78 :
79 : // 2. Take Keccak-256 hash of the public key
80 : final hash =
81 2 : keccak256(publicKey.sublist(1)); // Remove the 0x04 prefix before hashing
82 :
83 : // 3. Take the last 20 bytes of the hash
84 1 : return hash.sublist(12, 32);
85 : }
86 :
87 0 : String pubKeytoChecksumETHAddress(Uint8List seed) {
88 0 : final publicKey = derivePublicKeyETH(seed);
89 0 : final pubKeyWithoutPrefix = keccak256(publicKey.sublist(1));
90 :
91 0 : final address = '0x${pubKeyWithoutPrefix.sublist(12).toHex}';
92 :
93 0 : final addressWithoutPrefix = address.replaceFirst('0x', '').toLowerCase();
94 :
95 : // Compute the keccak-256 hash of the address
96 0 : final hash = keccak256(utf8.encode(addressWithoutPrefix));
97 0 : final hashHex = hex.encode(hash);
98 :
99 : // Apply the checksum
100 0 : final checksummedAddress = StringBuffer('0x');
101 0 : for (int i = 0; i < addressWithoutPrefix.length; i++) {
102 0 : if (int.parse(hashHex[i], radix: 16) > 7) {
103 0 : checksummedAddress.write(addressWithoutPrefix[i].toUpperCase());
104 : } else {
105 0 : checksummedAddress.write(addressWithoutPrefix[i].toLowerCase());
106 : }
107 : }
108 :
109 0 : return checksummedAddress.toString();
110 : }
111 :
112 0 : String getETHAddressFromMnemonic({
113 : required String mnemonic,
114 : }) {
115 0 : final seed = bip39.mnemonicToSeed(mnemonic);
116 :
117 0 : final publicKey = derivePublicKeyETH(seed);
118 :
119 0 : final publicKeyWithoutPrefix = keccak256(publicKey.sublist(1));
120 :
121 0 : final address = '0x${publicKeyWithoutPrefix.sublist(12).toHex}';
122 :
123 : return address;
124 : }
125 :
126 2 : Uint8List derivePrivateKeyETH(Uint8List seed) {
127 2 : final node = bip32.BIP32.fromSeed(seed);
128 :
129 2 : final bip32.BIP32 childNode = node.derivePath(
130 2 : ethereumBip44HDPath.defaultPath,
131 : );
132 2 : return childNode.privateKey!;
133 : }
134 :
135 0 : Uint8List derivePublicKeyETH(Uint8List seed) {
136 0 : final node = bip32.BIP32.fromSeed(seed);
137 :
138 0 : final bip32.BIP32 childNode = node.derivePath(
139 0 : ethereumBip44HDPath.defaultPath,
140 : );
141 0 : return childNode.publicKeyUncompressed;
142 : }
|