From 813904f72d5ff63828b0e56bcb238d0dcde9a895 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Thu, 25 Apr 2024 09:32:15 +0400 Subject: [PATCH 1/4] update encoding --- .../AuthRequest/AuthRequestPresenter.swift | 18 +++++++++--------- .../Auth/Services/SignRecapBuilder.swift | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Example/WalletApp/PresentationLayer/Wallet/AuthRequest/AuthRequestPresenter.swift b/Example/WalletApp/PresentationLayer/Wallet/AuthRequest/AuthRequestPresenter.swift index 131546b82..542593811 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/AuthRequest/AuthRequestPresenter.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/AuthRequest/AuthRequestPresenter.swift @@ -140,15 +140,6 @@ final class AuthRequestPresenter: ObservableObject { } private func buildAuthObjects() throws -> [AuthObject] { - guard let chain = getCommonAndRequestedChainsIntersection().first else { - throw Errors.noCommonChains - } - - let auth = try createAuthObjectForChain(chain: chain) - return [auth] - } - - private func buildOneAuthObject() throws -> [AuthObject] { var auths = [AuthObject]() try getCommonAndRequestedChainsIntersection().forEach { chain in @@ -158,6 +149,15 @@ final class AuthRequestPresenter: ObservableObject { return auths } + private func buildOneAuthObject() throws -> [AuthObject] { + guard let chain = getCommonAndRequestedChainsIntersection().first else { + throw Errors.noCommonChains + } + + let auth = try createAuthObjectForChain(chain: chain) + return [auth] + } + func getCommonAndRequestedChainsIntersection() -> Set { let requestedChains: Set = Set(request.payload.chains.compactMap { Blockchain($0) }) diff --git a/Sources/WalletConnectSign/Auth/Services/SignRecapBuilder.swift b/Sources/WalletConnectSign/Auth/Services/SignRecapBuilder.swift index fc008a98d..8d7b0ee63 100644 --- a/Sources/WalletConnectSign/Auth/Services/SignRecapBuilder.swift +++ b/Sources/WalletConnectSign/Auth/Services/SignRecapBuilder.swift @@ -45,7 +45,7 @@ struct SignRecapBuilder { guard let jsonData = try? encoder.encode(modifiedRecapData) else { throw SignRecap.Errors.invalidRecapStructure } - let jsonBase64String = jsonData.base64EncodedString() + let jsonBase64String = jsonData.base64urlEncodedString() let modifiedUrn = "urn:recap:\(jsonBase64String)" return try SignRecap(urn: modifiedUrn) From f65d041ffe337d809c1af3d4962ddabf0a7724ee Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Thu, 25 Apr 2024 09:47:07 +0400 Subject: [PATCH 2/4] update encoding --- .../Auth/Services/App/AuthenticatedSessionRecapUrnFactory.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/WalletConnectSign/Auth/Services/App/AuthenticatedSessionRecapUrnFactory.swift b/Sources/WalletConnectSign/Auth/Services/App/AuthenticatedSessionRecapUrnFactory.swift index bbc717920..683416d14 100644 --- a/Sources/WalletConnectSign/Auth/Services/App/AuthenticatedSessionRecapUrnFactory.swift +++ b/Sources/WalletConnectSign/Auth/Services/App/AuthenticatedSessionRecapUrnFactory.swift @@ -7,7 +7,7 @@ class AuthenticatedSessionRecapUrnFactory { let jsonEncoder = JSONEncoder() jsonEncoder.outputFormatting = .withoutEscapingSlashes let jsonData = try jsonEncoder.encode(recap) - let base64Encoded = jsonData.base64EncodedString() + let base64Encoded = jsonData.base64urlEncodedString() let urn = "urn:recap:\(base64Encoded)" return urn } From 08b6d19ac0f44a2bc0d361b611c69bf6c0534bbe Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Thu, 25 Apr 2024 11:22:55 +0400 Subject: [PATCH 3/4] retain additional attributes in sign recap --- .../AuthenticatedSessionRecapUrnFactory.swift | 4 +-- .../Auth/Services/SignRecapBuilder.swift | 20 +++++++------- .../SIWE/RecapUrnMergingService.swift | 2 +- .../SignRecapBuilderTests.swift | 27 +++++++++++++++++++ .../RecapUrnMergingServiceTests.swift | 4 +-- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/Sources/WalletConnectSign/Auth/Services/App/AuthenticatedSessionRecapUrnFactory.swift b/Sources/WalletConnectSign/Auth/Services/App/AuthenticatedSessionRecapUrnFactory.swift index 683416d14..478f3580d 100644 --- a/Sources/WalletConnectSign/Auth/Services/App/AuthenticatedSessionRecapUrnFactory.swift +++ b/Sources/WalletConnectSign/Auth/Services/App/AuthenticatedSessionRecapUrnFactory.swift @@ -7,8 +7,8 @@ class AuthenticatedSessionRecapUrnFactory { let jsonEncoder = JSONEncoder() jsonEncoder.outputFormatting = .withoutEscapingSlashes let jsonData = try jsonEncoder.encode(recap) - let base64Encoded = jsonData.base64urlEncodedString() - let urn = "urn:recap:\(base64Encoded)" + let base64urlEncoded = jsonData.base64urlEncodedString() + let urn = "urn:recap:\(base64urlEncoded)" return urn } } diff --git a/Sources/WalletConnectSign/Auth/Services/SignRecapBuilder.swift b/Sources/WalletConnectSign/Auth/Services/SignRecapBuilder.swift index 8d7b0ee63..2814591ec 100644 --- a/Sources/WalletConnectSign/Auth/Services/SignRecapBuilder.swift +++ b/Sources/WalletConnectSign/Auth/Services/SignRecapBuilder.swift @@ -17,27 +17,24 @@ struct SignRecapBuilder { throw BuilderError.nonEVMChainNamespace } - // Convert supportedEVMChains to string array for intersection let supportedChainStrings = supportedEVMChains.map { $0.absoluteString } - - // Find intersection of requestedChains and supportedEVMChains strings let commonChains = requestedChains.filter(supportedChainStrings.contains) guard !commonChains.isEmpty else { throw BuilderError.noCommonChains } let requestedRecap = try SignRecap(urn: urn) + var filteredActions = requestedRecap.recapData.att ?? [:] - var filteredActions: [String: [String: [AnyCodable]]] = [:] - - if let eip155Actions = requestedRecap.recapData.att?["eip155"] { + if let eip155Actions = filteredActions["eip155"] { + var newEip155Actions: [String: [AnyCodable]] = [:] for method in supportedMethods { let actionKey = "request/\(method)" - if eip155Actions.keys.contains(actionKey) { - // Use only common chains for each supported method - filteredActions["eip155", default: [:]][actionKey] = [AnyCodable(["chains": commonChains])] + if let actions = eip155Actions[actionKey] { + newEip155Actions[actionKey] = [AnyCodable(["chains": commonChains])] } } + filteredActions["eip155"] = newEip155Actions } let modifiedRecapData = SignRecap.RecapData(att: filteredActions, prf: requestedRecap.recapData.prf) @@ -45,9 +42,10 @@ struct SignRecapBuilder { guard let jsonData = try? encoder.encode(modifiedRecapData) else { throw SignRecap.Errors.invalidRecapStructure } - let jsonBase64String = jsonData.base64urlEncodedString() + let jsonBase64urlString = jsonData.base64urlEncodedString() - let modifiedUrn = "urn:recap:\(jsonBase64String)" + let modifiedUrn = "urn:recap:\(jsonBase64urlString)" return try SignRecap(urn: modifiedUrn) } + } diff --git a/Sources/WalletConnectUtils/SIWE/RecapUrnMergingService.swift b/Sources/WalletConnectUtils/SIWE/RecapUrnMergingService.swift index 02c7452d8..86e1e3523 100644 --- a/Sources/WalletConnectUtils/SIWE/RecapUrnMergingService.swift +++ b/Sources/WalletConnectUtils/SIWE/RecapUrnMergingService.swift @@ -49,7 +49,7 @@ public class RecapUrnMergingService { let encoder = JSONEncoder() encoder.outputFormatting = [.sortedKeys] guard let jsonData = try? encoder.encode(RecapData(att: sortedMergedAtt, prf: nil)), - let jsonBase64 = jsonData.base64EncodedString().addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) else { + let jsonBase64 = jsonData.base64urlEncodedString().addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) else { throw Errors.encodingFailed } diff --git a/Tests/WalletConnectSignTests/SignRecapBuilderTests.swift b/Tests/WalletConnectSignTests/SignRecapBuilderTests.swift index cd69c8475..8d002196e 100644 --- a/Tests/WalletConnectSignTests/SignRecapBuilderTests.swift +++ b/Tests/WalletConnectSignTests/SignRecapBuilderTests.swift @@ -91,4 +91,31 @@ class SignRecapBuilderTests: XCTestCase { XCTAssertFalse(result.recapData.att?["eip155"]?.keys.contains("request/extraUnsupportedMethod") ?? true, "Result should not contain 'extraUnsupportedMethod'") } + func testSessionRecapBuilder_RetainsAdditionalAttributes() throws { + // Given + let requestedRecap: [String: [String: [String: [[String: [String]]]]]] = [ + "att": [ + "eip155": [ + "request/eth_sendTransaction": [[:]], + "request/personal_sign": [[:]] + ], + "https://notify.walletconnect.com": [ + "manage/all-apps-notifications": [[:]] + ] + ] + ] + let encoded = try! JSONEncoder().encode(requestedRecap).base64EncodedString() + let urn = "urn:recap:\(encoded)" + + let supportedChains = [Blockchain("eip155:1")!, Blockchain("eip155:137")!] + let supportedMethods = ["eth_sendTransaction", "personal_sign"] + let requestedChains = ["eip155:1", "eip155:137"] + + // When + let result = try SignRecapBuilder.build(requestedSessionRecap: urn, requestedChains: requestedChains, supportedEVMChains: supportedChains, supportedMethods: supportedMethods) + + // Then + XCTAssertNotNil(result.recapData.att?["eip155"], "EIP155 namespace should be present") + XCTAssertNotNil(result.recapData.att?["https://notify.walletconnect.com"], "https://notify.walletconnect.com namespace should be retained") + } } diff --git a/Tests/WalletConnectUtilsTests/RecapUrnMergingServiceTests.swift b/Tests/WalletConnectUtilsTests/RecapUrnMergingServiceTests.swift index 719cf7a45..ead98da62 100644 --- a/Tests/WalletConnectUtilsTests/RecapUrnMergingServiceTests.swift +++ b/Tests/WalletConnectUtilsTests/RecapUrnMergingServiceTests.swift @@ -25,8 +25,8 @@ class RecapUrnMergingTests: XCTestCase { } """ - guard let notifyBase64 = notifyRecapJson.data(using: .utf8)?.base64EncodedString(), - let signBase64 = signRecapJson.data(using: .utf8)?.base64EncodedString() else { + guard let notifyBase64 = notifyRecapJson.data(using: .utf8)?.base64urlEncodedString(), + let signBase64 = signRecapJson.data(using: .utf8)?.base64urlEncodedString() else { XCTFail("Failed to encode JSON strings to Base64") return } From 30d95424d15a6d2d052a017cdca91e273687cc36 Mon Sep 17 00:00:00 2001 From: llbartekll Date: Thu, 25 Apr 2024 10:11:13 +0000 Subject: [PATCH 4/4] Set User Agent --- Sources/WalletConnectRelay/PackageConfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/WalletConnectRelay/PackageConfig.json b/Sources/WalletConnectRelay/PackageConfig.json index 21ba9ac25..c4ccda17c 100644 --- a/Sources/WalletConnectRelay/PackageConfig.json +++ b/Sources/WalletConnectRelay/PackageConfig.json @@ -1 +1 @@ -{"version": "1.18.5"} +{"version": "1.18.6"}