Skip to content

Commit

Permalink
Improve wallet session
Browse files Browse the repository at this point in the history
  • Loading branch information
john-s-morgan committed Aug 17, 2022
1 parent ea11c31 commit b87bcd8
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 21 deletions.
6 changes: 4 additions & 2 deletions WalletConnectSharp.Core/Events/EventDelegator.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Threading.Tasks.Sources;
using WalletConnectSharp.Core.Events.Request;
using WalletConnectSharp.Core.Events.Response;
using WalletConnectSharp.Core.Models;
using WalletConnectSharp.Core.Utils;

namespace WalletConnectSharp.Core.Events;

public class EventDelegator : IDisposable
public class EventDelegator : DisposableBase
{
private Dictionary<string, List<IEventProvider>> Listeners = new Dictionary<string, List<IEventProvider>>();

Expand Down Expand Up @@ -80,7 +82,7 @@ public bool Trigger(string topic, string json)
return false;
}

public void Dispose()
protected override void DisposeManaged()
{
Clear();
}
Expand Down
66 changes: 66 additions & 0 deletions WalletConnectSharp.Core/Utils/DisposableBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Threading.Tasks.Sources;

namespace WalletConnectSharp.Core.Utils;
public abstract class DisposableBase : IDisposable
{
private bool _isDisposed;

/// <summary>
/// Default Constructor
/// </summary>
public DisposableBase()
{
}

/// <summary>
/// dispose managed state (managed objects). This must be implemented.
/// </summary>
protected abstract void DisposeManaged();

/// <summary>
/// free unmanaged resources (unmanaged objects) and override finalizer
/// set large fields to null
/// </summary>
protected virtual void DisposeUnmanaged()
{
}

/// <summary>
/// Dispose Managed and Unmanaged resources
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (!_isDisposed)
{
if (disposing)
{
DisposeManaged();
}
DisposeUnmanaged();
_isDisposed = true;
}
}

/// <summary>
/// override finalizer only if 'Dispose(bool disposing)'
/// has code to free unmanaged resources
/// </summary>
~DisposableBase()
{
// Do not change this code.
// Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: false);
}

/// <summary>
/// Dispose the object
/// </summary>
public void Dispose()
{
// Do not change this code.
// Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
5 changes: 3 additions & 2 deletions WalletConnectSharp.Core/WalletConnectProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using WalletConnectSharp.Core.Events;
using WalletConnectSharp.Core.Models;
using WalletConnectSharp.Core.Network;
using WalletConnectSharp.Core.Utils;

namespace WalletConnectSharp.Core;

public class WalletConnectProtocol : IDisposable
public class WalletConnectProtocol : DisposableBase
{
public static readonly string[] SigningMethods = new[]
{
Expand Down Expand Up @@ -280,7 +281,7 @@ public virtual async Task SendRequest<T>(T requestObject, string sendingTopic =
await this.Transport.SendMessage(message);
}

public void Dispose()
protected override void DisposeManaged()
{
if (Transport != null)
{
Expand Down
46 changes: 29 additions & 17 deletions WalletConnectSharp.Core/WalletConnectSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ public virtual async Task<WCSessionData> ConnectSession()
//We do this now before subscribing
//This is in case we need to respond to a session disconnect and this is a
//resume session
Events.ListenForResponse<WCSessionRequestResponse>(this._handshakeId, HandleSessionResponse);
Events.ListenForResponse<WCSessionRequestResponse>(this._handshakeId, async (sender, @event) =>
await HandleSessionResponse(sender, @event));
}

if (!base.TransportConnected)
Expand Down Expand Up @@ -293,7 +294,7 @@ public virtual async Task DisconnectSession(string disconnectMessage = "Session

await base.Disconnect();

HandleSessionDisconnect(disconnectMessage, "disconnect", createNewSession);
await HandleSessionDisconnect(disconnectMessage, "disconnect", createNewSession);
}

public override async Task Disconnect()
Expand Down Expand Up @@ -358,9 +359,6 @@ public virtual async Task<string> EthPersonalSign(string address, string message

var response = await Send<EthPersonalSign, EthResponse>(request);




return response.Result;
}

Expand Down Expand Up @@ -473,12 +471,13 @@ protected async Task<WCSessionData> CreateSession()

//Listen for the _handshakeId response
//The response will be of type WCSessionRequestResponse
Events.ListenForResponse<WCSessionRequestResponse>(this._handshakeId, HandleSessionResponse);
Events.ListenForResponse<WCSessionRequestResponse>(this._handshakeId, async (sender, @event) =>
await HandleSessionResponse(sender, @event));

//Listen for wc_sessionUpdate requests
Events.ListenFor("wc_sessionUpdate",
(object sender, GenericEvent<WCSessionUpdate> @event) =>
HandleSessionUpdate(@event.Response.parameters[0]));
async (object sender, GenericEvent<WCSessionUpdate> @event) =>
await HandleSessionUpdate(@event.Response.parameters[0]));

//Listen for the "connect" event triggered by 'HandleSessionResponse' above
//This will have the type WCSessionData
Expand Down Expand Up @@ -515,25 +514,25 @@ protected async Task<WCSessionData> CreateSession()
return response;
}

protected void HandleSessionResponse(object sender, JsonRpcResponseEvent<WCSessionRequestResponse> jsonresponse)
protected async Task HandleSessionResponse(object sender, JsonRpcResponseEvent<WCSessionRequestResponse> jsonresponse)
{
var response = jsonresponse.Response.result;

if (response != null && response.approved)
{
HandleSessionUpdate(response);
await HandleSessionUpdate(response);
}
else if (jsonresponse.Response.IsError)
{
HandleSessionDisconnect(jsonresponse.Response.Error.Message, "session_failed");
await HandleSessionDisconnect(jsonresponse.Response.Error.Message, "session_failed");
}
else
{
HandleSessionDisconnect("Not Approved", "session_failed");
await HandleSessionDisconnect("Not Approved", "session_failed");
}
}

protected void HandleSessionUpdate(WCSessionData data)
protected async Task HandleSessionUpdate(WCSessionData data)
{
if (data == null) return;

Expand All @@ -560,7 +559,7 @@ protected void HandleSessionUpdate(WCSessionData data)
}
else if (wasConnected && !SessionConnected)
{
HandleSessionDisconnect("Wallet Disconnected");
await HandleSessionDisconnect("Wallet Disconnected");
}
else
{
Expand All @@ -571,7 +570,7 @@ protected void HandleSessionUpdate(WCSessionData data)
SessionUpdate(this, data);
}

protected void HandleSessionDisconnect(string msg, string topic = "disconnect", bool createNewSession = true)
protected async Task HandleSessionDisconnect(string msg, string topic = "disconnect", bool createNewSession = true)
{
SessionConnected = false;
Disconnected = true;
Expand All @@ -580,7 +579,7 @@ protected void HandleSessionDisconnect(string msg, string topic = "disconnect",

if (TransportConnected)
{
DisconnectTransport();
await DisconnectTransport();
}

_activeTopics.Clear();
Expand Down Expand Up @@ -665,5 +664,18 @@ public static SavedSession ReadSession(Stream stream, bool leaveStreamOpen = tru

return JsonConvert.DeserializeObject<SavedSession>(json);
}
}

protected override void DisposeManaged()
{
Events.Clear();
SessionConnected = false;
Disconnected = true;

if (TransportConnected)
{
DisconnectTransport().Wait();
}

base.DisposeManaged();
}
}

0 comments on commit b87bcd8

Please sign in to comment.