Skip to content

Commit

Permalink
Feat/configurable tezos instance (#133)
Browse files Browse the repository at this point in the history
* Added configurable Tezos singleton instance;

* Implemented configurable Tezos DApp name for WebGL;

* Added configurable RPC address for Tezos singleton;
  • Loading branch information
k-karuna authored Oct 24, 2023
1 parent 27f4801 commit adecba5
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 51 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [2.0.6]
## [2.0.7] - 2023-10-24
### Added
- Configurable parameters (`networkType`, `rpcUrl`, `dAppMetadata` and `logLevel`) for Tezos singleton instance.

### Changed
- Default RPC node to tezos.marigold.dev

## [2.0.6] - 2023-10-16
### Changed
- Disabled `Kukai Embed` silent signing
- Updated `Kukai Embed` to `0.8.9`
Expand Down Expand Up @@ -162,7 +169,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added auto releases with GH actions


[unreleased]: https://github.com/trilitech/tezos-unity-sdk/compare/2.0.6...HEAD
[unreleased]: https://github.com/trilitech/tezos-unity-sdk/compare/2.0.7...HEAD
[2.0.7]: https://github.com/trilitech/tezos-unity-sdk/releases/tag/2.0.7
[2.0.6]: https://github.com/trilitech/tezos-unity-sdk/releases/tag/2.0.6
[2.0.5]: https://github.com/trilitech/tezos-unity-sdk/releases/tag/2.0.5
[2.0.4]: https://github.com/trilitech/tezos-unity-sdk/releases/tag/2.0.4
Expand Down
7 changes: 5 additions & 2 deletions Runtime/Scripts/Beacon/BeaconConnection.jslib
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
mergeInto(LibraryManager.library, {
JsInitWallet: function (network, rpc, walletProvider) {
JsInitWallet: function (network, rpc, walletProvider, appName, appUrl, iconUrl) {
InitWalletProvider(
UTF8ToString(network),
UTF8ToString(rpc),
UTF8ToString(walletProvider)
UTF8ToString(walletProvider),
UTF8ToString(appName),
UTF8ToString(appUrl),
UTF8ToString(iconUrl)
);
},

Expand Down
16 changes: 11 additions & 5 deletions Runtime/Scripts/Beacon/BeaconConnectorDotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ public class BeaconConnectorDotNet : IBeaconConnector, IDisposable
private DappBeaconClient BeaconDappClient { get; set; }
private string _network;
private string _rpc;
private DAppMetadata _dAppMetadata;

#region IBeaconConnector

public async void ConnectAccount()
{
if (BeaconDappClient != null) return;
Expand All @@ -41,9 +42,9 @@ public async void ConnectAccount()

var options = new BeaconOptions
{
AppName = "Tezos Unity SDK",
AppUrl = "https://tezos.com/unity",
IconUrl = "https://unity.com/sites/default/files/2022-09/unity-tab-small.png",
AppName = _dAppMetadata.Name,
AppUrl = _dAppMetadata.Url,
IconUrl = _dAppMetadata.Icon,
KnownRelayServers = Constants.KnownRelayServers,
DatabaseConnectionString = $"Filename={pathToDb};Connection=direct;Upgrade=true"
};
Expand Down Expand Up @@ -92,10 +93,15 @@ public void DisconnectAccount()
UnityMainThreadDispatcher.Enqueue(_walletMessageReceiver.OnAccountDisconnected, string.Empty);
}

public void InitWalletProvider(string network, string rpc, WalletProviderType walletProviderType)
public void InitWalletProvider(
string network,
string rpc,
WalletProviderType walletProviderType,
DAppMetadata dAppMetadata)
{
_network = network;
_rpc = rpc;
_dAppMetadata = dAppMetadata;
}

public void SetWalletMessageReceiver(WalletMessageReceiver messageReceiver)
Expand Down
35 changes: 26 additions & 9 deletions Runtime/Scripts/Beacon/BeaconConnectorWebGl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Runtime.InteropServices;
using Beacon.Sdk.Beacon.Sign;
using TezosSDK.Tezos;
using TezosSDK.Tezos.Wallet;

namespace TezosSDK.Beacon
Expand All @@ -13,9 +14,15 @@ namespace TezosSDK.Beacon
public class BeaconConnectorWebGl : IBeaconConnector
{
#region Bridge to external functions

[DllImport("__Internal")]
private static extern void JsInitWallet(string network, string rpc, string walletProvider);
private static extern void JsInitWallet(
string network,
string rpc,
string walletProvider,
string appName,
string appUrl,
string iconUrl);

[DllImport("__Internal")]
private static extern void JsConnectAccount();
Expand All @@ -31,20 +38,30 @@ public class BeaconConnectorWebGl : IBeaconConnector

[DllImport("__Internal")]
private static extern string JsGetActiveAccountAddress();

[DllImport("__Internal")]
private static extern string JsRequestContractOrigination(string script, string delegateAddress);

[DllImport("__Internal")]
private static extern string JsUnityReadyEvent();

#endregion

private string _activeAccountAddress;

public void InitWalletProvider(string network, string rpc, WalletProviderType walletProviderType)

public void InitWalletProvider(
string network,
string rpc,
WalletProviderType walletProviderType,
DAppMetadata dAppMetadata)
{
JsInitWallet(network, rpc, walletProviderType.ToString());
JsInitWallet(
network,
rpc,
walletProviderType.ToString(),
dAppMetadata.Name,
dAppMetadata.Url,
dAppMetadata.Icon);
}

public void OnReady()
Expand Down Expand Up @@ -76,12 +93,12 @@ public void RequestTezosOperation(string destination, string entryPoint = "defau
{
JsSendContractCall(destination, amount.ToString(), entryPoint, arg);
}

public void RequestTezosSignPayload(SignPayloadType signingType, string payload)
{
JsSignPayload((int)signingType, payload);
}

public void RequestContractOrigination(string script, string delegateAddress = null)
{
JsRequestContractOrigination(script, delegateAddress);
Expand Down
8 changes: 7 additions & 1 deletion Runtime/Scripts/Beacon/IBeaconConnector.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Beacon.Sdk.Beacon.Sign;
using Netezos.Forging.Models;
using TezosSDK.Tezos;
using TezosSDK.Tezos.Wallet;

namespace TezosSDK.Beacon
Expand All @@ -16,7 +17,12 @@ public interface IBeaconConnector
/// <param name="network">Name of the network to connect</param>
/// <param name="rpc">Uri of an specific RPC.</param>
/// <param name="walletProviderType">Type of wallet, e.g. "beacon" or "kukai"</param>
void InitWalletProvider(string network, string rpc, WalletProviderType walletProviderType);
/// <param name="dAppMetadata">Metadata of SDK consumer DApp</param>
void InitWalletProvider(
string network,
string rpc,
WalletProviderType walletProviderType,
DAppMetadata dAppMetadata);

/// <summary>
/// Callback that needed in WebGL to determine that UI is rendered
Expand Down
20 changes: 20 additions & 0 deletions Runtime/Scripts/Tezos/DAppMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace TezosSDK.Tezos
{
public class DAppMetadata
{
/// <summary>
/// SDK consumer DApp name.
/// </summary>
public string Name { get; set; }

/// <summary>
/// SDK consumer App Url address, starts with https://.
/// </summary>
public string Url { get; set; }

/// <summary>
/// SDK consumer Icon Url address, starts with https://.
/// </summary>
public string Icon { get; set; }
}
}
3 changes: 3 additions & 0 deletions Runtime/Scripts/Tezos/DAppMetadata.cs.meta

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

19 changes: 13 additions & 6 deletions Runtime/Scripts/Tezos/Tezos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,40 @@ public class Tezos : ITezos
public IWalletProvider Wallet { get; }
public IFA2 TokenContract { get; set; }

public Tezos()
public Tezos(DAppMetadata providedDAppMetadata = null)
{
var dataProviderConfig = new TzKTProviderConfig();
API = new TezosAPI(dataProviderConfig);
Wallet = new WalletProvider();

var dAppMetadata = providedDAppMetadata ?? new DAppMetadata
{
Name = TezosConfig.Instance.DefaultDAppName,
Url = TezosConfig.Instance.DefaultDAppUrl,
Icon = TezosConfig.Instance.DefaultIconUrl
};

Wallet = new WalletProvider(dAppMetadata);

MessageReceiver = Wallet.MessageReceiver;

MessageReceiver.AccountConnected += _ =>
{
TokenContract = PlayerPrefs.HasKey("CurrentContract:" + Wallet.GetActiveAddress())
? new TokenContract(PlayerPrefs.GetString("CurrentContract:" + Wallet.GetActiveAddress()))
: new TokenContract();
};
}

public IEnumerator GetCurrentWalletBalance(Action<ulong> callback)
{
var address = Wallet.GetActiveAddress();
return API.GetTezosBalance(callback, address);
}

public IEnumerator GetOriginatedContracts(Action<IEnumerable<TokenContract>> callback)
{
var codeHash = Resources.Load<TextAsset>("Contracts/FA2TokenContractCodeHash")
.text;

return API.GetOriginatedContractsForOwner(
callback: callback,
creator: Wallet.GetActiveAddress(),
Expand Down
9 changes: 4 additions & 5 deletions Runtime/Scripts/Tezos/TezosConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ namespace TezosSDK.Tezos
public class TezosConfig
{
private static TezosConfig _instance;

// Singleton instance
public static TezosConfig Instance => _instance ??= new TezosConfig();

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

public string RpcBaseUrl => $"https://rpc.{Network}.teztnets.xyz";
public string DefaultDAppName => "Tezos Unity SDK";
public string DefaultDAppUrl => "https://tezos.com/unity";
public string DefaultIconUrl => "https://unity.com/sites/default/files/2022-09/unity-tab-small.png";
public string RpcBaseUrl { get; set; } = $"https://{NetworkType.ghostnet.ToString()}.tezos.marigold.dev";
public int DefaultTimeoutSeconds => 45;
}

Expand Down
28 changes: 25 additions & 3 deletions Runtime/Scripts/Tezos/TezosSingleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,32 @@ public class TezosSingleton : SingletonMonoBehaviour<TezosSingleton>, ITezos
protected override void Awake()
{
base.Awake();
_tezos ??= new Tezos();
}

public static ITezos ConfiguredInstance(
NetworkType networkType,
string rpcUrl = null,
DAppMetadata dAppMetadata = null,
Logger.LogLevel logLevel = Logger.LogLevel.Debug)
{
Logger.CurrentLogLevel = logLevel;

if (!string.IsNullOrEmpty(rpcUrl))
{
TezosConfig.Instance.RpcBaseUrl = rpcUrl;
}
else if (networkType != TezosConfig.Instance.Network)
{
TezosConfig.Instance.RpcBaseUrl = TezosConfig
.Instance
.RpcBaseUrl
.Replace(TezosConfig.Instance.Network.ToString(), networkType.ToString());
}

Logger.CurrentLogLevel = Logger.LogLevel.Debug;
TezosConfig.Instance.Network = NetworkType.ghostnet;
_tezos = new Tezos();
TezosConfig.Instance.Network = networkType;
_tezos ??= new Tezos(dAppMetadata);
return Instance;
}

void OnApplicationQuit()
Expand Down
11 changes: 7 additions & 4 deletions Runtime/Scripts/Tezos/Wallet/WalletProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ public class WalletProvider : IWalletProvider, IDisposable
{
public WalletMessageReceiver MessageReceiver { get; private set; }
private IBeaconConnector _beaconConnector;
private DAppMetadata _dAppMetadata;

private string _handshake;
private string _pubKey;
private string _signature;
private string _transactionHash;

public WalletProvider()
public WalletProvider(DAppMetadata dAppMetadata)
{
_dAppMetadata = dAppMetadata;
InitBeaconConnector();
}

Expand Down Expand Up @@ -102,11 +104,12 @@ public void OnReady()

public void Connect(WalletProviderType walletProvider, bool withRedirectToWallet)
{

_beaconConnector.InitWalletProvider(
network: TezosConfig.Instance.Network.ToString(),
rpc: TezosConfig.Instance.RpcBaseUrl,
walletProviderType: walletProvider);
walletProviderType: walletProvider,
dAppMetadata: _dAppMetadata);

_beaconConnector.ConnectAccount();
CoroutineRunner.Instance.StartWrappedCoroutine(OnOpenWallet(withRedirectToWallet));
}
Expand Down Expand Up @@ -162,4 +165,4 @@ public void Dispose()
}
}
}
}
}
16 changes: 13 additions & 3 deletions Samples~/Scripts/DemoExample/Core/ExampleManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using Beacon.Sdk.Beacon.Permission;
using Beacon.Sdk.Beacon.Sign;
using Netezos.Encoding;
using TezosSDK.Beacon;
using TezosSDK.Helpers;
using TezosSDK.Scripts.FileUploaders;
using TezosSDK.Scripts.IpfsUploader;
using TezosSDK.Tezos;
using TezosSDK.Tezos.API.Models;
using TezosSDK.Tezos.API.Models.Tokens;
Expand Down Expand Up @@ -36,7 +35,18 @@ public ExampleManager()

public void Init(Action<bool> callback = null)
{
Tezos = TezosSingleton.Instance;
Tezos = TezosSingleton.ConfiguredInstance(
networkType: NetworkType.ghostnet,
rpcUrl: "https://rpc.tzkt.io/ghostnet",
dAppMetadata: new DAppMetadata
{
Name = "Tezos Demo Example Game",
Url = "https://github.com/trilitech/tezos-unity-sdk/tree/main/Samples~",
Icon =
"https://raw.githubusercontent.com/" +
"trilitech/tezos-unity-sdk/main/Samples%7E/Art/UI/Icons/skill_icon_04.png"
});

_networkRPC = TezosConfig.Instance.RpcBaseUrl;
}

Expand Down
2 changes: 1 addition & 1 deletion WebGLFrontend/output/StreamingAssets/webgl-frontend.js

Large diffs are not rendered by default.

Loading

0 comments on commit adecba5

Please sign in to comment.