This repository was archived by the owner on Jan 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathWallet.cs
More file actions
598 lines (487 loc) · 21.5 KB
/
Wallet.cs
File metadata and controls
598 lines (487 loc) · 21.5 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Chaos.NaCl;
using dotnetstandard_bip39;
using NLog;
using Schnorrkel;
using Schnorrkel.Keys;
//using Schnorrkel;
using SubstrateNetApi;
using SubstrateNetApi.Model.Calls;
using SubstrateNetApi.Model.Rpc;
using SubstrateNetApi.Model.Types;
using SubstrateNetApi.Model.Types.Custom;
using SubstrateNetApi.Model.Types.Struct;
using SubstrateNetApi.TypeConverters;
[assembly: InternalsVisibleTo("SubstrateNetWalletTests")]
namespace SubstrateNetWallet
{
/// <summary>
/// Basic Wallet implementation
/// TODO: Make sure that a live runtime change is handled correctly.
/// </summary>
public class Wallet
{
private const string Websocketurl = "wss://mogiway-01.dotmog.com";
private const string FileType = "dat";
private const string DefaultWalletName = "wallet";
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
private readonly CancellationTokenSource _connectTokenSource;
private readonly Random _random = new Random();
private string _subscriptionIdNewHead, _subscriptionIdFinalizedHeads, _subscriptionAccountInfo;
private WalletFile _walletFile;
/// <summary>
/// Constructor
/// </summary>
public Wallet()
{
_connectTokenSource = new CancellationTokenSource();
}
/// <summary>
/// Gets a value indicating whether this instance is unlocked.
/// </summary>
/// <value>
/// <c>true</c> if this instance is unlocked; otherwise, <c>false</c>.
/// </value>
public bool IsUnlocked => Account != null;
/// <summary>
/// Gets a value indicating whether this instance is created.
/// </summary>
/// <value>
/// <c>true</c> if this instance is created; otherwise, <c>false</c>.
/// </value>
public bool IsCreated => _walletFile != null;
public Account Account { get; private set; }
public AccountInfo AccountInfo { get; private set; }
public ChainInfo ChainInfo { get; private set; }
public SubstrateClient Client { get; private set; }
/// <summary>
/// Gets a value indicating whether this instance is connected.
/// </summary>
/// <value>
/// <c>true</c> if this instance is connected; otherwise, <c>false</c>.
/// </value>
public bool IsConnected => Client != null && Client.IsConnected;
/// <summary>
/// Gets a value indicating whether this instance is online.
/// </summary>
/// <value>
/// <c>true</c> if this instance is online; otherwise, <c>false</c>.
/// </value>
public bool IsOnline => IsConnected && _subscriptionIdNewHead != string.Empty &&
_subscriptionIdFinalizedHeads != string.Empty;
/// <summary>
/// Determines whether [is valid wallet name] [the specified wallet name].
/// </summary>
/// <param name="walletName">Name of the wallet.</param>
/// <returns>
/// <c>true</c> if [is valid wallet name] [the specified wallet name]; otherwise, <c>false</c>.
/// </returns>
public bool IsValidWalletName(string walletName)
{
return walletName.Length > 4 && walletName.Length < 21 &&
walletName.All(c => char.IsLetterOrDigit(c) || c.Equals('_'));
}
/// <summary>
/// Determines whether [is valid password] [the specified password].
/// </summary>
/// <param name="password">The password.</param>
/// <returns>
/// <c>true</c> if [is valid password] [the specified password]; otherwise, <c>false</c>.
/// </returns>
public bool IsValidPassword(string password)
{
return password.Length > 7 && password.Length < 21 && password.Any(char.IsUpper) &&
password.Any(char.IsLower) && password.Any(char.IsDigit);
}
/// <summary>
/// Adds the type of the wallet file.
/// </summary>
/// <param name="walletName">Name of the wallet.</param>
/// <returns></returns>
public string AddWalletFileType(string walletName)
{
return $"{walletName}.{FileType}";
}
/// <summary>
/// Occurs when [chain information updated].
/// </summary>
public event EventHandler<ChainInfo> ChainInfoUpdated;
/// <summary>
/// Occurs when [account information updated].
/// </summary>
public event EventHandler<AccountInfo> AccountInfoUpdated;
/// <summary>
/// Load an existing wallet
/// </summary>
/// <param name="walletName"></param>
/// <returns></returns>
public bool Load(string walletName = DefaultWalletName)
{
if (!IsValidWalletName(walletName))
{
Logger.Warn("Wallet name is invalid, please provide a proper wallet name. [A-Za-Z_]{20}.");
return false;
}
var walletFileName = AddWalletFileType(walletName);
if (!Caching.TryReadFile(walletFileName, out _walletFile))
{
Logger.Warn($"Failed to load wallet file '{walletFileName}'!");
return false;
}
return true;
}
/// <summary>
/// Creates the asynchronous.
/// </summary>
/// <param name="password">The password.</param>
/// <param name="mnemonic">The mnemonic.</param>
/// <param name="walletName">Name of the wallet.</param>
/// <returns></returns>
public async Task<bool> CreateAsync(string password, string mnemonic, string walletName = DefaultWalletName)
{
if (IsCreated)
{
Logger.Warn("Wallet already created.");
return true;
}
if (!IsValidPassword(password))
{
Logger.Warn(
"Password isn't is invalid, please provide a proper password. Minmimu eight size and must have upper, lower and digits.");
return false;
}
Logger.Info("Creating new wallet from mnemonic.");
var seed = Mnemonic.GetSecretKeyFromMnemonic(mnemonic, "Substrate", BIP39Wordlist.English);
var randomBytes = new byte[48];
_random.NextBytes(randomBytes);
var memoryBytes = randomBytes.AsMemory();
var pswBytes = Encoding.UTF8.GetBytes(password);
var salt = memoryBytes.Slice(0, 16).ToArray();
pswBytes = SHA256.Create().ComputeHash(pswBytes);
var encryptedSeed =
ManagedAes.EncryptStringToBytes_Aes(Utils.Bytes2HexString(seed, Utils.HexStringFormat.Pure), pswBytes, salt);
var miniSecret = new MiniSecret(seed, ExpandMode.Ed25519);
var getPair = miniSecret.GetPair();
var keyType = KeyType.Sr25519;
_walletFile = new WalletFile(keyType, getPair.Public.Key, encryptedSeed, salt);
Caching.Persist(AddWalletFileType(walletName), _walletFile);
Account = Account.Build(keyType, getPair.Secret.ToBytes(), getPair.Public.Key);
if (IsOnline) _subscriptionAccountInfo = await SubscribeAccountInfoAsync();
return true;
}
/// <summary>
/// Creates the asynchronous.
/// </summary>
/// <param name="password">The password.</param>
/// <param name="walletName">Name of the wallet.</param>
/// <returns></returns>
public async Task<bool> CreateAsync(string password, string walletName = DefaultWalletName)
{
if (IsCreated)
{
Logger.Warn("Wallet already created.");
return true;
}
if (!IsValidPassword(password))
{
Logger.Warn(
"Password isn't is invalid, please provide a proper password. Minmimu eight size and must have upper, lower and digits.");
return false;
}
Logger.Info("Creating new wallet.");
var randomBytes = new byte[48];
_random.NextBytes(randomBytes);
var memoryBytes = randomBytes.AsMemory();
var pswBytes = Encoding.UTF8.GetBytes(password);
var salt = memoryBytes.Slice(0, 16).ToArray();
var seed = memoryBytes.Slice(16, 32).ToArray();
pswBytes = SHA256.Create().ComputeHash(pswBytes);
var encryptedSeed =
ManagedAes.EncryptStringToBytes_Aes(Utils.Bytes2HexString(seed, Utils.HexStringFormat.Pure), pswBytes,
salt);
Ed25519.KeyPairFromSeed(out var publicKey, out var privateKey, seed);
var keyType = KeyType.Ed25519;
_walletFile = new WalletFile(keyType, publicKey, encryptedSeed, salt);
Caching.Persist(AddWalletFileType(walletName), _walletFile);
Account = Account.Build(keyType, privateKey, publicKey);
if (IsOnline) _subscriptionAccountInfo = await SubscribeAccountInfoAsync();
return true;
}
/// <summary>
/// Unlocks the asynchronous.
/// </summary>
/// <param name="password">The password.</param>
/// <param name="noCheck">if set to <c>true</c> [no check].</param>
/// <returns></returns>
/// <exception cref="Exception">Public key check failed!</exception>
public async Task<bool> UnlockAsync(string password, bool noCheck = false)
{
if (IsUnlocked || !IsCreated)
{
Logger.Warn("Wallet is already unlocked or doesn't exist.");
return IsUnlocked && IsCreated;
}
Logger.Info("Unlock new wallet.");
try
{
var pswBytes = Encoding.UTF8.GetBytes(password);
pswBytes = SHA256.Create().ComputeHash(pswBytes);
var seed = ManagedAes.DecryptStringFromBytes_Aes(_walletFile.EncryptedSeed, pswBytes, _walletFile.Salt);
byte[] publicKey = null;
byte[] privateKey = null;
switch (_walletFile.KeyType)
{
case KeyType.Ed25519:
Ed25519.KeyPairFromSeed(out publicKey, out privateKey, Utils.HexToByteArray(seed));
break;
case KeyType.Sr25519:
var miniSecret = new MiniSecret(Utils.HexToByteArray(seed), ExpandMode.Ed25519);
var getPair = miniSecret.GetPair();
privateKey = getPair.Secret.ToBytes();
publicKey = getPair.Public.Key;
break;
}
if (noCheck || !publicKey.SequenceEqual(_walletFile.PublicKey))
throw new Exception("Public key check failed!");
Account = Account.Build(_walletFile.KeyType, privateKey, publicKey);
}
catch (Exception exception)
{
Logger.Warn($"Couldn't unlock the wallet with this password. {exception}");
return false;
}
if (IsOnline) _subscriptionAccountInfo = await SubscribeAccountInfoAsync();
return true;
}
/// <summary>
/// Tries the sign message.
/// </summary>
/// <param name="signer">The signer.</param>
/// <param name="data">The data.</param>
/// <param name="signature">The signature.</param>
/// <returns></returns>
/// <exception cref="NotImplementedException">KeyType {signer.KeyType} is currently not implemented for signing.</exception>
public bool TrySignMessage(Account signer, byte[] data, out byte[] signature)
{
signature = null;
if (signer?.PrivateKey == null)
{
Logger.Warn("Account or private key doesn't exists.");
return false;
}
switch (signer.KeyType)
{
case KeyType.Ed25519:
signature = Ed25519.Sign(data, signer.PrivateKey);
break;
case KeyType.Sr25519:
signature = Sr25519v091.SignSimple(signer.Bytes, signer.PrivateKey, data);
break;
default:
throw new NotImplementedException(
$"KeyType {signer.KeyType} is currently not implemented for signing.");
}
return true;
}
/// <summary>
/// Verifies the signature.
/// </summary>
/// <param name="signer">The signer.</param>
/// <param name="data">The data.</param>
/// <param name="signature">The signature.</param>
/// <returns></returns>
/// <exception cref="NotImplementedException">KeyType {signer.KeyType} is currently not implemented for verifying signatures.</exception>
public bool VerifySignature(Account signer, byte[] data, byte[] signature)
{
switch (signer.KeyType)
{
case KeyType.Ed25519:
return Ed25519.Verify(signature, data, signer.Bytes);
case KeyType.Sr25519:
return Sr25519v091.Verify(signature, signer.Bytes, data);
default:
throw new NotImplementedException(
$"KeyType {signer.KeyType} is currently not implemented for verifying signatures.");
}
}
/// <summary>
/// Subscribe to AccountInfo asynchronous
/// </summary>
/// <returns></returns>
public async Task<string> SubscribeAccountInfoAsync()
{
return await Client.SubscribeStorageKeyAsync("System", "Account",
new[] {Utils.Bytes2HexString(Utils.GetPublicKeyFrom(Account.Value))},
CallBackAccountChange, _connectTokenSource.Token);
}
/// <summary>
/// Submits the generic extrinsic asynchronous.
/// </summary>
/// <param name="genericExtrinsicCall">The generic extrinsic call.</param>
/// <returns></returns>
public async Task<string> SubmitGenericExtrinsicAsync(GenericExtrinsicCall genericExtrinsicCall)
{
return await Client.Author
.SubmitAndWatchExtrinsicAsync(CallBackExtrinsic, genericExtrinsicCall, Account, 0, 64,
_connectTokenSource.Token);
}
/// <summary>
/// Connects the asynchronous.
/// </summary>
/// <param name="webSocketUrl">The web socket URL.</param>
private async Task ConnectAsync(string webSocketUrl)
{
Logger.Info($"Connecting to {webSocketUrl}");
Client = new SubstrateClient(new Uri(webSocketUrl));
//TODO check if that can be made generic as parameter
Client.RegisterTypeConverter(new GenericTypeConverter<MogwaiStruct>());
Client.RegisterTypeConverter(new GenericTypeConverter<MogwaiBios>());
await Client.ConnectAsync(_connectTokenSource.Token);
if (!IsConnected)
{
Logger.Error("Connection couldn't be established!");
return;
}
var systemName = await Client.System.NameAsync(_connectTokenSource.Token);
var systemVersion = await Client.System.VersionAsync(_connectTokenSource.Token);
var systemChain = await Client.System.ChainAsync(_connectTokenSource.Token);
ChainInfo = new ChainInfo(systemName, systemVersion, systemChain, Client.RuntimeVersion);
Logger.Info($"Connection established to {ChainInfo}");
}
/// <summary>
/// Starts the asynchronous.
/// </summary>
/// <param name="webSocketUrl">The web socket URL.</param>
public async Task StartAsync(string webSocketUrl = Websocketurl)
{
// disconnect from node if we are already connected to one.
if (IsConnected)
{
Logger.Warn($"Wallet already connected, disconnecting from {ChainInfo} now");
await StopAsync();
}
// connect wallet
await ConnectAsync(webSocketUrl);
if (IsConnected)
{
Logger.Warn("Starting subscriptions now.");
await RefreshSubscriptionsAsync();
}
}
/// <summary>
/// Refreshes the subscriptions asynchronous.
/// </summary>
public async Task RefreshSubscriptionsAsync()
{
Logger.Info("Refreshing all subscriptions");
// unsubscribe all subscriptions
await UnsubscribeAllAsync();
// subscribe to new heads
_subscriptionIdNewHead =
await Client.Chain.SubscribeNewHeadsAsync(CallBackNewHeads, _connectTokenSource.Token);
// subscribe to finalized heads
_subscriptionIdFinalizedHeads =
await Client.Chain.SubscribeFinalizedHeadsAsync(CallBackFinalizedHeads, _connectTokenSource.Token);
if (IsUnlocked)
// subscribe to account info
_subscriptionAccountInfo = await SubscribeAccountInfoAsync();
}
/// <summary>
/// Unsubscribes all asynchronous.
/// </summary>
public async Task UnsubscribeAllAsync()
{
if (!string.IsNullOrEmpty(_subscriptionIdNewHead))
{
// unsubscribe from new heads
if (!await Client.Chain.UnsubscribeNewHeadsAsync(_subscriptionIdNewHead, _connectTokenSource.Token))
Logger.Warn($"Couldn't unsubscribe new heads {_subscriptionIdNewHead} id.");
_subscriptionIdNewHead = string.Empty;
}
if (!string.IsNullOrEmpty(_subscriptionIdNewHead))
{
// unsubscribe from finalized heads
if (!await Client.Chain.UnsubscribeFinalizedHeadsAsync(_subscriptionIdFinalizedHeads,
_connectTokenSource.Token))
Logger.Warn($"Couldn't unsubscribe finalized heads {_subscriptionIdFinalizedHeads} id.");
_subscriptionIdFinalizedHeads = string.Empty;
}
if (!string.IsNullOrEmpty(_subscriptionAccountInfo))
{
// unsubscribe from finalized heads
if (!await Client.State.UnsubscribeStorageAsync(_subscriptionAccountInfo, _connectTokenSource.Token))
Logger.Warn($"Couldn't unsubscribe storage subscription {_subscriptionAccountInfo} id.");
_subscriptionAccountInfo = string.Empty;
}
}
/// <summary>
/// Stops the asynchronous.
/// </summary>
public async Task StopAsync()
{
// unsubscribe all subscriptions
await UnsubscribeAllAsync();
//ChainInfoUpdated -= Wallet_ChainInfoUpdated;
// disconnect wallet
await Client.CloseAsync(_connectTokenSource.Token);
}
/// <summary>
/// Calls the back new heads.
/// </summary>
/// <param name="subscriptionId">The subscription identifier.</param>
/// <param name="header">The header.</param>
public virtual void CallBackNewHeads(string subscriptionId, Header header)
{
}
/// <summary>
/// Calls the back finalized heads.
/// </summary>
/// <param name="subscriptionId">The subscription identifier.</param>
/// <param name="header">The header.</param>
public virtual void CallBackFinalizedHeads(string subscriptionId, Header header)
{
ChainInfo.UpdateFinalizedHeader(header);
ChainInfoUpdated?.Invoke(this, ChainInfo);
}
/// <summary>
/// Call back for extrinsic.
/// </summary>
/// <param name="subscriptionId"></param>
/// <param name="extrinsicStatus"></param>
public virtual void CallBackExtrinsic(string subscriptionId, ExtrinsicStatus extrinsicStatus)
{
}
/// <summary>
/// Calls the back account change.
/// </summary>
/// <param name="subscriptionId">The subscription identifier.</param>
/// <param name="storageChangeSet">The storage change set.</param>
public virtual void CallBackAccountChange(string subscriptionId, StorageChangeSet storageChangeSet)
{
if (storageChangeSet.Changes == null
|| storageChangeSet.Changes.Length == 0
|| storageChangeSet.Changes[0].Length < 2)
{
Logger.Warn("Couldn't update account informations. Please check 'CallBackAccountChange'");
return;
}
var accountInfoStr = storageChangeSet.Changes[0][1];
if (string.IsNullOrEmpty(accountInfoStr))
{
Logger.Warn("Couldn't update account informations. Account doesn't exists, please check 'CallBackAccountChange'");
return;
}
var accountInfo = new AccountInfo();
accountInfo.Create(accountInfoStr);
AccountInfo = accountInfo;
AccountInfoUpdated?.Invoke(this, AccountInfo);
}
}
}