• Unity SDK
  • Get Started
  • Connect Wallet

Connect wallet and authenticate

Connecting to a Web3 wallet, such as MetaMask via WalletConnect provides a link between a Wallet Address and a user's Game Account.

There are two ways to get a Session Key from your wallet.

  • Connect via QRCode when building Desktop-based applications. Usually, it's the option used for the Standalone option (Linux, Windows, macOS machine) when the user cannot access to their wallet directly on their machine; they have to generate a QR code and scan it with a mobile app to open a wallet there. This is also an option for you to connect while running your project in the Unity Editor playmode.
  • Connect via WalletConnect.Instance.OpenMobileWallet when building mobile-based applications. Usually, it's the option used for an iOS/Android device with an installed wallet app.

Both of these methods generate a linking url that creates a request in a MetaMask wallet to connect.

Connect via QRCode

Connecting a wallet via QRCode involves the following steps:

  1. Creating and caching a smart contract.
  2. Connecting a wallet via QRCode.
  3. Accepting connection.
  4. Creating an instance of MirageSDKWrapper.

1. Create and cache a smart contract

To create and cache a smart contract, do the following:

  1. Initialize a WalletConnect session using WalletConnect.cs script.
    1. Attach WalletConnectUnityMonoAdapter script to a GameObject in your scene.
    2. Call WalletConnect.Connect() from your starter script Awake or Start method to create and connect the session.
    3. Wait until WalletConnect establishes a connection to the WalletConnect bridge and requests the wallet app to connect. The default wallet app is MetaMask, but you can change it by editing WalletConnectSettings scriptable object or using your own alternative instance for this settings file locally in your project.
    4. Approve the connection on the wallet side. If the platform you use is Android make sure to use the same wallet for subsequent transactions as the one that was used during the connection process.
  2. Use the static method below to create a new MirageSDK instance. You can only create this instance once WalletConnect.Status value is changed to WalletConnected. This happens only after user approves the connection on their wallet side.
    MirageSDKFactory.GetMirageSDKInstance(string ProviderURL);
  3. Create and cache a contract by using the IMirageSDK initialized instance.
    IContract GetContract(string contractAddress, string contractABI);

Now you can interact with the created contract via GetData() and CallMethod(). Read more about interacting with a smart contact.

2. Connect a wallet via QRCode

  1. To connect with QRCode, use a QRCodeImage component. The SDK already comes with a QRCode generator and a function that handles everything.

  2. Call UpdateQRCode(string url) by giving it a URL. You can then use .SetImageActive(bool) to activate/deactivate the QRCode.

Here is the way it is implemented in our Examples. We get the ConnectURL from a walletconnect Instance. Then we generate the QRCode from it and activate the QRCode's image so it can be scanned:

var connectURL = WalletConnect.Instance.ConnectURL;

This QRCode should be scanned from your MetaMask mobile app. (It asks you to connect the same way you would with the non QRCode version).

At this point, you are connected and any transactions that require the user signing a message will pop up in their MetaMask app on their phone.


Occasionally, the MetaMask mobile app does not pop up by itself. Should this be the case, open the app manually.

3. Accept connection

If you agree to connect, a Session Key is saved in PlayerPrefs for future use.

4. Create an instance of MirageSDKWrapper

Create an instance of an MirageSDKWrapper class via MirageSDKWrapper.GetSDKInstance() method after successful connection to your wallet.

IMirageSDK GetMirageSDKInstance(string providerURI, bool autoSetup = false)

Inside (MirageSDK/Examples/UseCases/LinkingAccountWallet) is an example script demonstrating how to link a Web3 wallet (Metamask) to a player account.

Reference example of connecting a wallet to an account

This is an example from the SDK on how to link a Web3 wallet to a player account.

  1. To connect a wallet, first make an instance of a Web3 class and call Initialize method after logging in MetaMask

    string provider_url = "<ethereum node url>";
    Web3 web3 = new Web3(provider_url);
  2. To prove ownership of the wallet address, the player should sign an arbitrary string > zero length. We recommend using uuid strings. The Web3 Wallet provides the sign functionality. To start this step call method Sign.

    string message = "Hi I am a message !"
    string signature = await web3.Sign(message); //returns the signature.
  3. The next step involves the backend server-side. You can view an example script here. It returns a signature if authentication is successful. Below is an extract from this script:

    package main
    import (
        cryptoTyped "github.com/ethersphere/bee/pkg/crypto"
        eip712 "github.com/ethersphere/bee/pkg/crypto/eip712"
    // Endpoint for evm rpc requests
    var ankr = "https://rpc.ankr.com/eth_rinkeby"
    var client, clientConnectErr = ethclient.Dial(ankr)
    // Simple ERC721 contract with methods to update nft fields and signature verification mechanism
    // For this demo use contract in "contracts/GameItem.sol"
    var contractAccount = "0x159D0A933137f3EC155f43834BDFCd534A8bfd61"
    // Private key should belong to account that deployed contract
    var privateKeyString = "a0022ef0d495da2ad7e639f9d93045661f149f31472cedf067a0712b391749df"
    // Private key on the server side For GD-3 (use case 8)
    var privateKey, _ = crypto.HexToECDSA(privateKeyString)
    // user's address associated with the hero id
    var clientAddress = common.HexToAddress("0x24d13b65bAbFc38f6eCA86D9e73C539a1e0C0196")
  4. To verify the user make a call using the method POST /account/verification/address with the payload i.e. returned signature

    "message": "Hi I am a message !", // your message
    "signature":"0x..." // result of Web3.Sign()
  5. This method gets the address of the user from the signature so you can write it to database.

    sigPublicKey := getAddrFromSign(input.Signature, data)
    address := string(sigPublicKey);
    // add address to a database

With these steps, you should now be able to connect a wallet to an account using the MirageSDK. The provided examples demonstrate how to link a Web3 wallet (such as MetaMask) to a player account and verify user ownership using signature verification.