From 6ed2b6813ddbc53b77b3dda714bf402997ba892e Mon Sep 17 00:00:00 2001 From: skibitsky Date: Tue, 14 May 2024 13:20:29 +0300 Subject: [PATCH 1/3] Rejection tags --- WalletConnectSharp.Sign/Engine.cs | 2 +- .../Internals/EngineHandler.cs | 2 +- .../SessionProposeResponseAutoReject.cs | 25 +++++++++++++++++++ .../Methods/SessionProposeResponseReject.cs | 25 +++++++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 WalletConnectSharp.Sign/Models/Engine/Methods/SessionProposeResponseAutoReject.cs create mode 100644 WalletConnectSharp.Sign/Models/Engine/Methods/SessionProposeResponseReject.cs diff --git a/WalletConnectSharp.Sign/Engine.cs b/WalletConnectSharp.Sign/Engine.cs index 01fc48f..7b0c77c 100644 --- a/WalletConnectSharp.Sign/Engine.cs +++ b/WalletConnectSharp.Sign/Engine.cs @@ -656,7 +656,7 @@ public async Task Reject(RejectParams @params) if (!string.IsNullOrWhiteSpace(pairingTopic)) { - await MessageHandler.SendError(id, pairingTopic, reason); + await MessageHandler.SendError(id, pairingTopic, reason); await this.Client.Proposal.Delete(id, Error.FromErrorType(ErrorType.USER_DISCONNECTED)); } } diff --git a/WalletConnectSharp.Sign/Internals/EngineHandler.cs b/WalletConnectSharp.Sign/Internals/EngineHandler.cs index c38748d..adc1753 100644 --- a/WalletConnectSharp.Sign/Internals/EngineHandler.cs +++ b/WalletConnectSharp.Sign/Internals/EngineHandler.cs @@ -77,7 +77,7 @@ async Task IEnginePrivate.OnSessionProposeRequest(string topic, JsonRpcRequest(id, topic, + await MessageHandler.SendError(id, topic, Error.FromException(e)); } } diff --git a/WalletConnectSharp.Sign/Models/Engine/Methods/SessionProposeResponseAutoReject.cs b/WalletConnectSharp.Sign/Models/Engine/Methods/SessionProposeResponseAutoReject.cs new file mode 100644 index 0000000..d078334 --- /dev/null +++ b/WalletConnectSharp.Sign/Models/Engine/Methods/SessionProposeResponseAutoReject.cs @@ -0,0 +1,25 @@ +using Newtonsoft.Json; +using WalletConnectSharp.Common.Utils; +using WalletConnectSharp.Core.Models.Relay; +using WalletConnectSharp.Network.Models; + +namespace WalletConnectSharp.Sign.Models.Engine.Methods; + +/// +/// A class that represents the response to wc_sessionPropose. Used to approve a session proposal +/// +[RpcResponseOptions(Clock.FIVE_MINUTES, 1121)] +public class SessionProposeResponseAutoReject +{ + /// + /// The protocol options that should be used in this session + /// + [JsonProperty("relay")] + public ProtocolOptions Relay; + + /// + /// The public key of the responder to this session proposal + /// + [JsonProperty("responderPublicKey")] + public string ResponderPublicKey; +} diff --git a/WalletConnectSharp.Sign/Models/Engine/Methods/SessionProposeResponseReject.cs b/WalletConnectSharp.Sign/Models/Engine/Methods/SessionProposeResponseReject.cs new file mode 100644 index 0000000..0659584 --- /dev/null +++ b/WalletConnectSharp.Sign/Models/Engine/Methods/SessionProposeResponseReject.cs @@ -0,0 +1,25 @@ +using Newtonsoft.Json; +using WalletConnectSharp.Common.Utils; +using WalletConnectSharp.Core.Models.Relay; +using WalletConnectSharp.Network.Models; + +namespace WalletConnectSharp.Sign.Models.Engine.Methods; + +/// +/// A class that represents the response to wc_sessionPropose. Used to reject a session proposal. +/// +[RpcResponseOptions(Clock.FIVE_MINUTES, 1120)] +public class SessionProposeResponseReject +{ + /// + /// The protocol options that should be used in this session + /// + [JsonProperty("relay")] + public ProtocolOptions Relay; + + /// + /// The public key of the responder to this session proposal + /// + [JsonProperty("responderPublicKey")] + public string ResponderPublicKey; +} From 1f2b6e01bc7487e6f2e3dec72577650a239f1f5e Mon Sep 17 00:00:00 2001 From: skibitsky Date: Tue, 14 May 2024 13:21:09 +0300 Subject: [PATCH 2/3] Improve chain id format validation --- WalletConnectSharp.Core/Utils.cs | 14 ++++++++++++-- WalletConnectSharp.Sign/Models/SessionStruct.cs | 10 ++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/WalletConnectSharp.Core/Utils.cs b/WalletConnectSharp.Core/Utils.cs index 64a0b28..bce9c31 100644 --- a/WalletConnectSharp.Core/Utils.cs +++ b/WalletConnectSharp.Core/Utils.cs @@ -1,14 +1,19 @@ -namespace WalletConnectSharp.Core; +using System.Text.RegularExpressions; + +namespace WalletConnectSharp.Core; public static class Utils { + private const string SessionIdPattern = @"^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$"; + private static readonly Regex SessionIdRegex = new(SessionIdPattern); + public static bool IsValidUrl(string url) { if (string.IsNullOrWhiteSpace(url)) return false; try { - new Uri(url); + _ = new Uri(url); return true; } catch (Exception e) @@ -17,6 +22,11 @@ public static bool IsValidUrl(string url) } } + public static bool IsValidChainId(string chainId) + { + return SessionIdRegex.IsMatch(chainId); + } + public static bool IsValidRequestExpiry(long expiry, long min, long max) { return expiry <= max && expiry >= min; diff --git a/WalletConnectSharp.Sign/Models/SessionStruct.cs b/WalletConnectSharp.Sign/Models/SessionStruct.cs index d92a29a..8ee1f63 100644 --- a/WalletConnectSharp.Sign/Models/SessionStruct.cs +++ b/WalletConnectSharp.Sign/Models/SessionStruct.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using WalletConnectSharp.Core; using WalletConnectSharp.Core.Interfaces; using WalletConnectSharp.Core.Models.Relay; @@ -144,9 +145,14 @@ private void ValidateNamespaceAndTopic(string @namespace) private void ValidateChainIdAndTopic(string chainId) { - if (chainId == null) + if (string.IsNullOrWhiteSpace(chainId)) { - throw new ArgumentException("chainId is null"); + throw new ArgumentException("chainId is null or empty"); + } + + if (!Utils.IsValidChainId(chainId)) + { + throw new ArgumentException("The format of 'chainId' is invalid. Must be in the format of 'namespace:chainId' (e.g. 'eip155:10'). See CAIP-2 for more information."); } if (string.IsNullOrWhiteSpace(Topic)) From 35daeafe2cf8150522cb505c07377e4b928e996c Mon Sep 17 00:00:00 2001 From: skibitsky Date: Tue, 14 May 2024 13:33:35 +0300 Subject: [PATCH 3/3] Set regex timeout --- WalletConnectSharp.Core/Utils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WalletConnectSharp.Core/Utils.cs b/WalletConnectSharp.Core/Utils.cs index bce9c31..f8804b8 100644 --- a/WalletConnectSharp.Core/Utils.cs +++ b/WalletConnectSharp.Core/Utils.cs @@ -5,7 +5,7 @@ namespace WalletConnectSharp.Core; public static class Utils { private const string SessionIdPattern = @"^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$"; - private static readonly Regex SessionIdRegex = new(SessionIdPattern); + private static readonly Regex SessionIdRegex = new(SessionIdPattern, RegexOptions.None, TimeSpan.FromSeconds(1)); public static bool IsValidUrl(string url) {