Skip to content

Commit

Permalink
Merge pull request #186 from WalletConnect/chore/address-provider-no-…
Browse files Browse the repository at this point in the history
…chains

chore: Update AddressProvider to work with sessions that have empty `chains`
  • Loading branch information
skibitsky authored Apr 17, 2024
2 parents 3d84159 + 4e00989 commit 4f8e701
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<DefaultVersion>2.3.4</DefaultVersion>
<DefaultVersion>2.3.5</DefaultVersion>
<DefaultTargetFrameworks>net6.0;net7.0;net8.0;netstandard2.1;</DefaultTargetFrameworks>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
Expand Down
21 changes: 17 additions & 4 deletions WalletConnectSharp.Sign/Controllers/AddressProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
41 changes: 38 additions & 3 deletions WalletConnectSharp.Sign/Models/Namespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<string> 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<Namespace>
{
public bool Equals(Namespace x, Namespace y)
Expand Down

0 comments on commit 4f8e701

Please sign in to comment.