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:
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.
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.
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.
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.
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.
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:
Last updated