How to connect to a Wallet

Assets/Emergence/Samples/Examples/ConnectingToWallet.cs

Connecting to a web3 wallet is a crucial step in enabling users to interact with blockchain-based applications. While the EmergenceSDK provides an overlay for handling wallet connections, you might want to bypass the default overlay and integrate the QR code directly into your custom user interface. This example demonstrates how to connect to a web3 wallet using the EmergenceSDK in Unity, giving you more control over the wallet connection process and allowing for a more seamless integration with your application's UI.

The example provided at the path above demonstrates how to connect to a web3 wallet using the EmergenceSDK. The following code creates a texture with a QR code that can be scanned with a phone. The connection is established when the handshake call returns successfully with the wallet address.

First, import the necessary namespaces:

using EmergenceSDK.Internal.Utils;
using EmergenceSDK.Services;
using UnityEngine;

These namespaces contain various utilities and services that we will need to interact with the EmergenceSDK in Unity.

Next, define a new class called ConnectingToWallet, which inherits from MonoBehaviour. This allows you to attach this script to a GameObject in Unity.

public class ConnectingToWallet : MonoBehaviour
{
    private IQRCodeService qrService;
    private IWalletService walletService;

    ...
}

Declare two private variables: qrService of type IQRCodeService and walletService of type IWalletService. These will be used to access the QR code and wallet-related services provided by EmergenceSDK.

In the Awake method, initialize the qrService and walletService variables by getting the respective service instances from the EmergenceServices class.

csharpCopy codeprivate void Awake()
{
    qrService = EmergenceServices.GetService<IQRCodeService>();
    walletService = EmergenceServices.GetService<IWalletService>();
}

The Start method is called at the beginning of the script's execution. Here, we call the qrService.GetQRCode method to generate a QR code.

csharpCopy codeprivate void Start()
{
    qrService.GetQRCode(GotQRCode, ErrorLogger.LogError);
}

The GotQRCode method is called when the qrService successfully returns a QR code (Texture2D) and a deviceid. You should present the QR code to the user for scanning. After the user scans the QR code and connects to the wallet, call the walletService.Handshake method to complete the handshake.

csharpCopy codeprivate void GotQRCode(Texture2D qrcode, string deviceid)
{
    // Present the QR code to the user for scanning

    // ....

    // Wait for the user to connect to the wallet
    walletService.Handshake(OnHandshakeSuccess, ErrorLogger.LogError);
}

The OnHandshakeSuccess method is called when the handshake is successful. It receives the wallet address as a parameter. You can now use the wallet address for any necessary operations. The example logs the wallet address to the console.

csharpCopy codeprivate void OnHandshakeSuccess(string walletaddress)
{
    // The user has connected to the wallet
    // You can now use the wallet address for whatever you need it for
    Debug.Log($"Wallet address: {walletaddress}");
}

By following this example, you can connect to a web3 wallet in your Unity project using the EmergenceSDK. The process involves generating a QR code, presenting it to the user for scanning, and completing the handshake to establish a connection with the wallet.

Full Code:

using EmergenceSDK.Internal.Utils;
using EmergenceSDK.Services;
using UnityEngine;

namespace EmergenceSDK.Samples.Examples
{
    public class ConnectingToWallet : MonoBehaviour
    {
        private IQRCodeService qrService;
        private IWalletService walletService;

        private void Awake()
        {
            qrService = EmergenceServices.GetService<IQRCodeService>();
            walletService = EmergenceServices.GetService<IWalletService>();
        }

        private void Start()
        {
            qrService.GetQRCode(GotQRCode, ErrorLogger.LogError);
        }

        private void GotQRCode(Texture2D qrcode, string deviceid)
        {
            //Present the QR code to the user for scanning

            //.....

            //Wait for the user to connect to the wallet
            walletService.Handshake(OnHandshakeSuccess, ErrorLogger.LogError);
        }

        private void OnHandshakeSuccess(string walletaddress)
        {
            //The user has connected to the wallet
            //You can now use the wallet address for whatever you need it for
            Debug.Log($"Wallet address: {walletaddress}");
        }
    }
}

Last updated