Skip to content

Commit 8a8fd30

Browse files
authored
TryRecoverSessionAsync and GetNativeTokenBalance (#264)
* added SequenceLogin.TryToRecoverSessionAsync() * Added GetNativeTokenBalance, GetEtherBalance marked obsolote * version bump, added TryRecoverSessionAsync to BoilerplateController * version bump * TryToRestoreSessionAsync returns bool StorageEnabled, fixed post-merge conflicts * Update BoilerplateController.cs
1 parent 30c20b4 commit 8a8fd30

File tree

10 files changed

+81
-20
lines changed

10 files changed

+81
-20
lines changed

Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsCached.cs

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public Chain GetChain()
4848
throw new System.NotImplementedException();
4949
}
5050

51+
public Task<EtherBalance> GetNativeTokenBalance(string accountAddress)
52+
{
53+
throw new System.NotImplementedException();
54+
}
55+
5156
public Task<EtherBalance> GetEtherBalance(string accountAddress)
5257
{
5358
throw new System.NotImplementedException();

Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsNull.cs

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public Chain GetChain()
4545
throw new System.NotImplementedException();
4646
}
4747

48+
public Task<EtherBalance> GetNativeTokenBalance(string accountAddress)
49+
{
50+
throw new System.NotImplementedException();
51+
}
52+
4853
public Task<EtherBalance> GetEtherBalance(string accountAddress)
4954
{
5055
throw new System.NotImplementedException();

Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsProvidedValue.cs

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public Chain GetChain()
4949
throw new System.NotImplementedException();
5050
}
5151

52+
public Task<EtherBalance> GetNativeTokenBalance(string accountAddress)
53+
{
54+
return GetEtherBalance(accountAddress);
55+
}
56+
5257
public async Task<EtherBalance> GetEtherBalance(string accountAddress)
5358
{
5459
return new EtherBalance()

Assets/SequenceSDK/Marketplace/Mocks/MockIndexerWrongChain.cs

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public Chain GetChain()
4141
return Chain.None;
4242
}
4343

44+
public Task<EtherBalance> GetNativeTokenBalance(string accountAddress)
45+
{
46+
throw new System.NotImplementedException();
47+
}
48+
4449
public Task<EtherBalance> GetEtherBalance(string accountAddress)
4550
{
4651
throw new System.NotImplementedException();

Packages/Sequence-Unity/Sequence/SequenceFrontend/Scripts/UI/Boilerplates/Common/BoilerplateController.cs

+6-16
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
using Newtonsoft.Json;
44
using Sequence.Boilerplates.Login;
55
using Sequence.Boilerplates.PlayerProfile;
6-
using Sequence.Config;
76
using Sequence.Contracts;
87
using Sequence.EmbeddedWallet;
98
using Sequence.Marketplace;
109
using Sequence.Pay;
11-
using Sequence.Utils.SecureStorage;
1210
using UnityEngine;
1311

1412
namespace Sequence.Boilerplates
@@ -98,22 +96,14 @@ private void HideFeatureSelection()
9896
_featureSelection.SetActive(false);
9997
}
10098

101-
private void TryRecoverSessionToOpenLoginWindow()
99+
private async void TryRecoverSessionToOpenLoginWindow()
102100
{
103101
HideFeatureSelection();
104-
var config = SequenceConfig.GetConfig();
105-
var storeSessionInfoAndSkipLoginWhenPossible = config.StoreSessionKey();
106-
var loginHandler = SequenceLogin.GetInstance();
107102

108-
if (SecureStorageFactory.IsSupportedPlatform() && storeSessionInfoAndSkipLoginWhenPossible)
109-
{
110-
loginHandler.TryToRestoreSession();
111-
loginHandler.SetupAuthenticator();
112-
}
113-
else
114-
{
115-
OnFailedToRecoverSession("Secure Storage disabled");
116-
}
103+
var loginHandler = SequenceLogin.GetInstance();
104+
var (storageEnabled, wallet) = await loginHandler.TryToRestoreSessionAsync();
105+
if (!storageEnabled)
106+
OnFailedToRecoverSession("Secure storage is disabled");
117107
}
118108

119109
private void OnFailedToRecoverSession(string error)
@@ -307,4 +297,4 @@ private async void DoShowCheckoutPanel()
307297
new SequenceCheckout(_wallet, Chain.Polygon, saleContract, collection, "1", 1), ShowDefaultWindow);
308298
}
309299
}
310-
}
300+
}

Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs

+42
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,48 @@ public void SetupAuthenticator(IValidator validator = null, IAuthenticator authe
157157
_emailConnector = new EmailConnector(_sessionId, _sessionWallet, _connector, _validator);
158158
}
159159

160+
/// <summary>
161+
/// Recover the current session asynchronously and get the associated wallet.
162+
/// </summary>
163+
/// <returns>
164+
/// Returns StorageEnabled bool indicating if the SDK is configured to store sessions.
165+
/// Returns Instance of IWallet if the session was recovered. Returns null if no session was found.
166+
/// </returns>
167+
public async Task<(bool StorageEnabled, IWallet Wallet)> TryToRestoreSessionAsync()
168+
{
169+
var config = SequenceConfig.GetConfig();
170+
var storeSessionInfoAndSkipLoginWhenPossible = config.StoreSessionKey();
171+
if (!SecureStorageFactory.IsSupportedPlatform() || !storeSessionInfoAndSkipLoginWhenPossible)
172+
return (false, null);
173+
174+
var done = false;
175+
SequenceWallet wallet = null;
176+
SequenceWallet.OnFailedToRecoverSession += HandleFailedToRecover;
177+
SequenceWallet.OnWalletCreated += HandleRecoveredWallet;
178+
179+
TryToRestoreSession();
180+
SetupAuthenticator();
181+
182+
while (!done)
183+
await Task.Yield();
184+
185+
return (true, wallet);
186+
187+
void HandleRecoveredWallet(SequenceWallet newWallet)
188+
{
189+
wallet = newWallet;
190+
done = true;
191+
192+
SequenceWallet.OnFailedToRecoverSession -= HandleFailedToRecover;
193+
SequenceWallet.OnWalletCreated -= HandleRecoveredWallet;
194+
}
195+
196+
void HandleFailedToRecover(string error)
197+
{
198+
HandleRecoveredWallet(null);
199+
}
200+
}
201+
160202
public void TryToRestoreSession()
161203
{
162204
if (!_storeSessionWallet)

Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/ChainIndexer.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,16 @@ public Chain GetChain()
8989
{
9090
return ChainDictionaries.ChainById[ChainId];
9191
}
92+
93+
public Task<EtherBalance> GetNativeTokenBalance(string accountAddress)
94+
{
95+
return Indexer.GetNativeTokenBalance(ChainId, accountAddress, 0, _customHttpHandler, this);
96+
}
9297

98+
[Obsolete("Call GetNativeTokenBalance instead.")]
9399
public Task<EtherBalance> GetEtherBalance(string accountAddress)
94100
{
95-
return Indexer.GetEtherBalance(ChainId, accountAddress, 0, _customHttpHandler, this);
101+
return Indexer.GetNativeTokenBalance(ChainId, accountAddress, 0, _customHttpHandler, this);
96102
}
97103

98104
public Task<GetTokenBalancesReturn> GetTokenBalances(GetTokenBalancesArgs args)

Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/IIndexer.cs

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public interface IIndexer
5656
/// Retrieve the balance of a network's native token for a given account address
5757
/// </summary>
5858
/// <exception cref="HttpRequestException">If the network request fails</exception>
59+
public Task<EtherBalance> GetNativeTokenBalance(string accountAddress);
60+
61+
[Obsolete("Call GetNativeTokenBalance instead.")]
5962
public Task<EtherBalance> GetEtherBalance(string accountAddress);
6063

6164
/// <summary>

Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/Indexer.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,14 @@ public static async Task<BigInteger> GetChainID(BigInteger chainID)
175175
[Obsolete]
176176
public static async Task<EtherBalance> GetEtherBalance(BigInteger chainID, string accountAddress)
177177
{
178-
return await GetEtherBalance(chainID.ToString(), accountAddress);
178+
return await GetNativeTokenBalance(chainID.ToString(), accountAddress);
179179
}
180180

181181
/// <summary>
182182
/// Retrieve the balance of a network's native token for a given account address
183183
/// </summary>
184184
/// <exception cref="HttpRequestException">If the network request fails</exception>
185-
public static async Task<EtherBalance> GetEtherBalance(string chainID, string accountAddress, int retries = 0, IHttpHandler httpHandler = null, IIndexer caller = null)
185+
public static async Task<EtherBalance> GetNativeTokenBalance(string chainID, string accountAddress, int retries = 0, IHttpHandler httpHandler = null, IIndexer caller = null)
186186
{
187187
var responseBody = await HttpPost(chainID, "GetEtherBalance", new GetEtherBalanceArgs(accountAddress), retries, httpHandler, caller);
188188
GetEtherBalanceReturn result = BuildResponse<GetEtherBalanceReturn>(responseBody, caller);

Packages/Sequence-Unity/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xyz.0xsequence.waas-unity",
3-
"version": "4.0.5",
3+
"version": "4.0.6",
44
"displayName": "Sequence Embedded Wallet SDK",
55
"description": "A Unity SDK for Sequence APIs",
66
"unity": "2021.3",

0 commit comments

Comments
 (0)