Skip to content

Commit

Permalink
remove hardcoded default reverse resolver (trustwallet#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
hewigovens authored and vikmeup committed May 17, 2018
1 parent 33c0452 commit a582f96
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions Trust/EtherClient/ENS/ENSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ typealias ENSResolveResult = (resolver: Address, address: Address)

struct ENSClient {

static let ensContrct = Address(string: "0x314159265dd8dbb310642f98f50c066173c1259b")!
static let publicResolverContract = Address(string: "0x5fbb459c49bb06083c33109fa4f14810ec2cf358")!
static let reverseResolverContract = Address(string: "0x5fbb459c49bb06083c33109fa4f14810ec2cf358")!
static let ensContract = Address(string: "0x314159265dd8dbb310642f98f50c066173c1259b")!
static let reverseSuffix = "addr.reverse"

func resolve(name: String) -> Promise<ENSResolveResult> {
Expand All @@ -50,7 +48,7 @@ struct ENSClient {
let node = namehash(name)
let encoded = ENSEncoder.encodeResolver(node: node)
let request = EtherServiceRequest(
batch: BatchFactory().create(CallRequest(to: ENSClient.ensContrct.description, data: encoded.hexEncoded))
batch: BatchFactory().create(CallRequest(to: ENSClient.ensContract.description, data: encoded.hexEncoded))
)
return self.sendAddr(request: request)
}
Expand All @@ -59,48 +57,55 @@ struct ENSClient {
let node = namehash(name)
let encoded = ENSEncoder.encodeOwner(node: node)
let request = EtherServiceRequest(
batch: BatchFactory().create(CallRequest(to: ENSClient.ensContrct.description, data: encoded.hexEncoded))
batch: BatchFactory().create(CallRequest(to: ENSClient.ensContract.description, data: encoded.hexEncoded))
)
return self.sendAddr(request: request)
}

private func sendAddr(request: EtherServiceRequest<Batch1<CallRequest>>) -> Promise<Address> {
return Promise { seal in
Session.send(request) { result in
switch result {
case .success(let response):
let data = Data(hex: response)
guard data.count == 32 else {
return seal.reject(ENSError.decodeError)
func lookup(address: Address) -> Promise<String> {
let addr = [address.data.hex, ENSClient.reverseSuffix].joined(separator: ".")
return firstly {
return self.resolverOf(name: addr)
}.then { resolver -> Promise<String> in
if resolver == Address.zero {
return Promise { $0.resolve("", nil) }
}
let node = namehash(addr)
let encoded = ReverseResolverEncoder.encodeName(node)
let request = EtherServiceRequest(
batch: BatchFactory().create(CallRequest(to: resolver.description, data: encoded.hexEncoded))
)
return Promise { seal in
Session.send(request) { result in
switch result {
case .success(let response):
let data = Data(hex: response)
let decoder = ABIDecoder(data: data)
let decoded = try? decoder.decodeTuple(types: [.string])
guard let string = decoded?.first?.nativeValue as? String else {
return seal.reject(ENSError.decodeError)
}
seal.fulfill(string)
case .failure(let error):
seal.reject(error)
}
//take the last 20 bytes for Address
let sub = data.suffix(20)
seal.fulfill(Address(data: sub))
case .failure(let error):
seal.reject(error)
}
}
}
}

func lookup(address: Address) -> Promise<String> {
let addr = [address.data.hex, ENSClient.reverseSuffix].joined(separator: ".")
let node = namehash(addr)
let encoded = ReverseResolverEncoder.encodeName(node)
let request = EtherServiceRequest(
batch: BatchFactory().create(CallRequest(to: ENSClient.reverseResolverContract.description, data: encoded.hexEncoded))
)
private func sendAddr(request: EtherServiceRequest<Batch1<CallRequest>>) -> Promise<Address> {
return Promise { seal in
Session.send(request) { result in
switch result {
case .success(let response):
let data = Data(hex: response)
let decoder = ABIDecoder(data: data)
let decoded = try? decoder.decodeTuple(types: [.string])
guard let string = decoded?.first?.nativeValue as? String else {
guard data.count == 32 else {
return seal.reject(ENSError.decodeError)
}
seal.fulfill(string)
//take the last 20 bytes for Address
let sub = data.suffix(20)
seal.fulfill(Address(data: sub))
case .failure(let error):
seal.reject(error)
}
Expand Down

0 comments on commit a582f96

Please sign in to comment.