diff --git a/Example/IntegrationTests/Push/NotifyTests.swift b/Example/IntegrationTests/Push/NotifyTests.swift index 98511240e..1b63c3d8f 100644 --- a/Example/IntegrationTests/Push/NotifyTests.swift +++ b/Example/IntegrationTests/Push/NotifyTests.swift @@ -258,8 +258,17 @@ final class NotifyTests: XCTestCase { private extension NotifyTests { - func sign(_ message: String) -> SigningResult { + func sign(_ message: String) -> CacaoSignature { let signer = MessageSignerFactory(signerFactory: DefaultSignerFactory()).create(projectId: InputConfig.projectId) - return .signed(try! signer.sign(message: message, privateKey: privateKey, type: .eip191)) + return try! signer.sign(message: message, privateKey: privateKey, type: .eip191) + } +} + +private extension NotifyClient { + + func register(account: Account, domain: String, isLimited: Bool = false, onSign: @escaping (String) -> CacaoSignature) async throws { + let params = try await prepareRegistration(account: account, domain: domain) + let signature = onSign(params.message) + try await register(params: params, signature: signature) } } diff --git a/Sources/Chat/ChatClient.swift b/Sources/Chat/ChatClient.swift index 13b66378b..80199e507 100644 --- a/Sources/Chat/ChatClient.swift +++ b/Sources/Chat/ChatClient.swift @@ -92,25 +92,34 @@ public class ChatClient { domain: String, onSign: @escaping SigningCallback ) async throws -> String { - let publicKey = try await identityClient.register( + + let params = try await identityClient.prepareRegistration( account: account, domain: domain, statement: "statement", - resources: ["https://keys.walletconnect.com"], - onSign: onSign + resources: ["https://keys.walletconnect.com"] ) - if !syncRegisterService.isRegistered(account: account) { - try await chatStorage.initializeHistory(account: account) - try await syncRegisterService.register(account: account, onSign: onSign) - } - guard !isPrivate else { - return publicKey - } + switch await onSign(params.message) { + case .signed(let signature): + let publicKey = try await identityClient.register(params: params, signature: signature) + + if !syncRegisterService.isRegistered(account: account) { + try await chatStorage.initializeHistory(account: account) + try await syncRegisterService.register(account: account, onSign: onSign) + } - try await goPublic(account: account) + guard !isPrivate else { + return publicKey + } - return publicKey + try await goPublic(account: account) + + return publicKey + + case .rejected: + fatalError("Not implemented") + } } /// Unregisters a blockchain account with previously registered identity key diff --git a/Sources/WalletConnectIdentity/IdentityClient.swift b/Sources/WalletConnectIdentity/IdentityClient.swift index d7a6e69a5..8958b5112 100644 --- a/Sources/WalletConnectIdentity/IdentityClient.swift +++ b/Sources/WalletConnectIdentity/IdentityClient.swift @@ -32,10 +32,12 @@ public final class IdentityClient { return registration } - public func register(params: IdentityRegistrationParams, signature: CacaoSignature) async throws { + @discardableResult + public func register(params: IdentityRegistrationParams, signature: CacaoSignature) async throws -> String { let account = try params.account - try await identityService.registerIdentity(params: params, signature: signature) + let pubKey = try await identityService.registerIdentity(params: params, signature: signature) logger.debug("Did register an account: \(account)") + return pubKey } public func goPublic(account: Account) async throws -> AgreementPublicKey { diff --git a/Sources/WalletConnectIdentity/IdentityService.swift b/Sources/WalletConnectIdentity/IdentityService.swift index b2e85834b..96cd34215 100644 --- a/Sources/WalletConnectIdentity/IdentityService.swift +++ b/Sources/WalletConnectIdentity/IdentityService.swift @@ -51,11 +51,11 @@ actor IdentityService { } // TODO: Verifications - func registerIdentity(params: IdentityRegistrationParams, signature: CacaoSignature) async throws { + func registerIdentity(params: IdentityRegistrationParams, signature: CacaoSignature) async throws -> String { let account = try params.account if let identityKey = try? storage.getIdentityKey(for: account) { - return + return identityKey.publicKey.hexRepresentation } let cacaoHeader = CacaoHeader(t: "eip4361") @@ -64,6 +64,7 @@ actor IdentityService { try await networkService.registerIdentity(cacao: cacao) try storage.saveIdentityKey(params.privateIdentityKey, for: account) + return params.privateIdentityKey.publicKey.hexRepresentation } func registerInvite(account: Account) async throws -> AgreementPublicKey {