mirror of
https://github.com/lil-org/tokenary.git
synced 2024-12-03 06:25:10 +03:00
resolve ens
This commit is contained in:
parent
f99da5968b
commit
7c502881a1
@ -1,6 +1,7 @@
|
|||||||
// Copyright © 2023 Tokenary. All rights reserved.
|
// Copyright © 2023 Tokenary. All rights reserved.
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
import WalletCore
|
||||||
|
|
||||||
private struct RPCResponse: Codable {
|
private struct RPCResponse: Codable {
|
||||||
let id: Int
|
let id: Int
|
||||||
@ -28,6 +29,31 @@ class EthereumRPC {
|
|||||||
request(method: "eth_gasPrice", params: [], rpcUrl: rpcUrl, completion: completion)
|
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) {
|
func getBalance(rpcUrl: String, for address: String, completion: @escaping (Result<String, Error>) -> Void) {
|
||||||
request(method: "eth_getBalance", params: [address, "pending"], rpcUrl: rpcUrl, completion: completion)
|
request(method: "eth_getBalance", params: [address, "pending"], rpcUrl: rpcUrl, completion: completion)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user