GithubHelp home page GithubHelp logo

isabella232 / beacon-dotnet-sdk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from baking-bad/beacon-dotnet-sdk

0.0 0.0 0.0 938 KB

Beacon .NET SDK for Tezos wallet developers to seamlessly connect to multiple dApps

Home Page: https://www.nuget.org/packages/Beacon.Sdk

License: MIT License

C# 99.92% Makefile 0.08%

beacon-dotnet-sdk's Introduction

Beacon .NET SDK

beacon-logo

Beacon is the implementation of the wallet interaction standard tzip-10 which describes the connnection of a dApp with a wallet.

Supported Platforms

  • .NET Standard 2.1

Installation

Beacon .NET SDK is available on NuGet:

dotnet add package Beacon.Sdk

Use the SDK in your app

For a complete example, refer to Sample.cs. You can also clone this repository and run Beacon.Sdk.Sample.Console.

Wallet

Here is step by step guide:

1. Create

Use WalletBeaconClientFactory to create an instance of WalletBeaconClient

const string path = "your-database-name.db";

var factory = new WalletBeaconClientFactory();

var options = new BeaconOptions
{
    AppName = "your-app-name",
    AppUrl = "optional-app-url", //string?
    IconUrl = "optional-icon-url", // string?
    DatabaseConnectionString = $"Filename={path}"
};

IWalletBeaconClient walletClient = factory.Create(options);

2. Start listening for incoming events

To listen for the incoming Beacon messages you need to subscribe to OnBeaconMessageReceived;

walletClient.OnBeaconMessageReceived += async (_, dAppClient) =>
{
    BaseBeaconMessage message = dAppClient.Request;

    if (message.Type == BeaconMessageType.permission_request)
    {
        var request = message as PermissionRequest;
        
        var network = new Network 
        {
            Type = NetworkType.hangzhounet,
            Name = "Hangzhounet",
            RpcUrl = "https://hangzhounet.tezblock.io"
        };

        var response = new PermissionResponse(
            id: request!.Id,
            senderId: walletClient.SenderId,
            network: network,
            scopes: request.Scopes,
            publicKey: walletKey.PubKey.ToString(),
            appMetadata: walletClient.Metadata);

        // var response = new BeaconAbortedError(message.Id, walletClient.SenderId);

        await walletClient.SendResponseAsync(receiverId: dAppClient.SenderId, response);
    }
    else if (message.Type == BeaconMessageType.operation_request)
    {
        var request = message as OperationRequest;
        
        string transactionHash = await MakeTransactionAsync(walletKey);

        var response = new OperationResponse(
            id: request!.Id,
            senderId: walletClient.SenderId,
            transactionHash: transactionHash);

        await walletClient.SendResponseAsync(receiverId: dAppClient.SenderId, response);
    }
};

I recommend that you don't use anonymous functions to subscribe to events if you have to unsubscribe from the event at some later point in your code.

3. Init

await walletClient.InitAsync()

4. Connect

walletClient.Connect();

5. Add Peer

string QrCode = "paste-qrcode-here";

byte[] decodedBytes = Base58CheckEncoding.Decode(QrCode);
string message = Encoding.Default.GetString(decodedBytes);

P2PPairingRequest pairingRequest = JsonConvert.DeserializeObject<P2PPairingRequest>(message);

await walletClient.AddPeerAsync(pairingRequest!);

6. Disconnect

walletClient.Disconnect();

Demo app

Follow these steps to reproduce the typical wallet workflow:

  1. Clone this repo and make sure you have dotnet command available in your shell
  2. Open Beacon playground, scroll to Setup and press "Run Code"
  3. Choose "Pair wallet on another device" and click on the QR code to copy
  4. In your shell run make sample and follow the instructions
  5. In the browser you should see "Got permissions" message
  6. Scroll down to "Operation Request" item and do the "Run Code" thing again
  7. You should see the sucessful operation message

beacon-dotnet-sdk's People

Contributors

iviich4el avatar m-kus 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.