Skip to content

Commit

Permalink
Merge pull request #149 from WalletConnect/fix/sign-events
Browse files Browse the repository at this point in the history
Document Sign Events
  • Loading branch information
skibitsky authored Nov 3, 2023
2 parents 883f0b7 + e5557f5 commit 32b148e
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 7 deletions.
88 changes: 85 additions & 3 deletions WalletConnectSharp.Sign/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ private void WrapPairingEvents()
{
this.Client.Core.Pairing.PairingPinged += (sender, @event) => this.PairingPinged?.Invoke(sender, @event);
this.Client.Core.Pairing.PairingDeleted += (sender, @event) => this.PairingDeleted?.Invoke(sender, @event);
this.Client.Core.Pairing.PairingExpired += (sender, @event) => this.PairingExpired?.Invoke(sender, @event);
}

private void RegisterExpirerEvents()
Expand All @@ -114,21 +115,97 @@ private void RegisterRelayerEvents()
MessageHandler.HandleMessageType<SessionDelete, bool>(PrivateThis.OnSessionDeleteRequest, null);
MessageHandler.HandleMessageType<SessionPing, bool>(PrivateThis.OnSessionPingRequest, PrivateThis.OnSessionPingResponse);
}


/// <summary>
/// This event is invoked when the given session has expired
/// Event Side: dApp & Wallet
/// </summary>
public event EventHandler<SessionStruct> SessionExpired;
public event EventHandler<PairingStruct> PairingExpired;

/// <summary>
/// This event is invoked when the given pairing has expired
/// Event Side: Wallet
/// </summary>
public event EventHandler<PairingEvent> PairingExpired;

/// <summary>
/// This event is invoked when a new session is proposed. This is usually invoked
/// after a new pairing has been activated from a URI
/// Event Side: Wallet
/// </summary>
public event EventHandler<SessionProposalEvent> SessionProposed;

/// <summary>
/// This event is invoked when a proposed session has been connected to a wallet. This event is
/// triggered after the session has been approved by a wallet
/// Event Side: dApp
/// </summary>
public event EventHandler<SessionStruct> SessionConnected;

/// <summary>
/// This event is invoked when a proposed session connection failed with an error
/// Event Side: dApp
/// </summary>
public event EventHandler<Exception> SessionConnectionErrored;

/// <summary>
/// This event is invoked when a given session sent a update request.
/// Event Side: Wallet
/// </summary>
public event EventHandler<SessionUpdateEvent> SessionUpdateRequest;

/// <summary>
/// This event is invoked when a given session sent a extend request.
/// Event Side: Wallet
/// </summary>
public event EventHandler<SessionEvent> SessionExtendRequest;

/// <summary>
/// This event is invoked when a given session update request was successful.
/// Event Side: dApp
/// </summary>
public event EventHandler<SessionEvent> SessionUpdated;

/// <summary>
/// This event is invoked when a given session extend request was successful.
/// Event Side: dApp
/// </summary>
public event EventHandler<SessionEvent> SessionExtended;

/// <summary>
/// This event is invoked when a given session has been pinged
/// Event Side: dApp & Wallet
/// </summary>
public event EventHandler<SessionEvent> SessionPinged;

/// <summary>
/// This event is invoked whenever a session has been deleted
/// Event Side: dApp & Wallet
/// </summary>
public event EventHandler<SessionEvent> SessionDeleted;

/// <summary>
/// This event is invoked whenever a session has been rejected
/// Event Side: Wallet
/// </summary>
public event EventHandler<SessionStruct> SessionRejected;

/// <summary>
/// This event is invoked whenever a session has been approved
/// Event Side: Wallet
/// </summary>
public event EventHandler<SessionStruct> SessionApproved;

/// <summary>
/// This event is invoked whenever a pairing is pinged
/// Event Side: dApp & Wallet
/// </summary>
public event EventHandler<PairingEvent> PairingPinged;

/// <summary>
/// This event is invoked whenever a pairing is deleted
/// Event Side: dApp & Wallet
/// </summary>
public event EventHandler<PairingEvent> PairingDeleted;

/// <summary>
Expand Down Expand Up @@ -705,13 +782,18 @@ public async Task Disconnect(string topic, Error reason)

if (this.Client.Session.Keys.Contains(topic))
{
await MessageHandler.SendRequest<SessionDelete, bool>(topic, new SessionDelete()
var id = await MessageHandler.SendRequest<SessionDelete, bool>(topic, new SessionDelete()
{
Code = error.Code,
Message = error.Message,
Data = error.Data
});
await PrivateThis.DeleteSession(topic);
this.SessionDeleted?.Invoke(this, new SessionEvent()
{
Topic = topic,
Id = id
});
}
else if (this.Client.Core.Pairing.Store.Keys.Contains(topic))
{
Expand Down
64 changes: 63 additions & 1 deletion WalletConnectSharp.Sign/Interfaces/IEngineAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,96 @@ namespace WalletConnectSharp.Sign.Interfaces
/// </summary>
public interface IEngineAPI
{
/// <summary>
/// This event is invoked when the given session has expired
/// Event Side: dApp & Wallet
/// </summary>
event EventHandler<SessionStruct> SessionExpired;

event EventHandler<PairingStruct> PairingExpired;
/// <summary>
/// This event is invoked when the given pairing has expired
/// Event Side: Wallet
/// </summary>
event EventHandler<PairingEvent> PairingExpired;

/// <summary>
/// This event is invoked when a new session is proposed. This is usually invoked
/// after a new pairing has been activated from a URI
/// Event Side: Wallet
/// </summary>
event EventHandler<SessionProposalEvent> SessionProposed;

/// <summary>
/// This event is invoked when a proposed session has been connected to a wallet. This event is
/// triggered after the session has been approved by a wallet
/// Event Side: dApp
/// </summary>
event EventHandler<SessionStruct> SessionConnected;

/// <summary>
/// This event is invoked when a proposed session connection failed with an error
/// Event Side: dApp
/// </summary>
event EventHandler<Exception> SessionConnectionErrored;

/// <summary>
/// This event is invoked when a given session sent a update request.
/// Event Side: Wallet
/// </summary>
event EventHandler<SessionUpdateEvent> SessionUpdateRequest;

/// <summary>
/// This event is invoked when a given session sent a extend request.
/// Event Side: Wallet
/// </summary>
event EventHandler<SessionEvent> SessionExtendRequest;

/// <summary>
/// This event is invoked when a given session update request was successful.
/// Event Side: dApp
/// </summary>
event EventHandler<SessionEvent> SessionUpdated;

/// <summary>
/// This event is invoked when a given session extend request was successful.
/// Event Side: dApp
/// </summary>
event EventHandler<SessionEvent> SessionExtended;

/// <summary>
/// This event is invoked when a given session has been pinged
/// Event Side: dApp & Wallet
/// </summary>
event EventHandler<SessionEvent> SessionPinged;

/// <summary>
/// This event is invoked whenever a session has been deleted
/// Event Side: dApp & Wallet
/// </summary>
event EventHandler<SessionEvent> SessionDeleted;

/// <summary>
/// This event is invoked whenever a session has been rejected
/// Event Side: Wallet
/// </summary>
event EventHandler<SessionStruct> SessionRejected;

/// <summary>
/// This event is invoked whenever a session has been approved
/// Event Side: Wallet
/// </summary>
event EventHandler<SessionStruct> SessionApproved;

/// <summary>
/// This event is invoked whenever a pairing is pinged
/// Event Side: dApp & Wallet
/// </summary>
event EventHandler<PairingEvent> PairingPinged;

/// <summary>
/// This event is invoked whenever a pairing is deleted
/// Event Side: dApp & Wallet
/// </summary>
event EventHandler<PairingEvent> PairingDeleted;

/// <summary>
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 @@ -35,6 +35,10 @@ await PrivateThis.DeletePendingSessionRequest((long)target.Id,
var session = this.Client.Session.Get(topic);
await PrivateThis.DeleteSession(topic);
this.SessionExpired?.Invoke(this, session);
this.SessionDeleted?.Invoke(this, new SessionEvent()
{
Topic = topic
});
}
else if (target.Id != null)
{
Expand Down
6 changes: 4 additions & 2 deletions WalletConnectSharp.Sign/Internals/EngineValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async Task IsValidPairingTopic(string topic)
}
}

async Task IsValidSessionTopic(string topic)
Task IsValidSessionTopic(string topic)
{
if (string.IsNullOrWhiteSpace(topic))
throw WalletConnectException.FromType(ErrorType.MISSING_OR_INVALID,
Expand All @@ -64,9 +64,11 @@ async Task IsValidSessionTopic(string topic)
var expiry = this.Client.Session.Get(topic).Expiry;
if (expiry != null && Clock.IsExpired(expiry.Value))
{
await PrivateThis.DeleteSession(topic);
//await PrivateThis.DeleteSession(topic);
throw WalletConnectException.FromType(ErrorType.EXPIRED, $"session topic: {topic}");
}

return Task.CompletedTask;
}

async Task IsValidProposalId(long id)
Expand Down
4 changes: 3 additions & 1 deletion WalletConnectSharp.Sign/WalletConnectSignClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public int Version


public event EventHandler<SessionStruct> SessionExpired;
public event EventHandler<PairingStruct> PairingExpired;
public event EventHandler<PairingEvent> PairingExpired;
public event EventHandler<SessionProposalEvent> SessionProposed;
public event EventHandler<SessionStruct> SessionConnected;
public event EventHandler<Exception> SessionConnectionErrored;
Expand Down Expand Up @@ -226,6 +226,8 @@ private void WrapEngineEvents()
Engine.SessionApproved += (sender, @struct) => this.SessionApproved?.Invoke(sender, @struct);
Engine.SessionExtended += (sender, @event) => this.SessionExtended?.Invoke(sender, @event);
Engine.SessionUpdated += (sender, @event) => this.SessionUpdated?.Invoke(sender, @event);
Engine.PairingDeleted += (sender, @event) => this.PairingDeleted?.Invoke(sender, @event);
Engine.PairingPinged += (sender, @event) => this.PairingPinged?.Invoke(sender, @event);
}

/// <summary>
Expand Down

0 comments on commit 32b148e

Please sign in to comment.