2023-09-25 13:47:39 +00:00
|
|
|
import 'package:cake_wallet/ethereum/ethereum.dart';
|
2023-12-02 02:26:43 +00:00
|
|
|
import 'package:cake_wallet/polygon/polygon.dart';
|
2023-09-25 13:47:39 +00:00
|
|
|
import 'package:cw_core/wallet_base.dart';
|
|
|
|
import 'package:cw_core/wallet_type.dart';
|
|
|
|
import 'package:ens_dart/ens_dart.dart';
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
import 'package:web3dart/web3dart.dart';
|
|
|
|
|
|
|
|
class EnsRecord {
|
|
|
|
static Future<String> fetchEnsAddress(String name, {WalletBase? wallet}) async {
|
|
|
|
Web3Client? _client;
|
|
|
|
|
|
|
|
if (wallet != null && wallet.type == WalletType.ethereum) {
|
2023-10-03 17:07:05 +00:00
|
|
|
_client = ethereum!.getWeb3Client(wallet);
|
2023-09-25 13:47:39 +00:00
|
|
|
}
|
2023-12-02 02:26:43 +00:00
|
|
|
|
|
|
|
if (wallet != null && wallet.type == WalletType.polygon) {
|
|
|
|
_client = polygon!.getWeb3Client(wallet);
|
|
|
|
}
|
2023-09-25 13:47:39 +00:00
|
|
|
|
|
|
|
if (_client == null) {
|
2023-09-29 15:24:07 +00:00
|
|
|
_client = Web3Client("https://ethereum.publicnode.com", Client());
|
2023-09-25 13:47:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
final ens = Ens(client: _client);
|
|
|
|
|
2023-09-25 13:56:34 +00:00
|
|
|
if (wallet != null) {
|
|
|
|
switch (wallet.type) {
|
|
|
|
case WalletType.monero:
|
|
|
|
return await ens.withName(name).getCoinAddress(CoinType.XMR);
|
|
|
|
case WalletType.bitcoin:
|
|
|
|
return await ens.withName(name).getCoinAddress(CoinType.BTC);
|
|
|
|
case WalletType.litecoin:
|
|
|
|
return await ens.withName(name).getCoinAddress(CoinType.LTC);
|
|
|
|
case WalletType.haven:
|
|
|
|
return await ens.withName(name).getCoinAddress(CoinType.XHV);
|
|
|
|
case WalletType.ethereum:
|
2023-12-02 02:26:43 +00:00
|
|
|
case WalletType.polygon:
|
2023-09-25 13:56:34 +00:00
|
|
|
default:
|
|
|
|
return (await ens.withName(name).getAddress()).hex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 13:47:39 +00:00
|
|
|
final addr = await ens.withName(name).getAddress();
|
|
|
|
return addr.hex;
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|