Skip to content

Commit

Permalink
Merge pull request #194 from WalletConnect/feat/rejection-tags
Browse files Browse the repository at this point in the history
feat: rejection tags
  • Loading branch information
skibitsky authored May 14, 2024
2 parents be2f77d + 35daeaf commit 52af444
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
14 changes: 12 additions & 2 deletions WalletConnectSharp.Core/Utils.cs
Original file line number Diff line number Diff line change
@@ -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, RegexOptions.None, TimeSpan.FromSeconds(1));

public static bool IsValidUrl(string url)
{
if (string.IsNullOrWhiteSpace(url)) return false;

try
{
new Uri(url);
_ = new Uri(url);
return true;
}
catch (Exception e)
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion WalletConnectSharp.Sign/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ public async Task Reject(RejectParams @params)

if (!string.IsNullOrWhiteSpace(pairingTopic))
{
await MessageHandler.SendError<SessionPropose, SessionProposeResponse>(id, pairingTopic, reason);
await MessageHandler.SendError<SessionPropose, SessionProposeResponseReject>(id, pairingTopic, reason);
await this.Client.Proposal.Delete(id, Error.FromErrorType(ErrorType.USER_DISCONNECTED));
}
}
Expand Down
2 changes: 1 addition & 1 deletion WalletConnectSharp.Sign/Internals/EngineHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async Task IEnginePrivate.OnSessionProposeRequest(string topic, JsonRpcRequest<S
}
catch (WalletConnectException e)
{
await MessageHandler.SendError<SessionPropose, SessionProposeResponse>(id, topic,
await MessageHandler.SendError<SessionPropose, SessionProposeResponseAutoReject>(id, topic,
Error.FromException(e));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// A class that represents the response to wc_sessionPropose. Used to approve a session proposal
/// </summary>
[RpcResponseOptions(Clock.FIVE_MINUTES, 1121)]
public class SessionProposeResponseAutoReject
{
/// <summary>
/// The protocol options that should be used in this session
/// </summary>
[JsonProperty("relay")]
public ProtocolOptions Relay;

/// <summary>
/// The public key of the responder to this session proposal
/// </summary>
[JsonProperty("responderPublicKey")]
public string ResponderPublicKey;
}
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// A class that represents the response to wc_sessionPropose. Used to reject a session proposal.
/// </summary>
[RpcResponseOptions(Clock.FIVE_MINUTES, 1120)]
public class SessionProposeResponseReject
{
/// <summary>
/// The protocol options that should be used in this session
/// </summary>
[JsonProperty("relay")]
public ProtocolOptions Relay;

/// <summary>
/// The public key of the responder to this session proposal
/// </summary>
[JsonProperty("responderPublicKey")]
public string ResponderPublicKey;
}
10 changes: 8 additions & 2 deletions WalletConnectSharp.Sign/Models/SessionStruct.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using WalletConnectSharp.Core;
using WalletConnectSharp.Core.Interfaces;
using WalletConnectSharp.Core.Models.Relay;

Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 52af444

Please sign in to comment.