resolve ens

This commit is contained in:
ivan grachev 2023-11-13 19:33:42 +03:00
parent f99da5968b
commit 7c502881a1

View File

@ -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<String, Error>) -> 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<String, Error>) -> Void) {
request(method: "eth_getBalance", params: [address, "pending"], rpcUrl: rpcUrl, completion: completion)
}