Skip to main content

How to use Unity SDK

Key Management

Keys with the combination of walletId and keyName (e.g. "jp.co.ginco.clientsdk" or "unity_secure_storage_service") are stored in a secure location on each platform. 128, 160, 192, 224, or 256 bits can be selected for strength when calling GenerateMnemonic. For example, the mnemonic is 12 characters for 128 bits, and 24 characters for 256 bits.

The password argument is optional, and if you include it, the mnemonic will be encrypted with a key derived from the password and stored. This is the feature for advanced users, since if the user loses the password, the mnemonic cannot be recovered anymore, while it can increase the level of security.

using jp.co.ginco.KeyManager;

// Generate Mnemonic
KeyManager.GenerateMnemonic(string walletId, string keyName, int strength, [string password]);

// Get Mnemonic
string mnemonic = KeyManager.GetMnemonic(string walletId, string keyName), [string password];

// Import Mnemonic
KeyManager.ImportMnemonic(string walletId, string keyName, string mnemonic, [string password]);

// Destroy Mnemonic
KeyManager.DestroyMnemonic(string walletId, string keyName);
info

Mnemonic is a representation of the seed key of a wallet, and the specification is defined in BIP39. A list of 2048 words, called a word list, is prepared and this list of randomly selected words is called Mnemonic.

https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki

Wallet

A seed key generated by generateMnemonic or importMnemonic can be used to create a wallet using the combination of walletId and keyName. From the wallet, you can obtain addresses, check balances, and generate transactions.

NetworkParam can be used to connect to Evm-based blockchains as in the below example. Please use chainId, networkId and rpcUrl of the chain you wish to connect.

Since wallet is made by HD wallet mechanism, it is possible to use multiple wallet accounts by specifying the index from 0 to 4294967295 from one seed key like wallet.Account(index).

An optional passphrase can be used when generating keys from mnemonics. If a password is used when storing mnemonics, a password is also required for calling each wallet method. Note, however, that the wallet address changes when a passphrase is used. This is the feature for advanced users, since if the user loses the passphrase or password, the mnemonic cannot be recovered anymore, while it can increase the level of security.

using ClientSdk = jp.co.ginco.ClientSdk;
using Ethereum = jp.co.ginco.Ethereum;

// Create Wallet
var sdk = new ClientSdk.SdkBase();
var networkParam = new Ethereum.NetworkParam(int chainId, int networkId, string rpcUrl);
var wallet = sdk.evm.CreateWallet(string walletId, string keyName, Ethereum.NetworkParam networkParam);

// Derive Private Key
string privKeyHex = wallet.Account(0).GetPrivateKey([string passphrase], [string password]);

// Derive Address
string address = wallet.Account(0).Address([string passphrase], [string password]);

// Get ETH Balance
await wallet.Account(0).Balance((balance, error) =>
{
if (error != null)
{
UnityEngine.Debug.Log(error);
}
else
{
string balance = ClientSdk.Helper.WeiToEther(balance);
}
}, [string passphrase], [string password]);


// Get FT Balance
await wallet.Account(0).FtBalance(string contractAddress, (balance, error) =>
{
if (error != null)
{
UnityEngine.Debug.Log(error);
}
else
{
string balance = ClientSdk.Helper.WeiToEther(balance);
}
}, [string passphrase], [string password]);
info

The HD wallet details is explained in detail in BIP32.

https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki

Transaction

A transaction is a cryptographically signed order from the wallet account. Transactions are sent to update the state of the blockchain network. The simplest transaction transfers a cryptographic asset from one account to another. You can create and sign a transaction with Ethereum.Txbuilder and send it with wallet.Account(0).SendTransaction(signedTx, callback).

// Create & Sign Transaction
await wallet.Account(0).NextNonce((nextNonce, error) =>
{
if (error != null)
{
UnityEngine.Debug.Log(error);
}
else
{
string signedTx = new Ethereum.TxBuilder()
.Value(String value) // in wei
.GasPrice(String gasPrice) // in wei
.GasLimit(int gasLimit)
.To(String toAddress)
.ChainId(int chainId)
.Nonce(nextNonce)
.Sign(wallet.Account(int index, [string passphrase], [string password]))
.Build();
}
}, [string passphrase], [string password]);

// Submit transaction
await wallet.Account(0).SendTransaction(signedTx, (txId, error) =>
{
if (error != null)
{
UnityEngine.Debug.Log(error);
}
else
{
UnityEngine.Debug.Log(txId);
}
});

Handle custom chainId

If you want to test using a local blockchain, you will need a custom chainId and networkId, which can be configured as follows.

using ClientSdk = jp.co.ginco.ClientSdk;

ClientSdk.ChainId.LOCAL_CHAIN = customChainId;
ClientSdk.ChainId.ChainIdToNetworkIdMap.Add(ClientSdk.ChainId.LOCAL_CHAIN, customNetworkId);
info

Interface is still being actively developed and might be subject to change