diff --git a/.gitignore b/.gitignore index 288fbd33..3d498f05 100644 --- a/.gitignore +++ b/.gitignore @@ -65,6 +65,7 @@ sysinfo.txt *.aab *.unitypackage *.app +webglbuild/ # Crashlytics generated file crashlytics-build.properties diff --git a/Assets/Thirdweb/Runtime/NET/Thirdweb.dll b/Assets/Thirdweb/Runtime/NET/Thirdweb.dll index 98e61181..a9080bda 100755 Binary files a/Assets/Thirdweb/Runtime/NET/Thirdweb.dll and b/Assets/Thirdweb/Runtime/NET/Thirdweb.dll differ diff --git a/Assets/Thirdweb/Runtime/Unity/ThirdwebManagerBase.cs b/Assets/Thirdweb/Runtime/Unity/ThirdwebManagerBase.cs index 8dbf3ff3..d65376c7 100755 --- a/Assets/Thirdweb/Runtime/Unity/ThirdwebManagerBase.cs +++ b/Assets/Thirdweb/Runtime/Unity/ThirdwebManagerBase.cs @@ -225,7 +225,7 @@ public abstract class ThirdwebManagerBase : MonoBehaviour public static ThirdwebManagerBase Instance { get; protected set; } - public static readonly string THIRDWEB_UNITY_SDK_VERSION = "5.17.2"; + public static readonly string THIRDWEB_UNITY_SDK_VERSION = "5.18.2"; protected const string THIRDWEB_AUTO_CONNECT_OPTIONS_KEY = "ThirdwebAutoConnectOptions"; diff --git a/Assets/Treasure/Example/Scenes/TDKHarness.unity b/Assets/Treasure/Example/Scenes/TDKHarness.unity index e738108a..f66e2afa 100644 --- a/Assets/Treasure/Example/Scenes/TDKHarness.unity +++ b/Assets/Treasure/Example/Scenes/TDKHarness.unity @@ -4741,6 +4741,139 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 843119459} m_CullTransparentMesh: 0 +--- !u!1 &920250282 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 920250283} + - component: {fileID: 920250286} + - component: {fileID: 920250285} + - component: {fileID: 920250284} + m_Layer: 5 + m_Name: Btn_WebGLExternalReconnect + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &920250283 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 920250282} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: + - {fileID: 1955608646} + m_Father: {fileID: 2119049937} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 1} + m_AnchorMax: {x: 0, y: 1} + m_AnchoredPosition: {x: 267.5, y: -50} + m_SizeDelta: {x: 175, y: 100} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &920250284 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 920250282} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Navigation: + m_Mode: 3 + m_WrapAround: 0 + m_SelectOnUp: {fileID: 0} + m_SelectOnDown: {fileID: 0} + m_SelectOnLeft: {fileID: 0} + m_SelectOnRight: {fileID: 0} + m_Transition: 1 + m_Colors: + m_NormalColor: {r: 1, g: 1, b: 1, a: 1} + m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1} + m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1} + m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1} + m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608} + m_ColorMultiplier: 1 + m_FadeDuration: 0.1 + m_SpriteState: + m_HighlightedSprite: {fileID: 0} + m_PressedSprite: {fileID: 0} + m_SelectedSprite: {fileID: 0} + m_DisabledSprite: {fileID: 0} + m_AnimationTriggers: + m_NormalTrigger: Normal + m_HighlightedTrigger: Highlighted + m_PressedTrigger: Pressed + m_SelectedTrigger: Selected + m_DisabledTrigger: Disabled + m_Interactable: 1 + m_TargetGraphic: {fileID: 920250285} + m_OnClick: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 2119049941} + m_TargetAssemblyTypeName: ConnectUI, Assembly-CSharp + m_MethodName: OnWebGLExternalReconnectBtn + m_Mode: 1 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 1 + m_CallState: 2 +--- !u!114 &920250285 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 920250282} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0} + m_Type: 1 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 + m_UseSpriteMesh: 0 + m_PixelsPerUnitMultiplier: 1 +--- !u!222 &920250286 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 920250282} + m_CullTransparentMesh: 0 --- !u!1 &948334912 GameObject: m_ObjectHideFlags: 0 @@ -7910,6 +8043,85 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1932496921} m_CullTransparentMesh: 0 +--- !u!1 &1955608645 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1955608646} + - component: {fileID: 1955608648} + - component: {fileID: 1955608647} + m_Layer: 5 + m_Name: Text + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &1955608646 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1955608645} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 920250283} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 1, y: 1} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 0, y: 0} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &1955608647 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1955608645} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_FontData: + m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0} + m_FontSize: 20 + m_FontStyle: 0 + m_BestFit: 0 + m_MinSize: 2 + m_MaxSize: 40 + m_Alignment: 4 + m_AlignByGeometry: 0 + m_RichText: 1 + m_HorizontalOverflow: 0 + m_VerticalOverflow: 0 + m_LineSpacing: 1 + m_Text: External reconnect (WebGL build) +--- !u!222 &1955608648 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1955608645} + m_CullTransparentMesh: 0 --- !u!1 &1969444263 GameObject: m_ObjectHideFlags: 0 @@ -8789,6 +9001,7 @@ RectTransform: m_Children: - {fileID: 1426026311} - {fileID: 1354350607} + - {fileID: 920250283} m_Father: {fileID: 1932496922} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} @@ -8841,6 +9054,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: dropdownDialogPrefab: {fileID: 3793558193243429674, guid: 5d64c9eae83be994eacd6780a6a8902d, type: 3} + webGLExternalReconnectButton: {fileID: 920250284} --- !u!1001 &2785764880306549791 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/Assets/Treasure/Example/Scripts/ConnectUI.cs b/Assets/Treasure/Example/Scripts/ConnectUI.cs index 4cc9599b..e2f5b58f 100644 --- a/Assets/Treasure/Example/Scripts/ConnectUI.cs +++ b/Assets/Treasure/Example/Scripts/ConnectUI.cs @@ -1,10 +1,12 @@ using UnityEngine; using Treasure; using System.Collections.Generic; +using UnityEngine.UI; public class ConnectUI : MonoBehaviour { [SerializeField] private DropDownPopUp dropdownDialogPrefab; + [SerializeField] private Button webGLExternalReconnectButton; private List _chainIdentifiers = new List { "arbitrum", @@ -25,6 +27,32 @@ public class ConnectUI : MonoBehaviour ChainId.TreasureTopaz }; + void Start() + { + if (TDKWebConnectInterface.IsActive()) + { + webGLExternalReconnectButton.interactable = true; + if (TDKWebConnectInterface.BrowserHasActiveWalletConnection()) + { + TDKLogger.LogInfo("ConnectUI: an active wallet connection has been detected in the browser"); + } + else + { + TDKWebConnectInterface.BrowserWalletConnectedAction += () => + { + if (TDKWebConnectInterface.BrowserHasActiveWalletConnection()) + { + TDKLogger.LogInfo("ConnectUI: an active wallet connection has been detected in the browser"); + } + }; + } + } + else + { + webGLExternalReconnectButton.interactable = false; + } + } + public void OnConnectWalletBtn() { TDK.Connect.ShowConnectModal(); @@ -40,4 +68,32 @@ public async void OnChainDropdownSubmit(int value) { await TDK.Connect.SetChainId(_chainIds[value]); } + + public async void OnWebGLExternalReconnectBtn() + { + var thirdwebService = TDKServiceLocator.GetService(); + var isWalletConnected = await thirdwebService.IsWalletConnected(); + if (isWalletConnected) + { + TDKLogger.LogInfo("Wallet already connected, aborting."); + return; + } + if (TDKWebConnectInterface.BrowserHasActiveWalletConnection()) + { + TDKLogger.LogInfo("Requesting auth data from the browser..."); + TDKWebConnectInterface.AttemptReconnect(); + } + else + { + var browserConnectionState = TDKWebConnectInterface.GetBrowserConnectionState(); + if (browserConnectionState == TDKWebConnectInterface.BrowserConnectionState.Disconnected) + { + TDKLogger.LogInfo("Invalid request: no active connection found in the browser"); + } + else if (browserConnectionState == TDKWebConnectInterface.BrowserConnectionState.NewConnection) + { + TDKLogger.LogInfo("Invalid request: a new connection attempt was started"); + } + } + } } diff --git a/Assets/Treasure/TDK/JSPlugins.meta b/Assets/Treasure/TDK/JSPlugins.meta new file mode 100644 index 00000000..5b2b51f0 --- /dev/null +++ b/Assets/Treasure/TDK/JSPlugins.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 7e8217f8ef8c749d4a45c70e4a2e7600 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Treasure/TDK/JSPlugins/openConnectModal.jslib b/Assets/Treasure/TDK/JSPlugins/openConnectModal.jslib new file mode 100644 index 00000000..a9b08de9 --- /dev/null +++ b/Assets/Treasure/TDK/JSPlugins/openConnectModal.jslib @@ -0,0 +1,23 @@ +mergeInto(LibraryManager.library, { + + WebGLNotifyReady: function () { + window.dispatchEvent(new CustomEvent("tdkReady")) + }, + + WebGLOpenConnectModal: function () { + window.dispatchEvent(new CustomEvent("tdkOpenConnectModal")) + }, + + WebGLOpenWalletConnectModal: function () { + window.dispatchEvent(new CustomEvent("tdkOpenWalletConnectModal")) + }, + + WebGLLogOut: function () { + window.dispatchEvent(new CustomEvent("tdkLogOut")) + }, + + WebGLRequestReconnect: function () { + window.dispatchEvent(new CustomEvent("tdkRequestReconnect")) + }, + +}); \ No newline at end of file diff --git a/Assets/Treasure/TDK/JSPlugins/openConnectModal.jslib.meta b/Assets/Treasure/TDK/JSPlugins/openConnectModal.jslib.meta new file mode 100644 index 00000000..f7617fd2 --- /dev/null +++ b/Assets/Treasure/TDK/JSPlugins/openConnectModal.jslib.meta @@ -0,0 +1,32 @@ +fileFormatVersion: 2 +guid: 365f9fdb4868f4abf8d99b8f70a4f98e +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 0 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + - first: + WebGL: WebGL + second: + enabled: 1 + settings: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Treasure/TDK/Runtime/Connect/Modals/LoginModal.cs b/Assets/Treasure/TDK/Runtime/Connect/Modals/LoginModal.cs index a6f56221..293fcabc 100644 --- a/Assets/Treasure/TDK/Runtime/Connect/Modals/LoginModal.cs +++ b/Assets/Treasure/TDK/Runtime/Connect/Modals/LoginModal.cs @@ -131,6 +131,11 @@ private async void ConnectExternalWallet() socialsErrorText.gameObject.SetActive(true); return; } + if (TDKWebConnectInterface.IsActive() && TDK.AppConfig.ConnectModalMode == TDKConfig.ConnectUIModalMode.WebGLExternalForWalletLoginOnly) + { + TDKWebConnectInterface.OpenConnectModal(); + return; + } var thirdwebService = TDKServiceLocator.GetService(); thirdwebService.EnsureWalletConnectInitialized(); diff --git a/Assets/Treasure/TDK/Runtime/Connect/TDK.Connect.cs b/Assets/Treasure/TDK/Runtime/Connect/TDK.Connect.cs index 9d9aef96..d044a9ed 100644 --- a/Assets/Treasure/TDK/Runtime/Connect/TDK.Connect.cs +++ b/Assets/Treasure/TDK/Runtime/Connect/TDK.Connect.cs @@ -206,6 +206,8 @@ public async Task ConnectExternalWallet() public async Task ConnectViaCookie(string authCookie, AuthProvider authProvider, string email = null) { + TDKLogger.LogDebug($"[TDK.Connect:ConnectViaCookie] Connecting via auth cookie (provider: {authProvider})"); + var thirdwebService = TDKServiceLocator.GetService(); var ecosystemWalletOptions = new EcosystemWalletOptions(authprovider: authProvider, email: email); @@ -218,12 +220,19 @@ await thirdwebService.ConnectWallet( if (await thirdwebService.IsWalletConnected()) { await UpdateConnectInfo(ChainId); - TDKLogger.LogDebug($"[TDK.Connect:ConnectViaLauncherCookie] Connection success!"); + TDKLogger.LogDebug($"[TDK.Connect:ConnectViaCookie] Connection success!"); return true; } + TDKLogger.LogDebug($"[TDK.Connect:ConnectViaCookie] Connection failed"); return false; } + public string GetStoredAuthCookie() + { + var thirdwebService = TDKServiceLocator.GetService(); + return thirdwebService.GetStoredAuthCookie(); + } + public async Task Reconnect(string email) { TDKLogger.LogDebug($"[TDK.Connect:Reconnect] Reconnecting email ({email})..."); @@ -241,6 +250,10 @@ public async Task Disconnect() var thirdwebService = TDKServiceLocator.GetService(); await thirdwebService.DisconnectWallet(); OnDisconnected?.Invoke(); + if (TDKWebConnectInterface.IsActive()) + { + TDKWebConnectInterface.LogOut(); + } _address = null; TDK.Analytics.TrackCustomEvent(AnalyticsConstants.EVT_TREASURECONNECT_DISCONNECTED); } diff --git a/Assets/Treasure/TDK/Runtime/Connect/UI/TDKConnectUIManager.cs b/Assets/Treasure/TDK/Runtime/Connect/UI/TDKConnectUIManager.cs index c35f2dec..1505a1eb 100644 --- a/Assets/Treasure/TDK/Runtime/Connect/UI/TDKConnectUIManager.cs +++ b/Assets/Treasure/TDK/Runtime/Connect/UI/TDKConnectUIManager.cs @@ -68,6 +68,11 @@ IEnumerator SwitchScene() public void ShowLoginModal() { Activate(); + if (TDKWebConnectInterface.IsActive() && TDK.AppConfig.ConnectModalMode == TDKConfig.ConnectUIModalMode.WebGLExternal) + { + TDKWebConnectInterface.OpenConnectModal(); + return; + } if (currentModalOpended != null) currentModalOpended.Hide(); @@ -145,7 +150,11 @@ public void Hide() public void LogOut() { accountModal.Hide(); - + if (TDKWebConnectInterface.IsActive() && TDK.AppConfig.ConnectModalMode == TDKConfig.ConnectUIModalMode.WebGLExternal) + { + Hide(); + return; + } loginModal.Show(); currentModalOpended = loginModal; } diff --git a/Assets/Treasure/TDK/Runtime/Identity/TDK.Identity.cs b/Assets/Treasure/TDK/Runtime/Identity/TDK.Identity.cs index 856e1037..01702972 100644 --- a/Assets/Treasure/TDK/Runtime/Identity/TDK.Identity.cs +++ b/Assets/Treasure/TDK/Runtime/Identity/TDK.Identity.cs @@ -376,7 +376,6 @@ public async Task AttemptConnectionViaLauncherAuth() { return; } - TDKLogger.LogDebug($"Connecting via auth cookie (provider: {launcherAuthProvider.Value})"); var didConnect = await TDK.Connect.ConnectViaCookie( launcherAuthCookie, launcherAuthProvider.Value, diff --git a/Assets/Treasure/TDK/Runtime/Infrastructure/TDKMainThreadDispatcher.cs.meta b/Assets/Treasure/TDK/Runtime/Infrastructure/TDKMainThreadDispatcher.cs.meta index 4d0306dc..a181d3f8 100644 --- a/Assets/Treasure/TDK/Runtime/Infrastructure/TDKMainThreadDispatcher.cs.meta +++ b/Assets/Treasure/TDK/Runtime/Infrastructure/TDKMainThreadDispatcher.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 1ac1013a93f6f42ca965bf95f4934e8e +guid: 7866706a8baf44d6a92798cd9f1e296c MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Treasure/TDK/Runtime/Infrastructure/TDKWebConnectInterface.cs b/Assets/Treasure/TDK/Runtime/Infrastructure/TDKWebConnectInterface.cs new file mode 100644 index 00000000..e234c85b --- /dev/null +++ b/Assets/Treasure/TDK/Runtime/Infrastructure/TDKWebConnectInterface.cs @@ -0,0 +1,149 @@ +using System; +using UnityEngine; +using System.Runtime.InteropServices; +using UnityEngine.Events; + +namespace Treasure +{ + public class TDKWebConnectInterface : MonoBehaviour + { + public enum BrowserConnectionState + { + Disconnected, Connected, NewConnection + } + + public static Action BrowserWalletConnectedAction; + private static TDKWebConnectInterface _instance = null; + private BrowserConnectionState _connectionState = BrowserConnectionState.Disconnected; + + // create a GameObject to grab stuff from the queue + public static void Initialize() { + if (_instance == null) { + _instance = FindObjectOfType(typeof(TDKWebConnectInterface)) as TDKWebConnectInterface; + + if (_instance == null) + { + // create a new instance + _instance = new GameObject("TDKWebConnectInterface", new Type[] { + typeof(TDKWebConnectInterface), + }).GetComponent(); + + DontDestroyOnLoad(_instance.gameObject); + } + } + if (IsActive()) + { + WebGLNotifyReady(); + } + } + + public static bool IsActive() + { +#if UNITY_WEBGL && !UNITY_EDITOR + return true; +#else + return false; +#endif + } + + public static void AttemptReconnect() + { + WebGLRequestReconnect(); + } + + public static BrowserConnectionState GetBrowserConnectionState() + { + return _instance._connectionState; + } + + public static bool BrowserHasActiveWalletConnection() + { + return _instance._connectionState == BrowserConnectionState.Connected; + } + +#region Unity -> Browser + public static void OpenConnectModal() + { + _instance._connectionState = BrowserConnectionState.NewConnection; + TDKConnectUIManager.Instance.ShowTransitionModal( + "", + "Proceed with login in the popup", + buttonText: "Cancel", + buttonAction: () => { + TDK.Connect.HideConnectModal(); + } + ); + if (TDK.AppConfig.ConnectModalMode == TDKConfig.ConnectUIModalMode.WebGLExternalForWalletLoginOnly) + { + WebGLOpenWalletConnectModal(); + } + else + { + WebGLOpenConnectModal(); + } + } + + public static void LogOut() + { + WebGLLogOut(); + } + +#if UNITY_WEBGL + [DllImport("__Internal")] + private static extern void WebGLNotifyReady(); + + [DllImport("__Internal")] + private static extern void WebGLOpenConnectModal(); + + [DllImport("__Internal")] + private static extern void WebGLOpenWalletConnectModal(); + + [DllImport("__Internal")] + private static extern void WebGLLogOut(); + + [DllImport("__Internal")] + private static extern void WebGLRequestReconnect(); +#else + private static void WebGLNotifyReady() {} + + private static void WebGLOpenConnectModal() {} + + private static void WebGLOpenWalletConnectModal() {} + + private static void WebGLLogOut() {} + + private static void WebGLRequestReconnect() {} +#endif + +#endregion + +#region Browser -> Unity + public void OnWalletConnected() + { + if (_connectionState == BrowserConnectionState.Disconnected) + { + // browser has notified that active wallet is connected before any user interaction + _connectionState = BrowserConnectionState.Connected; + } + BrowserWalletConnectedAction?.Invoke(); + } + + // passing params together since SendMessage only accepts 1 arg + public void OnConnectViaCookie(string authMethodAndCookie) + { + var splitResult = authMethodAndCookie.Split("@"); + var authMethod = TreasureLauncherUtils.ParseAuthProviderString(splitResult[0]); + var authCookie = splitResult[1]; + _ = TDK.Connect.ConnectViaCookie(authCookie, authMethod.Value); + } + + public void OnConnectViaCookieError(string error) + { + TDKConnectUIManager.Instance.GetTransitionModal().SetInfoLabels( + "An error occurred while trying to connect", + error + ); + } +#endregion + } +} diff --git a/Assets/Treasure/TDK/Runtime/Infrastructure/TDKWebConnectInterface.cs.meta b/Assets/Treasure/TDK/Runtime/Infrastructure/TDKWebConnectInterface.cs.meta new file mode 100644 index 00000000..4d0306dc --- /dev/null +++ b/Assets/Treasure/TDK/Runtime/Infrastructure/TDKWebConnectInterface.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1ac1013a93f6f42ca965bf95f4934e8e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Treasure/TDK/Runtime/Services/Thirdweb/TDKThirdwebService.cs b/Assets/Treasure/TDK/Runtime/Services/Thirdweb/TDKThirdwebService.cs index 1964a94e..21dceb3b 100644 --- a/Assets/Treasure/TDK/Runtime/Services/Thirdweb/TDKThirdwebService.cs +++ b/Assets/Treasure/TDK/Runtime/Services/Thirdweb/TDKThirdwebService.cs @@ -22,6 +22,7 @@ public class TDKThirdwebService : TDKBaseService private CancellationTokenSource _connectionCancelationTokenSource; private bool _didInitWalletConnect = false; + private string _authCookie = ""; public override void Awake() { @@ -142,6 +143,7 @@ public async Task ConnectWallet(EcosystemWalletOptions ecosystemWalletOptions, i "Upgrading to smart wallet" ); } + _authCookie = ExtractAuthCookie(ecosystemWallet); smartWallet = await SmartWallet.Create( personalWallet: ecosystemWallet, chainId: chainId, @@ -166,6 +168,25 @@ public async Task ConnectWallet(EcosystemWalletOptions ecosystemWalletOptions, i } } + private string ExtractAuthCookie(EcosystemWallet ecosystemWallet) + { + try + { + var link = ecosystemWallet.GenerateExternalLoginLink("localhost:3456"); + return link.Split("authCookie=")[1]; + } + catch (Exception ex) + { + TDKLogger.LogException("error parsing auth cookie", ex); + return ""; + } + } + + public string GetStoredAuthCookie() + { + return _authCookie; + } + public async Task ConnectExternalWallet(int chainId) { _connectionCancelationTokenSource?.Cancel(); diff --git a/Assets/Treasure/TDK/Runtime/TDK.cs b/Assets/Treasure/TDK/Runtime/TDK.cs index b88f6113..0f92e4fa 100644 --- a/Assets/Treasure/TDK/Runtime/TDK.cs +++ b/Assets/Treasure/TDK/Runtime/TDK.cs @@ -53,6 +53,7 @@ public static TDK Instance } TDKMainThreadDispatcher.StartProcessing(); + TDKWebConnectInterface.Initialize(); return _instance; } @@ -103,6 +104,13 @@ LocalSettings localSettings Initialized = true; _ = Identity.AttemptConnectionViaLauncherAuth(); + + // TODO figure out why email hangs up if email is not provided + // _ = TDK.Connect.ConnectViaCookie("", Thirdweb.AuthProvider.Default); + // NOTE: google log out works properly, gives an error next time + // _ = TDK.Connect.ConnectViaCookie("", Thirdweb.AuthProvider.Google); + // TODO figure out why this still connects even after logging out + // _ = TDK.Connect.ConnectViaCookie("", Thirdweb.AuthProvider.Siwe); } private void InitializeProperties( diff --git a/Assets/Treasure/TDK/Runtime/TDKConfig.cs b/Assets/Treasure/TDK/Runtime/TDKConfig.cs index 7db766b5..0d698327 100644 --- a/Assets/Treasure/TDK/Runtime/TDKConfig.cs +++ b/Assets/Treasure/TDK/Runtime/TDKConfig.cs @@ -73,10 +73,12 @@ public enum LoggerLevelValue { SILENT = 100, ERROR = 40, WARNING = 30, INFO = 20 [SerializeField] private ScriptableObjectDictionary moduleConfigurations = null; public enum ConnectUIHideBehavior { HideOnOutsideClick, DoNotHideOnOtpScreen, NeverHide } + public enum ConnectUIModalMode { InGame, WebGLExternal, WebGLExternalForWalletLoginOnly } [Header("Misc")] [SerializeField] private ConnectUIHideBehavior _connectHideBehavior = ConnectUIHideBehavior.DoNotHideOnOtpScreen; [SerializeField] private bool _enableWalletLogin = false; + [SerializeField] private ConnectUIModalMode _connectModalMode = ConnectUIModalMode.InGame; [SerializeField] private LoggerLevelValue _devLoggerLevel = LoggerLevelValue.INFO; [SerializeField] private LoggerLevelValue _prodLoggerLevel = LoggerLevelValue.INFO; [SerializeField] private bool _autoInitialize = true; @@ -120,6 +122,7 @@ public ConnectUIHideBehavior ConnectHideBehavior set { _connectHideBehavior = value; } } public bool EnableWalletLogin => _enableWalletLogin; + public ConnectUIModalMode ConnectModalMode => _connectModalMode; public string ApiKey => Environment == Env.DEV ? _general._devApiKey : _general._prodApiKey; public bool AutoInitialize => _autoInitialize; @@ -227,6 +230,17 @@ public void SetConfig(SerializedTDKConfig config, TDKConfig previousConfig) _devApiUrl = config.analytics.devApiUrl, _prodApiUrl = config.analytics.prodApiUrl, }; + + // Misc + if (previousConfig != null) + { + _enableWalletLogin = previousConfig._enableWalletLogin; + _connectModalMode = previousConfig._connectModalMode; + _connectHideBehavior = previousConfig._connectHideBehavior; + _devLoggerLevel = previousConfig._devLoggerLevel; + _prodLoggerLevel = previousConfig._prodLoggerLevel; + _autoInitialize = previousConfig._autoInitialize; + } } } diff --git a/Assets/Treasure/TDK/Runtime/Utils/TreasureLauncherUtils.cs b/Assets/Treasure/TDK/Runtime/Utils/TreasureLauncherUtils.cs index 08e744bf..bbaa2f9a 100644 --- a/Assets/Treasure/TDK/Runtime/Utils/TreasureLauncherUtils.cs +++ b/Assets/Treasure/TDK/Runtime/Utils/TreasureLauncherUtils.cs @@ -61,8 +61,9 @@ public static string GetEmailAddressFromAuthCookie() "x" => Thirdweb.AuthProvider.X, "apple" => Thirdweb.AuthProvider.Apple, "siwe" => Thirdweb.AuthProvider.Siwe, + "wallet" => Thirdweb.AuthProvider.Siwe, "email" => Thirdweb.AuthProvider.Default, - _ => null, + _ => Thirdweb.AuthProvider.Siwe, }; } diff --git a/Assets/WebGLTemplates.meta b/Assets/WebGLTemplates.meta new file mode 100644 index 00000000..3062c26c --- /dev/null +++ b/Assets/WebGLTemplates.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: aad996756d5bd450f98e9972ca5f8b56 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/WebGLTemplates/BrowserWalletLogin.meta b/Assets/WebGLTemplates/BrowserWalletLogin.meta new file mode 100644 index 00000000..6b34995d --- /dev/null +++ b/Assets/WebGLTemplates/BrowserWalletLogin.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d1d7cb606dac54825b9852cdb25d2a16 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/WebGLTemplates/BrowserWalletLogin/index.html b/Assets/WebGLTemplates/BrowserWalletLogin/index.html new file mode 100644 index 00000000..797c0f5f --- /dev/null +++ b/Assets/WebGLTemplates/BrowserWalletLogin/index.html @@ -0,0 +1,58 @@ + + + + + + Unity WebGL Player | {{{ PRODUCT_NAME }}} + + + + +
+ + + + + + diff --git a/Assets/WebGLTemplates/BrowserWalletLogin/index.html.meta b/Assets/WebGLTemplates/BrowserWalletLogin/index.html.meta new file mode 100644 index 00000000..ec52c1ed --- /dev/null +++ b/Assets/WebGLTemplates/BrowserWalletLogin/index.html.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 1b285b8f6d0ae46879ca8ddfd8813687 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/WebGLTemplates/BrowserWalletLogin/thumbnail.png b/Assets/WebGLTemplates/BrowserWalletLogin/thumbnail.png new file mode 100644 index 00000000..0aeb0897 Binary files /dev/null and b/Assets/WebGLTemplates/BrowserWalletLogin/thumbnail.png differ diff --git a/Assets/WebGLTemplates/BrowserWalletLogin/thumbnail.png.meta b/Assets/WebGLTemplates/BrowserWalletLogin/thumbnail.png.meta new file mode 100644 index 00000000..7eb0fa5f --- /dev/null +++ b/Assets/WebGLTemplates/BrowserWalletLogin/thumbnail.png.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 65b77e68130bc482ba8dc460af7284ae +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: