diff --git a/web3sTests/ENS/ENSOffchainTests.swift b/web3sTests/ENS/ENSOffchainTests.swift index 42e30b38..f918f2ad 100644 --- a/web3sTests/ENS/ENSOffchainTests.swift +++ b/web3sTests/ENS/ENSOffchainTests.swift @@ -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).") + } + } } diff --git a/web3swift/src/ENS/ENSResolver.swift b/web3swift/src/ENS/ENSResolver.swift index 17997195..de85f2ee 100644 --- a/web3swift/src/ENS/ENSResolver.swift +++ b/web3swift/src/ENS/ENSResolver.swift @@ -13,7 +13,6 @@ class ENSResolver { let address: EthereumAddress let callResolution: CallResolution private (set) var supportsWildCard: Bool? - private let mustSupportWilcard: Bool private let client: EthereumClientProtocol @@ -21,18 +20,17 @@ class ENSResolver { 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 { @@ -42,7 +40,7 @@ class ENSResolver { } self.supportsWildCard = wildcardResolution - if mustSupportWilcard && !wildcardResolution { + if mustSupportWildCard && !wildcardResolution { // Wildcard name resolution (ENSIP-10) throw EthereumNameServiceError.ensUnknown } diff --git a/web3swift/src/ENS/EthereumNameService.swift b/web3swift/src/ENS/EthereumNameService.swift index 9e5810ec..e0e4988c 100644 --- a/web3swift/src/ENS/EthereumNameService.swift +++ b/web3swift/src/ENS/EthereumNameService.swift @@ -114,7 +114,7 @@ public class EthereumNameService: EthereumNameServiceProtocol { } Task { do { - let resolver = try await getResolver( + let (resolver, supportingWildCard) = try await getResolver( for: ens, fullName: ens, registryAddress: registryAddress, @@ -122,7 +122,8 @@ public class EthereumNameService: EthereumNameServiceProtocol { ) let address = try await resolver.resolve( - name: ens + name: ens, + supportingWildcard: supportingWildCard ) completion(nil, address) } catch let error { @@ -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) @@ -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 }