Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Notify] Identity refactor #1087

Merged
merged 8 commits into from
Sep 12, 2023
4 changes: 2 additions & 2 deletions Example/IntegrationTests/Chat/ChatTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ final class ChatTests: XCTestCase {
invitee1 = makeClient(prefix: "🦖 Invitee", account: inviteeAccount)
inviter1 = makeClient(prefix: "🍄 Inviter", account: inviterAccount)

try await invitee1.register(account: inviteeAccount) { message in
try await invitee1.register(account: inviteeAccount, domain: "") { message in
return self.sign(message, privateKey: self.privateKey1)
}
try await inviter1.register(account: inviterAccount) { message in
try await inviter1.register(account: inviterAccount, domain: "") { message in
return self.sign(message, privateKey: self.privateKey2)
}
}
Expand Down
14 changes: 7 additions & 7 deletions Example/IntegrationTests/Push/NotifyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ final class NotifyTests: XCTestCase {
}
}.store(in: &publishers)

try! await walletNotifyClientA.register(account: account, onSign: sign)
try! await walletNotifyClientA.register(account: account, domain: gmDappDomain, isLimited: false, onSign: sign)
try! await walletNotifyClientA.subscribe(appDomain: gmDappDomain, account: account)

wait(for: [expectation], timeout: InputConfig.defaultTimeout)
Expand All @@ -122,12 +122,12 @@ final class NotifyTests: XCTestCase {
}
}.store(in: &publishers)

try! await walletNotifyClientA.register(account: account, onSign: sign)
try! await walletNotifyClientA.register(account: account, domain: gmDappDomain, isLimited: false, onSign: sign)
try! await walletNotifyClientA.subscribe(appDomain: gmDappDomain, account: account)

sleep(1)

try! await clientB.register(account: account, onSign: sign)
try! await clientB.register(account: account, domain: gmDappDomain, isLimited: false, onSign: sign)

wait(for: [expectation], timeout: InputConfig.defaultTimeout)
}
Expand All @@ -145,8 +145,8 @@ final class NotifyTests: XCTestCase {
}
}.store(in: &publishers)

try! await walletNotifyClientA.register(account: account, onSign: sign)
try! await clientB.register(account: account, onSign: sign)
try! await walletNotifyClientA.register(account: account, domain: gmDappDomain, isLimited: false, onSign: sign)
try! await clientB.register(account: account, domain: gmDappDomain, isLimited: false, onSign: sign)

sleep(1)

Expand All @@ -159,7 +159,7 @@ final class NotifyTests: XCTestCase {
let expectation = expectation(description: "expects to create and update notify subscription")
let updateScope: Set<String> = ["alerts"]

try! await walletNotifyClientA.register(account: account, onSign: sign)
try! await walletNotifyClientA.register(account: account, domain: gmDappDomain, isLimited: false, onSign: sign)
try! await walletNotifyClientA.subscribe(appDomain: gmDappDomain, account: account)

walletNotifyClientA.newSubscriptionPublisher
Expand Down Expand Up @@ -190,7 +190,7 @@ final class NotifyTests: XCTestCase {

let metadata = AppMetadata(name: "GM Dapp", description: "", url: gmDappDomain, icons: [])

try! await walletNotifyClientA.register(account: account, onSign: sign)
try! await walletNotifyClientA.register(account: account, domain: gmDappDomain, isLimited: false, onSign: sign)
try! await walletNotifyClientA.subscribe(appDomain: gmDappDomain, account: account)

walletNotifyClientA.newSubscriptionPublisher
Expand Down
10 changes: 8 additions & 2 deletions Sources/Chat/ChatClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,16 @@ public class ChatClient {
@discardableResult
public func register(account: Account,
isPrivate: Bool = false,
domain: String,
onSign: @escaping SigningCallback
) async throws -> String {
let publicKey = try await identityClient.register(account: account, onSign: onSign)

let publicKey = try await identityClient.register(
account: account,
domain: domain,
statement: "statement",
resources: ["https://keys.walletconnect.com"],
onSign: onSign
)
if !syncRegisterService.isRegistered(account: account) {
try await chatStorage.initializeHistory(account: account)
try await syncRegisterService.register(account: account, onSign: onSign)
Expand Down
4 changes: 2 additions & 2 deletions Sources/WalletConnectIdentity/IdentityClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public final class IdentityClient {
self.logger = logger
}

public func register(account: Account, onSign: SigningCallback) async throws -> String {
let pubKey = try await identityService.registerIdentity(account: account, onSign: onSign)
public func register(account: Account, domain: String, statement: String, resources: [String], onSign: SigningCallback) async throws -> String {
let pubKey = try await identityService.registerIdentity(account: account, domain: domain, statement: statement, resources: resources, onSign: onSign)
logger.debug("Did register an account: \(account)")
return pubKey
}
Expand Down
28 changes: 16 additions & 12 deletions Sources/WalletConnectIdentity/IdentityService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ actor IdentityService {
}

func registerIdentity(account: Account,
domain: String,
statement: String,
resources: [String],
onSign: SigningCallback
) async throws -> String {

Expand All @@ -34,7 +37,8 @@ actor IdentityService {
}

let identityKey = SigningPrivateKey()
let cacao = try await makeCacao(DIDKey: identityKey.publicKey.did, account: account, onSign: onSign)
let audience = identityKey.publicKey.did
let cacao = try await makeCacao(account: account, domain: domain, statement: statement, resources: resources, audience: audience, onSign: onSign)
try await networkService.registerIdentity(cacao: cacao)

return try storage.saveIdentityKey(identityKey, for: account).publicKey.hexRepresentation
Expand Down Expand Up @@ -85,22 +89,26 @@ actor IdentityService {

private extension IdentityService {

func makeCacao(
DIDKey: String,
account: Account,
func makeCacao(account: Account,
domain: String,
statement: String,
resources: [String],
audience: String,
onSign: SigningCallback
) async throws -> Cacao {

let cacaoHeader = CacaoHeader(t: "eip4361")
let cacaoPayload = CacaoPayload(
iss: account.did,
domain: keyserverURL.host!,
aud: getAudience(),
domain: domain,
aud: audience,
version: getVersion(),
nonce: getNonce(),
iat: iatProvader.iat,
nbf: nil, exp: nil, statement: "statement", requestId: nil,
resources: [DIDKey]
nbf: nil, exp: nil,
statement: statement,
requestId: nil,
resources: resources
)

let result = await onSign(try messageFormatter.formatMessage(from: cacaoPayload))
Expand Down Expand Up @@ -132,8 +140,4 @@ private extension IdentityService {
private func getVersion() -> String {
return "1"
}

private func getAudience() -> String {
return keyserverURL.absoluteString
}
}
12 changes: 6 additions & 6 deletions Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class NotifyClient {
public let logger: ConsoleLogging

private let pushClient: PushClient
private let identityClient: IdentityClient
private let identityService: NotifyIdentityService
private let notifyStorage: NotifyStorage
private let notifyMessageSubscriber: NotifyMessageSubscriber
private let resubscribeService: NotifyResubscribeService
Expand All @@ -52,7 +52,7 @@ public class NotifyClient {

init(logger: ConsoleLogging,
kms: KeyManagementServiceProtocol,
identityClient: IdentityClient,
identityService: NotifyIdentityService,
pushClient: PushClient,
notifyMessageSubscriber: NotifyMessageSubscriber,
notifyStorage: NotifyStorage,
Expand All @@ -70,7 +70,7 @@ public class NotifyClient {
) {
self.logger = logger
self.pushClient = pushClient
self.identityClient = identityClient
self.identityService = identityService
self.notifyMessageSubscriber = notifyMessageSubscriber
self.notifyStorage = notifyStorage
self.deleteNotifySubscriptionRequester = deleteNotifySubscriptionRequester
Expand All @@ -86,8 +86,8 @@ public class NotifyClient {
self.notifySubscriptionsChangedRequestSubscriber = notifySubscriptionsChangedRequestSubscriber
}

public func register(account: Account, onSign: @escaping SigningCallback) async throws {
_ = try await identityClient.register(account: account, onSign: onSign)
public func register(account: Account, domain: String, isLimited: Bool, onSign: @escaping SigningCallback) async throws {
flypaper0 marked this conversation as resolved.
Show resolved Hide resolved
try await identityService.register(account: account, domain: domain, isLimited: isLimited, onSign: onSign)
try await notifyWatchSubscriptionsRequester.watchSubscriptions(account: account)
}

Expand Down Expand Up @@ -124,7 +124,7 @@ public class NotifyClient {
}

public func isIdentityRegistered(account: Account) -> Bool {
return identityClient.isIdentityRegistered(account: account)
return identityService.isIdentityRegistered(account: account)
}

public func messagesPublisher(topic: String) -> AnyPublisher<[NotifyMessageRecord], Never> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ public struct NotifyClientFactory {
let notifyWatchSubscriptionsResponseSubscriber = NotifyWatchSubscriptionsResponseSubscriber(networkingInteractor: networkInteractor, kms: kms, logger: logger, notifyStorage: notifyStorage, notifySubscriptionsBuilder: notifySubscriptionsBuilder)
let notifySubscriptionsChangedRequestSubscriber = NotifySubscriptionsChangedRequestSubscriber(keyserver: keyserverURL, networkingInteractor: networkInteractor, kms: kms, identityClient: identityClient, logger: logger, notifyStorage: notifyStorage, notifySubscriptionsBuilder: notifySubscriptionsBuilder)

let identityService = NotifyIdentityService(keyserverURL: keyserverURL, identityClient: identityClient, logger: logger)

return NotifyClient(
logger: logger,
kms: kms,
identityClient: identityClient,
identityService: identityService,
pushClient: pushClient,
notifyMessageSubscriber: notifyMessageSubscriber,
notifyStorage: notifyStorage,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Foundation

final class NotifyIdentityService {

private let keyserverURL: URL
private let identityClient: IdentityClient
private let logger: ConsoleLogging

init(keyserverURL: URL, identityClient: IdentityClient, logger: ConsoleLogging) {
self.keyserverURL = keyserverURL
self.identityClient = identityClient
self.logger = logger
}

public func register(account: Account, domain: String, isLimited: Bool, onSign: @escaping SigningCallback) async throws {
let statement = makeStatement(isLimited: isLimited, domain: domain, keyserverHost: keyserverURL.host!)
let pubKey = try await identityClient.register(account: account,
domain: domain,
statement: statement,
resources: [keyserverURL.absoluteString],
onSign: onSign)
logger.debug("Did register an account: \(account)")
}

func isIdentityRegistered(account: Account) -> Bool {
return identityClient.isIdentityRegistered(account: account)
}
}

private extension NotifyIdentityService {

func makeStatement(isLimited: Bool, domain: String, keyserverHost: String) -> String {
switch isLimited {
case true:
return "I further authorize this DAPP to send and receive messages on my behalf for this domain and manage my identity at \(keyserverHost)."
case false:
return "I further authorize this WALLET to send and receive messages on my behalf for ALL domains and manage my identity at \(keyserverHost)."
}
}
}
3 changes: 2 additions & 1 deletion Sources/Web3Inbox/ChatClient/ChatClientProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class ChatClientProxy {

case .register:
let params = try parse(RegisterRequest.self, params: request.params)
try await client.register(account: params.account, onSign: onSign)
try await client.register(account: params.account, domain: params.domain, onSign: onSign)
try await respond(request: request)

case .resolve:
Expand Down Expand Up @@ -81,6 +81,7 @@ private extension ChatClientProxy {

struct RegisterRequest: Codable {
let account: Account
let domain: String
}

struct ResolveRequest: Codable {
Expand Down
4 changes: 3 additions & 1 deletion Sources/Web3Inbox/NotifyClientProxy/NotifyClientProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class NotifyClientProxy {
try await respond(request: request)
case .register:
let params = try parse(RegisterRequest.self, params: request.params)
try await client.register(account: params.account, onSign: onSign)
try await client.register(account: params.account, domain: params.domain, isLimited: params.isLimited, onSign: onSign)
try await respond(request: request)
}
}
Expand Down Expand Up @@ -91,6 +91,8 @@ private extension NotifyClientProxy {

struct RegisterRequest: Codable {
let account: Account
let domain: String
let isLimited: Bool
}

func parse<Request: Codable>(_ type: Request.Type, params: AnyCodable?) throws -> Request {
Expand Down