From 6ddc6d56f1730a0d8002d72cafcc6e56ad8dfa2b Mon Sep 17 00:00:00 2001 From: dangershony Date: Tue, 21 Nov 2023 13:48:48 +0000 Subject: [PATCH 1/2] generate address qrcode --- src/Angor/Client/Angor.Client.csproj | 1 + src/Angor/Client/Pages/Wallet.razor | 36 ++++++++++++++++++++++++---- src/Angor/Client/Shared/Util.cs | 19 +++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 src/Angor/Client/Shared/Util.cs diff --git a/src/Angor/Client/Angor.Client.csproj b/src/Angor/Client/Angor.Client.csproj index fa627785..6095f236 100644 --- a/src/Angor/Client/Angor.Client.csproj +++ b/src/Angor/Client/Angor.Client.csproj @@ -15,6 +15,7 @@ + diff --git a/src/Angor/Client/Pages/Wallet.razor b/src/Angor/Client/Pages/Wallet.razor index 639188b4..c0ece45d 100644 --- a/src/Angor/Client/Pages/Wallet.razor +++ b/src/Angor/Client/Pages/Wallet.razor @@ -169,15 +169,13 @@

QR Code

- QR Code + QR Code
- - @@ -391,6 +389,8 @@ private int _feeMax = 3; DateTime _lastFeeRefresh = DateTime.MinValue; + private string base64qrcode; + protected override Task OnInitializedAsync() { hasWallet = _walletStorage.HasWallet(); @@ -399,6 +399,9 @@ { localAccountInfo = GetAccountInfoFromStorage(); } + + GenerateQRCode(); + return Task.CompletedTask; } @@ -475,7 +478,7 @@ ClearWalletWords(); StateHasChanged(); } - + private void ClearWalletWords() { walletWords = null; @@ -520,7 +523,30 @@ await _clipboardService.WriteTextAsync(address); } - + + private static string lastqrcode; + private static string lastaddress; + + private void GenerateQRCode() + { + if (lastaddress == localAccountInfo.GetNextReceiveAddress()) + { + base64qrcode = lastqrcode; + return; + } + + Task.Run(() => + { + var address = localAccountInfo.GetNextReceiveAddress(); + + base64qrcode = Util.GenerateQRCode(address); + lastqrcode = base64qrcode; + lastaddress = address; + + StateHasChanged(); + }); + } + private async Task RefreshFee() { // refresh fee if last refresh was 60 seconds ago diff --git a/src/Angor/Client/Shared/Util.cs b/src/Angor/Client/Shared/Util.cs new file mode 100644 index 00000000..c28cbeb7 --- /dev/null +++ b/src/Angor/Client/Shared/Util.cs @@ -0,0 +1,19 @@ +using Angor.Client.Storage; +using Angor.Shared.Models; +using Microsoft.AspNetCore.Components; +using QRCoder; + +namespace Angor.Client.Shared +{ + public class Util + { + public static string GenerateQRCode(string content) + { + using QRCodeGenerator qrGenerator = new QRCodeGenerator(); + using QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q); + using PngByteQRCode pngByteQRCode = new PngByteQRCode(qrCodeData); + return Convert.ToBase64String(pngByteQRCode.GetGraphic(10)); + } + + } +} From e318c8464b24b92bce28f802645dd1cbdb0b9005 Mon Sep 17 00:00:00 2001 From: dangershony Date: Wed, 22 Nov 2023 16:19:47 +0000 Subject: [PATCH 2/2] Create a component --- src/Angor/Client/Components/ShowQrCode.razor | 58 ++++++++++++++++++++ src/Angor/Client/Pages/Wallet.razor | 56 ++++++------------- src/Angor/Client/Shared/Util.cs | 19 ------- 3 files changed, 75 insertions(+), 58 deletions(-) create mode 100644 src/Angor/Client/Components/ShowQrCode.razor delete mode 100644 src/Angor/Client/Shared/Util.cs diff --git a/src/Angor/Client/Components/ShowQrCode.razor b/src/Angor/Client/Components/ShowQrCode.razor new file mode 100644 index 00000000..dd54bf46 --- /dev/null +++ b/src/Angor/Client/Components/ShowQrCode.razor @@ -0,0 +1,58 @@ +@using Angor.Shared.Services +@using Angor.Client.Models +@using Nostr.Client.Messages +@using Nostr.Client.Messages.Metadata +@using QRCoder + + +
+

QR Code

+ @if (!string.IsNullOrEmpty(base64qrcode)) + { + QR Code + } +
+ +@code { + + [Parameter] + public string Data { get; set; } + + private static string lastqrcode; + private static string lastaddress; + private string base64qrcode; + + protected override async Task OnInitializedAsync() + { + GenerateQRCode(Data); + } + + public Task GenerateQRCode(string newData) + { + Data = newData; + + if (lastaddress == Data) + { + base64qrcode = lastqrcode; + return Task.CompletedTask; + } + + return Task.Run(() => + { + base64qrcode = GenerateQRCodeInternal(Data); + lastqrcode = base64qrcode; + lastaddress = Data; + + StateHasChanged(); + + }); + } + + public static string GenerateQRCodeInternal(string content) + { + using QRCodeGenerator qrGenerator = new QRCodeGenerator(); + using QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q); + using PngByteQRCode pngByteQRCode = new PngByteQRCode(qrCodeData); + return Convert.ToBase64String(pngByteQRCode.GetGraphic(10)); + } +} \ No newline at end of file diff --git a/src/Angor/Client/Pages/Wallet.razor b/src/Angor/Client/Pages/Wallet.razor index c0ece45d..91787a94 100644 --- a/src/Angor/Client/Pages/Wallet.razor +++ b/src/Angor/Client/Pages/Wallet.razor @@ -5,6 +5,7 @@ @using Angor.Client.Services @using Angor.Client.Storage @using Angor.Shared.Models +@using Angor.Client.Components @inject HttpClient Http @inject IClientStorage storage; @@ -25,8 +26,7 @@ - - + @if (!hasWallet) { @@ -163,14 +163,12 @@

Receive Address

-

@localAccountInfo.GetNextReceiveAddress()

+

@nextReceiveAddress

-
-

QR Code

- - QR Code -
+ + +
@@ -389,7 +387,9 @@ private int _feeMax = 3; DateTime _lastFeeRefresh = DateTime.MinValue; - private string base64qrcode; + string? nextReceiveAddress = string.Empty; + + ShowQrCode showQrCode; protected override Task OnInitializedAsync() { @@ -398,10 +398,9 @@ if (hasWallet) { localAccountInfo = GetAccountInfoFromStorage(); + nextReceiveAddress = localAccountInfo.GetNextReceiveAddress(); } - GenerateQRCode(); - return Task.CompletedTask; } @@ -419,7 +418,11 @@ storage.SetAccountInfo(network.Name, accountInfo); localAccountInfo = accountInfo; - + + nextReceiveAddress = localAccountInfo.GetNextReceiveAddress(); + + showQrCode.GenerateQRCode(nextReceiveAddress); + return new OperationResult { Success = true }; }); @@ -513,38 +516,13 @@ public async Task CopyNextReceiveAddress() { - var address = localAccountInfo.GetNextReceiveAddress(); - - if (string.IsNullOrEmpty(address)) + if (string.IsNullOrEmpty(nextReceiveAddress)) { notificationComponent.ShowErrorMessage("New address was not created"); return; } - await _clipboardService.WriteTextAsync(address); - } - - private static string lastqrcode; - private static string lastaddress; - - private void GenerateQRCode() - { - if (lastaddress == localAccountInfo.GetNextReceiveAddress()) - { - base64qrcode = lastqrcode; - return; - } - - Task.Run(() => - { - var address = localAccountInfo.GetNextReceiveAddress(); - - base64qrcode = Util.GenerateQRCode(address); - lastqrcode = base64qrcode; - lastaddress = address; - - StateHasChanged(); - }); + await _clipboardService.WriteTextAsync(nextReceiveAddress); } private async Task RefreshFee() diff --git a/src/Angor/Client/Shared/Util.cs b/src/Angor/Client/Shared/Util.cs deleted file mode 100644 index c28cbeb7..00000000 --- a/src/Angor/Client/Shared/Util.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Angor.Client.Storage; -using Angor.Shared.Models; -using Microsoft.AspNetCore.Components; -using QRCoder; - -namespace Angor.Client.Shared -{ - public class Util - { - public static string GenerateQRCode(string content) - { - using QRCodeGenerator qrGenerator = new QRCodeGenerator(); - using QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q); - using PngByteQRCode pngByteQRCode = new PngByteQRCode(qrCodeData); - return Convert.ToBase64String(pngByteQRCode.GetGraphic(10)); - } - - } -}