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

Fix android login #134

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
70 changes: 70 additions & 0 deletions Runtime/Scripts/Helpers/WaitUntilForSeconds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using UnityEngine;

namespace TezosSDK.Helpers
{
public class WaitUntilForSeconds: CustomYieldInstruction
{
float pauseTime;
float timer;
bool waitingForFirst;
Func<bool> myChecker;
Action<float> onInterrupt;
bool alwaysTrue;

public WaitUntilForSeconds(Func<bool> myChecker, float pauseTime,
Action<float> 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;
}
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Scripts/Helpers/WaitUntilForSeconds.cs.meta

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

1 change: 1 addition & 0 deletions Runtime/Scripts/Tezos/Tezos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public Tezos(DAppMetadata providedDAppMetadata = null)
};

Wallet = new WalletProvider(dAppMetadata);
Wallet.Connect(WalletProviderType.beacon, false);

MessageReceiver = Wallet.MessageReceiver;
MessageReceiver.AccountConnected += _ =>
Expand Down
63 changes: 27 additions & 36 deletions Runtime/Scripts/Tezos/Wallet/WalletProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,22 +34,6 @@ private void InitBeaconConnector()
? unityBeacon.GetComponent<WalletMessageReceiver>()
: new GameObject("UnityBeacon").AddComponent<WalletMessageReceiver>();

// 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 =>
{
Expand All @@ -72,28 +57,35 @@ private void InitBeaconConnector()
CoroutineRunner.Instance.StartWrappedCoroutine(
new CoroutineWrapper<object>(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
}

Expand All @@ -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()
Expand Down
Loading