From a4c73c52d1149629cec3cec366c7f55b55d9370a Mon Sep 17 00:00:00 2001 From: Quinn Purdy Date: Mon, 24 Feb 2025 16:26:23 -0500 Subject: [PATCH] Cache config objects in a dictionary so we can load multiple configs at once and target different envs for different services successfully --- .../WaaS/Tests/IntentSenderTests.cs | 2 +- .../SequenceSDK/Config/SequenceConfig.cs | 33 ++++++++++++------- .../SequenceSDK/EmbeddedWallet/HttpClient.cs | 2 +- .../EmbeddedWallet/SequenceLogin.cs | 4 +-- Packages/Sequence-Unity/package.json | 2 +- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/Assets/SequenceSDK/WaaS/Tests/IntentSenderTests.cs b/Assets/SequenceSDK/WaaS/Tests/IntentSenderTests.cs index ccb6d58c..c65b7ac6 100644 --- a/Assets/SequenceSDK/WaaS/Tests/IntentSenderTests.cs +++ b/Assets/SequenceSDK/WaaS/Tests/IntentSenderTests.cs @@ -138,7 +138,7 @@ public async Task TestGetTransactionReceipt_noWallet() public async Task TestTimeMismatchExceptionResultsInRetry(int timeOffset) { SequenceConfig config = SequenceConfig.GetConfig(SequenceService.WaaS); - ConfigJwt configJwt = SequenceConfig.GetConfigJwt(); + ConfigJwt configJwt = SequenceConfig.GetConfigJwt(config); IntentSender intentSender = new IntentSender(new HttpClient($"{configJwt.rpcServer.AppendTrailingSlashIfNeeded()}rpc/WaasAuthenticator"), new EOAWallet(), "", configJwt.projectId, config.WaaSVersion); LogAssert.Expect(LogType.Warning, new Regex("Time mismatch*")); try diff --git a/Packages/Sequence-Unity/Sequence/SequenceSDK/Config/SequenceConfig.cs b/Packages/Sequence-Unity/Sequence/SequenceSDK/Config/SequenceConfig.cs index c5403937..a819ac56 100644 --- a/Packages/Sequence-Unity/Sequence/SequenceSDK/Config/SequenceConfig.cs +++ b/Packages/Sequence-Unity/Sequence/SequenceSDK/Config/SequenceConfig.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using NUnit.Framework; using NUnit.Framework.Internal; using UnityEditor; @@ -33,13 +34,18 @@ public class SequenceConfig : ScriptableObject public bool StoreSessionPrivateKeyInSecureStorage = false; public bool EditorStoreSessionPrivateKeyInSecureStorage = false; - private static SequenceConfig _config; + private static Dictionary _configs = new Dictionary(); public static SequenceConfig GetConfig(SequenceService sequenceService = SequenceService.Unspecified) { - if (_config == null) + if (_configs.TryGetValue(sequenceService, out SequenceConfig cachedConfig)) { - _config = GetAppropriateConfig(sequenceService); - if (_config == null) + return cachedConfig; + } + else + { + _configs[sequenceService] = GetAppropriateConfig(sequenceService); + SequenceConfig config = _configs[sequenceService]; + if (config == null) { throw new Exception("SequenceConfig not found. Make sure to create and configure it and place it at the root of your Resources folder. Create it from the top bar with Assets > Create > Sequence > SequenceConfig"); } @@ -47,19 +53,18 @@ public static SequenceConfig GetConfig(SequenceService sequenceService = Sequenc TextAsset versionFile = Resources.Load("sequence-unity-version"); if (versionFile != null) { - _config.WaaSVersion = $"1 (Unity {versionFile.text})"; + config.WaaSVersion = $"1 (Unity {versionFile.text})"; } else { - _config.WaaSVersion = $"1 (Unity {PackageVersionReader.GetVersion()})"; + config.WaaSVersion = $"1 (Unity {PackageVersionReader.GetVersion()})"; } #if UNITY_EDITOR - _config.WaaSVersion = $"1 (Unity {PackageVersionReader.GetVersion()})"; // version file is only updated when building + config.WaaSVersion = $"1 (Unity {PackageVersionReader.GetVersion()})"; // version file is only updated when building #endif + return config; } - - return _config; } private static SequenceConfig GetAppropriateConfig(SequenceService sequenceService) @@ -140,9 +145,15 @@ public static Exception MissingConfigError(string valueName) return new Exception($"{valueName} is not set. Please set it in SequenceConfig asset in your Resources folder."); } - public static ConfigJwt GetConfigJwt() + public static ConfigJwt GetConfigJwt(SequenceService service = SequenceService.Unspecified) + { + SequenceConfig config = GetConfig(service); + return GetConfigJwt(config); + } + + public static ConfigJwt GetConfigJwt(SequenceConfig config) { - string configKey = _config.WaaSConfigKey; + string configKey = config.WaaSConfigKey; if (string.IsNullOrWhiteSpace(configKey)) { throw SequenceConfig.MissingConfigError("WaaS Config Key"); diff --git a/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/HttpClient.cs b/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/HttpClient.cs index 948355f8..6c976200 100644 --- a/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/HttpClient.cs +++ b/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/HttpClient.cs @@ -46,7 +46,7 @@ public HttpClient(string url) throw SequenceConfig.MissingConfigError("Builder API Key"); } - ConfigJwt configJwt = SequenceConfig.GetConfigJwt(); + ConfigJwt configJwt = SequenceConfig.GetConfigJwt(config); string rpcUrl = configJwt.rpcServer; if (string.IsNullOrWhiteSpace(rpcUrl)) { diff --git a/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs b/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs index a0d8da17..0b7e12e7 100644 --- a/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs +++ b/Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs @@ -128,7 +128,7 @@ public void ResetLoginAfterTest() public void SetupAuthenticator(IValidator validator = null, IAuthenticator authenticator = null) { - ConfigJwt configJwt = SequenceConfig.GetConfigJwt(); + ConfigJwt configJwt = SequenceConfig.GetConfigJwt(SequenceConfig.GetConfig(SequenceService.WaaS)); if (_connectedWalletAddress == null || _sessionWallet == null) { _sessionWallet = new EOAWallet(); @@ -198,7 +198,7 @@ private void Configure() } _waasVersion = waasVersion; - ConfigJwt configJwt = SequenceConfig.GetConfigJwt(); + ConfigJwt configJwt = SequenceConfig.GetConfigJwt(config); string rpcUrl = configJwt.rpcServer; if (string.IsNullOrWhiteSpace(rpcUrl)) diff --git a/Packages/Sequence-Unity/package.json b/Packages/Sequence-Unity/package.json index 489d6fc6..fb1a1647 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.3", "displayName": "Sequence Embedded Wallet SDK", "description": "A Unity SDK for the Sequence WaaS API", "unity": "2021.3",