From 3ecd116db363cfe746554d65d495700b2e417716 Mon Sep 17 00:00:00 2001 From: Natalie Bunduwongse Date: Tue, 17 Dec 2024 17:24:18 +1300 Subject: [PATCH] refactor: remove unused imports, nullables, update readmes --- src/Packages/Marketplace/README.md | 72 ++------- src/Packages/Passport/README.md | 4 +- .../Private/Model/Request/InitRequest.cs | 1 - .../Runtime/Scripts/Private/PassportImpl.cs | 139 +++++++++--------- .../Runtime/Scripts/Public/Passport.cs | 4 +- 5 files changed, 82 insertions(+), 138 deletions(-) diff --git a/src/Packages/Marketplace/README.md b/src/Packages/Marketplace/README.md index c42ab0dd..37bd3f16 100644 --- a/src/Packages/Marketplace/README.md +++ b/src/Packages/Marketplace/README.md @@ -1,65 +1,17 @@ -# Immutable.Marketplace - C# library for Immutable X Marketplace +
+

+ + + +

+
-This C# library provides functionality for interacting with the Immutable X Marketplace, including on-ramp services. +--- -## Version Support +# Immutable Unity SDK - Marketplace -This library supports: +The Immutable SDK Commerce package for Unity simplifies integrating marketplace functionality, such as adding funds to a player's wallet. -- Unity 2020.3 (LTS) and up -- .NET Standard 2.1 / .NET Framework +## Documentation -## Dependencies - -- [UniTask](https://github.com/Cysharp/UniTask) - For asynchronous operations -- Unity Engine - -## Installation - -Add the dependencies to your Unity project. You can do this through the Package Manager or by adding them to your `Packages/manifest.json`: - -```json -{ - "dependencies": { - "com.cysharp.unitask": "https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask", - "com.unity.nuget.newtonsoft-json": "3.0.2" - } -} -``` - -## Usage - -Here's an example of how to use the OnRamp functionality with Immutable Passport: - -```csharp -using Immutable.Marketplace.OnRamp; -using Immutable.Passport; -using Immutable.Passport.Model; -using System.Collections.Generic; -using UnityEngine; - -public class MarketplaceExample : MonoBehaviour -{ - async void Start() - { - string environment = Environment.SANDBOX; - string email = await Passport.Instance.GetEmail(); - List walletAddresses = await Passport.Instance.ZkEvmRequestAccounts(); - - OnRamp onRamp = new OnRamp(environment, email, walletAddresses.FirstOrDefault()); - - try - { - string link = await onRamp.GetLink(); - Debug.Log($"onRamp.GetOnRampLink: {link}"); - - // Open the generated link in the default browser - Application.OpenURL(link); - } - catch (System.Exception e) - { - Debug.LogError($"Error getting on-ramp link: {e.Message}"); - } - } -} -``` \ No newline at end of file +- [Immutable zkEVM Documentation](https://docs.immutable.com/docs/zkEVM/sdks/unity) \ No newline at end of file diff --git a/src/Packages/Passport/README.md b/src/Packages/Passport/README.md index 39eb07e8..094e7cc3 100644 --- a/src/Packages/Passport/README.md +++ b/src/Packages/Passport/README.md @@ -8,9 +8,9 @@ --- -# Immutable Unity SDK +# Immutable Unity SDK - Passport -The Immutable SDK for Unity helps you integrate your game with Immutable Passport. +The Immutable SDK Passport package for Unity helps you integrate your game with Immutable Passport. # Documentation diff --git a/src/Packages/Passport/Runtime/Scripts/Private/Model/Request/InitRequest.cs b/src/Packages/Passport/Runtime/Scripts/Private/Model/Request/InitRequest.cs index 61ceb5ce..da20f9c7 100644 --- a/src/Packages/Passport/Runtime/Scripts/Private/Model/Request/InitRequest.cs +++ b/src/Packages/Passport/Runtime/Scripts/Private/Model/Request/InitRequest.cs @@ -1,5 +1,4 @@ using System; -using Immutable.Passport.Model; namespace Immutable.Passport.Model { diff --git a/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs b/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs index b7437ac7..890ded5f 100644 --- a/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs +++ b/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs @@ -22,16 +22,16 @@ public class PassportImpl { private const string TAG = "[Passport Implementation]"; public readonly IBrowserCommunicationsManager communicationsManager; - private PassportAnalytics analytics = new PassportAnalytics(); + private PassportAnalytics analytics = new(); // Used for device code auth - private DeviceConnectResponse deviceConnectResponse; + private DeviceConnectResponse? deviceConnectResponse; // Used for PKCE - private bool pkceLoginOnly = false; // Used to differentiate between a login and connect - private UniTaskCompletionSource pkceCompletionSource; - private string redirectUri = null; - private string logoutRedirectUri = null; + private bool pkceLoginOnly; // Used to differentiate between a login and connect + private UniTaskCompletionSource? pkceCompletionSource; + private string? redirectUri; + private string? logoutRedirectUri; #if UNITY_ANDROID // Used for the PKCE callback @@ -42,15 +42,15 @@ public class PassportImpl // Used to prevent calling login/connect functions multiple times private bool isLoggedIn = false; - public event OnAuthEventDelegate OnAuthEvent; + public event OnAuthEventDelegate? OnAuthEvent; public PassportImpl(IBrowserCommunicationsManager communicationsManager) { this.communicationsManager = communicationsManager; } - public async UniTask Init(string clientId, string environment, string redirectUri = null, - string logoutRedirectUri = null, string deeplink = null) + public async UniTask Init(string clientId, string environment, string? redirectUri = null, + string? logoutRedirectUri = null, string? deeplink = null) { this.redirectUri = redirectUri; this.logoutRedirectUri = logoutRedirectUri; @@ -85,7 +85,7 @@ public async UniTask Init(string clientId, string environment, string redirectUr } else { - InitRequest request = new InitRequest() + InitRequest request = new InitRequest { clientId = clientId, environment = environment, @@ -113,34 +113,32 @@ public async UniTask Init(string clientId, string environment, string redirectUr public async UniTask Login(bool useCachedSession = false, Nullable timeoutMs = null) { - string functionName = "Login"; if (useCachedSession) { return await Relogin(); } - else + + try { - try - { - Track(PassportAnalytics.EventName.START_LOGIN); - SendAuthEvent(PassportAuthEvent.LoggingIn); - - await InitialiseDeviceCodeAuth(functionName); - await ConfirmCode( - PassportAuthEvent.LoginOpeningBrowser, PassportAuthEvent.PendingBrowserLogin, functionName, - PassportFunction.LOGIN_CONFIRM_CODE, timeoutMs); - - Track(PassportAnalytics.EventName.COMPLETE_LOGIN, success: true); - SendAuthEvent(PassportAuthEvent.LoginSuccess); - isLoggedIn = true; - return true; - } - catch (Exception ex) - { - Track(PassportAnalytics.EventName.COMPLETE_LOGIN, success: false); - SendAuthEvent(PassportAuthEvent.LoginFailed); - throw ex; - } + const string functionName = "Login"; + Track(PassportAnalytics.EventName.START_LOGIN); + SendAuthEvent(PassportAuthEvent.LoggingIn); + + await InitialiseDeviceCodeAuth(functionName); + await ConfirmCode( + PassportAuthEvent.LoginOpeningBrowser, PassportAuthEvent.PendingBrowserLogin, functionName, + PassportFunction.LOGIN_CONFIRM_CODE, timeoutMs); + + Track(PassportAnalytics.EventName.COMPLETE_LOGIN, success: true); + SendAuthEvent(PassportAuthEvent.LoginSuccess); + isLoggedIn = true; + return true; + } + catch (Exception ex) + { + Track(PassportAnalytics.EventName.COMPLETE_LOGIN, success: false); + SendAuthEvent(PassportAuthEvent.LoginFailed); + throw ex; } } @@ -176,49 +174,47 @@ private async UniTask Relogin() return false; } - public async UniTask ConnectImx(bool useCachedSession = false, Nullable timeoutMs = null) + public async UniTask ConnectImx(bool useCachedSession = false, long? timeoutMs = null, int? width = null, int? height = null) { - string functionName = "ConnectImx"; if (useCachedSession) { return await Reconnect(); } - else + + // If the user called Login before and then ConnectImx, there is no point triggering device flow again + bool hasCredsSaved = await HasCredentialsSaved(); + if (hasCredsSaved) { - // If the user called Login before and then ConnectImx, there is no point triggering device flow again - bool hasCredsSaved = await HasCredentialsSaved(); - if (hasCredsSaved) + bool reconnected = await Reconnect(); + if (reconnected) { - bool reconnected = await Reconnect(); - if (reconnected) - { - // Successfully reconnected - return reconnected; - } - // Otherwise fallback to device code flow + // Successfully reconnected + return reconnected; } + // Otherwise fallback to device code flow + } - try - { - Track(PassportAnalytics.EventName.START_CONNECT_IMX); - SendAuthEvent(PassportAuthEvent.ConnectingImx); - - await InitialiseDeviceCodeAuth(functionName); - await ConfirmCode( - PassportAuthEvent.ConnectImxOpeningBrowser, PassportAuthEvent.PendingBrowserLoginAndProviderSetup, - functionName, PassportFunction.CONNECT_CONFIRM_CODE, timeoutMs); - - Track(PassportAnalytics.EventName.COMPLETE_CONNECT_IMX, success: true); - SendAuthEvent(PassportAuthEvent.ConnectImxSuccess); - isLoggedIn = true; - return true; - } - catch (Exception ex) - { - Track(PassportAnalytics.EventName.COMPLETE_CONNECT_IMX, success: false); - SendAuthEvent(PassportAuthEvent.ConnectImxFailed); - throw ex; - } + try + { + const string functionName = "ConnectImx"; + Track(PassportAnalytics.EventName.START_CONNECT_IMX); + SendAuthEvent(PassportAuthEvent.ConnectingImx); + + await InitialiseDeviceCodeAuth(functionName); + await ConfirmCode( + PassportAuthEvent.ConnectImxOpeningBrowser, PassportAuthEvent.PendingBrowserLoginAndProviderSetup, + functionName, PassportFunction.CONNECT_CONFIRM_CODE, timeoutMs); + + Track(PassportAnalytics.EventName.COMPLETE_CONNECT_IMX, success: true); + SendAuthEvent(PassportAuthEvent.ConnectImxSuccess); + isLoggedIn = true; + return true; + } + catch (Exception ex) + { + Track(PassportAnalytics.EventName.COMPLETE_CONNECT_IMX, success: false); + SendAuthEvent(PassportAuthEvent.ConnectImxFailed); + throw ex; } } @@ -274,7 +270,7 @@ private async UniTask InitialiseDeviceCodeAuth(string callingFu private async UniTask ConfirmCode( PassportAuthEvent openingBrowserAuthEvent, PassportAuthEvent pendingAuthEvent, - string callingFunction, string functionToCall, Nullable timeoutMs = null) + string callingFunction, string functionToCall, long? timeoutMs) { if (deviceConnectResponse != null) { @@ -553,9 +549,9 @@ public async UniTask Logout(bool hardLogout = true) { SendAuthEvent(PassportAuthEvent.LoggingOut); - string logoutUrl = await GetLogoutUrl(); if (hardLogout) { + var logoutUrl = await GetLogoutUrl(); OpenUrl(logoutUrl); } @@ -859,10 +855,7 @@ private void TrySetPKCECanceled() private void SendAuthEvent(PassportAuthEvent authEvent) { PassportLogger.Debug($"{TAG} Send auth event: {authEvent}"); - if (OnAuthEvent != null) - { - OnAuthEvent.Invoke(authEvent); - } + OnAuthEvent?.Invoke(authEvent); } protected virtual void OpenUrl(string url) diff --git a/src/Packages/Passport/Runtime/Scripts/Public/Passport.cs b/src/Packages/Passport/Runtime/Scripts/Public/Passport.cs index cdae9617..e5d28579 100644 --- a/src/Packages/Passport/Runtime/Scripts/Public/Passport.cs +++ b/src/Packages/Passport/Runtime/Scripts/Public/Passport.cs @@ -257,7 +257,7 @@ public void SetCallTimeout(int ms) /// /// Returns true if login is successful, otherwise false. /// - public async UniTask Login(bool useCachedSession = false, Nullable timeoutMs = null) + public async UniTask Login(bool useCachedSession = false, long? timeoutMs = null) { return await GetPassportImpl().Login(useCachedSession, timeoutMs); } @@ -268,7 +268,7 @@ public async UniTask Login(bool useCachedSession = false, Nullable t /// If true, the saved access token or refresh token will be used to connect the user. If this fails, it will not fallback to device code auth. /// (Optional) The maximum time, in milliseconds, the function is allowed to take before a TimeoutException is thrown. If not set, the function will wait indefinitely. /// - public async UniTask ConnectImx(bool useCachedSession = false, Nullable timeoutMs = null) + public async UniTask ConnectImx(bool useCachedSession = false, long? timeoutMs = null) { return await GetPassportImpl().ConnectImx(useCachedSession, timeoutMs); }