From e4a056b114796bf9493ce9423a8379a6167319c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andy=20Gru=CC=88ning?= Date: Wed, 26 Feb 2025 16:32:25 +0100 Subject: [PATCH 1/6] added SequenceLogin.TryToRecoverSessionAsync() --- .../EmbeddedWallet/SequenceLogin.cs | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs b/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs index a0d8da17..d66e7d62 100644 --- a/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs +++ b/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs @@ -155,6 +155,45 @@ public void SetupAuthenticator(IValidator validator = null, IAuthenticator authe _emailConnector = new EmailConnector(_sessionId, _sessionWallet, _connector, _validator); } + /// + /// Recover the current session asynchronously and get the associated wallet. + /// + /// Instance of IWallet if the session was recovered. Returns null if no session was found. + public async Task TryToRestoreSessionAsync() + { + var config = SequenceConfig.GetConfig(); + var storeSessionInfoAndSkipLoginWhenPossible = config.StoreSessionKey(); + if (!SecureStorageFactory.IsSupportedPlatform() || !storeSessionInfoAndSkipLoginWhenPossible) + return null; + + var done = false; + IWallet wallet = null; + SequenceWallet.OnFailedToRecoverSession += HandleFailedToRecover; + SequenceWallet.OnWalletCreated += HandleRecoveredWallet; + + TryToRestoreSession(); + SetupAuthenticator(); + + while (!done) + await Task.Yield(); + + return wallet; + + void HandleRecoveredWallet(IWallet newWallet) + { + wallet = newWallet; + done = true; + + SequenceWallet.OnFailedToRecoverSession -= HandleFailedToRecover; + SequenceWallet.OnWalletCreated -= HandleRecoveredWallet; + } + + void HandleFailedToRecover(string error) + { + HandleRecoveredWallet(null); + } + } + public void TryToRestoreSession() { if (!_storeSessionWallet) From c83f7b9b9182f5b14a31728fb769f33371ece037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andy=20Gru=CC=88ning?= Date: Tue, 4 Mar 2025 11:53:31 +0100 Subject: [PATCH 2/6] Added GetNativeTokenBalance, GetEtherBalance marked obsolote --- .../Sequence/SequenceSDK/Indexer/ChainIndexer.cs | 8 +++++++- .../Sequence/SequenceSDK/Indexer/IIndexer.cs | 3 +++ .../Sequence/SequenceSDK/Indexer/Indexer.cs | 4 ++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/ChainIndexer.cs b/Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/ChainIndexer.cs index 844c9b0b..2162f861 100644 --- a/Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/ChainIndexer.cs +++ b/Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/ChainIndexer.cs @@ -89,10 +89,16 @@ public Chain GetChain() { return ChainDictionaries.ChainById[ChainId]; } + + public Task GetNativeTokenBalance(string accountAddress) + { + return Indexer.GetNativeTokenBalance(ChainId, accountAddress, 0, _customHttpHandler, this); + } + [Obsolete("Call GetNativeTokenBalance instead.")] public Task GetEtherBalance(string accountAddress) { - return Indexer.GetEtherBalance(ChainId, accountAddress, 0, _customHttpHandler, this); + return Indexer.GetNativeTokenBalance(ChainId, accountAddress, 0, _customHttpHandler, this); } public Task GetTokenBalances(GetTokenBalancesArgs args) diff --git a/Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/IIndexer.cs b/Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/IIndexer.cs index 7f947718..6e9809be 100644 --- a/Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/IIndexer.cs +++ b/Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/IIndexer.cs @@ -56,6 +56,9 @@ public interface IIndexer /// Retrieve the balance of a network's native token for a given account address /// /// If the network request fails + public Task GetNativeTokenBalance(string accountAddress); + + [Obsolete("Call GetNativeTokenBalance instead.")] public Task GetEtherBalance(string accountAddress); /// diff --git a/Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/Indexer.cs b/Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/Indexer.cs index 49ff44fc..2feb6b7c 100644 --- a/Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/Indexer.cs +++ b/Packages/Sequence-Unity/Sequence/SequenceSDK/Indexer/Indexer.cs @@ -230,14 +230,14 @@ public static async Task GetChainID(BigInteger chainID) [Obsolete] public static async Task GetEtherBalance(BigInteger chainID, string accountAddress) { - return await GetEtherBalance(chainID.ToString(), accountAddress); + return await GetNativeTokenBalance(chainID.ToString(), accountAddress); } /// /// Retrieve the balance of a network's native token for a given account address /// /// If the network request fails - public static async Task GetEtherBalance(string chainID, string accountAddress, int retries = 0, IHttpHandler httpHandler = null, IIndexer caller = null) + public static async Task 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(responseBody, caller); From d016c6272bfb3bf05e3313cc460c51f96745ee1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andy=20Gru=CC=88ning?= Date: Tue, 4 Mar 2025 12:09:04 +0100 Subject: [PATCH 3/6] version bump, added TryRecoverSessionAsync to BoilerplateController --- .../Scripts/Common/BoilerplateController.cs | 81 +++++++++---------- .../EmbeddedWallet/SequenceLogin.cs | 6 +- Packages/Sequence-Unity/package.json | 2 +- 3 files changed, 42 insertions(+), 47 deletions(-) diff --git a/Packages/Sequence-Unity/Sequence/SequenceBoilerplates/Scripts/Common/BoilerplateController.cs b/Packages/Sequence-Unity/Sequence/SequenceBoilerplates/Scripts/Common/BoilerplateController.cs index 79b9f23f..ab3d3140 100644 --- a/Packages/Sequence-Unity/Sequence/SequenceBoilerplates/Scripts/Common/BoilerplateController.cs +++ b/Packages/Sequence-Unity/Sequence/SequenceBoilerplates/Scripts/Common/BoilerplateController.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using Sequence.Boilerplates.Login; using Sequence.Boilerplates.PlayerProfile; using Sequence.Config; @@ -20,41 +21,19 @@ public class BoilerplateController : MonoBehaviour [Header("Components")] [SerializeField] private GameObject _featureSelection; - private IWallet _wallet; + private SequenceWallet _wallet; private SequenceLoginWindow _loginWindow; private SequencePlayerProfile _playerProfile; - - private void Awake() - { - SequenceWallet.OnFailedToRecoverSession += OnFailedToRecoverSession; - SequenceWallet.OnWalletCreated += wallet => - { - _wallet = wallet; - ShowFeatureSelection(); - - if (_loginWindow) - _loginWindow.Hide(); - - wallet.OnDropSessionComplete += s => - { - if (s == wallet.SessionId) - { - if (_playerProfile) - _playerProfile.Hide(); - - TryRecoverSessionToOpenLoginWindow(); - } - }; - }; - } - private void Start() + private async void Start() { - TryRecoverSessionToOpenLoginWindow(); + SequenceWallet.OnWalletCreated += HandleWalletCreated; + await TryRecoverSessionToOpenLoginWindow(); } private void OnDestroy() { + SequenceWallet.OnWalletCreated -= HandleWalletCreated; BoilerplateFactory.CleanUp(); } @@ -99,28 +78,44 @@ private void HideFeatureSelection() _featureSelection.SetActive(false); } - private void TryRecoverSessionToOpenLoginWindow() + /// + /// This function is called at the start of the scene and whenever the user logged out. + /// + private async Task 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 login = SequenceLogin.GetInstance(); + var wallet = await login.TryToRestoreSessionAsync(); + if (wallet != null) + return; + + Debug.LogError("Failed to recover session. Please login."); + _loginWindow = BoilerplateFactory.OpenSequenceLoginWindow(transform); } - private void OnFailedToRecoverSession(string error) + /// + /// This function is called after the login or session recovery succeeded. + /// + /// Instance of SequenceWallet + private void HandleWalletCreated(SequenceWallet wallet) { - Debug.LogError($"Error attempting to recover Sequence session: {error}"); - _loginWindow = BoilerplateFactory.OpenSequenceLoginWindow(transform); + _wallet = wallet; + ShowFeatureSelection(); + + if (_loginWindow) + _loginWindow.Hide(); + + wallet.OnDropSessionComplete += async s => + { + if (s == wallet.SessionId) + { + if (_playerProfile) + _playerProfile.Hide(); + + await TryRecoverSessionToOpenLoginWindow(); + } + }; } } } \ No newline at end of file diff --git a/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs b/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs index d66e7d62..b58cbb68 100644 --- a/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs +++ b/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs @@ -159,7 +159,7 @@ public void SetupAuthenticator(IValidator validator = null, IAuthenticator authe /// Recover the current session asynchronously and get the associated wallet. /// /// Instance of IWallet if the session was recovered. Returns null if no session was found. - public async Task TryToRestoreSessionAsync() + public async Task TryToRestoreSessionAsync() { var config = SequenceConfig.GetConfig(); var storeSessionInfoAndSkipLoginWhenPossible = config.StoreSessionKey(); @@ -167,7 +167,7 @@ public async Task TryToRestoreSessionAsync() return null; var done = false; - IWallet wallet = null; + SequenceWallet wallet = null; SequenceWallet.OnFailedToRecoverSession += HandleFailedToRecover; SequenceWallet.OnWalletCreated += HandleRecoveredWallet; @@ -179,7 +179,7 @@ public async Task TryToRestoreSessionAsync() return wallet; - void HandleRecoveredWallet(IWallet newWallet) + void HandleRecoveredWallet(SequenceWallet newWallet) { wallet = newWallet; done = true; diff --git a/Packages/Sequence-Unity/package.json b/Packages/Sequence-Unity/package.json index 489d6fc6..87096279 100644 --- a/Packages/Sequence-Unity/package.json +++ b/Packages/Sequence-Unity/package.json @@ -1,6 +1,6 @@ { "name": "xyz.0xsequence.waas-unity", - "version": "3.19.2", + "version": "3.19.5", "displayName": "Sequence Embedded Wallet SDK", "description": "A Unity SDK for the Sequence WaaS API", "unity": "2021.3", From dd564470643be7e81881b28bd25cfcd7dbc02f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andy=20Gru=CC=88ning?= Date: Tue, 4 Mar 2025 12:11:07 +0100 Subject: [PATCH 4/6] version bump --- Packages/Sequence-Unity/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Packages/Sequence-Unity/package.json b/Packages/Sequence-Unity/package.json index 9aeffb28..87096279 100644 --- a/Packages/Sequence-Unity/package.json +++ b/Packages/Sequence-Unity/package.json @@ -1,6 +1,6 @@ { "name": "xyz.0xsequence.waas-unity", - "version": "3.19.4", + "version": "3.19.5", "displayName": "Sequence Embedded Wallet SDK", "description": "A Unity SDK for the Sequence WaaS API", "unity": "2021.3", From a90eef9f729b623d3aa06f6646b16c03a4c3dd5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andy=20Gru=CC=88ning?= Date: Fri, 2 May 2025 14:27:41 +0200 Subject: [PATCH 5/6] TryToRestoreSessionAsync returns bool StorageEnabled, fixed post-merge conflicts --- .../Mocks/MockIndexerReturnsCached.cs | 5 +++++ .../Mocks/MockIndexerReturnsNull.cs | 5 +++++ .../Mocks/MockIndexerReturnsProvidedValue.cs | 5 +++++ .../Mocks/MockIndexerWrongChain.cs | 5 +++++ .../Common/BoilerplateController.cs | 20 +++++-------------- .../EmbeddedWallet/SequenceLogin.cs | 11 ++++++---- 6 files changed, 32 insertions(+), 19 deletions(-) diff --git a/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsCached.cs b/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsCached.cs index a1eccf4b..ddd4716c 100644 --- a/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsCached.cs +++ b/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsCached.cs @@ -48,6 +48,11 @@ public Chain GetChain() throw new System.NotImplementedException(); } + public Task GetNativeTokenBalance(string accountAddress) + { + throw new System.NotImplementedException(); + } + public Task GetEtherBalance(string accountAddress) { throw new System.NotImplementedException(); diff --git a/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsNull.cs b/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsNull.cs index 2265dca7..d1976298 100644 --- a/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsNull.cs +++ b/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsNull.cs @@ -45,6 +45,11 @@ public Chain GetChain() throw new System.NotImplementedException(); } + public Task GetNativeTokenBalance(string accountAddress) + { + throw new System.NotImplementedException(); + } + public Task GetEtherBalance(string accountAddress) { throw new System.NotImplementedException(); diff --git a/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsProvidedValue.cs b/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsProvidedValue.cs index 4fd45efd..3617062c 100644 --- a/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsProvidedValue.cs +++ b/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerReturnsProvidedValue.cs @@ -49,6 +49,11 @@ public Chain GetChain() throw new System.NotImplementedException(); } + public Task GetNativeTokenBalance(string accountAddress) + { + return GetEtherBalance(accountAddress); + } + public async Task GetEtherBalance(string accountAddress) { return new EtherBalance() diff --git a/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerWrongChain.cs b/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerWrongChain.cs index 6c707f97..6e4ddd07 100644 --- a/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerWrongChain.cs +++ b/Assets/SequenceSDK/Marketplace/Mocks/MockIndexerWrongChain.cs @@ -41,6 +41,11 @@ public Chain GetChain() return Chain.None; } + public Task GetNativeTokenBalance(string accountAddress) + { + throw new System.NotImplementedException(); + } + public Task GetEtherBalance(string accountAddress) { throw new System.NotImplementedException(); diff --git a/Packages/Sequence-Unity/Sequence/SequenceFrontend/Scripts/UI/Boilerplates/Common/BoilerplateController.cs b/Packages/Sequence-Unity/Sequence/SequenceFrontend/Scripts/UI/Boilerplates/Common/BoilerplateController.cs index 8dd8bee5..8a48d579 100644 --- a/Packages/Sequence-Unity/Sequence/SequenceFrontend/Scripts/UI/Boilerplates/Common/BoilerplateController.cs +++ b/Packages/Sequence-Unity/Sequence/SequenceFrontend/Scripts/UI/Boilerplates/Common/BoilerplateController.cs @@ -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 @@ -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) diff --git a/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs b/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs index 74016f7a..0af9c374 100644 --- a/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs +++ b/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs @@ -160,13 +160,16 @@ public void SetupAuthenticator(IValidator validator = null, IAuthenticator authe /// /// Recover the current session asynchronously and get the associated wallet. /// - /// Instance of IWallet if the session was recovered. Returns null if no session was found. - public async Task TryToRestoreSessionAsync() + /// + /// 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. + /// + public async Task<(bool StorageEnabled, IWallet Wallet)> TryToRestoreSessionAsync() { var config = SequenceConfig.GetConfig(); var storeSessionInfoAndSkipLoginWhenPossible = config.StoreSessionKey(); if (!SecureStorageFactory.IsSupportedPlatform() || !storeSessionInfoAndSkipLoginWhenPossible) - return null; + return (false, null); var done = false; SequenceWallet wallet = null; @@ -179,7 +182,7 @@ public async Task TryToRestoreSessionAsync() while (!done) await Task.Yield(); - return wallet; + return (true, wallet); void HandleRecoveredWallet(SequenceWallet newWallet) { From 57d5efd9c60cdcde764db2b79b9465c5ba64fda7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andy=20Gr=C3=BCning?= Date: Fri, 2 May 2025 15:22:54 +0200 Subject: [PATCH 6/6] Update BoilerplateController.cs --- .../Scripts/UI/Boilerplates/Common/BoilerplateController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Packages/Sequence-Unity/Sequence/SequenceFrontend/Scripts/UI/Boilerplates/Common/BoilerplateController.cs b/Packages/Sequence-Unity/Sequence/SequenceFrontend/Scripts/UI/Boilerplates/Common/BoilerplateController.cs index 8a48d579..9b0da63c 100644 --- a/Packages/Sequence-Unity/Sequence/SequenceFrontend/Scripts/UI/Boilerplates/Common/BoilerplateController.cs +++ b/Packages/Sequence-Unity/Sequence/SequenceFrontend/Scripts/UI/Boilerplates/Common/BoilerplateController.cs @@ -102,7 +102,7 @@ private async void TryRecoverSessionToOpenLoginWindow() var loginHandler = SequenceLogin.GetInstance(); var (storageEnabled, wallet) = await loginHandler.TryToRestoreSessionAsync(); - if (!storageEnabled && wallet == null) + if (!storageEnabled) OnFailedToRecoverSession("Secure storage is disabled"); } @@ -297,4 +297,4 @@ private async void DoShowCheckoutPanel() new SequenceCheckout(_wallet, Chain.Polygon, saleContract, collection, "1", 1), ShowDefaultWindow); } } -} \ No newline at end of file +}