Skip to content

michielpost/MetaMask.Blazor

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
November 15, 2023 16:57
November 15, 2023 16:57
November 15, 2023 16:57
May 17, 2021 13:02
August 19, 2022 10:30
May 17, 2021 13:02
March 22, 2023 09:51

MetaMask.Blazor

Use MetaMask with Blazor WebAssembly

This library provides an easy helper to use MetaMask with Blazor WebAssembly.

Live Demo

Real world implementation, login with MetaMask: SkyDocs

How to use

See included Blazor sample app.

Install: MetaMask.Blazor on NuGet

Register in Program.cs:

builder.Services.AddMetaMaskBlazor();

Inject the MetaMaskService in your Razor page

@using MetaMask.Blazor
@inject IMetaMaskService MetaMaskService

or class when using a .razor.cs file:

[Inject]
public IMetaMaskService MetaMaskService { get; set; } = default!;

Use the MetaMaskService:

Check if the user has MetaMask installed:

bool hasMetaMask = await MetaMaskService.HasMetaMask();

Check if the user has previously connected to your site.

bool isSiteConnected = await MetaMaskService.IsSiteConnected();

Initialize a new connection with MetaMask

try
{
    await MetaMaskService.ConnectMetaMask();
}
catch (UserDeniedException)
{
   //Handle "User Denied" case;
}

This can throw exceptions if the user decides to not allow the connection.

Once there is a connection, you can use other method calls like:

  • GetSelectedAddress
  • GetTransactionCount
  • SignTypedData
  • SendTransaction
  • or use the generic RPC method: GenericRpc

Events

Listen to events: await MetaMaskService.ListenToEvents();

You can subscribe to two events:

  • IMetaMaskService.AccountChangedEvent
  • IMetaMaskService.NetworkChangedEvent

Reference

Acknowledgements

Development of MetaMask.Blazor has been made possible with a grant from The Graph.