From 7c1c4dbbca1881ad3e9e2188e16ff5b8366a8198 Mon Sep 17 00:00:00 2001 From: Felipe Cardozo Date: Thu, 19 Oct 2023 20:59:56 -0300 Subject: [PATCH] fixing mobile login with Awaitable --- .../Scripts/Beacon/BeaconConnectorDotNet.cs | 3 +- Runtime/Scripts/Beacon/IBeaconConnector.cs | 4 +- .../Singleton/SingletonMonoBehaviour.cs | 2 +- .../Scripts/Tezos/Wallet/IWalletProvider.cs | 3 +- .../Scripts/Tezos/Wallet/WalletProvider.cs | 49 ++++++++----------- Runtime/View/AuthenticationManager.cs | 4 +- 6 files changed, 31 insertions(+), 34 deletions(-) diff --git a/Runtime/Scripts/Beacon/BeaconConnectorDotNet.cs b/Runtime/Scripts/Beacon/BeaconConnectorDotNet.cs index 263f1640..be61e3fc 100644 --- a/Runtime/Scripts/Beacon/BeaconConnectorDotNet.cs +++ b/Runtime/Scripts/Beacon/BeaconConnectorDotNet.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading.Tasks; using Beacon.Sdk; using Beacon.Sdk.Beacon; using Beacon.Sdk.Beacon.Operation; @@ -103,7 +104,7 @@ public void SetWalletMessageReceiver(WalletMessageReceiver messageReceiver) _walletMessageReceiver = messageReceiver; } - public async void RequestTezosPermission(string networkName = "", string networkRPC = "") + public async Awaitable RequestTezosPermission(string networkName = "", string networkRPC = "") { if (!Enum.TryParse(networkName, out NetworkType networkType)) networkType = TezosConfig.Instance.Network; diff --git a/Runtime/Scripts/Beacon/IBeaconConnector.cs b/Runtime/Scripts/Beacon/IBeaconConnector.cs index 5489a95c..0cb69121 100644 --- a/Runtime/Scripts/Beacon/IBeaconConnector.cs +++ b/Runtime/Scripts/Beacon/IBeaconConnector.cs @@ -1,6 +1,8 @@ +using System.Threading.Tasks; using Beacon.Sdk.Beacon.Sign; using Netezos.Forging.Models; using TezosSDK.Tezos.Wallet; +using UnityEngine; namespace TezosSDK.Beacon { @@ -40,7 +42,7 @@ public interface IBeaconConnector /// /// The name of the desired network. /// The RPC to the desired network - public void RequestTezosPermission(string networkName = "", string networkRPC = ""); + public Awaitable RequestTezosPermission(string networkName = "", string networkRPC = ""); /// /// Allows requesting a new operation such as sending tezos or initiating a smart contract. diff --git a/Runtime/Scripts/DesignPattern/Singleton/SingletonMonoBehaviour.cs b/Runtime/Scripts/DesignPattern/Singleton/SingletonMonoBehaviour.cs index f74127d0..5f76d554 100644 --- a/Runtime/Scripts/DesignPattern/Singleton/SingletonMonoBehaviour.cs +++ b/Runtime/Scripts/DesignPattern/Singleton/SingletonMonoBehaviour.cs @@ -54,7 +54,7 @@ public static T Instantiate() { if (!IsInstantiated()) { - T t = GameObject.FindObjectOfType(); + T t = FindFirstObjectByType(); GameObject go = null; if (t != null) { diff --git a/Runtime/Scripts/Tezos/Wallet/IWalletProvider.cs b/Runtime/Scripts/Tezos/Wallet/IWalletProvider.cs index 90a4b6bc..2b1de239 100644 --- a/Runtime/Scripts/Tezos/Wallet/IWalletProvider.cs +++ b/Runtime/Scripts/Tezos/Wallet/IWalletProvider.cs @@ -1,5 +1,6 @@ using Beacon.Sdk.Beacon.Sign; using TezosSDK.Beacon; +using UnityEngine; namespace TezosSDK.Tezos.Wallet { @@ -19,7 +20,7 @@ public interface IWalletProvider /// Makes a call to connect with a wallet /// Should we open wallet app on mobiles after connect? /// - void Connect(WalletProviderType walletProvider, bool withRedirectToWallet = true); + Awaitable Connect(WalletProviderType walletProvider, bool withRedirectToWallet = true); /// /// Unpair with wallet and disconnect diff --git a/Runtime/Scripts/Tezos/Wallet/WalletProvider.cs b/Runtime/Scripts/Tezos/Wallet/WalletProvider.cs index 9adb40a5..7a00d197 100644 --- a/Runtime/Scripts/Tezos/Wallet/WalletProvider.cs +++ b/Runtime/Scripts/Tezos/Wallet/WalletProvider.cs @@ -1,6 +1,8 @@ using System; using System.Collections; +using System.Runtime.CompilerServices; using System.Text.Json; +using System.Threading.Tasks; using Beacon.Sdk.Beacon.Sign; using TezosSDK.Beacon; using TezosSDK.Helpers; @@ -23,7 +25,7 @@ public WalletProvider() InitBeaconConnector(); } - private void InitBeaconConnector() + private async void InitBeaconConnector() { // Create or get a WalletMessageReceiver Game object to receive callback messages var unityBeacon = GameObject.Find("UnityBeacon"); @@ -37,7 +39,7 @@ private void InitBeaconConnector() #else _beaconConnector = new BeaconConnectorDotNet(); (_beaconConnector as BeaconConnectorDotNet)?.SetWalletMessageReceiver(MessageReceiver); - Connect(WalletProviderType.beacon, withRedirectToWallet: false); + await Connect(WalletProviderType.beacon, withRedirectToWallet: false); // todo: maybe call RequestTezosPermission from _beaconConnector? MessageReceiver.PairingCompleted += _ => @@ -72,43 +74,34 @@ private void InitBeaconConnector() }; } - // Below there are some async/wait workarounds and magic numbers, - // we should rewrite the Beacon connector to be coroutine compatible. - private IEnumerator OnOpenWallet(bool withRedirectToWallet) - { - if (string.IsNullOrEmpty(_handshake)) - { - //No handshake, Waiting for handshake... - yield return new WaitForSeconds(2.5f); - } - -#if UNITY_ANDROID || UNITY_IOS - if (withRedirectToWallet){ - _beaconConnector.RequestTezosPermission( - networkName: TezosConfig.Instance.Network.ToString(), - networkRPC: TezosConfig.Instance.RpcBaseUrl); - yield return new WaitForSeconds(2.5f); - if (!string.IsNullOrEmpty(_handshake)){ - Application.OpenURL($"tezos://?type=tzip10&data={_handshake}"); - } - } -#endif - } - public void OnReady() { _beaconConnector.OnReady(); } - public void Connect(WalletProviderType walletProvider, bool withRedirectToWallet) + public async Awaitable Connect(WalletProviderType walletProvider, bool withRedirectToWallet) { - _beaconConnector.InitWalletProvider( network: TezosConfig.Instance.Network.ToString(), rpc: TezosConfig.Instance.RpcBaseUrl, walletProviderType: walletProvider); _beaconConnector.ConnectAccount(); - CoroutineRunner.Instance.StartWrappedCoroutine(OnOpenWallet(withRedirectToWallet)); + +#if UNITY_ANDROID || UNITY_IOS + if (withRedirectToWallet){ + await _beaconConnector.RequestTezosPermission( + networkName: TezosConfig.Instance.Network.ToString(), + networkRPC: TezosConfig.Instance.RpcBaseUrl); + if (string.IsNullOrEmpty(_handshake)) + { + //No handshake, Waiting for handshake... + await Awaitable.WaitForSecondsAsync(2.5f); + } + if (!string.IsNullOrEmpty(_handshake)){ + Application.OpenURL($"tezos://?type=tzip10&data={_handshake}"); + } + } +#endif } public void Disconnect() diff --git a/Runtime/View/AuthenticationManager.cs b/Runtime/View/AuthenticationManager.cs index 791abff8..03e6ef9d 100644 --- a/Runtime/View/AuthenticationManager.cs +++ b/Runtime/View/AuthenticationManager.cs @@ -52,9 +52,9 @@ public void DisconnectWallet() _tezos.Wallet.Disconnect(); } - public void ConnectByDeeplink() + public async void ConnectByDeeplink() { - _tezos.Wallet.Connect(WalletProviderType.beacon); + await _tezos.Wallet.Connect(WalletProviderType.beacon); } void EnableUI(bool isAuthenticated)