From d4f80a6c43fedd1662caf9cef728b1de7b96783a Mon Sep 17 00:00:00 2001 From: Felipe Cardozo Date: Sun, 22 Oct 2023 19:39:44 -0300 Subject: [PATCH 1/2] add WaintUntilForSeconds yield instruction --- .../Scripts/Helpers/WaitUntilForSeconds.cs | 70 +++++++++++++++++++ .../Helpers/WaitUntilForSeconds.cs.meta | 3 + 2 files changed, 73 insertions(+) create mode 100644 Runtime/Scripts/Helpers/WaitUntilForSeconds.cs create mode 100644 Runtime/Scripts/Helpers/WaitUntilForSeconds.cs.meta diff --git a/Runtime/Scripts/Helpers/WaitUntilForSeconds.cs b/Runtime/Scripts/Helpers/WaitUntilForSeconds.cs new file mode 100644 index 00000000..05d2992f --- /dev/null +++ b/Runtime/Scripts/Helpers/WaitUntilForSeconds.cs @@ -0,0 +1,70 @@ +using System; +using UnityEngine; + +namespace TezosSDK.Helpers +{ + public class WaitUntilForSeconds: CustomYieldInstruction + { + float pauseTime; + float timer; + bool waitingForFirst; + Func myChecker; + Action onInterrupt; + bool alwaysTrue; + + public WaitUntilForSeconds(Func myChecker, float pauseTime, + Action onInterrupt = null) + { + this.myChecker = myChecker; + this.pauseTime = pauseTime; + this.onInterrupt = onInterrupt; + + waitingForFirst = true; + } + + public override bool keepWaiting + { + get + { + bool checkThisTurn = myChecker(); + if (waitingForFirst) + { + if (checkThisTurn) + { + waitingForFirst = false; + timer = pauseTime; + alwaysTrue = true; + } + } + else + { + timer -= Time.deltaTime; + + if (onInterrupt != null && !checkThisTurn && alwaysTrue) + { + onInterrupt(timer); + } + alwaysTrue &= checkThisTurn; + + // Alternate version: Interrupt the timer on false, + // and restart the wait + // if (!alwaysTrue || timer <= 0) + + if (timer <= 0) + { + if (alwaysTrue) + { + return false; + } + else + { + waitingForFirst = true; + } + } + } + + return true; + } + } + } +} \ No newline at end of file diff --git a/Runtime/Scripts/Helpers/WaitUntilForSeconds.cs.meta b/Runtime/Scripts/Helpers/WaitUntilForSeconds.cs.meta new file mode 100644 index 00000000..b68a7b11 --- /dev/null +++ b/Runtime/Scripts/Helpers/WaitUntilForSeconds.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5c59c7110b7b4dc484a01aa7799d0a86 +timeCreated: 1698013317 \ No newline at end of file From c895fd83dcc081617eeba2ea292b44cb05e83620 Mon Sep 17 00:00:00 2001 From: Felipe Cardozo Date: Tue, 24 Oct 2023 20:37:41 -0300 Subject: [PATCH 2/2] fix android login --- Runtime/Scripts/Tezos/Tezos.cs | 1 + .../Scripts/Tezos/Wallet/WalletProvider.cs | 63 ++++++++----------- 2 files changed, 28 insertions(+), 36 deletions(-) diff --git a/Runtime/Scripts/Tezos/Tezos.cs b/Runtime/Scripts/Tezos/Tezos.cs index bcb70f9f..b6172d04 100644 --- a/Runtime/Scripts/Tezos/Tezos.cs +++ b/Runtime/Scripts/Tezos/Tezos.cs @@ -35,6 +35,7 @@ public Tezos(DAppMetadata providedDAppMetadata = null) }; Wallet = new WalletProvider(dAppMetadata); + Wallet.Connect(WalletProviderType.beacon, false); MessageReceiver = Wallet.MessageReceiver; MessageReceiver.AccountConnected += _ => diff --git a/Runtime/Scripts/Tezos/Wallet/WalletProvider.cs b/Runtime/Scripts/Tezos/Wallet/WalletProvider.cs index f70ee658..59c25d4a 100644 --- a/Runtime/Scripts/Tezos/Wallet/WalletProvider.cs +++ b/Runtime/Scripts/Tezos/Wallet/WalletProvider.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Text.Json; using Beacon.Sdk.Beacon.Sign; +using Beacon.Sdk.Core.Domain.Entities; using TezosSDK.Beacon; using TezosSDK.Helpers; using UnityEngine; @@ -33,22 +34,6 @@ private void InitBeaconConnector() ? unityBeacon.GetComponent() : new GameObject("UnityBeacon").AddComponent(); - // Assign the BeaconConnector depending on the platform. -#if !UNITY_EDITOR && UNITY_WEBGL - _beaconConnector = new BeaconConnectorWebGl(); -#else - _beaconConnector = new BeaconConnectorDotNet(); - (_beaconConnector as BeaconConnectorDotNet)?.SetWalletMessageReceiver(MessageReceiver); - Connect(WalletProviderType.beacon, withRedirectToWallet: false); - - // todo: maybe call RequestTezosPermission from _beaconConnector? - MessageReceiver.PairingCompleted += _ => - { - _beaconConnector.RequestTezosPermission( - networkName: TezosConfig.Instance.Network.ToString(), - networkRPC: TezosConfig.Instance.RpcBaseUrl); - }; -#endif MessageReceiver.HandshakeReceived += handshake => { _handshake = handshake; }; MessageReceiver.AccountConnected += account => { @@ -72,28 +57,35 @@ private void InitBeaconConnector() CoroutineRunner.Instance.StartWrappedCoroutine( new CoroutineWrapper(MessageReceiver.TrackTransaction(transactionHash))); }; + // Assign the BeaconConnector depending on the platform. +#if !UNITY_EDITOR && UNITY_WEBGL + _beaconConnector = new BeaconConnectorWebGl(); +#else + _beaconConnector = new BeaconConnectorDotNet(); + MessageReceiver.PairingCompleted += _ => + { + _beaconConnector.RequestTezosPermission( + networkName: TezosConfig.Instance.Network.ToString(), + networkRPC: TezosConfig.Instance.RpcBaseUrl); + }; + (_beaconConnector as BeaconConnectorDotNet)?.SetWalletMessageReceiver(MessageReceiver); +#endif } - // 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) + private IEnumerator OpenWallet(bool withRedirectToWallet) { - if (string.IsNullOrEmpty(_handshake)) - { - //No handshake, Waiting for handshake... - yield return new WaitForSeconds(2.5f); - } + yield return new WaitUntilForSeconds(() => !string.IsNullOrEmpty(_handshake), 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}"); - } - } + if (!withRedirectToWallet) yield break; +#if UNITY_IOS + // TODO: improve background peer pairing for iOS, then we can remove this workaround + _beaconConnector.RequestTezosPermission( + networkName: TezosConfig.Instance.Network.ToString(), + networkRPC: TezosConfig.Instance.RpcBaseUrl + ); +#endif + Application.OpenURL($"tezos://?type=tzip10&data={_handshake}"); #endif } @@ -102,16 +94,15 @@ public void OnReady() _beaconConnector.OnReady(); } - public void Connect(WalletProviderType walletProvider, bool withRedirectToWallet) + public void Connect(WalletProviderType walletProvider, bool withRedirectToWallet = true) { _beaconConnector.InitWalletProvider( network: TezosConfig.Instance.Network.ToString(), rpc: TezosConfig.Instance.RpcBaseUrl, walletProviderType: walletProvider, dAppMetadata: _dAppMetadata); - _beaconConnector.ConnectAccount(); - CoroutineRunner.Instance.StartWrappedCoroutine(OnOpenWallet(withRedirectToWallet)); + CoroutineRunner.Instance.StartWrappedCoroutine(OpenWallet(withRedirectToWallet)); } public void Disconnect()