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

chore: Update AddressProvider to work with sessions that have empty chains #186

Merged
merged 2 commits into from
Apr 17, 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
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
Loading