Skip to content

Commit

Permalink
Merge pull request #201 from WalletConnect/fix/unhandled-event-exception
Browse files Browse the repository at this point in the history
fix: unhandledable session event exception
  • Loading branch information
skibitsky committed Jul 29, 2024
2 parents 183db5a + dabb1a2 commit 059db27
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<DefaultVersion>2.4.0</DefaultVersion>
<DefaultVersion>2.4.1</DefaultVersion>
<DefaultTargetFrameworks>net6.0;net7.0;net8.0;netstandard2.1;</DefaultTargetFrameworks>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
Expand Down
4 changes: 4 additions & 0 deletions Tests/WalletConnectSharp.Web3Wallet.Tests/SignTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,10 @@ await Task.WhenAll(

Assert.True(_dapp.TryUnsubscribeFromSessionEvent(referenceTypeEventData.Name, ReferenceTypeEventHandler));
Assert.True(_dapp.TryUnsubscribeFromSessionEvent(valueTypeEventData.Name, ValueTypeEventHandler));

// Test invalid chains
await Assert.ThrowsAsync<FormatException>(() => _wallet.EmitSessionEvent(session.Topic, valueTypeEventData, "invalid chain"));
await Assert.ThrowsAsync<NamespacesException>(() => _wallet.EmitSessionEvent(session.Topic, valueTypeEventData, "123:321"));
}

[Fact, Trait("Category", "unit")]
Expand Down
2 changes: 2 additions & 0 deletions WalletConnectSharp.Sign/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using WalletConnectSharp.Common.Model.Errors;
using WalletConnectSharp.Common.Model.Relay;
using WalletConnectSharp.Common.Utils;
using WalletConnectSharp.Core;
using WalletConnectSharp.Core.Interfaces;
using WalletConnectSharp.Core.Models;
using WalletConnectSharp.Core.Models.Pairing;
Expand Down Expand Up @@ -818,6 +819,7 @@ public async Task Respond<T, TR>(string topic, JsonRpcResponse<TR> response)
public async Task Emit<T>(string topic, EventData<T> eventData, string chainId = null)
{
IsInitialized();
await PrivateThis.IsValidEmit(topic, eventData, chainId);
await MessageHandler.SendRequest<SessionEvent<T>, object>(topic,
new SessionEvent<T> { ChainId = chainId, Event = eventData, Topic = topic });
}
Expand Down
4 changes: 4 additions & 0 deletions WalletConnectSharp.Sign/Internals/EngineHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ async Task IEnginePrivate.OnSessionEventRequest(string topic, JsonRpcRequest<Ses
{
await MessageHandler.SendError<SessionEvent<JToken>, bool>(id, topic, Error.FromException(e));
}
catch (Exception e) // to avoid unhandled exceptions caused by invalid events sent by another peer
{
logger.LogError(e);
}
}
}
}
2 changes: 1 addition & 1 deletion WalletConnectSharp.Sign/Internals/EngineValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ private void ValidateNamespacesChainId(Namespaces namespaces, string chainId)
{
if (!Utils.IsValidChainId(chainId))
{
throw new FormatException($"ChainId {chainId} should be a string and conform to 'chainId:chainId' format.");
throw new FormatException($"ChainId {chainId} should be a string and conform to CAIP-2.");
}

var chains = GetNamespacesChains(namespaces);
Expand Down

0 comments on commit 059db27

Please sign in to comment.