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

fix: defaults loading in Address Provider #192

Merged
merged 1 commit into from
May 8, 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
9 changes: 7 additions & 2 deletions Tests/WalletConnectSharp.Sign.Test/SignTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ public async Task TestTwoUniqueSessionRequestResponseUsingAddressProviderDefault

var dappClient = ClientA;
var walletClient = ClientB;

await dappClient.AddressProvider.LoadDefaultsAsync();
await walletClient.AddressProvider.LoadDefaultsAsync();

if (!dappClient.AddressProvider.HasDefaultSession && !walletClient.AddressProvider.HasDefaultSession)
{
var testAddress = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
Expand Down Expand Up @@ -579,17 +583,18 @@ public async Task TestAddressProviderDefaultsSaving()

var defaultSessionTopic = _cryptoFixture.ClientA.AddressProvider.DefaultSession.Topic;

Assert.NotNull(defaultSessionTopic);

_cryptoFixture.StorageOverrideA = _cryptoFixture.ClientA.Core.Storage;
_cryptoFixture.StorageOverrideB = _cryptoFixture.ClientB.Core.Storage;

await Task.Delay(500);

await _cryptoFixture.DisposeAndReset();

_testOutputHelper.WriteLine(string.Join(",", _cryptoFixture.ClientB.Core.Crypto.KeyChain.Keychain.Values));

await Task.Delay(500);

await _cryptoFixture.ClientA.AddressProvider.LoadDefaultsAsync();
var reloadedDefaultSessionTopic = _cryptoFixture.ClientA.AddressProvider.DefaultSession.Topic;

Assert.Equal(defaultSessionTopic, reloadedDefaultSessionTopic);
Expand Down
17 changes: 9 additions & 8 deletions WalletConnectSharp.Sign/Controllers/AddressProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using WalletConnectSharp.Sign.Interfaces;
using WalletConnectSharp.Common.Utils;
using WalletConnectSharp.Sign.Interfaces;
using WalletConnectSharp.Sign.Models;
using WalletConnectSharp.Sign.Models.Engine.Events;

Expand Down Expand Up @@ -100,12 +101,17 @@ public virtual async Task SaveDefaults()
await _client.Core.Storage.SetItem($"{Context}-default-session", _state);
}

public virtual async Task LoadDefaults()
public virtual async Task LoadDefaultsAsync()
{
var key = $"{Context}-default-session";
if (await _client.Core.Storage.HasItem(key))
{
_state = await _client.Core.Storage.GetItem<DefaultData>(key);
var state = await _client.Core.Storage.GetItem<DefaultData>(key);
var sessionExpiry = state.Session.Expiry;

_state = sessionExpiry != null && !Clock.IsExpired(sessionExpiry.Value)
? state
: new DefaultData();
}
else
{
Expand Down Expand Up @@ -191,11 +197,6 @@ private async Task UpdateDefaultChainIdAndNamespaceAsync()
}
}

public async Task InitAsync()
{
await this.LoadDefaults();
}

public async Task SetDefaultNamespaceAsync(string @namespace)
{
if (string.IsNullOrWhiteSpace(@namespace))
Expand Down
7 changes: 3 additions & 4 deletions WalletConnectSharp.Sign/Interfaces/IAddressProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ public interface IAddressProvider : IModule
string DefaultChainId { get; }

ISession Sessions { get; }


Task InitAsync();


Task SetDefaultNamespaceAsync(string @namespace);

Task SetDefaultChainIdAsync(string chainId);

Caip25Address CurrentAddress(string chainId = null, SessionStruct session = default);

IEnumerable<Caip25Address> AllAddresses(string @namespace = null, SessionStruct session = default);

public Task LoadDefaultsAsync();
}
1 change: 0 additions & 1 deletion WalletConnectSharp.Sign/WalletConnectSignClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,6 @@ private async Task Initialize()
await Session.Init();
await Proposal.Init();
await Engine.Init();
await AddressProvider.InitAsync();
}

public void Dispose()
Expand Down
Loading