From 955a0b448930fb30f7103d9235548951ce78d5d1 Mon Sep 17 00:00:00 2001 From: sneakzttv <57473220+sneakzttv@users.noreply.github.com> Date: Wed, 19 Jun 2024 07:35:30 +0800 Subject: [PATCH 1/5] metamask logout fix metamask logout fixed with task completion source --- .../MetaMaskController.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs b/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs index 57923913c..615e4a3fc 100644 --- a/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs +++ b/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs @@ -41,6 +41,11 @@ public class MetaMaskController : MonoBehaviour /// public string ConnectedAddress { get; private set; } + /// + /// Task completion source to track connection. + /// + private TaskCompletionSource ConnectedTsc { get; set; } + /// /// Initialize script with references. /// @@ -56,7 +61,7 @@ public void Initialize(ILogWriter logWriter) /// Connected account address. public Task Connect() { - var taskCompletionSource = new TaskCompletionSource(); + ConnectedTsc = new TaskCompletionSource(); // Unsubscribe in case we're already subscribed from a previous login. OnAccountConnected -= Connected; @@ -68,9 +73,9 @@ void Connected(string address) { ConnectedAddress = address; - if (!taskCompletionSource.TrySetResult(ConnectedAddress)) + if (!ConnectedTsc.TrySetResult(ConnectedAddress)) { - taskCompletionSource.SetException(new Web3Exception("Error setting connected account address.")); + ConnectedTsc.SetException(new Web3Exception("Error setting connected account address.")); } else { @@ -93,7 +98,7 @@ void Connected(string address) return null; } - return taskCompletionSource.Task; + return ConnectedTsc.Task; } /// @@ -195,6 +200,10 @@ public void ChainSelected(string chainId) private void DisplayError(string message) { logger.LogError(message); + if (ConnectedTsc != null && !ConnectedTsc.Task.IsCompleted) + { + ConnectedTsc.SetException(new Web3Exception(message)); + } } } } \ No newline at end of file From 130d27d3e2b8c8332e5ac70db86373931c1e4f15 Mon Sep 17 00:00:00 2001 From: sneakzttv <57473220+sneakzttv@users.noreply.github.com> Date: Wed, 19 Jun 2024 08:19:16 +0800 Subject: [PATCH 2/5] fixed --- .../Runtime/Common/Scripts/LoginProvider.cs | 4 +-- .../MetaMaskController.cs | 5 +-- .../Scripts/Scenes/MetaMaskLoginProvider.cs | 31 ++++++++++++++++++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Packages/io.chainsafe.web3-unity/Runtime/Common/Scripts/LoginProvider.cs b/Packages/io.chainsafe.web3-unity/Runtime/Common/Scripts/LoginProvider.cs index fdebe4a0d..60c44ca56 100644 --- a/Packages/io.chainsafe.web3-unity/Runtime/Common/Scripts/LoginProvider.cs +++ b/Packages/io.chainsafe.web3-unity/Runtime/Common/Scripts/LoginProvider.cs @@ -12,7 +12,7 @@ public abstract class LoginProvider : MonoBehaviour, ILoginProvider { [SerializeField] private string gelatoApiKey = ""; - [SerializeField] private ErrorPopup errorPopup; + [SerializeField] public ErrorPopup errorPopup; public string GelatoApiKey => gelatoApiKey; public IWeb3BuilderServiceAdapter[] Web3BuilderServiceAdapters { get; private set; } @@ -36,7 +36,7 @@ protected virtual void Initialize() /// /// Try to Login and displays error and throws exception on a failed attempt. /// - public async Task TryLogin() + public virtual async Task TryLogin() { try { diff --git a/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs b/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs index 615e4a3fc..c0112cc6d 100644 --- a/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs +++ b/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs @@ -44,7 +44,7 @@ public class MetaMaskController : MonoBehaviour /// /// Task completion source to track connection. /// - private TaskCompletionSource ConnectedTsc { get; set; } + public TaskCompletionSource ConnectedTsc { get; set; } /// /// Initialize script with references. @@ -199,11 +199,12 @@ public void ChainSelected(string chainId) // Callback for displaying an error if operation fails. private void DisplayError(string message) { - logger.LogError(message); if (ConnectedTsc != null && !ConnectedTsc.Task.IsCompleted) { ConnectedTsc.SetException(new Web3Exception(message)); } + + logger.LogError(message); } } } \ No newline at end of file diff --git a/src/UnitySampleProject/Assets/Samples/web3.unity SDK/2.5.0/Web3.Unity Samples/Scripts/Scenes/MetaMaskLoginProvider.cs b/src/UnitySampleProject/Assets/Samples/web3.unity SDK/2.5.0/Web3.Unity Samples/Scripts/Scenes/MetaMaskLoginProvider.cs index 21c2dc125..66f0989c2 100644 --- a/src/UnitySampleProject/Assets/Samples/web3.unity SDK/2.5.0/Web3.Unity Samples/Scripts/Scenes/MetaMaskLoginProvider.cs +++ b/src/UnitySampleProject/Assets/Samples/web3.unity SDK/2.5.0/Web3.Unity Samples/Scripts/Scenes/MetaMaskLoginProvider.cs @@ -1,5 +1,7 @@ using System.Collections; +using System.Threading.Tasks; using ChainSafe.Gaming.UnityPackage.Common; +using ChainSafe.Gaming.Web3; #if UNITY_WEBGL && !UNITY_EDITOR using ChainSafe.Gaming.MetaMask; using ChainSafe.Gaming.MetaMask.Unity; @@ -17,12 +19,39 @@ public class MetaMaskLoginProvider : LoginProvider, IWeb3BuilderServiceAdapter { [SerializeField] private Button loginButton; + #if UNITY_WEBGL && !UNITY_EDITOR + private MetaMaskController metaMaskController; + #endif protected override void Initialize() { base.Initialize(); - loginButton.onClick.AddListener(LoginClicked); + #if UNITY_WEBGL && !UNITY_EDITOR + metaMaskController = Object.FindObjectOfType(); + #endif + } + + public override async Task TryLogin() + { + try + { + #if UNITY_WEBGL && !UNITY_EDITOR + metaMaskController.ConnectedTsc = new TaskCompletionSource(); + #endif + await base.TryLogin(); + } + catch (Web3Exception e) + { + errorPopup.ShowError($"Login failed, please try again\n{e.Message}"); + #if UNITY_WEBGL && !UNITY_EDITOR + if (metaMaskController.ConnectedTsc != null && !metaMaskController.ConnectedTsc.Task.IsCompleted) + { + metaMaskController.ConnectedTsc.SetException(e); + } + #endif + throw; + } } private async void LoginClicked() From daa8df326dd715565a082a0f3c2b8c11976f59f4 Mon Sep 17 00:00:00 2001 From: sneakzttv <57473220+sneakzttv@users.noreply.github.com> Date: Wed, 19 Jun 2024 15:19:08 +0800 Subject: [PATCH 3/5] fixes fixes --- .../MetaMaskController.cs | 20 ++++++++++++------- .../Scripts/Scenes/MetaMaskLoginProvider.cs | 14 +++---------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs b/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs index c0112cc6d..c4d728354 100644 --- a/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs +++ b/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs @@ -44,7 +44,7 @@ public class MetaMaskController : MonoBehaviour /// /// Task completion source to track connection. /// - public TaskCompletionSource ConnectedTsc { get; set; } + public TaskCompletionSource ConnectedTcs { get; set; } /// /// Initialize script with references. @@ -61,7 +61,13 @@ public void Initialize(ILogWriter logWriter) /// Connected account address. public Task Connect() { - ConnectedTsc = new TaskCompletionSource(); + if (ConnectedTcs != null && !ConnectedTcs.Task.IsCompleted) + { + ConnectedTcs.SetException(new Web3Exception("Already trying to connect.")); + return ConnectedTcs.Task; + } + + ConnectedTcs = new TaskCompletionSource(); // Unsubscribe in case we're already subscribed from a previous login. OnAccountConnected -= Connected; @@ -73,9 +79,9 @@ void Connected(string address) { ConnectedAddress = address; - if (!ConnectedTsc.TrySetResult(ConnectedAddress)) + if (!ConnectedTcs.TrySetResult(ConnectedAddress)) { - ConnectedTsc.SetException(new Web3Exception("Error setting connected account address.")); + ConnectedTcs.SetException(new Web3Exception("Error setting connected account address.")); } else { @@ -98,7 +104,7 @@ void Connected(string address) return null; } - return ConnectedTsc.Task; + return ConnectedTcs.Task; } /// @@ -199,9 +205,9 @@ public void ChainSelected(string chainId) // Callback for displaying an error if operation fails. private void DisplayError(string message) { - if (ConnectedTsc != null && !ConnectedTsc.Task.IsCompleted) + if (ConnectedTcs != null && !ConnectedTcs.Task.IsCompleted) { - ConnectedTsc.SetException(new Web3Exception(message)); + ConnectedTcs.SetException(new Web3Exception(message)); } logger.LogError(message); diff --git a/src/UnitySampleProject/Assets/Samples/web3.unity SDK/2.5.0/Web3.Unity Samples/Scripts/Scenes/MetaMaskLoginProvider.cs b/src/UnitySampleProject/Assets/Samples/web3.unity SDK/2.5.0/Web3.Unity Samples/Scripts/Scenes/MetaMaskLoginProvider.cs index 66f0989c2..f354d0504 100644 --- a/src/UnitySampleProject/Assets/Samples/web3.unity SDK/2.5.0/Web3.Unity Samples/Scripts/Scenes/MetaMaskLoginProvider.cs +++ b/src/UnitySampleProject/Assets/Samples/web3.unity SDK/2.5.0/Web3.Unity Samples/Scripts/Scenes/MetaMaskLoginProvider.cs @@ -19,35 +19,27 @@ public class MetaMaskLoginProvider : LoginProvider, IWeb3BuilderServiceAdapter { [SerializeField] private Button loginButton; - #if UNITY_WEBGL && !UNITY_EDITOR - private MetaMaskController metaMaskController; - #endif protected override void Initialize() { base.Initialize(); loginButton.onClick.AddListener(LoginClicked); - #if UNITY_WEBGL && !UNITY_EDITOR - metaMaskController = Object.FindObjectOfType(); - #endif } public override async Task TryLogin() { try { - #if UNITY_WEBGL && !UNITY_EDITOR - metaMaskController.ConnectedTsc = new TaskCompletionSource(); - #endif await base.TryLogin(); } catch (Web3Exception e) { errorPopup.ShowError($"Login failed, please try again\n{e.Message}"); #if UNITY_WEBGL && !UNITY_EDITOR - if (metaMaskController.ConnectedTsc != null && !metaMaskController.ConnectedTsc.Task.IsCompleted) + var metaMaskController = Object.FindObjectOfType(); + if (metaMaskController.ConnectedTcs != null && !metaMaskController.ConnectedTcs.Task.IsCompleted) { - metaMaskController.ConnectedTsc.SetException(e); + metaMaskController.ConnectedTcs.SetException(e); } #endif throw; From 8b92e3f702f32bc53e6f94f77582bc3a9840290c Mon Sep 17 00:00:00 2001 From: rob1997 Date: Wed, 19 Jun 2024 10:35:05 +0300 Subject: [PATCH 4/5] small fixes --- .../Runtime/Common/Scripts/LoginProvider.cs | 4 +-- .../MetaMaskController.cs | 34 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Packages/io.chainsafe.web3-unity/Runtime/Common/Scripts/LoginProvider.cs b/Packages/io.chainsafe.web3-unity/Runtime/Common/Scripts/LoginProvider.cs index 60c44ca56..fdebe4a0d 100644 --- a/Packages/io.chainsafe.web3-unity/Runtime/Common/Scripts/LoginProvider.cs +++ b/Packages/io.chainsafe.web3-unity/Runtime/Common/Scripts/LoginProvider.cs @@ -12,7 +12,7 @@ public abstract class LoginProvider : MonoBehaviour, ILoginProvider { [SerializeField] private string gelatoApiKey = ""; - [SerializeField] public ErrorPopup errorPopup; + [SerializeField] private ErrorPopup errorPopup; public string GelatoApiKey => gelatoApiKey; public IWeb3BuilderServiceAdapter[] Web3BuilderServiceAdapters { get; private set; } @@ -36,7 +36,7 @@ protected virtual void Initialize() /// /// Try to Login and displays error and throws exception on a failed attempt. /// - public virtual async Task TryLogin() + public async Task TryLogin() { try { diff --git a/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs b/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs index c4d728354..abcaf92bc 100644 --- a/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs +++ b/src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs @@ -17,6 +17,8 @@ public class MetaMaskController : MonoBehaviour { private bool isInitialized; + private TaskCompletionSource connectionTcs; + /// Use this instead of . private ILogWriter logger; @@ -41,11 +43,6 @@ public class MetaMaskController : MonoBehaviour /// public string ConnectedAddress { get; private set; } - /// - /// Task completion source to track connection. - /// - public TaskCompletionSource ConnectedTcs { get; set; } - /// /// Initialize script with references. /// @@ -61,13 +58,14 @@ public void Initialize(ILogWriter logWriter) /// Connected account address. public Task Connect() { - if (ConnectedTcs != null && !ConnectedTcs.Task.IsCompleted) + if (connectionTcs != null && !connectionTcs.Task.IsCompleted) { - ConnectedTcs.SetException(new Web3Exception("Already trying to connect.")); - return ConnectedTcs.Task; + connectionTcs.SetException(new Web3Exception("Metamask connection not completed.")); + + return connectionTcs.Task; } - ConnectedTcs = new TaskCompletionSource(); + connectionTcs = new TaskCompletionSource(); // Unsubscribe in case we're already subscribed from a previous login. OnAccountConnected -= Connected; @@ -79,9 +77,9 @@ void Connected(string address) { ConnectedAddress = address; - if (!ConnectedTcs.TrySetResult(ConnectedAddress)) + if (!connectionTcs.TrySetResult(ConnectedAddress)) { - ConnectedTcs.SetException(new Web3Exception("Error setting connected account address.")); + connectionTcs.SetException(new Web3Exception("Error setting connected account address.")); } else { @@ -96,7 +94,7 @@ void Connected(string address) } else { - logger.LogError("Metamask is not available, please install it first."); + connectionTcs.SetException(new Web3Exception("Metamask is not available, please install it first.")); // Unsubscribe to event. OnAccountConnected -= Connected; @@ -104,7 +102,7 @@ void Connected(string address) return null; } - return ConnectedTcs.Task; + return connectionTcs.Task; } /// @@ -205,12 +203,14 @@ public void ChainSelected(string chainId) // Callback for displaying an error if operation fails. private void DisplayError(string message) { - if (ConnectedTcs != null && !ConnectedTcs.Task.IsCompleted) + if (connectionTcs != null && !connectionTcs.Task.IsCompleted) { - ConnectedTcs.SetException(new Web3Exception(message)); + connectionTcs.SetException(new Web3Exception(message)); + } + else + { + logger.LogError(message); } - - logger.LogError(message); } } } \ No newline at end of file From 3f9d0dc299eb7ffa37fb58357c0ec55070256aae Mon Sep 17 00:00:00 2001 From: rob1997 Date: Wed, 19 Jun 2024 10:36:49 +0300 Subject: [PATCH 5/5] reverted MetaMaskLoginProvider --- .../Scripts/Scenes/MetaMaskLoginProvider.cs | 23 +------------------ 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/src/UnitySampleProject/Assets/Samples/web3.unity SDK/2.5.0/Web3.Unity Samples/Scripts/Scenes/MetaMaskLoginProvider.cs b/src/UnitySampleProject/Assets/Samples/web3.unity SDK/2.5.0/Web3.Unity Samples/Scripts/Scenes/MetaMaskLoginProvider.cs index f354d0504..21c2dc125 100644 --- a/src/UnitySampleProject/Assets/Samples/web3.unity SDK/2.5.0/Web3.Unity Samples/Scripts/Scenes/MetaMaskLoginProvider.cs +++ b/src/UnitySampleProject/Assets/Samples/web3.unity SDK/2.5.0/Web3.Unity Samples/Scripts/Scenes/MetaMaskLoginProvider.cs @@ -1,7 +1,5 @@ using System.Collections; -using System.Threading.Tasks; using ChainSafe.Gaming.UnityPackage.Common; -using ChainSafe.Gaming.Web3; #if UNITY_WEBGL && !UNITY_EDITOR using ChainSafe.Gaming.MetaMask; using ChainSafe.Gaming.MetaMask.Unity; @@ -23,28 +21,9 @@ public class MetaMaskLoginProvider : LoginProvider, IWeb3BuilderServiceAdapter protected override void Initialize() { base.Initialize(); + loginButton.onClick.AddListener(LoginClicked); } - - public override async Task TryLogin() - { - try - { - await base.TryLogin(); - } - catch (Web3Exception e) - { - errorPopup.ShowError($"Login failed, please try again\n{e.Message}"); - #if UNITY_WEBGL && !UNITY_EDITOR - var metaMaskController = Object.FindObjectOfType(); - if (metaMaskController.ConnectedTcs != null && !metaMaskController.ConnectedTcs.Task.IsCompleted) - { - metaMaskController.ConnectedTcs.SetException(e); - } - #endif - throw; - } - } private async void LoginClicked() {