Skip to content

Commit

Permalink
refactor: remove unused imports, nullables, update readmes
Browse files Browse the repository at this point in the history
  • Loading branch information
nattb8 committed Dec 17, 2024
1 parent da1e4ed commit c8f23e8
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 138 deletions.
72 changes: 12 additions & 60 deletions src/Packages/Marketplace/README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,17 @@
# Immutable.Marketplace - C# library for Immutable X Marketplace
<div align="center">
<p align="center">
<a href="https://docs.x.immutable.com/docs">
<img src="https://cdn.dribbble.com/users/1299339/screenshots/7133657/media/837237d447d36581ebd59ec36d30daea.gif" width="280"/>
</a>
</p>
</div>

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<string> 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}");
}
}
}
```
- [Immutable zkEVM Documentation](https://docs.immutable.com/docs/zkEVM/sdks/unity)
4 changes: 2 additions & 2 deletions src/Packages/Passport/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Immutable.Passport.Model;

namespace Immutable.Passport.Model
{
Expand Down
139 changes: 66 additions & 73 deletions src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> pkceCompletionSource;
private string redirectUri = null;
private string logoutRedirectUri = null;
private bool pkceLoginOnly; // Used to differentiate between a login and connect
private UniTaskCompletionSource<bool>? pkceCompletionSource;
private string? redirectUri;
private string? logoutRedirectUri;

#if UNITY_ANDROID
// Used for the PKCE callback
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -113,34 +113,32 @@ public async UniTask Init(string clientId, string environment, string redirectUr

public async UniTask<bool> Login(bool useCachedSession = false, Nullable<long> 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;
}
}

Expand Down Expand Up @@ -176,49 +174,47 @@ private async UniTask<bool> Relogin()
return false;
}

public async UniTask<bool> ConnectImx(bool useCachedSession = false, Nullable<long> timeoutMs = null)
public async UniTask<bool> ConnectImx(bool useCachedSession = false, long? timeoutMs = 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;
}
}

Expand Down Expand Up @@ -274,7 +270,7 @@ private async UniTask<ConnectResponse> InitialiseDeviceCodeAuth(string callingFu

private async UniTask ConfirmCode(
PassportAuthEvent openingBrowserAuthEvent, PassportAuthEvent pendingAuthEvent,
string callingFunction, string functionToCall, Nullable<long> timeoutMs = null)
string callingFunction, string functionToCall, long? timeoutMs)
{
if (deviceConnectResponse != null)
{
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/Packages/Passport/Runtime/Scripts/Public/Passport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void SetCallTimeout(int ms)
/// <returns>
/// Returns true if login is successful, otherwise false.
/// </returns>
public async UniTask<bool> Login(bool useCachedSession = false, Nullable<long> timeoutMs = null)
public async UniTask<bool> Login(bool useCachedSession = false, long? timeoutMs = null)
{
return await GetPassportImpl().Login(useCachedSession, timeoutMs);
}
Expand All @@ -268,7 +268,7 @@ public async UniTask<bool> Login(bool useCachedSession = false, Nullable<long> t
/// <param name="useCachedSession">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.</param>
/// <param name="timeoutMs">(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.</param>
/// </summary>
public async UniTask<bool> ConnectImx(bool useCachedSession = false, Nullable<long> timeoutMs = null)
public async UniTask<bool> ConnectImx(bool useCachedSession = false, long? timeoutMs = null)
{
return await GetPassportImpl().ConnectImx(useCachedSession, timeoutMs);
}
Expand Down

0 comments on commit c8f23e8

Please sign in to comment.