Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] ENS wildcard resolution caching #216

Merged
merged 1 commit into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions web3sTests/ENS/ENSOffchainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,69 @@ class ENSOffchainTests: XCTestCase {
XCTFail("Expected ens but failed \(error).")
}
}

func testGivenRopstenRegistry_WhenWildcardNOTSupported_AndAddressHasSubdomain_ThenFailsResolving() async {
do {
let nameService = EthereumNameService(client: client!)

_ = try await nameService.resolve(
ens: "1.resolver.eth",
mode: .allowOffchainLookup
)

XCTFail("Expected error")
} catch {
XCTAssertEqual(error as? EthereumNameServiceError, .ensUnknown)
}
}

func testGivenRopstenRegistry_WhenTwoRequestsWithAndWithoutSubdomain_ThenBothResolveCorrectly() async {
let nameService = EthereumNameService(client: client!)

do {
let ens = try await nameService.resolve(
ens: "resolver.eth",
mode: .allowOffchainLookup
)
XCTAssertEqual(EthereumAddress("0x42d63ae25990889e35f215bc95884039ba354115"), ens)
} catch {
XCTFail("Expected ens but failed \(error).")
}
do {
_ = try await nameService.resolve(
ens: "1.resolver.eth",
mode: .allowOffchainLookup
)

XCTFail("Expected error")
} catch {
XCTAssertEqual(error as? EthereumNameServiceError, .ensUnknown)
}
}

func testGivenRopstenRegistry_WhenTwoRequestsWithoutAndWithSubdomain_ThenBothResolveCorrectly() async {
let nameService = EthereumNameService(client: client!)

do {
_ = try await nameService.resolve(
ens: "1.resolver.eth",
mode: .allowOffchainLookup
)

XCTFail("Expected error")
} catch {
XCTAssertEqual(error as? EthereumNameServiceError, .ensUnknown)
}

do {
let ens = try await nameService.resolve(
ens: "resolver.eth",
mode: .allowOffchainLookup
)
XCTAssertEqual(EthereumAddress("0x42d63ae25990889e35f215bc95884039ba354115"), ens)
} catch {
XCTFail("Expected ens but failed \(error).")
}
}
}

10 changes: 4 additions & 6 deletions web3swift/src/ENS/ENSResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,24 @@ class ENSResolver {
let address: EthereumAddress
let callResolution: CallResolution
private (set) var supportsWildCard: Bool?
private let mustSupportWilcard: Bool

private let client: EthereumClientProtocol

init(
address: EthereumAddress,
client: EthereumClientProtocol,
callResolution: CallResolution,
supportsWildCard: Bool? = nil,
mustSupportWildcard: Bool = false
supportsWildCard: Bool? = nil
) {
self.address = address
self.callResolution = callResolution
self.client = client
self.supportsWildCard = supportsWildCard
self.mustSupportWilcard = mustSupportWildcard
}

func resolve(
name: String
name: String,
supportingWildcard mustSupportWildCard: Bool
) async throws -> EthereumAddress {
let wildcardResolution: Bool
if let supportsWildCard = self.supportsWildCard {
Expand All @@ -42,7 +40,7 @@ class ENSResolver {
}
self.supportsWildCard = wildcardResolution

if mustSupportWilcard && !wildcardResolution {
if mustSupportWildCard && !wildcardResolution {
// Wildcard name resolution (ENSIP-10)
throw EthereumNameServiceError.ensUnknown
}
Expand Down
12 changes: 6 additions & 6 deletions web3swift/src/ENS/EthereumNameService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,16 @@ public class EthereumNameService: EthereumNameServiceProtocol {
}
Task {
do {
let resolver = try await getResolver(
let (resolver, supportingWildCard) = try await getResolver(
for: ens,
fullName: ens,
registryAddress: registryAddress,
mode: mode
)

let address = try await resolver.resolve(
name: ens
name: ens,
supportingWildcard: supportingWildCard
)
completion(nil, address)
} catch let error {
Expand Down Expand Up @@ -229,7 +230,7 @@ extension EthereumNameService {
fullName: String,
registryAddress: EthereumAddress,
mode: ResolutionMode
) async throws -> ENSResolver {
) async throws -> (ENSResolver, Bool) {
let function = ENSContracts.ENSRegistryFunctions.resolver(
contract: registryAddress,
parameter: .name(name)
Expand Down Expand Up @@ -263,11 +264,10 @@ extension EthereumNameService {
let resolver = resolversByAddress[resolverAddress] ?? ENSResolver(
address: resolverAddress,
client: client,
callResolution: mode.callResolution(maxRedirects: self.maximumRedirections),
mustSupportWildcard: fullName != name
callResolution: mode.callResolution(maxRedirects: self.maximumRedirections)
)
self.resolversByAddress[resolverAddress] = resolver
return resolver
return (resolver, fullName != name)
} catch {
throw error as? EthereumNameServiceError ?? .ensUnknown
}
Expand Down