Coder Social home page Coder Social logo

walletconnectsharp's Introduction

WalletConnectSharp

WalletConnectSharp is an implementation of the WalletConnect protocol v2 using .NET. This library implements the WalletConnect Technical Specification in .NET to allow C# dApps makers and wallet makers to add support for the open WalletConnect protocol.

Installation

install via Nuget

dotnet add package WalletConnect.Sign

Usage

Dapp Usage

First you must setup SignClientOptions which stores both the ProjectId and Metadata. You may also optionally specify the storage module to use. By default, the FileSystemStorage module is used if none is specified.

var dappOptions = new SignClientOptions()
{
    ProjectId = "39f3dc0a2c604ec9885799f9fc5feb7c",
    Metadata = new Metadata()
    {
        Description = "An example dapp to showcase WalletConnectSharpv2",
        Icons = new[] { "https://walletconnect.com/meta/favicon.ico" },
        Name = "WalletConnectSharpv2 Dapp Example",
        Url = "https://walletconnect.com"
    },
    // Uncomment to disable persistant storage
    // Storage = new InMemoryStorage()
};

Then, you must setup the ConnectOptions which define what blockchain, RPC methods and events your dapp will use.

C# Constructor

var dappConnectOptions = new ConnectOptions()
{
    RequiredNamespaces = new RequiredNamespaces()
    {
        {
            "eip155", new RequiredNamespace()
            {
                Methods = new[]
                {
                    "eth_sendTransaction",
                    "eth_signTransaction",
                    "eth_sign",
                    "personal_sign",
                    "eth_signTypedData",
                },
                Chains = new[]
                {
                    "eip155:1"
                },
                Events = new[]
                {
                    "chainChanged",
                    "accountsChanged",
                }
            }
        }
    }
};

Builder Functions Style

var dappConnectOptions1 = new ConnectOptions()
    .RequireNamespace("eip155", new RequiredNamespace()
        .WithMethod("eth_sendTransaction")
        .WithMethod("eth_signTransaction")
        .WithMethod("eth_sign")
        .WithMethod("personal_sign")
        .WithMethod("eth_signTypedData")
        .WithChain("eip155:1")
        .WithEvent("chainChanged")
        .WithEvent("accountsChanged")
    );

With both options defined, you can initialize and connect the SDK

var dappClient = await WalletConnectSignClient.Init(dappOptions);
var connectData = await dappClient.Connect(dappConnectOptions);

You can grab the Uri for the connection request from connectData

ExampleShowQRCode(connectData.Uri);

and await for connection approval using the Approval Task object

Task<SessionData> sessionConnectTask = connectData.Approval;
SessionData sessionData = await sessionConnectTask;

// or
// SessionData sessionData = await connectData.Approval;

This Task will return the SessionData when the session was approved, or throw an exception when the session rquest has either

  • Timed out
  • Been Rejected

Wallet Usage

First you must setup SignClientOptions which stores both the ProjectId and Metadata. You may also optionally specify the storage module to use. By default, the FileSystemStorage module is used if none is specified.

var walletOptions = new SignClientOptions()
{
    ProjectId = "39f3dc0a2c604ec9885799f9fc5feb7c",
    Metadata = new Metadata()
    {
        Description = "An example wallet to showcase WalletConnectSharpv2",
        Icons = new[] { "https://walletconnect.com/meta/favicon.ico" },
        Name = "WalletConnectSharpv2 Wallet Example",
        Url = "https://walletconnect.com"
    },
    // Uncomment to disable persistant storage
    // Storage = new InMemoryStorage()
};

Once you have options defined, you can initialize the SDK

var walletClient = await WalletConnectSignClient.Init(walletOptions);

Wallets can pair an incoming session using the session's Uri. Pairing a session lets the Wallet obtain the connection proposal which can then be approved or denied.

ProposalStruct proposal = await walletClient.Pair(connectData.Uri);

The wallet can then approve or reject the proposal using either of the following

string addressToConnect = ...;
var approveData = await walletClient.Approve(proposal, addressToConnect);
await approveData.Acknowledged();
string[] addressesToConnect = ...;
var approveData = await walletClient.Approve(proposal, addressesToConnect);
await approveData.Acknowledged();
await walletClient.Reject(proposal, "User rejected");

Examples

There are examples and unit tests in the Tests directory. Some examples include

  • BiDirectional Communication
  • Basic dApp Example

walletconnectsharp's People

Contributors

ecp4224 avatar gigajuwels avatar john-s-morgan avatar addressxception avatar dependabot[bot] avatar aarlington avatar koenrijpstra avatar moodmosaic avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.