From a5bccbc5f2bc2306b3a4ff6116221e9c4d24acbd Mon Sep 17 00:00:00 2001 From: skibitsky Date: Thu, 16 May 2024 12:35:29 +0300 Subject: [PATCH 1/2] Fixed missing pairing topic in session struct --- WalletConnectSharp.Core/Controllers/Store.cs | 10 +++----- .../Controllers/TopicMap.cs | 25 ++++++++----------- WalletConnectSharp.Sign/Engine.cs | 1 + .../Internals/EngineHandler.cs | 15 ++++++----- .../Models/Engine/Methods/SessionSettle.cs | 1 + .../Models/ProposalStruct.cs | 6 +++++ 6 files changed, 31 insertions(+), 27 deletions(-) diff --git a/WalletConnectSharp.Core/Controllers/Store.cs b/WalletConnectSharp.Core/Controllers/Store.cs index 4e6091c..0e04122 100644 --- a/WalletConnectSharp.Core/Controllers/Store.cs +++ b/WalletConnectSharp.Core/Controllers/Store.cs @@ -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(); } /// diff --git a/WalletConnectSharp.Core/Controllers/TopicMap.cs b/WalletConnectSharp.Core/Controllers/TopicMap.cs index 04df5f2..5a00768 100644 --- a/WalletConnectSharp.Core/Controllers/TopicMap.cs +++ b/WalletConnectSharp.Core/Controllers/TopicMap.cs @@ -1,6 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; using WalletConnectSharp.Core.Interfaces; namespace WalletConnectSharp.Core.Controllers @@ -66,23 +63,23 @@ public bool Exists(string topic, string id) /// The subscription id to remove, if set to null then all ids are removed from the topic 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); + } + } } /// diff --git a/WalletConnectSharp.Sign/Engine.cs b/WalletConnectSharp.Sign/Engine.cs index 7b0c77c..3a8c98d 100644 --- a/WalletConnectSharp.Sign/Engine.cs +++ b/WalletConnectSharp.Sign/Engine.cs @@ -464,6 +464,7 @@ public async Task Connect(ConnectOptions options) Expiry = expiry, Id = id, Proposer = proposal.Proposer, + PairingTopic = topic, Relays = proposal.Relays, RequiredNamespaces = proposal.RequiredNamespaces, OptionalNamespaces = proposal.OptionalNamespaces, diff --git a/WalletConnectSharp.Sign/Internals/EngineHandler.cs b/WalletConnectSharp.Sign/Internals/EngineHandler.cs index adc1753..a437ac1 100644 --- a/WalletConnectSharp.Sign/Internals/EngineHandler.cs +++ b/WalletConnectSharp.Sign/Internals/EngineHandler.cs @@ -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; @@ -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 { @@ -137,7 +137,10 @@ async Task IEnginePrivate.OnSessionSettleRequest(string topic, JsonRpcRequest p.SessionTopic == topic); + + var pairingTopic = proposal.PairingTopic; var relay = @params.Relay; var controller = @params.Controller; var expiry = @params.Expiry; @@ -163,7 +166,7 @@ async Task IEnginePrivate.OnSessionSettleRequest(string topic, JsonRpcRequest p.PairingTopic == pairingTopic).RequiredNamespaces + RequiredNamespaces = proposal.RequiredNamespaces #pragma warning restore S6602 }; await MessageHandler.SendResult(payload.Id, topic, true); diff --git a/WalletConnectSharp.Sign/Models/Engine/Methods/SessionSettle.cs b/WalletConnectSharp.Sign/Models/Engine/Methods/SessionSettle.cs index 482c4f7..fa94ad6 100644 --- a/WalletConnectSharp.Sign/Models/Engine/Methods/SessionSettle.cs +++ b/WalletConnectSharp.Sign/Models/Engine/Methods/SessionSettle.cs @@ -18,6 +18,7 @@ public class SessionSettle : IWcMethod /// Pairing topic for this session /// [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; /// diff --git a/WalletConnectSharp.Sign/Models/ProposalStruct.cs b/WalletConnectSharp.Sign/Models/ProposalStruct.cs index 714228c..657e0cd 100644 --- a/WalletConnectSharp.Sign/Models/ProposalStruct.cs +++ b/WalletConnectSharp.Sign/Models/ProposalStruct.cs @@ -79,6 +79,12 @@ public long Key [JsonProperty("pairingTopic")] public string PairingTopic; + /// + /// The topic of the session. Set after the proposal is approved. + /// + [JsonProperty("sessionTopic")] + public string SessionTopic; + /// /// Approve this proposal with a single address and (optional) protocol options. The /// protocolOption given must exist in this proposal From 79aa7660976453d582ce6e834d9072fed768d75f Mon Sep 17 00:00:00 2001 From: skibitsky Date: Thu, 16 May 2024 12:49:48 +0300 Subject: [PATCH 2/2] =?UTF-8?q?Don=E2=80=99t=20set=20pairing=20topic=20ins?= =?UTF-8?q?ide=20of=20session=20settle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WalletConnectSharp.Sign/Engine.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/WalletConnectSharp.Sign/Engine.cs b/WalletConnectSharp.Sign/Engine.cs index 3a8c98d..deb2928 100644 --- a/WalletConnectSharp.Sign/Engine.cs +++ b/WalletConnectSharp.Sign/Engine.cs @@ -588,7 +588,6 @@ public async Task 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);