Unity

Create your game utilizing our c# sdk

Setup

Before doing any call to the blockchain we need to setup the correct packages to kleversdk become available in your unity project

First, you need to download the nugets below and the correct versions of it.

kleversdk.dll - ^0.0.2
dotnetstandard-bip39.dll - ^1.0.2
BouncyCastle.Crypto.dll - ^1.8.9

You can download it to extract the dlls at the Nuget website or you can clone the Unity example we've created.

Interacting with Kleverchain

Initializing Klever Provider

After doing the setup above in your project, it's very easy to start working with kleverchain, initially, you must create a KleverProvider to be able to use the functions of our package.

using kleversdk.core;
using kleversdk.provider;
using kleversdk.provider.Dto;


private KleverProvider kleverProvider;


private void Awake(){
        // need to instantiate the kleverProvider to use the kleverchain sdk
        environment = new NetworkConfig(kleversdk.provider.Network.TestNet);
	kleverProvider = new KleverProvider(environment);
  }

If you want to use the project on MainNet, just change the environment to

environment = new NetworkConfig(kleversdk.provider.Network.MainNet)

Initializing Wallet

To create a wallet and be able to sign Klever Provider transactions, you must have an initialized wallet object.

There are 2 ways to initialize a wallet object in klever sdk

  • Initializing with your seed(mnemonic bip-39)

var mnemonic = "word1 word2 ..."
var wallet = Wallet.DeriveFromMnemonic(mnemonic)

var otherWallet = Wallet.DeriveFromMnemonic(mnemonic,5)

If you do not pass the second parameter, the address generated will have an index of 0.

  • Initializing with Private Key

var privateKey = "hex private key"
var wallet = new Wallet(privateKey)

Sync Wallet

After you initialize your wallet, you need to do a sync to fetch the nonce and balance values of your wallet that were initialized.

Account acc =  wallet.GetAccount();
try { 
    await acc.Sync(kleverProvider);
}catch(Exception e) {
    Debug.Log("account does not exist in blockchain: {0}", e.ToString());
}

Send Transaction

To send a send transaction, both kleverProvider and your wallet must be initialized. Once you've secured this, sending a transaction is very simple, just follow the steps below.

Account acc =  wallet.GetAccount();
string message = "Be Klever!"
string toAddress = "other address here";

var tx = await kleverProvider.SendWithMessage(acc.Address.Bech32,acc.Nonce,toAddress,0.000001f,message);
var decoded = await this.kleverProvider.Decode(tx);
var signature = wallet.SignHex(decoded.Hash);
tx.AddSignature(signature);

var broadcastResult = await this.kleverProvider.Broadcast(tx);

Debug.Log(broadcastResult.String());

Congratulations, you successfully sent a transaction through your game to Kleverchain!!

You can see the example game we've created using the c# sdk at this link and if you want a example of other uses of Klever Provider you can see in this demo.

Last updated