OpenParable
Skip to content
← Back to skills

Blazor Wallet Connect

Apps & Automation
blazor-wallet-connect

Blazor Wallet Connect is an Agent Skill for apps & automation published in the Nethereum/Nethereum repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.

Connect browser wallets in Blazor using EIP-6963, MetaMask, or WalletConnect/Reown (.NET/C#). Use this skill when the user asks about connecting wallets in Blazor, EIP-6963, MetaMask Blazor integration, WalletConnect, Reown AppKit, or browser wallet discovery in a Blazor web app.

View on GitHub ↗
QUICK START

How to use this skill

Bring this guide into your coding agent with a prompt tailored to the tool you use.

  1. Open your project in Codex.
  2. Copy and paste the prompt below.
  3. Review files and risks before approving any action.
Prompt to paste
Inspect the Agent Skill at https://github.com/Nethereum/Nethereum/blob/HEAD/plugins/nethereum-skills/skills/blazor-wallet-connect/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/blazor-wallet-connect, but wait for my approval before writing files or running scripts.

Copying this prompt does not install or run the skill. Review third-party files before use.

Blazor Wallet Connection

Connect browser wallets in Blazor using EIP-6963 multi-wallet discovery (recommended), MetaMask, or WalletConnect/Reown.

Option 1: EIP-6963 Multi-Wallet Discovery (Recommended)

EIP-6963 discovers all installed browser wallets. Users choose which to connect. This is the modern replacement for window.ethereum.

NuGet: Nethereum.Blazor

Setup

Add the JS interop script to your index.html or _Host.cshtml:

<script src="_content/Nethereum.Blazor/NethereumEIP6963.js"></script>

Register services:

builder.Services.AddSingleton<IEthereumHostProvider, EIP6963HostProvider>();
builder.Services.AddSingleton<AuthenticationStateProvider, EthereumAuthenticationStateProvider>();

Use the Component

@using Nethereum.Blazor
@inject IEthereumHostProvider EthereumHostProvider

<EIP6963Wallet OnWalletConnected="HandleWalletConnected" />

@code {
    private async Task HandleWalletConnected(string address)
    {
        var web3 = await EthereumHostProvider.GetWeb3Async();
        var balance = await web3.Eth.GetBalance.SendRequestAsync(address);
    }
}

Option 2: MetaMask Direct

NuGet: Nethereum.Metamask.Blazor

Setup (Program.cs)

builder.Services.AddSingleton<IMetamaskInterop, MetamaskBlazorInterop>();
builder.Services.AddSingleton<MetamaskHostProvider>();
builder.Services.AddSingleton(services =>
{
    var metamaskHostProvider = services.GetService<MetamaskHostProvider>();
    var selectedHostProvider = new SelectedEthereumHostProviderService();
    selectedHostProvider.SetSelectedEthereumHostProvider(metamaskHostProvider);
    return selectedHostProvider;
});
builder.Services.AddSingleton<AuthenticationStateProvider, EthereumAuthenticationStateProvider>();

Connect and Use

@inject MetamaskHostProvider MetamaskHostProvider

<button @onclick="ConnectAsync">Connect MetaMask</button>
<p>Address: @Address</p>

@code {
    private string? Address;

    protected override Task OnInitializedAsync()
    {
        MetamaskHostProvider.SelectedAccountChanged += async (address) =>
        {
            Address = address;
            await InvokeAsync(StateHasChanged);
        };
        return Task.CompletedTask;
    }

    private async Task ConnectAsync()
    {
        await MetamaskHostProvider.EnableProviderAsync();
        Address = await MetamaskHostProvider.GetProviderSelectedAccountAsync();
        var web3 = await MetamaskHostProvider.GetWeb3Async();
    }
}

Full example: consoletests/MetamaskExampleBlazor.Wasm/

Option 3: WalletConnect / Reown AppKit

NuGet: Nethereum.Reown.AppKit.Blazor

Setup (Program.cs)

builder.Services.AddAppKit(new()
{
    ProjectId = "YOUR_REOWN_PROJECT_ID",
    Name = "My dApp",
    Networks = NetworkConstants.Networks.All,
    Url = builder.HostEnvironment.BaseAddress,
});

Get a project ID from https://cloud.reown.com/sign-in

Use the AppKit Button

@using Nethereum.Reown.AppKit.Blazor
@inject IEthereumHostProvider EthereumHostProvider

<appkit-button />
<appkit-network-button />

@if (!string.IsNullOrEmpty(Address))
{
    <p>Connected: @Address on chain @ChainId</p>
}

@code {
    private string? Address;
    private long? ChainId;

    protected override async Task OnInitializedAsync()
    {
        EthereumHostProvider.SelectedAccountChanged += async (address) =>
        {
            Address = address;
            await InvokeAsync(StateHasChanged);
        };

        EthereumHostProvider.NetworkChanged += async (chainId) =>
        {
            ChainId = chainId;
            await InvokeAsync(StateHasChanged);
        };
    }
}

Full example: consoletests/NethereumReownAppKitBlazor/

Getting Web3 After Connection

All providers implement IEthereumHostProvider. Once connected:

var web3 = await ethereumHostProvider.GetWeb3Async();
var blockNumber = await web3.Eth.Blocks.GetBlockNumber.SendRequestAsync();
var balance = await web3.Eth.GetBalance.SendRequestAsync(address);

Event Handling

All providers raise events for account and network changes:

ethereumHostProvider.SelectedAccountChanged += async (address) => { ... };
ethereumHostProvider.NetworkChanged += async (chainId) => { ... };
ethereumHostProvider.EnabledChanged += async (enabled) => { ... };

Switching and Adding Chains

await web3.Eth.HostWallet.SwitchEthereumChain.SendRequestAsync(
    new SwitchEthereumChainParameter { ChainId = 10.ToHexBigInteger() });

var chainFeature = ChainDefaultFeaturesServicesRepository.GetDefaultChainFeature(Chain.Optimism);
await web3.Eth.HostWallet.AddEthereumChain.SendRequestAsync(chainFeature.ToAddEthereumChainParameter());

AuthorizeView Integration

All providers work with Blazor's <AuthorizeView>:

<AuthorizeView Roles="EthereumConnected">
    <Authorized>
        <p>Connected as @context.User.Identity?.Name</p>
    </Authorized>
    <NotAuthorized>
        <button @onclick="ConnectAsync">Connect Wallet</button>
    </NotAuthorized>
</AuthorizeView>

For full SIWE authentication with JWT tokens, see the blazor-authentication skill.

For full documentation, see: https://docs.nethereum.com/docs/blazor-dapp-integration/overview