Skip to main content

Integrate Web3Auth with the Klaytn 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, fetch balance, sign transaction, send transaction, read from and write to the smart contract, etc. We have highlighted a few here to get you started quickly on that.

Installation

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

  • 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 Klaytn

  • Chain ID: 0x2019
  • Public RPC URL: https://public-node-api.klaytnapi.com/v1/cypress (Avoid using public rpcTarget in production, use services like Infura, Quicknode etc)
  • Display Name: Klaytn Mainnet
  • Block Explorer Link: https://scope.klaytn.com
  • Ticker: KLAY
  • Ticker Name: KLAY

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.

  • 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.

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);
}