Skip to main content

Integrate Web3Auth with the 5ire Blockchain in Unity

While using the Web3Auth Unity SDK, you get the private key within the user scope. This private key can interact with the Nethereum Library to make EVM-based blockchain calls, like getting the user's account, fetching balance, signing transactions, sending transactions, reading from and writing to smart contracts. We have highlighted a few examples to get you started.

Installation

In this reference, we're using the Nethereum library to demonstrate how to make blockchain calls using it with Web3Auth.

Package installation instructions (Official repository)

Install via Package Manager using OpenUpm

  • open Edit/Project Settings/Package Manager

  • add a new Scoped Registry (or edit the existing OpenUPM entry)

  • click Save or Apply

  • Open Window/Package Manager

  • click +

  • select Add package by name... or Add package from git URL...

  • paste com.nethereum.unity into name

  • paste 4.19.2 into version (or your preferred one)

  • click Add

Installing package for old version

  • Download the latest net461dllsAOT.zip package from Nethereum's latest release

  • Extract and the rename the folder to NethereumLib for easy identification.

  • Move the folder to the Assets/Plugins folder of your Unity project.

  • You might have to delete a few files from the NethereumLib folder, if you're getting any errors while building the project. For our implementation, we deleted the following files: Newtonsoft.Json.dll, all the files starting with System.*, UnityEngine.dll, Nethereum.Web3Lite.dll, Nethereum.HdWallet.dll, NBitcoin.dll, Nethereum.RPC.Reactive.dll and Common.Logging.Core.dll.

info

We have followed this guide to set up the Nethereum package in our app. You can check their sample applications as well for a decent reference.

You can also check the Web3Auth Unity Sample Apps, where we have added the required packages.

Chain Details for 5ire

  • Chain ID: 0x3E3
  • Public RPC URL: https://rpc.5ire.network (Avoid using public rpcTarget in production, use services like Infura, Quicknode etc)
  • Display Name: 5ireChain Mainnet
  • Block Explorer Link: https://5irescan.io
  • Ticker: 5ire
  • Ticker Name: 5ire

Initialize

using Nethereum.Web3;
using Nethereum.Util;
using Nethereum.Signer;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.ABI.Encoders;
using Nethereum.Hex.HexTypes;
using Nethereum.Web3.Accounts;
using Nethereum.Web3.Accounts.Managed;

public class Web3AuthScript : MonoBehaviour
{
Web3 web3;
Web3Auth web3Auth;
private string privateKey;
private Account account;

const string rpcURL = "" // EVM chain RPC URL

void Start()
{

web3Auth = GetComponent<Web3Auth>();

// Add Web3Auth Unity SDK Initialisation Code here

web3Auth.onLogin += onLogin;
web3Auth.onLogout += onLogout;
web3 = new Web3(rpcURL);
}

private void onLogin(Web3AuthResponse response)
{
privateKey = response.privKey;
var newAccount = new Account(privateKey);
account = newAccount;
}
// ...
}

Get User Info

You get the user information after a successful login returned from the login method. The userInfo object contains the user information, whereas the privKey object contains the private key that can be used to make blockchain calls.

private string userInfo;

private void onLogin(Web3AuthResponse response)
{
userInfo = JsonConvert.SerializeObject(response.userInfo, Formatting.Indented);
}

Get Account

In this reference, we're using the Nethereum library to demonstrate how to make blockchain calls using it with Web3Auth.

Package installation instructions (Official repository)

Install via Package Manager using OpenUpm

  • open Edit/Project Settings/Package Manager

  • add a new Scoped Registry (or edit the existing OpenUPM entry)

  • click Save or Apply

  • Open Window/Package Manager

  • click +

  • select Add package by name... or Add package from git URL...

  • paste com.nethereum.unity into name

  • paste 4.19.2 into version (or your preferred one)

  • click Add

Installing package for old version

  • Download the latest net461dllsAOT.zip package from Nethereum's latest release

  • Extract and the rename the folder to NethereumLib for easy identification.

  • Move the folder to the Assets/Plugins folder of your Unity project.

  • You might have to delete a few files from the NethereumLib fold er, if you're getting any errors while building the project. For our implementation, we deleted the following files: Newtonsoft.Json.dll, all the files starting with System.*, UnityEngine.dll, Nethereum.Web3Lite.dll, Nethereum.HdWallet.dll, NBitcoin.dll, Nethereum.RPC.Reactive.dll and Common.Logging.Core.dll.

info

We have followed this guide to set up the Nethereum package in our app. You can check their sample applications as well for a decent reference.

You can also check the Web3Auth Unity Sample Apps, where we have added the required packages.

Get Balance

public void getBalance() {
if (account == null) {
Debug.Log("Please Login First");
return;
}
var balance = web3.Eth.GetBalance.SendRequestAsync(account.Address).Result.Value;

Debug.Log(balance);
}

Sign a message

public void signMessage() {
if (account == null) {
Debug.Log("Please Login First");
return;
}
var msg = "test message 19/01/2023 02:55PM";
var signer = new EthereumMessageSigner();
var signature = signer.EncodeUTF8AndSign(msg, new EthECKey(privateKey));

Debug.Log(signature);
}

Send Transaction

public async void sendTransaction() {
if (account == null) {
Debug.Log("Please Login First");
return;
}
var toAddress = "0x2E464..82D5057fB507Cc21";
var transaction = await web3.TransactionManager.SendTransactionAsync(account.Address, toAddress, new Nethereum.Hex.HexTypes.HexBigInteger(1));

Debug.Log(transaction);
}