Skip to content

TryRecoverSessionAsync and GetNativeTokenBalance #264

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public Chain GetChain()
throw new System.NotImplementedException();
}

public Task<EtherBalance> GetNativeTokenBalance(string accountAddress)
{
throw new System.NotImplementedException();
}

public Task<EtherBalance> GetEtherBalance(string accountAddress)
{
throw new System.NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public Chain GetChain()
throw new System.NotImplementedException();
}

public Task<EtherBalance> GetNativeTokenBalance(string accountAddress)
{
throw new System.NotImplementedException();
}

public Task<EtherBalance> GetEtherBalance(string accountAddress)
{
throw new System.NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public Chain GetChain()
throw new System.NotImplementedException();
}

public Task<EtherBalance> GetNativeTokenBalance(string accountAddress)
{
return GetEtherBalance(accountAddress);
}

public async Task<EtherBalance> GetEtherBalance(string accountAddress)
{
return new EtherBalance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public Chain GetChain()
return Chain.None;
}

public Task<EtherBalance> GetNativeTokenBalance(string accountAddress)
{
throw new System.NotImplementedException();
}

public Task<EtherBalance> GetEtherBalance(string accountAddress)
{
throw new System.NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
using Newtonsoft.Json;
using Sequence.Boilerplates.Login;
using Sequence.Boilerplates.PlayerProfile;
using Sequence.Config;
using Sequence.Contracts;
using Sequence.EmbeddedWallet;
using Sequence.Marketplace;
using Sequence.Pay;
using Sequence.Utils.SecureStorage;
using UnityEngine;

namespace Sequence.Boilerplates
Expand Down Expand Up @@ -98,22 +96,14 @@ private void HideFeatureSelection()
_featureSelection.SetActive(false);
}

private void TryRecoverSessionToOpenLoginWindow()
private async void TryRecoverSessionToOpenLoginWindow()
{
HideFeatureSelection();
var config = SequenceConfig.GetConfig();
var storeSessionInfoAndSkipLoginWhenPossible = config.StoreSessionKey();
var loginHandler = SequenceLogin.GetInstance();

if (SecureStorageFactory.IsSupportedPlatform() && storeSessionInfoAndSkipLoginWhenPossible)
{
loginHandler.TryToRestoreSession();
loginHandler.SetupAuthenticator();
}
else
{
OnFailedToRecoverSession("Secure Storage disabled");
}
var loginHandler = SequenceLogin.GetInstance();
var (storageEnabled, wallet) = await loginHandler.TryToRestoreSessionAsync();
if (!storageEnabled && wallet == null)
OnFailedToRecoverSession("Secure storage is disabled");
}

private void OnFailedToRecoverSession(string error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,48 @@ public void SetupAuthenticator(IValidator validator = null, IAuthenticator authe
_emailConnector = new EmailConnector(_sessionId, _sessionWallet, _connector, _validator);
}

/// <summary>
/// Recover the current session asynchronously and get the associated wallet.
/// </summary>
/// <returns>
/// Returns StorageEnabled bool indicating if the SDK is configured to store sessions.
/// Returns Instance of IWallet if the session was recovered. Returns null if no session was found.
/// </returns>
public async Task<(bool StorageEnabled, IWallet Wallet)> TryToRestoreSessionAsync()
{
var config = SequenceConfig.GetConfig();
var storeSessionInfoAndSkipLoginWhenPossible = config.StoreSessionKey();
if (!SecureStorageFactory.IsSupportedPlatform() || !storeSessionInfoAndSkipLoginWhenPossible)
return (false, null);

var done = false;
SequenceWallet wallet = null;
SequenceWallet.OnFailedToRecoverSession += HandleFailedToRecover;
SequenceWallet.OnWalletCreated += HandleRecoveredWallet;

TryToRestoreSession();
SetupAuthenticator();

while (!done)
await Task.Yield();

return (true, wallet);

void HandleRecoveredWallet(SequenceWallet newWallet)
{
wallet = newWallet;
done = true;

SequenceWallet.OnFailedToRecoverSession -= HandleFailedToRecover;
SequenceWallet.OnWalletCreated -= HandleRecoveredWallet;
}

void HandleFailedToRecover(string error)
{
HandleRecoveredWallet(null);
}
}

public void TryToRestoreSession()
{
if (!_storeSessionWallet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,16 @@ public Chain GetChain()
{
return ChainDictionaries.ChainById[ChainId];
}

public Task<EtherBalance> GetNativeTokenBalance(string accountAddress)
{
return Indexer.GetNativeTokenBalance(ChainId, accountAddress, 0, _customHttpHandler, this);
}

[Obsolete("Call GetNativeTokenBalance instead.")]
public Task<EtherBalance> GetEtherBalance(string accountAddress)
{
return Indexer.GetEtherBalance(ChainId, accountAddress, 0, _customHttpHandler, this);
return Indexer.GetNativeTokenBalance(ChainId, accountAddress, 0, _customHttpHandler, this);
}

public Task<GetTokenBalancesReturn> GetTokenBalances(GetTokenBalancesArgs args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public interface IIndexer
/// Retrieve the balance of a network's native token for a given account address
/// </summary>
/// <exception cref="HttpRequestException">If the network request fails</exception>
public Task<EtherBalance> GetNativeTokenBalance(string accountAddress);

[Obsolete("Call GetNativeTokenBalance instead.")]
public Task<EtherBalance> GetEtherBalance(string accountAddress);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ public static async Task<BigInteger> GetChainID(BigInteger chainID)
[Obsolete]
public static async Task<EtherBalance> GetEtherBalance(BigInteger chainID, string accountAddress)
{
return await GetEtherBalance(chainID.ToString(), accountAddress);
return await GetNativeTokenBalance(chainID.ToString(), accountAddress);
}

/// <summary>
/// Retrieve the balance of a network's native token for a given account address
/// </summary>
/// <exception cref="HttpRequestException">If the network request fails</exception>
public static async Task<EtherBalance> GetEtherBalance(string chainID, string accountAddress, int retries = 0, IHttpHandler httpHandler = null, IIndexer caller = null)
public static async Task<EtherBalance> GetNativeTokenBalance(string chainID, string accountAddress, int retries = 0, IHttpHandler httpHandler = null, IIndexer caller = null)
{
var responseBody = await HttpPost(chainID, "GetEtherBalance", new GetEtherBalanceArgs(accountAddress), retries, httpHandler, caller);
GetEtherBalanceReturn result = BuildResponse<GetEtherBalanceReturn>(responseBody, caller);
Expand Down
2 changes: 1 addition & 1 deletion Packages/Sequence-Unity/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xyz.0xsequence.waas-unity",
"version": "4.0.5",
"version": "4.0.6",
"displayName": "Sequence Embedded Wallet SDK",
"description": "A Unity SDK for Sequence APIs",
"unity": "2021.3",
Expand Down