diff --git a/Directory.Build.props b/Directory.Build.props index b8f8019..fa5f5d8 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 2.3.4 + 2.3.5 net6.0;net7.0;net8.0;netstandard2.1; true diff --git a/WalletConnectSharp.Sign/Controllers/AddressProvider.cs b/WalletConnectSharp.Sign/Controllers/AddressProvider.cs index 53c370c..fafd537 100644 --- a/WalletConnectSharp.Sign/Controllers/AddressProvider.cs +++ b/WalletConnectSharp.Sign/Controllers/AddressProvider.cs @@ -145,23 +145,36 @@ private async Task UpdateDefaultChainIdAndNamespaceAsync() { // Check if current default namespace is still valid with the current session var currentDefault = DefaultNamespace; + + if (currentDefault != null && DefaultSession.Namespaces.ContainsKey(currentDefault)) { + if (!DefaultSession.Namespaces[DefaultNamespace].TryGetChains(out var approvedChains)) + { + throw new InvalidOperationException("Could not get chains for current default namespace"); + } + // Check if current default chain is still valid with the current session var currentChain = DefaultChainId; - if (currentChain == null || !DefaultSession.Namespaces[DefaultNamespace].Chains.Contains(currentChain)) + + if (currentChain == null || !approvedChains.Contains(currentChain)) { // If the current default chain is not valid, let's use the first one - DefaultChainId = DefaultSession.Namespaces[DefaultNamespace].Chains[0]; + DefaultChainId = approvedChains[0]; } } else { // If DefaultNamespace is null or not found in current available spaces, update it DefaultNamespace = DefaultSession.Namespaces.Keys.FirstOrDefault(); - if (DefaultNamespace != null && DefaultSession.Namespaces[DefaultNamespace].Chains != null) + if (DefaultNamespace != null) { - DefaultChainId = DefaultSession.Namespaces[DefaultNamespace].Chains[0]; + if (!DefaultSession.Namespaces[DefaultNamespace].TryGetChains(out var approvedChains)) + { + throw new InvalidOperationException("Could not get chains for current default namespace"); + } + + DefaultChainId = approvedChains[0]; } else { diff --git a/WalletConnectSharp.Sign/Models/Namespace.cs b/WalletConnectSharp.Sign/Models/Namespace.cs index 6dc436c..2b1cdef 100644 --- a/WalletConnectSharp.Sign/Models/Namespace.cs +++ b/WalletConnectSharp.Sign/Models/Namespace.cs @@ -72,12 +72,12 @@ public Namespace WithAccount(string account) return this; } - protected static bool ArrayEquals(string[] a, string[] b) + private static bool ArrayEquals(string[] a, string[] b) { - return a.Length == b.Length && a.All(b.Contains) && b.All(a.Contains); + return a.Length == b.Length && Array.TrueForAll(a, b.Contains) && Array.TrueForAll(b, a.Contains); } - protected bool Equals(Namespace other) + private bool Equals(Namespace other) { return ArrayEquals(Accounts, other.Accounts) && ArrayEquals(Methods, other.Methods) && ArrayEquals(Events, other.Events); @@ -108,6 +108,41 @@ public override int GetHashCode() return HashCode.Combine(Accounts, Methods, Events); } + public bool TryGetChains(out string[] chainIds) + { + if (Chains is { Length: 0 }) + { + chainIds = Chains; + return true; + } + + HashSet chainSet = []; + foreach (var account in Accounts) + { + var t = false; + for (var i = 0; i < account.Length; i++) + { + if (account[i] != ':') + { + continue; + } + + if (!t) + { + t = true; + } + else + { + chainSet.Add(account[..i]); + break; + } + } + } + + chainIds = chainSet.ToArray(); + return true; + } + private sealed class NamespaceEqualityComparer : IEqualityComparer { public bool Equals(Namespace x, Namespace y)