-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSample.cs
180 lines (146 loc) · 7.2 KB
/
Sample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
namespace Beacon.Sdk.Sample.Wallet
{
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Beacon;
using Beacon.Error;
using Beacon.Operation;
using Beacon.Permission;
using Beacon.Sign;
using BeaconClients;
using BeaconClients.Abstract;
using Core.Domain.Services;
using Microsoft.Extensions.Logging;
using Netezos.Encoding;
using Netezos.Keys;
using Serilog;
using Serilog.Extensions.Logging;
using ILogger = Serilog.ILogger;
public class Sample
{
private IWalletBeaconClient BeaconWalletClient { get; set; }
private ILogger Logger { get; set; }
private static Key TestKey => Key.FromBase58("edsk35muRVNaRkd7ojbHzFdms8E66SADLYyy1enNKYb8k332vCsZ9N");
public async Task Run()
{
const string path = "wallet-beacon-sample.db";
var options = new BeaconOptions
{
AppName = "Wallet sample",
AppUrl = "https://awesome-wallet.io",
IconUrl = "https://services.tzkt.io/v1/avatars/KT1TxqZ8QtKvLu3V3JH7Gx58n7Co8pgtpQU5",
KnownRelayServers = Constants.KnownRelayServers,
DatabaseConnectionString = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? $"Filename={path}; Connection=Shared;"
: $"Filename={path}; Mode=Exclusive;"
};
Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console()
.CreateLogger();
ILoggerProvider loggerProvider = new SerilogLoggerProvider(Logger);
BeaconWalletClient = BeaconClientFactory.Create<IWalletBeaconClient>(options, loggerProvider);
BeaconWalletClient.OnBeaconMessageReceived += OnBeaconWalletClientMessageReceived;
BeaconWalletClient.OnConnectedClientsListChanged += OnConnectedClientsListChanged;
await BeaconWalletClient.InitAsync();
BeaconWalletClient.Connect();
Logger.Information("Paste pairing Qr code here to start pairing with dApp:\n");
var pairingQrCode = System.Console.ReadLine();
if (pairingQrCode != null)
{
var pairingRequest = BeaconWalletClient.GetPairingRequest(pairingQrCode);
await BeaconWalletClient.AddPeerAsync(pairingRequest);
}
}
private async void OnBeaconWalletClientMessageReceived(object sender, BeaconMessageEventArgs e)
{
var message = e.Request;
if (message == null) return;
switch (message.Type)
{
case BeaconMessageType.permission_request:
{
if (message is not PermissionRequest permissionRequest)
return;
var permissionsString = permissionRequest.Scopes.Aggregate(string.Empty,
(res, scope) => res + $"{scope}, ");
Logger.Information("Permission request received from {Dapp}, requested {Permissions}",
permissionRequest.AppMetadata.Name, permissionsString);
var response = new PermissionResponse(
id: permissionRequest.Id,
senderId: BeaconWalletClient.SenderId,
appMetadata: BeaconWalletClient.Metadata,
network: permissionRequest.Network,
scopes: permissionRequest.Scopes,
publicKey: TestKey.PubKey.ToString(),
version: permissionRequest.Version);
await BeaconWalletClient.SendResponseAsync(receiverId: permissionRequest.SenderId, response);
break;
}
case BeaconMessageType.sign_payload_request:
{
if (message is not SignPayloadRequest signRequest)
return;
var permissions = BeaconWalletClient
.PermissionInfoRepository
.TryReadBySenderIdAsync(signRequest.SenderId)
.Result;
if (permissions == null) return;
Logger.Information("Sign payload request received from {Dapp}, payload {Payload}",
permissions.AppMetadata.Name, signRequest.Payload);
var parsed = Hex.TryParse(signRequest.Payload, out var payloadBytes);
if (!parsed)
{
await BeaconWalletClient.SendResponseAsync(
receiverId: signRequest.SenderId,
response: new SignatureTypeNotSupportedBeaconError(signRequest.Id,
BeaconWalletClient.SenderId));
return;
}
var response = new SignPayloadResponse(
signature: TestKey.Sign(payloadBytes),
version: signRequest.Version,
id: signRequest.Id,
senderId: BeaconWalletClient.SenderId);
await BeaconWalletClient.SendResponseAsync(receiverId: signRequest.SenderId, response);
break;
}
case BeaconMessageType.operation_request:
{
if (message is not OperationRequest operationRequest)
return;
var permissions = BeaconWalletClient
.PermissionInfoRepository
.TryReadBySenderIdAsync(operationRequest.SenderId)
.Result;
if (permissions == null) return;
Logger.Information("Received operation request from {Dapp}", permissions.AppMetadata.Name);
// here you should do Tezos transaction and send response with success transaction hash.
// we mock transaction hash here.
const string transactionHash = "ooRAfDhmSNiwEdGQi2M5qt27EVtBdh3WD7LX3Rpoet3BTUssKTT";
var response = new OperationResponse(
id: operationRequest.Id,
senderId: BeaconWalletClient.SenderId,
transactionHash: transactionHash,
operationRequest.Version);
await BeaconWalletClient.SendResponseAsync(receiverId: operationRequest.SenderId, response);
break;
}
default:
{
var error = new BeaconAbortedError(
id: KeyPairService.CreateGuid(),
senderId: BeaconWalletClient.SenderId);
await BeaconWalletClient.SendResponseAsync(receiverId: message.SenderId, error);
break;
}
}
}
private void OnConnectedClientsListChanged(object sender, ConnectedClientsListChangedEventArgs e)
{
if (sender is not WalletBeaconClient) return;
Logger.Information("Connected dApp {Name}", e?.Metadata.Name);
}
}
}