From 6ca7b02c249887b8a20ae014328aaa8eeb334de4 Mon Sep 17 00:00:00 2001 From: Milad Raeisi Date: Wed, 23 Oct 2024 14:39:06 +0400 Subject: [PATCH] Add NostrConversionHelper --- src/Angor/Client/Pages/View.razor | 64 ++++++++++++---- .../Shared/Utilities/NostrConversionHelper.cs | 73 +++++++++++++++++++ 2 files changed, 124 insertions(+), 13 deletions(-) create mode 100644 src/Angor/Shared/Utilities/NostrConversionHelper.cs diff --git a/src/Angor/Client/Pages/View.razor b/src/Angor/Client/Pages/View.razor index 5b68b853..5002f09d 100644 --- a/src/Angor/Client/Pages/View.razor +++ b/src/Angor/Client/Pages/View.razor @@ -2,6 +2,7 @@ @using Angor.Shared @using Angor.Client.Storage @using Angor.Shared.Models +@using Angor.Shared.Utilities @using Blockcore.NBitcoin @using Angor.Shared.Services @using Angor.Client.Models @@ -326,9 +327,21 @@ {
- + @{ + var Npub = NostrConversionHelper.ConvertHexToNpub(project.ProjectInfo.NostrPubKey); + } + + +
+ + +
+
+
- + @@ -353,13 +366,22 @@
} - @if (!string.IsNullOrEmpty(NostrSecKey)) + @if (!string.IsNullOrEmpty(NostrHexSecKey) || !string.IsNullOrEmpty(NostrNsecSecKey)) {
- + +
+ + +
+ +
+
- -
@@ -394,10 +416,13 @@ string myProjectExplorerLink; string projectExplorerLink; - private string NostrSecKey { get; set; } = string.Empty; + private string NostrNsecSecKey { get; set; } = string.Empty; + private string NostrHexSecKey { get; set; } = string.Empty; + + private bool isGeneratingNsec = false; private string errorMessage = string.Empty; - + private string error; private List<(string Hash, int Amount)> SelectedSeeders = new List<(string hash, int amount)> @@ -591,8 +616,11 @@ { var words = await passwordComponent.GetWalletAsync(); var nostrKey = _derivationOperations.DeriveProjectNostrPrivateKey(words, founderProject.ProjectIndex); - var nsec = NBitcoin.DataEncoders.Encoders.Hex.EncodeData(nostrKey.ToBytes()); - NostrSecKey = nsec; + var hex = NBitcoin.DataEncoders.Encoders.Hex.EncodeData(nostrKey.ToBytes()); + NostrHexSecKey = hex; + var nsec = NostrConversionHelper.ConvertHexToNsec(hex); + NostrNsecSecKey = nsec!; + StateHasChanged(); } } @@ -608,11 +636,21 @@ } } - private async Task CopyNsecToClipboardAsync() + + private async Task CopyNsecSecKeyToClipboardAsync() + { + if (!string.IsNullOrEmpty(NostrNsecSecKey)) + { + await _clipboardService.WriteTextAsync(NostrNsecSecKey); + notificationComponent.ShowNotificationMessage("Copied to clipboard!", 3); + } + } + + private async Task CopyHexSecKeyToClipboardAsync() { - if (!string.IsNullOrEmpty(NostrSecKey)) + if (!string.IsNullOrEmpty(NostrHexSecKey)) { - await _clipboardService.WriteTextAsync(NostrSecKey); + await _clipboardService.WriteTextAsync(NostrHexSecKey); notificationComponent.ShowNotificationMessage("Copied to clipboard!", 3); } } diff --git a/src/Angor/Shared/Utilities/NostrConversionHelper.cs b/src/Angor/Shared/Utilities/NostrConversionHelper.cs new file mode 100644 index 00000000..b0162e71 --- /dev/null +++ b/src/Angor/Shared/Utilities/NostrConversionHelper.cs @@ -0,0 +1,73 @@ +using Nostr.Client.Utils; +using System; + +namespace Angor.Shared.Utilities +{ + public static class NostrConversionHelper + { + /// + /// Convert Bech32 key to hex + /// + public static string? ConvertBech32ToHex(string bech32Key) + { + try + { + string? hrp; + return NostrConverter.ToHex(bech32Key, out hrp); + } + catch (Exception ex) + { + Console.WriteLine($"Error converting Bech32 to hex: {ex.Message}"); + return null; + } + } + + /// + /// Convert hex key to Bech32 with a specified prefix + /// + public static string? ConvertHexToBech32(string hexKey, string prefix) + { + try + { + return NostrConverter.ToBech32(hexKey, prefix); + } + catch (Exception ex) + { + Console.WriteLine($"Error converting hex to Bech32: {ex.Message}"); + return null; + } + } + + /// + /// Check if the given hex key is valid + /// + public static bool IsHexValid(string hexKey) + { + return NostrConverter.IsHex(hexKey); + } + + /// + /// Convert hex key to npub (Bech32 format) + /// + public static string? ConvertHexToNpub(string hexKey) + { + return NostrConverter.ToNpub(hexKey); + } + + /// + /// Convert hex key to nsec (Bech32 format) + /// + public static string? ConvertHexToNsec(string hexKey) + { + return NostrConverter.ToNsec(hexKey); + } + + /// + /// Convert hex key to note (Bech32 format) + /// + public static string? ConvertHexToNote(string hexKey) + { + return NostrConverter.ToNote(hexKey); + } + } +}