# Integrate a global wallet in Unity

:::warning
To make these instructions concrete, we have created a sample global wallet called **Rapidfire ID**. To interact with it, you can find its SDK in the NPM package directory: [@rapidfire/id](https://www.npmjs.com/package/@rapidfire/id).

You can check out the GitHub [repository for Rapidfire Wallet](https://github.com/openfort-xyz/ecosystem-sample) to learn how to create your own wallet.
:::

This guide will walk you through adding support for any **global wallet** into a Unity app by integrating the [Mobile Wallet Protocol](https://mobilewalletprotocol.github.io/wallet-mobile-sdk/).

:::note\[TARGET PLATFORM VS UNITY EDITOR PLATFORM]
We have added compilation flags to the Unity SDK to ensure that specific Unity editors can only build certain platform targets. Please note that the table below indicates which editor you can use to build a platform target, but it does not determine whether you can run the SDK in that editor.

For example, the SDK allows you to build iOS games using a macOS Unity Editor, but you cannot use the Windows Unity Editor.

| Target Platform | Windows Unity Editor | macOS Unity Editor |
|-----------------|:---------------------:|:-------------------:|
| Windows         | ✅                   | ❌                 |
| macOS           | ❌                   | ✅                 |
| [Android](/docs/products/cross-app-wallet/usage/unity-android)         | ✅                   | ✅                 |
| iOS             | ❌                   | ✅                 |
| [Web](/docs/products/cross-app-wallet/usage/unity-webgl)             | ✅                   | ✅                 |
:::

:::tip
If you need a template or scaffold to start with, you can check out the [Global Wallet Unity Example](https://github.com/openfort-xyz/mobile-wallet-protocol-unity-client/tree/main/Project).
:::

## Setup

### Install Mobile Wallet Protocol Client

There are two ways to install the SDK:

<MultiOptionDisplay
  options={[
  { id: 'cursor', label: 'UPM' },
  { id: 'windsurf', label: 'manifest.json' },
]}
/>

<span id="cursor" className="hidden [&>*]:mb-6!">
  1. Open the Package Manager
  2. Click the add + button and select "Add package from git URL..."
     Enter `https://github.com/openfort-xyz/mobile-wallet-protocol-unity-client.git?path=com.openfort.mobile-wallet-protocol` and click 'Add'
</span>

<span id="windsurf" className="hidden [&>*]:mb-6!">
  1. Open your project's Packages/manifest.json file
  2. Add `com.openfort.mobile-wallet-protocol`: `https://github.com/openfort-xyz/mobile-wallet-protocol-unity-client.git?path=com.openfort.mobile-wallet-protocol` in the dependencies block
</span>

## Usage

Mobile Wallet Protocol Client provides an interface for Unity to interact with the Global Wallet, an EIP-1193 compliant provider interface.

The EIP-1193 provider implements a method called `request` that accepts an object with the following fields:

* **`method`**: the name of a JSON-RPC request to send to the wallet
* **`params`**: any parameters to include with the request

### Methods

| Method                                   | Function Name |
|-------------------------------------------|--------|
| eth\_requestAccounts                        | EthRequestAccounts      |
| eth\_sendTransaction                        | EthSendTransaction      |
| personal\_sign                        | PersonalSign      |
| eth\_signTypedData\_v4                        | EthSignTypedDataV4      |
| wallet\_sendCalls                        | WalletSendCalls      |
| wallet\_showCallsStatus                        | WalletShowCallsStatus      |
| wallet\_grantPermissions                        | WalletGrantPermissions      |
| wallet\_getCapabilities                        | WalletGetCapabilities      |

### EIP-1193 Provider

Create a new `EIP1193Provider` instance, which is EIP-1193 compliant.

```cs ClientController.cs
using UnityEngine;
using MobileWalletProtocol;

public class ClientController : MonoBehaviour
{
    [SerializeField]
    MWPClientOptions m_Options = new MWPClientOptions()
    {
        Metadata = new AppMetadata()
        {
            Name = "Smart Wallet",
            CustomScheme = "exp://",
            ChainIds = new string[] { "0xaa36a7" }
        },
        Wallet = Wallet.CreateWebWallet(
            name: "Rapid fire wallet",
            // The scheme should be the target wallet's URL
            scheme: "https://id.sample.openfort.io#policy=pol_a909d815-9b6c-40b2-9f6c-e93505281137",
            iconUrl: "https://purple-magnificent-bat-958.mypinata.cloud/ipfs/QmfQrh2BiCzugFauYF9Weu9SFddsVh9qV82uw43cxH8UDV"
        )
    };

    MWPClient m_Client;
    string m_Account;

    void Awake()
    {
        m_Client = new MWPClient(m_Options);
    }

    public async void RequestAccounts()
    {
        var result = await m_Client.EthRequestAccounts();

        if (result.IsSuccess)
        {
            var accounts = result.Value;

            m_Account = accounts[0];

            foreach (var account in accounts)
            {
                Debug.Log("Account: " + account);
            }
        }
        else
        {
            Debug.LogError("Error: " + result.Error);
        }
    }

    public void Disconnect()
    {
        m_Client.Reset();
    }
}
```

## Gas Sponsorship

If you want to sponsor transactions, you need to add `#policy=POLICY_ID` to the wallet scheme.

```text
#policy=POLICY_ID
```

## Troubleshooting

1. If no network appears in your wallet transaction request, make sure you:
   * Have the correct chain ID in the metadata
   * That chain is supported by your global wallet.
