diff --git a/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyDelete/DeleteNotifySubscriptionRequester.swift b/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyDelete/DeleteNotifySubscriptionRequester.swift index e23ae9d61..c52fd40cb 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyDelete/DeleteNotifySubscriptionRequester.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyDelete/DeleteNotifySubscriptionRequester.swift @@ -42,7 +42,7 @@ class DeleteNotifySubscriptionRequester { let wrapper = try createJWTWrapper( dappPubKey: DIDKey(rawData: dappAuthenticationKey), reason: NotifyDeleteParams.userDisconnected.message, - app: subscription.metadata.url, + app: DIDWeb(host: subscription.metadata.url), account: subscription.account ) @@ -66,7 +66,7 @@ class DeleteNotifySubscriptionRequester { private extension DeleteNotifySubscriptionRequester { - func createJWTWrapper(dappPubKey: DIDKey, reason: String, app: String, account: Account) throws -> NotifyDeletePayload.Wrapper { + func createJWTWrapper(dappPubKey: DIDKey, reason: String, app: DIDWeb, account: Account) throws -> NotifyDeletePayload.Wrapper { let jwtPayload = NotifyDeletePayload(keyserver: keyserver, dappPubKey: dappPubKey, reason: reason, app: app) return try identityClient.signAndCreateWrapper( payload: jwtPayload, diff --git a/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyUpdate/NotifyUpdateRequester.swift b/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyUpdate/NotifyUpdateRequester.swift index 18876c53e..7ddb67379 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyUpdate/NotifyUpdateRequester.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyUpdate/NotifyUpdateRequester.swift @@ -45,7 +45,7 @@ class NotifyUpdateRequester: NotifyUpdateRequesting { let request = try createJWTRequest( dappPubKey: DIDKey(rawData: dappAuthenticationKey), subscriptionAccount: subscription.account, - dappUrl: subscription.metadata.url, scope: scope + appDomain: subscription.metadata.url, scope: scope ) let protocolMethod = NotifyUpdateProtocolMethod() @@ -53,10 +53,11 @@ class NotifyUpdateRequester: NotifyUpdateRequesting { try await networkingInteractor.request(request, topic: topic, protocolMethod: protocolMethod) } - private func createJWTRequest(dappPubKey: DIDKey, subscriptionAccount: Account, dappUrl: String, scope: Set) throws -> RPCRequest { + private func createJWTRequest(dappPubKey: DIDKey, subscriptionAccount: Account, appDomain: String, scope: Set) throws -> RPCRequest { let protocolMethod = NotifyUpdateProtocolMethod().method let scopeClaim = scope.joined(separator: " ") - let jwtPayload = NotifyUpdatePayload(dappPubKey: dappPubKey, keyserver: keyserverURL, subscriptionAccount: subscriptionAccount, dappUrl: dappUrl, scope: scopeClaim) + let app = DIDWeb(host: appDomain) + let jwtPayload = NotifyUpdatePayload(dappPubKey: dappPubKey, keyserver: keyserverURL, subscriptionAccount: subscriptionAccount, app: app, scope: scopeClaim) let wrapper = try identityClient.signAndCreateWrapper( payload: jwtPayload, account: subscriptionAccount diff --git a/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyUpdate/NotifyUpdateResponseSubscriber.swift b/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyUpdate/NotifyUpdateResponseSubscriber.swift index 549ede05d..a6f60cab6 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyUpdate/NotifyUpdateResponseSubscriber.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyUpdate/NotifyUpdateResponseSubscriber.swift @@ -43,7 +43,7 @@ private extension NotifyUpdateResponseSubscriber { let (requestPayload, requestClaims) = try NotifyUpdatePayload.decodeAndVerify(from: payload.request) let (_, _) = try NotifyUpdateResponsePayload.decodeAndVerify(from: payload.response) - let scope = try await buildScope(selected: requestPayload.scope, dappUrl: requestPayload.dappUrl) + let scope = try await buildScope(selected: requestPayload.scope, appDomain: requestPayload.app.host) guard let oldSubscription = notifyStorage.getSubscription(topic: subscriptionTopic) else { logger.debug("NotifyUpdateResponseSubscriber Subscription does not exist") @@ -56,9 +56,9 @@ private extension NotifyUpdateResponseSubscriber { } } - func buildScope(selected: String, dappUrl: String) async throws -> [String: ScopeValue] { + func buildScope(selected: String, appDomain: String) async throws -> [String: ScopeValue] { let selectedScope = selected.components(separatedBy: " ") - let availableScope = try await nofityConfigProvider.getSubscriptionScope(appDomain: dappUrl) + let availableScope = try await nofityConfigProvider.getSubscriptionScope(appDomain: appDomain) return availableScope.reduce(into: [:]) { $0[$1.name] = ScopeValue(description: $1.description, enabled: selectedScope.contains($1.name)) } diff --git a/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_pushSubscribe/NotifySubscribeRequester.swift b/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_pushSubscribe/NotifySubscribeRequester.swift index 932c71f90..6601d77b7 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_pushSubscribe/NotifySubscribeRequester.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_pushSubscribe/NotifySubscribeRequester.swift @@ -60,7 +60,7 @@ class NotifySubscribeRequester { let subscriptionAuthWrapper = try await createJWTWrapper( dappPubKey: DIDKey(did: peerPublicKey.did), subscriptionAccount: account, - dappUrl: appDomain + appDomain: appDomain ) let request = RPCRequest(method: protocolMethod.method, params: subscriptionAuthWrapper) @@ -79,10 +79,11 @@ class NotifySubscribeRequester { return keys } - private func createJWTWrapper(dappPubKey: DIDKey, subscriptionAccount: Account, dappUrl: String) async throws -> NotifySubscriptionPayload.Wrapper { - let types = try await notifyConfigProvider.getSubscriptionScope(appDomain: dappUrl) + private func createJWTWrapper(dappPubKey: DIDKey, subscriptionAccount: Account, appDomain: String) async throws -> NotifySubscriptionPayload.Wrapper { + let types = try await notifyConfigProvider.getSubscriptionScope(appDomain: appDomain) let scope = types.map{$0.name}.joined(separator: " ") - let jwtPayload = NotifySubscriptionPayload(dappPubKey: dappPubKey, keyserver: keyserverURL, subscriptionAccount: subscriptionAccount, dappUrl: dappUrl, scope: scope) + let app = DIDWeb(host: appDomain) + let jwtPayload = NotifySubscriptionPayload(dappPubKey: dappPubKey, keyserver: keyserverURL, subscriptionAccount: subscriptionAccount, app: app, scope: scope) return try identityClient.signAndCreateWrapper( payload: jwtPayload, account: subscriptionAccount diff --git a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyDeletePayload.swift b/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyDeletePayload.swift index 62aa74204..454b79fa5 100644 --- a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyDeletePayload.swift +++ b/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyDeletePayload.swift @@ -41,13 +41,13 @@ struct NotifyDeletePayload: JWTClaimsCodable { let keyserver: URL let dappPubKey: DIDKey let reason: String - let app: String + let app: DIDWeb init( keyserver: URL, dappPubKey: DIDKey, reason: String, - app: String + app: DIDWeb ) { self.keyserver = keyserver self.dappPubKey = dappPubKey @@ -59,7 +59,7 @@ struct NotifyDeletePayload: JWTClaimsCodable { self.keyserver = try claims.ksu.asURL() self.dappPubKey = try DIDKey(did: claims.aud) self.reason = claims.sub - self.app = claims.app + self.app = try DIDWeb(did: claims.app) } func encode(iss: String) throws -> Claims { @@ -71,7 +71,7 @@ struct NotifyDeletePayload: JWTClaimsCodable { iss: iss, aud: dappPubKey.did(variant: .ED25519), sub: reason, - app: app + app: app.did ) } } diff --git a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyDeleteResponsePayload.swift b/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyDeleteResponsePayload.swift index 83be1586c..ed5d897a3 100644 --- a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyDeleteResponsePayload.swift +++ b/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyDeleteResponsePayload.swift @@ -38,12 +38,12 @@ struct NotifyDeleteResponsePayload: JWTClaimsCodable { let selfPubKey: DIDKey let subscriptionHash: String - let app: String + let app: DIDWeb init(claims: Claims) throws { self.selfPubKey = try DIDKey(did: claims.aud) self.subscriptionHash = claims.sub - self.app = claims.app + self.app = try DIDWeb(did: claims.app) } func encode(iss: String) throws -> Claims { @@ -54,7 +54,7 @@ struct NotifyDeleteResponsePayload: JWTClaimsCodable { iss: iss, aud: selfPubKey.did(variant: .ED25519), sub: subscriptionHash, - app: app + app: app.did ) } } diff --git a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyMessagePayload.swift b/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyMessagePayload.swift index 8d220a5c0..916757b18 100644 --- a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyMessagePayload.swift +++ b/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyMessagePayload.swift @@ -41,14 +41,14 @@ struct NotifyMessagePayload: JWTClaimsCodable { let dappAuthenticationKey: DIDKey let account: Account let subscriptionId: String - let app: String + let app: DIDWeb let message: NotifyMessage init(claims: Claims) throws { self.dappAuthenticationKey = try DIDKey(did: claims.iss) self.account = try DIDPKH(did: claims.aud).account self.subscriptionId = claims.sub - self.app = claims.app + self.app = try DIDWeb(did: claims.app) self.message = claims.msg } @@ -60,9 +60,9 @@ struct NotifyMessagePayload: JWTClaimsCodable { iss: dappAuthenticationKey.multibase(variant: .ED25519), aud: account.did, sub: subscriptionId, - app: app, + app: app.did, msg: message ) } -} \ No newline at end of file +} diff --git a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyMessageReceiptPayload.swift b/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyMessageReceiptPayload.swift index c3bdf466c..f31fa1f5a 100644 --- a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyMessageReceiptPayload.swift +++ b/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyMessageReceiptPayload.swift @@ -41,13 +41,13 @@ struct NotifyMessageReceiptPayload: JWTClaimsCodable { let keyserver: URL let dappPubKey: DIDKey let messageHash: String - let app: String + let app: DIDWeb init( keyserver: URL, dappPubKey: DIDKey, messageHash: String, - app: String + app: DIDWeb ) { self.keyserver = keyserver self.dappPubKey = dappPubKey @@ -59,7 +59,7 @@ struct NotifyMessageReceiptPayload: JWTClaimsCodable { self.keyserver = try claims.ksu.asURL() self.dappPubKey = try DIDKey(did: claims.aud) self.messageHash = claims.sub - self.app = claims.app + self.app = try DIDWeb(did: claims.app) } func encode(iss: String) throws -> Claims { @@ -71,7 +71,7 @@ struct NotifyMessageReceiptPayload: JWTClaimsCodable { iss: iss, aud: dappPubKey.did(variant: .ED25519), sub: messageHash, - app: app + app: app.did ) } } diff --git a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifySubscriptionPayload.swift b/Sources/WalletConnectNotify/Types/JWTPayloads/NotifySubscriptionPayload.swift index 847635da0..03e1d065a 100644 --- a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifySubscriptionPayload.swift +++ b/Sources/WalletConnectNotify/Types/JWTPayloads/NotifySubscriptionPayload.swift @@ -43,14 +43,14 @@ struct NotifySubscriptionPayload: JWTClaimsCodable { let dappPubKey: DIDKey let keyserver: URL let subscriptionAccount: Account - let dappUrl: String + let app: DIDWeb let scope: String - init(dappPubKey: DIDKey, keyserver: URL, subscriptionAccount: Account, dappUrl: String, scope: String) { + init(dappPubKey: DIDKey, keyserver: URL, subscriptionAccount: Account, app: DIDWeb, scope: String) { self.dappPubKey = dappPubKey self.keyserver = keyserver self.subscriptionAccount = subscriptionAccount - self.dappUrl = dappUrl + self.app = app self.scope = scope } @@ -58,7 +58,7 @@ struct NotifySubscriptionPayload: JWTClaimsCodable { self.dappPubKey = try DIDKey(did: claims.aud) self.keyserver = try claims.ksu.asURL() self.subscriptionAccount = try Account(DIDPKHString: claims.sub) - self.dappUrl = claims.app + self.app = try DIDWeb(did: claims.app) self.scope = claims.scp } @@ -72,7 +72,7 @@ struct NotifySubscriptionPayload: JWTClaimsCodable { aud: dappPubKey.did(variant: .ED25519), sub: subscriptionAccount.did, scp: scope, - app: dappUrl + app: app.did ) } } diff --git a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyUpdatePayload.swift b/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyUpdatePayload.swift index ef88ebc87..c3195c7d6 100644 --- a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyUpdatePayload.swift +++ b/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyUpdatePayload.swift @@ -43,14 +43,14 @@ struct NotifyUpdatePayload: JWTClaimsCodable { let dappPubKey: DIDKey let keyserver: URL let subscriptionAccount: Account - let dappUrl: String + let app: DIDWeb let scope: String - init(dappPubKey: DIDKey, keyserver: URL, subscriptionAccount: Account, dappUrl: String, scope: String) { + init(dappPubKey: DIDKey, keyserver: URL, subscriptionAccount: Account, app: DIDWeb, scope: String) { self.dappPubKey = dappPubKey self.keyserver = keyserver self.subscriptionAccount = subscriptionAccount - self.dappUrl = dappUrl + self.app = app self.scope = scope } @@ -58,7 +58,7 @@ struct NotifyUpdatePayload: JWTClaimsCodable { self.dappPubKey = try DIDKey(did: claims.aud) self.keyserver = try claims.ksu.asURL() self.subscriptionAccount = try Account(DIDPKHString: claims.sub) - self.dappUrl = claims.app + self.app = try DIDWeb(did: claims.app) self.scope = claims.scp } @@ -72,7 +72,7 @@ struct NotifyUpdatePayload: JWTClaimsCodable { aud: dappPubKey.did(variant: .ED25519), sub: subscriptionAccount.did, scp: scope, - app: dappUrl + app: app.did ) } } diff --git a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyUpdateResponsePayload.swift b/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyUpdateResponsePayload.swift index 2153c7f9b..08accba0b 100644 --- a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyUpdateResponsePayload.swift +++ b/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyUpdateResponsePayload.swift @@ -38,12 +38,12 @@ struct NotifyUpdateResponsePayload: JWTClaimsCodable { let selfPubKey: DIDKey let subscriptionHash: String - let app: String + let app: DIDWeb init(claims: Claims) throws { self.selfPubKey = try DIDKey(did: claims.aud) self.subscriptionHash = claims.sub - self.app = claims.app + self.app = try DIDWeb(did: claims.app) } func encode(iss: String) throws -> Claims { @@ -54,7 +54,7 @@ struct NotifyUpdateResponsePayload: JWTClaimsCodable { iss: iss, aud: selfPubKey.did(variant: .ED25519), sub: subscriptionHash, - app: app + app: app.did ) } } diff --git a/Sources/WalletConnectUtils/DID/DIDWeb.swift b/Sources/WalletConnectUtils/DID/DIDWeb.swift new file mode 100644 index 000000000..61d46ab9b --- /dev/null +++ b/Sources/WalletConnectUtils/DID/DIDWeb.swift @@ -0,0 +1,32 @@ +import Foundation + +public struct DIDWeb { + + public let host: String + + public init(url: URL) throws { + guard let host = url.host else { throw Errors.invalidUrl } + self.host = host + } + + public init(did: String) throws { + guard let host = did.components(separatedBy: ":").last else { throw Errors.invalidDid } + self.host = host + } + + public init(host: String) { + self.host = host + } + + public var did: String { + return "did:web:\(host)" + } +} + +extension DIDWeb { + + enum Errors: Error { + case invalidUrl + case invalidDid + } +}