-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1185 from WalletConnect/feature/notify-remove-config
[Notify] Config from explorer
- Loading branch information
Showing
17 changed files
with
163 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
Sources/WalletConnectNotify/Client/Wallet/NotifyConfig.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Foundation | ||
|
||
struct NotifyConfig: Codable { | ||
struct NotificationType: Codable { | ||
let id: String | ||
let name: String | ||
let description: String | ||
} | ||
struct ImageUrl: Codable { | ||
let sm: String? | ||
let md: String? | ||
let lg: String? | ||
} | ||
let id: String | ||
let name: String | ||
let homepage: String | ||
let description: String | ||
let dapp_url: String | ||
let image_url: ImageUrl? | ||
let notificationTypes: [NotificationType] | ||
|
||
var appDomain: String { | ||
return URL(string: dapp_url)?.host ?? dapp_url | ||
} | ||
|
||
var metadata: AppMetadata { | ||
return AppMetadata( | ||
name: name, | ||
description: | ||
description, | ||
url: appDomain, | ||
icons: [image_url?.sm, image_url?.md, image_url?.lg].compactMap { $0 } | ||
) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
Sources/WalletConnectNotify/Client/Wallet/NotifyConfigAPI.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Foundation | ||
|
||
enum NotifyConfigAPI: HTTPService { | ||
|
||
var path: String { | ||
return "/w3i/v1/notify-config" | ||
} | ||
|
||
var method: HTTPMethod { | ||
return .get | ||
} | ||
|
||
var body: Data? { | ||
return nil | ||
} | ||
|
||
var queryParameters: [String : String]? { | ||
switch self { | ||
case .notifyDApps(let projectId, let appDomain): | ||
return ["projectId": projectId, "appDomain": appDomain] | ||
} | ||
} | ||
|
||
var additionalHeaderFields: [String : String]? { | ||
return nil | ||
} | ||
|
||
var scheme: String { | ||
return "https" | ||
} | ||
|
||
case notifyDApps(projectId: String, appDomain: String) | ||
} |
55 changes: 37 additions & 18 deletions
55
Sources/WalletConnectNotify/Client/Wallet/NotifyConfigProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,48 @@ | ||
|
||
import Foundation | ||
|
||
actor NotifyConfigProvider { | ||
enum Errors: Error { | ||
case invalidUrl | ||
|
||
private let projectId: String | ||
|
||
private var cache: [String: NotifyConfig] = [:] | ||
|
||
init(projectId: String) { | ||
self.projectId = projectId | ||
} | ||
|
||
private var cache = [String: Set<NotificationType>]() | ||
func resolveNotifyConfig(appDomain: String) async -> NotifyConfig { | ||
if let config = cache[appDomain] { | ||
return config | ||
} | ||
|
||
func getSubscriptionScope(appDomain: String) async throws -> Set<NotificationType> { | ||
if let availableScope = cache[appDomain] { | ||
return availableScope | ||
do { | ||
let httpClient = HTTPNetworkClient(host: "explorer-api.walletconnect.com") | ||
let request = NotifyConfigAPI.notifyDApps(projectId: projectId, appDomain: appDomain) | ||
let response = try await httpClient.request(NotifyConfigResponse.self, at: request) | ||
let config = response.data | ||
cache[appDomain] = config | ||
return config | ||
} catch { | ||
return emptyConfig(appDomain: appDomain) | ||
} | ||
guard let notifyConfigUrl = URL(string: "https://\(appDomain)/.well-known/wc-notify-config.json") else { throw Errors.invalidUrl } | ||
let (data, _) = try await URLSession.shared.data(from: notifyConfigUrl) | ||
let config = try JSONDecoder().decode(NotificationConfig.self, from: data) | ||
let availableScope = Set(config.types) | ||
cache[appDomain] = availableScope | ||
return availableScope | ||
} | ||
} | ||
|
||
private extension NotifyConfigProvider { | ||
|
||
struct NotifyConfigResponse: Codable { | ||
let data: NotifyConfig | ||
} | ||
|
||
func getMetadata(appDomain: String) async throws -> AppMetadata { | ||
guard let notifyConfigUrl = URL(string: "https://\(appDomain)/.well-known/wc-notify-config.json") else { throw Errors.invalidUrl } | ||
let (data, _) = try await URLSession.shared.data(from: notifyConfigUrl) | ||
let config = try JSONDecoder().decode(NotificationConfig.self, from: data) | ||
return AppMetadata(name: config.name, description: config.description, url: appDomain, icons: config.icons) | ||
func emptyConfig(appDomain: String) -> NotifyConfig { | ||
return NotifyConfig( | ||
id: UUID().uuidString, | ||
name: appDomain, | ||
homepage: "https://\(appDomain)", | ||
description: "", | ||
dapp_url: "https://\(appDomain)", | ||
image_url: nil, | ||
notificationTypes: [] | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.