import { Callout } from "components";
Connecting to a Web3 wallet, such as MetaMask via WalletConnect, establishes 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: This method is typically used for desktop-based applications or when running your project in Unity Editor play mode. It involves generating a QR code that the user can scan using a mobile app to open their wallet. To connect via QRCode, follow these steps:
-
Attach the
WalletConnectUnityMonoAdapter
script to a GameObject in your scene. -
Get a reference to a
WalletConnect
instance using theConnectProvider
class and call the Connect method. Here’s an example implementation:
public class ConnectProviderUsageExample
{
public async void LaunchWalletConnect()
{
var walletConnect = ConnectProvider<WalletConnect>.GetConnect();
await walletConnect.Connect();
}
}
- To display the QRCode, use a
QRCodeImage
component. The SDK includes a QRCode generator and a function that handles it. CallUpdateQRCode(string url)
with the generated URL, and use.SetImageActive(bool)
to activate or deactivate the QRCode. Here's an example:
var connectURL = WalletConnect.Instance.ConnectURL;
_qrCodeImage.UpdateQRCode(connectURL);
_qrCodeImage.SetImageActive(true);
- Users should scan the QRCode using their MetaMask mobile app. This will initiate the connection process. If the MetaMask mobile app does not open automatically, users should manually open the app.
- Connect via
WalletConnect.OpenMobileWallet
orWalletConnect.OpenDeepLink
: This method is typically used for mobile-based applications. The steps are similar to the QRCode method, but instead of showing and scanning a QR code, theWalletConnect.OpenMobileWallet
orWalletConnect.OpenDeepLink
methods are called from the mobile application.
Once the user agrees to connect on the wallet side, the WalletConnect
instance status (stored in the WalletConnect.Status
property) will change to WalletConnected
, and a Session Key will be saved in PlayerPrefs
for future use.
Examples demonstrating these connection methods and the general usage examples can be found in the Examples
scene and the ConnectionController.cs
script.
To create and cache a smart contract, do the following:
- Initialize a
WalletConnect
session usingWalletConnect.cs
script.- Attach
WalletConnectUnityMonoAdapter
script to aGameObject
in your scene. - Call
WalletConnect.Connect()
from your starter scriptAwake
orStart
method to create and connect the session. - 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 editingWalletConnectSettings
scriptable object or using your own alternative instance for this settings file locally in your project. - 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.
- Attach
- Use the static method below to create a new MirageSDK instance. You can only create this instance once
WalletConnect.Status
value is changed toWalletConnected
. This happens only after user approves the connection on their wallet side.MirageSDKFactory.GetMirageSDKInstance(string ProviderURL);
- 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.
This is an example from the SDK on how to link a Web3 wallet to a player account.
-
To connect a wallet, first make an instance of a
Web3
class and callInitialize
method after logging in MetaMaskstring provider_url = "<ethereum node url>"; Web3 web3 = new Web3(provider_url); web3.Initialize();
-
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.
-
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 ( "encoding/hex" "encoding/json" "log" "math/big" "net/http" "strconv" "fmt" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" cryptoTyped "github.com/ethersphere/bee/pkg/crypto" eip712 "github.com/ethersphere/bee/pkg/crypto/eip712" "github.com/gin-gonic/gin" ) // 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") }
-
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() }
-
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.