Skip to content

Commit a17a5bb

Browse files
fix: ENS parsing requires chainID if an ENS address is expected to be requested/decoded - we do not assume that client is using Ethereum Mainnet!
According to EIP-681: chain_id is optional and contains the decimal chain ID, such that transactions on various test- and private networks can be requested. If no chain_id is present, the client's current network setting remains effective. In our case if no chainID is present we cannot validate ENS address and will not attempt to guess the network.
1 parent ed43d2d commit a17a5bb

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

Sources/web3swift/Utils/EIP/EIP681.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ extension Web3 {
348348

349349
private static func parseFunctionArgument(_ inputType: ABI.Element.ParameterType,
350350
_ rawValue: String,
351-
chainID: BigUInt? = Networks.Mainnet.chainID,
351+
chainID: BigUInt?,
352352
inputNumber: Int) async -> FunctionArgument? {
353353
var nativeValue: Any?
354354
switch inputType {
@@ -358,8 +358,9 @@ extension Web3 {
358358
case .ethereumAddress(let ethereumAddress):
359359
nativeValue = ethereumAddress
360360
case .ensAddress(let ens):
361+
guard let chainID = chainID else { return nil }
361362
do {
362-
let web = await Web3(provider: InfuraProvider(chainID == nil ? .Mainnet : .fromInt(UInt(chainID!)))!)
363+
let web = await Web3(provider: InfuraProvider(.fromInt(UInt(chainID)))!)
363364
let ensModel = ENS(web3: web)
364365
try await ensModel?.setENSResolver(withDomain: ens)
365366
let address = try await ensModel?.getAddress(forNode: ens)

Tests/web3swiftTests/localTests/EIP681Tests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class EIP681Tests: XCTestCase {
8383

8484
func testENSParsing() async throws {
8585
let testAddress = "somename.eth"
86-
let eip681Code = await Web3.EIP681CodeParser.parse("ethereum:\(testAddress)/transfer?address=somename.eth&uint256=1")
86+
let eip681Code = await Web3.EIP681CodeParser.parse("ethereum:\(testAddress)@1/transfer?address=somename.eth&uint256=1")
8787
XCTAssert(eip681Code != nil)
8888
guard let eip681Code = eip681Code else { return }
8989
switch eip681Code.targetAddress {
@@ -110,7 +110,7 @@ class EIP681Tests: XCTestCase {
110110

111111
func testENSParsingWithEncoding() async throws {
112112
let testAddress = "somename.eth"
113-
let eip681Code = await Web3.EIP681CodeParser.parse("ethereum:\(testAddress)/transfer?address=somename.eth&uint256=1".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
113+
let eip681Code = await Web3.EIP681CodeParser.parse("ethereum:\(testAddress)@1/transfer?address=somename.eth&uint256=1".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
114114
XCTAssert(eip681Code != nil)
115115
guard let eip681Code = eip681Code else { return }
116116
switch eip681Code.targetAddress {

0 commit comments

Comments
 (0)