From 00dc3944770a124d6572ab9318cea388a47b8d1e Mon Sep 17 00:00:00 2001
From: Bartosz Rozwarski <bartus000@gmail.com>
Date: Wed, 12 Jun 2024 10:06:53 +0200
Subject: [PATCH 1/4] add verifySIWE function

---
 .../WalletConnectSign/Sign/SignClient.swift   | 22 ++++++++++++++-----
 .../Sign/SignClientFactory.swift              |  3 ++-
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/Sources/WalletConnectSign/Sign/SignClient.swift b/Sources/WalletConnectSign/Sign/SignClient.swift
index 5694955c3..d2f994268 100644
--- a/Sources/WalletConnectSign/Sign/SignClient.swift
+++ b/Sources/WalletConnectSign/Sign/SignClient.swift
@@ -193,6 +193,7 @@ public final class SignClient: SignClientProtocol {
     private let linkSessionRequestSubscriber: LinkSessionRequestSubscriber
     private let sessionResponderDispatcher: SessionResponderDispatcher
     private let linkSessionRequestResponseSubscriber: LinkSessionRequestResponseSubscriber
+    private let messageVerifier: MessageVerifier
 
     private var publishers = Set<AnyCancellable>()
 
@@ -231,7 +232,8 @@ public final class SignClient: SignClientProtocol {
          linkSessionRequestSubscriber: LinkSessionRequestSubscriber,
          sessionResponderDispatcher: SessionResponderDispatcher,
          linkSessionRequestResponseSubscriber: LinkSessionRequestResponseSubscriber,
-         authenticateTransportTypeSwitcher: AuthenticateTransportTypeSwitcher
+         authenticateTransportTypeSwitcher: AuthenticateTransportTypeSwitcher,
+         messageVerifier: MessageVerifier
     ) {
         self.logger = logger
         self.networkingClient = networkingClient
@@ -267,6 +269,7 @@ public final class SignClient: SignClientProtocol {
         self.sessionResponderDispatcher = sessionResponderDispatcher
         self.linkSessionRequestResponseSubscriber = linkSessionRequestResponseSubscriber
         self.authenticateTransportTypeSwitcher = authenticateTransportTypeSwitcher
+        self.messageVerifier = messageVerifier
 
         setUpConnectionObserving()
         setUpEnginesCallbacks()
@@ -374,11 +377,6 @@ public final class SignClient: SignClientProtocol {
         return try pendingRequestsProvider.getPendingRequests()
     }
 
-    public func formatAuthMessage(payload: AuthPayload, account: Account) throws -> String {
-        let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(authPayload: payload, account: account)
-        return try SIWEFromCacaoPayloadFormatter().formatMessage(from: cacaoPayload)
-    }
-
     public func buildSignedAuthObject(authPayload: AuthPayload, signature: CacaoSignature, account: Account) throws -> AuthObject {
         try CacaosBuilder.makeCacao(authPayload: authPayload, signature: signature, account: account)
     }
@@ -387,6 +385,17 @@ public final class SignClient: SignClientProtocol {
         try AuthPayloadBuilder.build(payload: payload, supportedEVMChains: supportedEVMChains, supportedMethods: supportedMethods)
     }
 
+    // MARK: - SIWE
+
+    public func formatAuthMessage(payload: AuthPayload, account: Account) throws -> String {
+        let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(authPayload: payload, account: account)
+        return try SIWEFromCacaoPayloadFormatter().formatMessage(from: cacaoPayload)
+    }
+
+    public func verifySIWE(signature: CacaoSignature, message: String, address: String, chainId: String) async throws {
+
+    }
+
     //-----------------------------------------------------------------------------------
 
     /// For a wallet to approve a session proposal.
@@ -590,3 +599,4 @@ public final class SignClient: SignClientProtocol {
         }.store(in: &publishers)
     }
 }
+
diff --git a/Sources/WalletConnectSign/Sign/SignClientFactory.swift b/Sources/WalletConnectSign/Sign/SignClientFactory.swift
index f1fa294ea..4acff725c 100644
--- a/Sources/WalletConnectSign/Sign/SignClientFactory.swift
+++ b/Sources/WalletConnectSign/Sign/SignClientFactory.swift
@@ -178,7 +178,8 @@ public struct SignClientFactory {
             linkSessionRequestSubscriber: linkSessionRequestSubscriber,
             sessionResponderDispatcher: sessionResponderDispatcher,
             linkSessionRequestResponseSubscriber: linkSessionRequestResponseSubscriber,
-            authenticateTransportTypeSwitcher: authenticateTransportTypeSwitcher
+            authenticateTransportTypeSwitcher: authenticateTransportTypeSwitcher,
+            messageVerifier: signatureVerifier
         )
         return client
     }

From b0bf7892844fac62207aa8dcd27cda6c72287645 Mon Sep 17 00:00:00 2001
From: Bartosz Rozwarski <bartus000@gmail.com>
Date: Wed, 12 Jun 2024 17:01:43 +0200
Subject: [PATCH 2/4] savepoint

---
 Sources/WalletConnectSign/Auth/Types/AuthPayload.swift | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Sources/WalletConnectSign/Auth/Types/AuthPayload.swift b/Sources/WalletConnectSign/Auth/Types/AuthPayload.swift
index 8edab5317..bca5ec3fa 100644
--- a/Sources/WalletConnectSign/Auth/Types/AuthPayload.swift
+++ b/Sources/WalletConnectSign/Auth/Types/AuthPayload.swift
@@ -43,7 +43,7 @@ public struct AuthPayload: Codable, Equatable {
     }
 
 
-    init(requestParams: AuthRequestParams, iat: String) {
+    public init(requestParams: AuthRequestParams, iat: String) {
         self.type = "eip4361"
         self.chains = requestParams.chains
         self.domain = requestParams.domain

From 8baa41899eff1fbd31edf2d615999ff14ffc4115 Mon Sep 17 00:00:00 2001
From: Bartosz Rozwarski <bartus000@gmail.com>
Date: Thu, 13 Jun 2024 09:05:01 +0200
Subject: [PATCH 3/4] Add siwe verification

---
 .../WalletConnectSign/Sign/SignClient.swift   |  4 +-
 .../Verifier/MessageVerifier.swift            | 39 ++++++++++++++++---
 2 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/Sources/WalletConnectSign/Sign/SignClient.swift b/Sources/WalletConnectSign/Sign/SignClient.swift
index d2f994268..135b14b6a 100644
--- a/Sources/WalletConnectSign/Sign/SignClient.swift
+++ b/Sources/WalletConnectSign/Sign/SignClient.swift
@@ -392,8 +392,8 @@ public final class SignClient: SignClientProtocol {
         return try SIWEFromCacaoPayloadFormatter().formatMessage(from: cacaoPayload)
     }
 
-    public func verifySIWE(signature: CacaoSignature, message: String, address: String, chainId: String) async throws {
-
+    public func verifySIWE(signature: Data, message: String, address: String, chainId: String) async throws {
+        try await messageVerifier.verify(signature: signature, message: message, address: address, chainId: chainId)
     }
 
     //-----------------------------------------------------------------------------------
diff --git a/Sources/WalletConnectSigner/Verifier/MessageVerifier.swift b/Sources/WalletConnectSigner/Verifier/MessageVerifier.swift
index 12b82b40e..f0120942c 100644
--- a/Sources/WalletConnectSigner/Verifier/MessageVerifier.swift
+++ b/Sources/WalletConnectSigner/Verifier/MessageVerifier.swift
@@ -15,8 +15,8 @@ public struct MessageVerifier {
     }
 
     public func verify(signature: CacaoSignature,
-        message: String,
-        account: Account
+                       message: String,
+                       account: Account
     ) async throws {
         try await self.verify(
             signature: signature,
@@ -27,9 +27,9 @@ public struct MessageVerifier {
     }
 
     public func verify(signature: CacaoSignature,
-        message: String,
-        address: String,
-        chainId: String
+                       message: String,
+                       address: String,
+                       chainId: String
     ) async throws {
 
         guard let messageData = message.data(using: .utf8) else {
@@ -54,4 +54,33 @@ public struct MessageVerifier {
             )
         }
     }
+
+    public func verify(signature: Data,
+                       message: String,
+                       address: String,
+                       chainId: String
+    ) async throws {
+
+        guard let messageData = message.data(using: .utf8) else {
+            throw Errors.utf8EncodingFailed
+        }
+
+        let prefixedMessage = messageData.prefixed
+
+        do {
+            try await eip191Verifier.verify(
+                signature: signature,
+                message: prefixedMessage,
+                address: address
+            )
+        } catch {
+            // If eip191 verification fails, try eip1271 verification
+            try await eip1271Verifier.verify(
+                signature: signature,
+                message: prefixedMessage,
+                address: address,
+                chainId: chainId
+            )
+        }
+    }
 }

From 997be80a6f86be4ba340ac8121acde24fc428520 Mon Sep 17 00:00:00 2001
From: Bartosz Rozwarski <bartus000@gmail.com>
Date: Thu, 13 Jun 2024 09:16:29 +0200
Subject: [PATCH 4/4] savepoint

---
 Sources/WalletConnectSign/Sign/SignClient.swift            | 2 +-
 Sources/WalletConnectSigner/Verifier/MessageVerifier.swift | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/Sources/WalletConnectSign/Sign/SignClient.swift b/Sources/WalletConnectSign/Sign/SignClient.swift
index 135b14b6a..35b7941ce 100644
--- a/Sources/WalletConnectSign/Sign/SignClient.swift
+++ b/Sources/WalletConnectSign/Sign/SignClient.swift
@@ -392,7 +392,7 @@ public final class SignClient: SignClientProtocol {
         return try SIWEFromCacaoPayloadFormatter().formatMessage(from: cacaoPayload)
     }
 
-    public func verifySIWE(signature: Data, message: String, address: String, chainId: String) async throws {
+    public func verifySIWE(signature: String, message: String, address: String, chainId: String) async throws {
         try await messageVerifier.verify(signature: signature, message: message, address: address, chainId: chainId)
     }
 
diff --git a/Sources/WalletConnectSigner/Verifier/MessageVerifier.swift b/Sources/WalletConnectSigner/Verifier/MessageVerifier.swift
index f0120942c..d1a9680a6 100644
--- a/Sources/WalletConnectSigner/Verifier/MessageVerifier.swift
+++ b/Sources/WalletConnectSigner/Verifier/MessageVerifier.swift
@@ -55,7 +55,7 @@ public struct MessageVerifier {
         }
     }
 
-    public func verify(signature: Data,
+    public func verify(signature: String,
                        message: String,
                        address: String,
                        chainId: String
@@ -64,19 +64,20 @@ public struct MessageVerifier {
         guard let messageData = message.data(using: .utf8) else {
             throw Errors.utf8EncodingFailed
         }
+        let signatureData = Data(hex: signature)
 
         let prefixedMessage = messageData.prefixed
 
         do {
             try await eip191Verifier.verify(
-                signature: signature,
+                signature: signatureData,
                 message: prefixedMessage,
                 address: address
             )
         } catch {
             // If eip191 verification fails, try eip1271 verification
             try await eip1271Verifier.verify(
-                signature: signature,
+                signature: signatureData,
                 message: prefixedMessage,
                 address: address,
                 chainId: chainId