Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing pairing topic in session struct #195

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions WalletConnectSharp.Core/Controllers/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,9 @@ public Task Set(TKey key, TValue value)
{
IsInitialized();

if (map.ContainsKey(key))
{
return Update(key, value);
}

map.Add(key, value);
return Persist();
return !map.TryAdd(key, value)
? Update(key, value)
: Persist();
}

/// <summary>
Expand Down
25 changes: 11 additions & 14 deletions WalletConnectSharp.Core/Controllers/TopicMap.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using WalletConnectSharp.Core.Interfaces;

namespace WalletConnectSharp.Core.Controllers
Expand Down Expand Up @@ -66,23 +63,23 @@ public bool Exists(string topic, string id)
/// <param name="id">The subscription id to remove, if set to null then all ids are removed from the topic</param>
public void Delete(string topic, string id = null)
{
if (!topicMap.ContainsKey(topic))
if (!topicMap.TryGetValue(topic, out var ids))
{
return;
}

if (id == null)
{
topicMap.Remove(topic);
return;
}

if (!Exists(topic, id))
return;

var ids = topicMap[topic];
ids.Remove(id);

if (ids.Count == 0)
topicMap.Remove(topic);
else
{
ids.Remove(id);
if (ids.Count == 0)
{
topicMap.Remove(topic);
}
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion WalletConnectSharp.Sign/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ public async Task<ConnectedData> Connect(ConnectOptions options)
Expiry = expiry,
Id = id,
Proposer = proposal.Proposer,
PairingTopic = topic,
Relays = proposal.Relays,
RequiredNamespaces = proposal.RequiredNamespaces,
OptionalNamespaces = proposal.OptionalNamespaces,
Expand Down Expand Up @@ -587,7 +588,6 @@ public async Task<IApprovedData> Approve(ApproveParams @params)
Namespaces = namespaces,
Controller = new Participant() { PublicKey = selfPublicKey, Metadata = this.Client.Metadata },
Expiry = Clock.CalculateExpiry(SessionExpiry),
PairingTopic = pairingTopic
};

await this.Client.Core.Relayer.Subscribe(sessionTopic);
Expand Down
15 changes: 9 additions & 6 deletions WalletConnectSharp.Sign/Internals/EngineHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,11 @@ async Task IEnginePrivate.OnSessionProposeResponse(string topic, JsonRpcResponse
logger.Log($"Got session propose response with id {id}");
if (payload.IsError)
{
logger.LogError("response was error");
await this.Client.Proposal.Delete(id, Error.FromErrorType(ErrorType.USER_DISCONNECTED));
this.SessionConnectionErrored?.Invoke(this, payload.Error.ToException());
}
else
{
logger.Log("response was success");
var result = payload.Result;
var proposal = this.Client.Proposal.Get(id);
var selfPublicKey = proposal.Proposer.PublicKey;
Expand All @@ -104,10 +102,12 @@ async Task IEnginePrivate.OnSessionProposeResponse(string topic, JsonRpcResponse
selfPublicKey,
peerPublicKey
);

proposal.SessionTopic = sessionTopic;
await Client.Proposal.Set(id, proposal);
await this.Client.Core.Pairing.Activate(topic);
logger.Log($"pairing activated for topic {topic}");
logger.Log($"Pairing activated for topic {topic}");

// try to do this a couple of times .. do it until it works?
int attempts = 5;
do
{
Expand Down Expand Up @@ -137,7 +137,10 @@ async Task IEnginePrivate.OnSessionSettleRequest(string topic, JsonRpcRequest<Se
try
{
await PrivateThis.IsValidSessionSettleRequest(@params);
var pairingTopic = @params.PairingTopic;

var proposal = Array.Find(Client.Proposal.Values, p => p.SessionTopic == topic);

var pairingTopic = proposal.PairingTopic;
var relay = @params.Relay;
var controller = @params.Controller;
var expiry = @params.Expiry;
Expand All @@ -163,7 +166,7 @@ async Task IEnginePrivate.OnSessionSettleRequest(string topic, JsonRpcRequest<Se
Metadata = controller.Metadata
},
#pragma warning disable S6602
RequiredNamespaces = Client.Proposal.Values.FirstOrDefault(p => p.PairingTopic == pairingTopic).RequiredNamespaces
RequiredNamespaces = proposal.RequiredNamespaces
#pragma warning restore S6602
};
await MessageHandler.SendResult<SessionSettle, bool>(payload.Id, topic, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class SessionSettle : IWcMethod
/// Pairing topic for this session
/// </summary>
[JsonProperty("pairingTopic")]
[Obsolete("This isn't a standard property of the Sign API. Other Sign implementations may not support this property whcih could lead to unexpected behavior.")]
public string PairingTopic;

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions WalletConnectSharp.Sign/Models/ProposalStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public long Key
[JsonProperty("pairingTopic")]
public string PairingTopic;

/// <summary>
/// The topic of the session. Set after the proposal is approved.
/// </summary>
[JsonProperty("sessionTopic")]
public string SessionTopic;

/// <summary>
/// Approve this proposal with a single address and (optional) protocol options. The
/// protocolOption given must exist in this proposal
Expand Down
Loading