Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing mobile login with Awaitable #132

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 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 @@ -32,7 +33,7 @@ public class BeaconConnectorDotNet : IBeaconConnector, IDisposable

#region IBeaconConnector

public async void ConnectAccount()
public async Task ConnectAccount()
{
if (BeaconDappClient != null) return;

Expand Down Expand Up @@ -102,8 +103,7 @@ public void SetWalletMessageReceiver(WalletMessageReceiver messageReceiver)
{
_walletMessageReceiver = messageReceiver;
}

public async void RequestTezosPermission(string networkName = "", string networkRPC = "")
public async Task RequestTezosPermission(string networkName = "", string networkRPC = "")
{
if (!Enum.TryParse(networkName, out NetworkType networkType))
networkType = TezosConfig.Instance.Network;
Expand Down
7 changes: 4 additions & 3 deletions Runtime/Scripts/Beacon/IBeaconConnector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using Beacon.Sdk.Beacon.Sign;
using Netezos.Forging.Models;
using TezosSDK.Tezos.Wallet;
using UnityEngine;

namespace TezosSDK.Beacon
{
Expand All @@ -27,7 +28,7 @@ public interface IBeaconConnector
/// Starts the connection between Beacon SDK and a wallet to connect to
/// an account
/// </summary>
void ConnectAccount();
Task ConnectAccount();

/// <summary>
/// Checks if there is an active account paired.
Expand All @@ -40,7 +41,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 Task 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
15 changes: 15 additions & 0 deletions Runtime/Scripts/Helpers/TaskExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;

namespace TezosSDK.Helpers
{
public static class TaskExtension
{
public static IEnumerator ToCoroutine(this Task task)
{
var t = Task.Run(async () => await task);
yield return new WaitUntil(() => t.IsCompleted);
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Scripts/Helpers/TaskExtension.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Runtime/Scripts/Tezos/TezosConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TezosConfig

public NetworkType Network { get; set; } = NetworkType.ghostnet;

public string RpcBaseUrl => $"https://rpc.{Network}.teztnets.xyz";
public string RpcBaseUrl => $"https://{Network}.tezos.marigold.dev";
public int DefaultTimeoutSeconds => 45;
}

Expand Down
7 changes: 5 additions & 2 deletions Runtime/Scripts/Tezos/Wallet/IWalletProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Beacon.Sdk.Beacon.Sign;
using System.Collections;
using System.Threading.Tasks;
using Beacon.Sdk.Beacon.Sign;
using TezosSDK.Beacon;
using UnityEngine;

namespace TezosSDK.Tezos.Wallet
{
Expand All @@ -19,7 +22,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);
IEnumerator Connect(WalletProviderType walletProvider, bool withRedirectToWallet = true);

/// <summary>
/// Unpair with wallet and disconnect
Expand Down
66 changes: 35 additions & 31 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 @@ -20,10 +22,10 @@ public class WalletProvider : IWalletProvider, IDisposable

public WalletProvider()
{
InitBeaconConnector();
CoroutineRunner.Instance.StartWrappedCoroutine(InitBeaconConnector());
}

private void InitBeaconConnector()
private IEnumerator 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);
yield return Connect(WalletProviderType.beacon, withRedirectToWallet: false);

// todo: maybe call RequestTezosPermission from _beaconConnector?
MessageReceiver.PairingCompleted += _ =>
Expand All @@ -47,7 +49,11 @@ private void InitBeaconConnector()
networkRPC: TezosConfig.Instance.RpcBaseUrl);
};
#endif
MessageReceiver.HandshakeReceived += handshake => { _handshake = handshake; };
MessageReceiver.HandshakeReceived += handshake =>
{
Debug.Log($"Handshake Received: ${handshake}");
_handshake = handshake;
};
MessageReceiver.AccountConnected += account =>
{
var json = JsonSerializer.Deserialize<JsonElement>(account);
Expand All @@ -72,43 +78,41 @@ 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 IEnumerator Connect(WalletProviderType walletProvider, bool withRedirectToWallet)
{

Debug.Log("InitWalletProvider");
_beaconConnector.InitWalletProvider(
network: TezosConfig.Instance.Network.ToString(),
rpc: TezosConfig.Instance.RpcBaseUrl,
walletProviderType: walletProvider);
_beaconConnector.ConnectAccount();
CoroutineRunner.Instance.StartWrappedCoroutine(OnOpenWallet(withRedirectToWallet));
Debug.Log("ConnectAccount");
var connectAccountCoroutine = _beaconConnector.ConnectAccount().ToCoroutine();
yield return connectAccountCoroutine;

#if UNITY_ANDROID || UNITY_IOS
if (withRedirectToWallet){
Debug.Log("RequestTezosPermission");
yield return _beaconConnector.RequestTezosPermission(
networkName: TezosConfig.Instance.Network.ToString(),
networkRPC: TezosConfig.Instance.RpcBaseUrl).ToCoroutine();
if (string.IsNullOrEmpty(_handshake))
{
//No handshake, Waiting for handshake...
Debug.Log("No handshake, Waiting for handshake...");
// yield return new WaitUntil(() => string.IsNullOrEmpty(_handshake));
yield return new WaitForSecondsRealtime(2.5f);
}
if (!string.IsNullOrEmpty(_handshake)){
Debug.Log("tezos://?type=tzip10&data=" + _handshake);
Application.OpenURL($"tezos://?type=tzip10&data={_handshake}");
}
}
#endif
}

public void Disconnect()
Expand Down
Loading