Beacon is the implementation of the wallet interaction standard tzip-10 which describes the connnection of a dApp with a wallet.
- .NET Standard 2.1
Beacon .NET SDK is available on NuGet:
dotnet add package Beacon.Sdk
For a complete example, refer to Sample.cs
.
You can also clone this repository and run Beacon.Sdk.Sample.Console
.
Here is step by step guide:
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);
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.
await walletClient.InitAsync()
walletClient.Connect();
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!);
walletClient.Disconnect();
Follow these steps to reproduce the typical wallet workflow:
- Clone this repo and make sure you have
dotnet
command available in your shell - Open Beacon playground, scroll to Setup and press "Run Code"
- Choose "Pair wallet on another device" and click on the QR code to copy
- In your shell run
make sample
and follow the instructions - In the browser you should see "Got permissions" message
- Scroll down to "Operation Request" item and do the "Run Code" thing again
- You should see the sucessful operation message