diff --git a/Sources/WalletConnectRelay/EnvironmentInfo.swift b/Sources/WalletConnectRelay/EnvironmentInfo.swift index 59947745e..676ef7a3c 100644 --- a/Sources/WalletConnectRelay/EnvironmentInfo.swift +++ b/Sources/WalletConnectRelay/EnvironmentInfo.swift @@ -3,28 +3,28 @@ import UIKit #endif import Foundation -enum EnvironmentInfo { +public enum EnvironmentInfo { - static var userAgent: String { + public static var userAgent: String { "\(protocolName)/\(sdkName)/\(operatingSystem)" } - static var protocolName: String { + public static var protocolName: String { "wc-2" } - static var sdkName: String { + public static var sdkName: String { "swift-v\(packageVersion)" } - static var packageVersion: String { + public static var packageVersion: String { let configURL = Bundle.resourceBundle.url(forResource: "PackageConfig", withExtension: "json")! let jsonData = try! Data(contentsOf: configURL) let config = try! JSONDecoder().decode(PackageConfig.self, from: jsonData) return config.version } - static var operatingSystem: String { + public static var operatingSystem: String { #if os(iOS) return "\(UIDevice.current.systemName)-\(UIDevice.current.systemVersion)" #elseif os(macOS) diff --git a/Sources/Web3Modal/Modal/ModalInteractor.swift b/Sources/Web3Modal/Modal/ModalInteractor.swift index 30cdf30e6..a321d0d2e 100644 --- a/Sources/Web3Modal/Modal/ModalInteractor.swift +++ b/Sources/Web3Modal/Modal/ModalInteractor.swift @@ -20,7 +20,10 @@ final class DefaultModalSheetInteractor: ModalSheetInteractor { let httpClient = HTTPNetworkClient(host: "explorer-api.walletconnect.com") let response = try await httpClient.request( ListingsResponse.self, - at: ExplorerAPI.getListings(projectId: Web3Modal.config.projectId) + at: ExplorerAPI.getListings( + projectId: Web3Modal.config.projectId, + metadata: Web3Modal.config.metadata + ) ) return response.listings.values.compactMap { $0 } diff --git a/Sources/Web3Modal/Networking/Explorer/ExplorerAPI.swift b/Sources/Web3Modal/Networking/Explorer/ExplorerAPI.swift index 1f37011b3..377046182 100644 --- a/Sources/Web3Modal/Networking/Explorer/ExplorerAPI.swift +++ b/Sources/Web3Modal/Networking/Explorer/ExplorerAPI.swift @@ -2,7 +2,7 @@ import Foundation import HTTPClient enum ExplorerAPI: HTTPService { - case getListings(projectId: String) + case getListings(projectId: String, metadata: AppMetadata) var path: String { switch self { @@ -22,7 +22,7 @@ enum ExplorerAPI: HTTPService { var queryParameters: [String: String]? { switch self { - case let .getListings(projectId): + case let .getListings(projectId, _): return [ "projectId": projectId, "page": "1", @@ -36,6 +36,21 @@ enum ExplorerAPI: HTTPService { } var additionalHeaderFields: [String: String]? { - nil + + switch self { + case let .getListings(_, metadata): + return [ + "User-Agent": ExplorerAPI.userAgent, + "referer": metadata.name + ] + } + } + + private static var protocolName: String { + "w3m-ios-1.0.0" + } + + static var userAgent: String { + "\(protocolName)/\(EnvironmentInfo.sdkName)/\(EnvironmentInfo.operatingSystem)" } } diff --git a/Sources/Web3Modal/UI/Common/AsyncImage.swift b/Sources/Web3Modal/UI/Common/AsyncImage.swift index 3ce9283c4..a7d733b25 100644 --- a/Sources/Web3Modal/UI/Common/AsyncImage.swift +++ b/Sources/Web3Modal/UI/Common/AsyncImage.swift @@ -10,7 +10,11 @@ struct AsyncImage: View where Content: View { init(_ url: URL?) { guard let url = url else { return } - URLSession.shared.dataTaskPublisher(for: url) + var request = URLRequest(url: url) + request.setValue(ExplorerAPI.userAgent, forHTTPHeaderField: "User-Agent") + request.setValue(Web3Modal.config.metadata.name, forHTTPHeaderField: "Referer") + + URLSession.shared.dataTaskPublisher(for: request) .map(\.data) .map { $0 as Data? } .replaceError(with: nil) diff --git a/Sources/Web3Modal/Web3Modal.swift b/Sources/Web3Modal/Web3Modal.swift index d9a150a79..b91feab34 100644 --- a/Sources/Web3Modal/Web3Modal.swift +++ b/Sources/Web3Modal/Web3Modal.swift @@ -32,6 +32,7 @@ public class Web3Modal { struct Config { let projectId: String + var metadata: AppMetadata var sessionParams: SessionParams } @@ -48,7 +49,11 @@ public class Web3Modal { sessionParams: SessionParams = .default ) { Pair.configure(metadata: metadata) - Web3Modal.config = Web3Modal.Config(projectId: projectId, sessionParams: sessionParams) + Web3Modal.config = Web3Modal.Config( + projectId: projectId, + metadata: metadata, + sessionParams: sessionParams + ) } public static func set(sessionParams: SessionParams) { diff --git a/Sources/Web3Modal/Web3ModalImports.swift b/Sources/Web3Modal/Web3ModalImports.swift index e0ddb7a79..cf703af54 100644 --- a/Sources/Web3Modal/Web3ModalImports.swift +++ b/Sources/Web3Modal/Web3ModalImports.swift @@ -1,4 +1,5 @@ #if !CocoaPods @_exported import WalletConnectSign @_exported import WalletConnectPairing +@_exported import WalletConnectRelay #endif diff --git a/Tests/Web3ModalTests/ExplorerAPITests.swift b/Tests/Web3ModalTests/ExplorerAPITests.swift new file mode 100644 index 000000000..f4db68e44 --- /dev/null +++ b/Tests/Web3ModalTests/ExplorerAPITests.swift @@ -0,0 +1,17 @@ +import TestingUtils +@testable import Web3Modal +import XCTest + +final class ExplorerAPITests: XCTestCase { + + func testCorrectUserAgent() throws { + + let request = ExplorerAPI + .getListings(projectId: "foo", metadata: .stub()) + .resolve(for: "www.google.com") + + XCTAssertEqual(request?.allHTTPHeaderFields?["Referer"], "Wallet Connect") + // Should look something like this: w3m-ios-1.0.0/swift-v1.6.8/iOS-16.1 + XCTAssertTrue(request?.allHTTPHeaderFields?["User-Agent"]?.starts(with: "w3m-ios-1.0.0/swift-v") ?? false) + } +}