Skip to content

Commit

Permalink
fixing mobile login with Awaitable
Browse files Browse the repository at this point in the history
  • Loading branch information
fraidev committed Oct 19, 2023
1 parent 27f4801 commit 7c1c4db
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 34 deletions.
3 changes: 2 additions & 1 deletion Runtime/Scripts/Beacon/BeaconConnectorDotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion Runtime/Scripts/Beacon/IBeaconConnector.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -40,7 +42,7 @@ public interface IBeaconConnector
/// </summary>
/// <param name="networkName">The name of the desired network.</param>
/// <param name="networkRPC">The RPC to the desired network</param>
public void RequestTezosPermission(string networkName = "", string networkRPC = "");
public Awaitable RequestTezosPermission(string networkName = "", string networkRPC = "");

/// <summary>
/// Allows requesting a new operation such as sending tezos or initiating a smart contract.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static T Instantiate()
{
if (!IsInstantiated())
{
T t = GameObject.FindObjectOfType<T>();
T t = FindFirstObjectByType<T>();
GameObject go = null;
if (t != null)
{
Expand Down
3 changes: 2 additions & 1 deletion Runtime/Scripts/Tezos/Wallet/IWalletProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Beacon.Sdk.Beacon.Sign;
using TezosSDK.Beacon;
using UnityEngine;

namespace TezosSDK.Tezos.Wallet
{
Expand All @@ -19,7 +20,7 @@ public interface IWalletProvider
/// Makes a call to connect with a wallet
/// <param name="withRedirectToWallet">Should we open wallet app on mobiles after connect?</param>
/// </summary>
void Connect(WalletProviderType walletProvider, bool withRedirectToWallet = true);
Awaitable Connect(WalletProviderType walletProvider, bool withRedirectToWallet = true);

/// <summary>
/// Unpair with wallet and disconnect
Expand Down
49 changes: 21 additions & 28 deletions Runtime/Scripts/Tezos/Wallet/WalletProvider.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
Expand All @@ -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 += _ =>
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions Runtime/View/AuthenticationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7c1c4db

Please sign in to comment.