Using Wallets with Eidolon
The IWallet
interface is used universally between Desktop, Mobile and WebGL builds. The only thing that differs is their constructors and recommended usage flow. Below we dive into how the interface is used for each platform!
When building for WebGL
, the WebGLWallet
class is compatible with most popular browser wallets following the JSON-RPC
standard. It works seamlessly with wallets like MetaMask
, TrustWallet
, Enkrypt
, and many others, providing users with a wide range of wallet options for interacting with your Unity-based blockchain game.
Constructor
The WebGLWallet
class has a constructor that initializes a new instance. You can use this constructor to create instances of wallet providers for different blockchains. Both single-chain and multi-chain constructors are dynamic and work even if mixed together in the same script. Feel free to creatively use this to create awesome experiences.
Single-Chain Constructor
Example:
Once your wallet instance has been instantiated, you can use it to invoke functions like Connect
, SwitchChains
, SendTransaction
and so much more!
Multi-Chain Constructor
provider
(JsonRpcProvider): The RPC (Remote Procedure Call) endpoint URL of the blockchain network.
Example:
With the multi-chain constructor, you can connect to different blockchains and interact with smart contracts on those networks. It allows you to set a default network using the Project Setup window, whilst still allowing you to interact with contracts on other networks.
Methods
After instantiating your WebGLWallet
instance, you can utilize any of the below wallet methods anywhere in your Unity project.
Connect
Method Description
Connects the wallet provider to a specific Ethereum network using the provided chain ID. THis function will return the connected public address.
Code Example
Parameter Explanation
chainId
(string): The chain ID of the target network.
Connect (Overload)
Method Description
Connects the wallet provider to a specific network, adding the network if it’s not available on the users browser wallet. This function will return the connected public address.
Code Example
Parameter Explanation
chainId
(string): The chain ID of the target network.rpcUrl
(string): The RPC URL of the network.chainName
(string): The name of the network.nativeCurrencyName
(string): The native currency name.nativeCurrencySymbol
(string): The native currency symbol.nativeCurrencyDecimals
(string): The number of decimals for the native currency.blockExplorer
(string): The URL of the block explorer for the network.
SwitchChains
Method Description
Switches the connected Ethereum network to a different one using the specified chain ID.
Code Example
Parameter Explanation
chainId
(string): The chain ID of the target network.
SwitchChains (Overload)
Method Description
Switches the connected network, adding the network if it’s not available on the users browser wallet.
Code Example
Parameter Explanation
chainId
(string): The chain ID of the target network.rpcURL
(string): The RPC URL of the network.chainName
(string): The name of the network.nativeCurrencyName
(string): The native currency name.nativeCurrencySymbol
(string): The native currency symbol.nativeCurrencyDecimals
(string): The number of decimals for the native currency.blockExplorer
(string): The URL of the block explorer for the network.
Disconnect
Method Description
Disconnects from the currently connected wallet and clears the account information.
Code Example
GetTransactionReceipt
Method Description
Retrieves the transaction receipt after sending a transaction.
Code Example
Parameter Explanation
- None
GetChainId
Method Description
Retrieves the users current connected chain ID.
Code Example
Parameter Explanation
- None
SendTransaction
Method Description
Sends a transaction to a smart contract method. Parameters like gas, gasPrice and value are optional. This function will return a transaction hash if successful, or an exception if not.
Code Example
Parameter Explanation
to
(string): The address your sending the transaction to.gas
(optional) (string): The gas limit for the transaction.gasPrice
(optional) (string): The gas price for the transaction.value
(optional) (string): The value for chain native tokens.nonce
(optional) (string): The current transaction nonce count.data
(optional): The encoded data for a contract method and set of parameters.
Sign Message
Method Description
Prompts the connected account to sign a message.
Code Example
Parameter Explanation
message
(string): The message data you want the user to sign.
When building for either Desktop
or Mobile
, the EmbeddedWallet
class can be used and goes hand in hand with the Signer
class.
The Signer
class provides functionality to create and manage Ethereum accounts (wallets) for users within your Unity project. It includes methods for creating new accounts and loading existing ones. The class utilizes key storage mechanisms to store private keys and allows for interactive and invisible account creation and login.
By default, the generated keys are encrypted and stored within a Nethereum keystore. However, if you are developing on Android or Windows, you have the option to store the wallet file in the device-specific keystore, such as the Android Keystore or Windows Keystore.
Please note that in order to connect a signer to a session, you need to set it using the EmbeddedWallet
class:
Single-Chain Constructor
privateKey
(string): The private key that will handle signing transactions.- The
chainID
andRPC
is automatically assigned based on yourProject Setup
configuration.
Multi-Chain Constructor
privateKey
(string): The private key that will handle signing transactions.chainId
(string): The chain ID that you want this signer to connect to.provider
(JsonRpcProvider): The RPC (Remote Procedure Call) endpoint URL of the blockchain network.
This is important as it allows you to sign transactions to the blockchain from within your unity game. Let’s explore how we can use the signer class below:
Methods
After instantiating your Embedded Wallet
instance, you can utilize any of the below wallet methods anywhere in your Unity project.
Create
The Create
method allows developers to create a new Ethereum account (wallet) for a user. It takes a username and password as input parameters and performs the following steps:
- Checks if a wallet with the provided username already exists.
- If not, generates a new Ethereum private key and creates a wallet file.
- Encrypts the private key using the provided password and stores it in your keystore of choice.
Load
The Load
method enables developers to load an existing Ethereum account (wallet) for a user. It takes a username and an optional password (used for decryption) as input parameters and performs the following steps:
- Checks if a wallet with the provided username exists.
- If found, reads the KeyStore file and deserializes it.
- Decrypts the private key using the provided password.
- Provides you with access to the private key and public key for use in-game.
Please note that the public account can be accessed through PlayerPrefs.GetString("Account")
.
WalletExists
The WalletExists
method is a private helper method that checks if a wallet file exists for the given username.
Sign Message
Method Description
Prompts the connected account to sign a message.
Code Example
Parameter Explanation
message
(string): The message data you want the user to sign.
Example Usage
This foundation allows you as a developer to have various different approaches when it comes to onboarding new or logging in existing users. Let’s explore two common ways of utilizing the signers class:
Interactive Login
With this example, we allow the player to sign up and login using a very traditional system. By manually passing their preferred username and password, they can manage their own account and store information where they think is best.
The Interactive login example provides a user-friendly interface for account creation and login while connecting to Ethereum blockchain features, allowing for seamless interaction with decentralized applications.
Invisible Login
With this example, we generate a wallet ID and secret which is then stored within the device keystore for secure long-term storage. This allows you as the developer to create seamless background login flows which don’t require any input from your players.
The InvisibleLogin class provides a method for developers to create and manage Ethereum accounts on behalf of players, allowing them to seamlessly interact with decentralized applications without the need for players to handle username and password creation.