Skip to content

Commit

Permalink
Fixed missing pairing topic in session struct
Browse files Browse the repository at this point in the history
  • Loading branch information
skibitsky committed May 16, 2024
1 parent 52af444 commit a5bccbc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 27 deletions.
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
1 change: 1 addition & 0 deletions 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
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

0 comments on commit a5bccbc

Please sign in to comment.