diff --git a/Shared/Ethereum/EthereumRPC.swift b/Shared/Ethereum/EthereumRPC.swift index e7d37e97..8701c51a 100644 --- a/Shared/Ethereum/EthereumRPC.swift +++ b/Shared/Ethereum/EthereumRPC.swift @@ -1,6 +1,7 @@ // Copyright © 2023 Tokenary. All rights reserved. import Foundation +import WalletCore private struct RPCResponse: Codable { let id: Int @@ -28,6 +29,31 @@ class EthereumRPC { request(method: "eth_gasPrice", params: [], rpcUrl: rpcUrl, completion: completion) } + func resolveENS(rpcUrl: String, for address: String, completion: @escaping (Result) -> Void) { + let reverseRecord = "\(address.lowercased().cleanHex).addr.reverse" + + var node = Data(repeating: 0, count: 32) + if !reverseRecord.isEmpty { + node = reverseRecord.split(separator: ".").reversed().reduce(node) { (currentNode, label) in + var node = currentNode + guard let data = label.data(using: .utf8) else { return Data() } + node.append(Hash.keccak256(data: data)) + return Hash.keccak256(data: node) + } + } + + let nameHash = node.hexString + let method = "eth_call" + let params: [Any] = [ + [ + "to": "0x231b0Ee14048e9dCcD1d247744d114a4EB5E8E63", + "data": "0x691f3431" + String(repeating: "0", count: 64 - nameHash.count) + nameHash + ], + "latest" + ] + request(method: method, params: params, rpcUrl: rpcUrl, completion: completion) + } + func getBalance(rpcUrl: String, for address: String, completion: @escaping (Result) -> Void) { request(method: "eth_getBalance", params: [address, "pending"], rpcUrl: rpcUrl, completion: completion) }