Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/michielpost/MetaMask.Blazor
Use MetaMask with Blazor WebAssembly
https://github.com/michielpost/MetaMask.Blazor
blazor blazor-webassembly dapps ethereum metamask
Last synced: 3 months ago
JSON representation
Use MetaMask with Blazor WebAssembly
- Host: GitHub
- URL: https://github.com/michielpost/MetaMask.Blazor
- Owner: michielpost
- License: mit
- Created: 2021-05-17T11:02:30.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-04-13T14:58:12.000Z (7 months ago)
- Last Synced: 2024-07-13T14:25:23.008Z (4 months ago)
- Topics: blazor, blazor-webassembly, dapps, ethereum, metamask
- Language: C#
- Homepage:
- Size: 55.3 MB
- Stars: 43
- Watchers: 3
- Forks: 20
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-blazor - MetaMask.Blazor - ![last commit](https://img.shields.io/github/last-commit/michielpost/MetaMask.Blazor?style=flat-square&cacheSeconds=86400) A library that provides an easy helper to use [MetaMask](https://metamask.io/) with Blazor WebAssembly. (Libraries & Extensions / 2D/3D Rendering engines)
README
# MetaMask.Blazor
Use MetaMask with Blazor WebAssemblyThis library provides an easy helper to use MetaMask with Blazor WebAssembly.
**[Live Demo](https://michielpost.github.io/MetaMask.Blazor/)**
Real world implementation, login with MetaMask: [SkyDocs](https://github.com/michielpost/SkyDocs/)
## How to use
See included Blazor sample app.Install: [MetaMask.Blazor on NuGet](https://www.nuget.org/packages/MetaMask.Blazor/)
Register in `Program.cs`:
```cs
builder.Services.AddMetaMaskBlazor();
```Inject the `MetaMaskService` in your Razor page
```cs
@using MetaMask.Blazor
@inject IMetaMaskService MetaMaskService
```or class when using a .razor.cs file:
```cs
[Inject]
public IMetaMaskService MetaMaskService { get; set; } = default!;
```Use the `MetaMaskService`:
Check if the user has MetaMask installed:
```cs
bool hasMetaMask = await MetaMaskService.HasMetaMask();
```Check if the user has previously connected to your site.
```cs
bool isSiteConnected = await MetaMaskService.IsSiteConnected();
```Initialize a new connection with MetaMask
```cs
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
- https://docs.metamask.io
- https://metamask.io
- [Logging in with MetaMask in a Blazor WebAssembly app](https://www.michielpost.nl/posts/logging-in-with-metamask-in-a-blazor-webassembly-app)## Acknowledgements
Development of MetaMask.Blazor has been made possible with a grant from [The Graph](https://thegraph.com/blog/wave-one-funding).