-
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.
- Loading branch information
Showing
12 changed files
with
116 additions
and
60 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
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
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 image_url: ImageUrl? | ||
let notificationTypes: [NotificationType] | ||
|
||
var metadata: AppMetadata { | ||
return AppMetadata( | ||
name: name, | ||
description: | ||
description, | ||
url: homepage, | ||
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) | ||
} |
35 changes: 15 additions & 20 deletions
35
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,24 @@ | ||
|
||
import Foundation | ||
|
||
actor NotifyConfigProvider { | ||
enum Errors: Error { | ||
case invalidUrl | ||
} | ||
|
||
private var cache = [String: Set<NotificationType>]() | ||
private let projectId: String | ||
|
||
init(projectId: String) { | ||
self.projectId = projectId | ||
} | ||
|
||
func getSubscriptionScope(appDomain: String) async throws -> Set<NotificationType> { | ||
if let availableScope = cache[appDomain] { | ||
return availableScope | ||
} | ||
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 | ||
func resolveNotifyConfig(appDomain: String) async throws -> NotifyConfig { | ||
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) | ||
return response.data | ||
} | ||
} | ||
|
||
private extension NotifyConfigProvider { | ||
|
||
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) | ||
struct NotifyConfigResponse: Codable { | ||
let data: NotifyConfig | ||
} | ||
} |
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
10 changes: 0 additions & 10 deletions
10
Sources/WalletConnectNotify/Types/DataStructures/NotificationConfig.swift
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
Sources/WalletConnectNotify/Types/DataStructures/NotificationType.swift
This file was deleted.
Oops, something went wrong.