Skip to content

Commit

Permalink
Add NostrConversionHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
miladsoft committed Oct 23, 2024
1 parent 153cc3c commit 6ca7b02
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 13 deletions.
64 changes: 51 additions & 13 deletions src/Angor/Client/Pages/View.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -326,9 +327,21 @@
{
<div class="mb-4 mt-4">

<label for="NostrPublicKey" class="form-label">Project NOSTR public key (hex)</label>
@{
var Npub = NostrConversionHelper.ConvertHexToNpub(project.ProjectInfo.NostrPubKey);
}
<label for="NostrNpubPublicKey" class="form-label">Project NOSTR public key (npub)</label>

<div class="input-group">
<InputText id="NostrNpubPublicKey" @bind-Value="@Npub" class="form-control" placeholder="@Npub" readonly />
<button @onclick="OpenInBrowseAsync" class="btn btn-border">
<Icon IconName="link"></Icon>
</button>
</div>
<br />
<label for="NostrHexPublicKey" class="form-label">Project NOSTR public key (hex)</label>
<div class="input-group">
<InputText id="NostrPublicKey" @bind-Value="project.ProjectInfo.NostrPubKey" class="form-control" placeholder="@project.ProjectInfo.NostrPubKey" readonly />
<InputText id="NostrHexPublicKey" @bind-Value="project.ProjectInfo.NostrPubKey" class="form-control" placeholder="@project.ProjectInfo.NostrPubKey" readonly />
<button @onclick="OpenInBrowseAsync" class="btn btn-border">
<Icon IconName="link"></Icon>
</button>
Expand All @@ -353,13 +366,22 @@
</div>
}

@if (!string.IsNullOrEmpty(NostrSecKey))
@if (!string.IsNullOrEmpty(NostrHexSecKey) || !string.IsNullOrEmpty(NostrNsecSecKey))
{
<br />
<label for="NostrSecKey" class="form-label mt-4">Project NOSTR private key (hex)</label>
<label for="NostrNsecSecKey" class="form-label mt-4">Project NOSTR private key (nsec)</label>
<div class="input-group">
<InputText id="NostrNsecSecKey" @bind-Value="NostrNsecSecKey" class="form-control nsec-box" readonly />
<button @onclick="CopyNsecSecKeyToClipboardAsync" class="btn btn-border">
<Icon IconName="copy"></Icon>
</button>
</div>

<br />
<label for="NostrHexSecKey" class="form-label mt-4">Project NOSTR private key (hex)</label>
<div class="input-group">
<InputText id="NostrSecKey" @bind-Value="NostrSecKey" class="form-control nsec-box" readonly />
<button @onclick="CopyNsecToClipboardAsync" class="btn btn-border">
<InputText id="NostrHexSecKey" @bind-Value="NostrHexSecKey" class="form-control nsec-box" readonly />
<button @onclick="CopyHexSecKeyToClipboardAsync" class="btn btn-border">
<Icon IconName="copy"></Icon>
</button>
</div>
Expand Down Expand Up @@ -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)>
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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);
}
}
Expand Down
73 changes: 73 additions & 0 deletions src/Angor/Shared/Utilities/NostrConversionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Nostr.Client.Utils;
using System;

namespace Angor.Shared.Utilities
{
public static class NostrConversionHelper
{
/// <summary>
/// Convert Bech32 key to hex
/// </summary>
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;
}
}

/// <summary>
/// Convert hex key to Bech32 with a specified prefix
/// </summary>
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;
}
}

/// <summary>
/// Check if the given hex key is valid
/// </summary>
public static bool IsHexValid(string hexKey)
{
return NostrConverter.IsHex(hexKey);
}

/// <summary>
/// Convert hex key to npub (Bech32 format)
/// </summary>
public static string? ConvertHexToNpub(string hexKey)
{
return NostrConverter.ToNpub(hexKey);
}

/// <summary>
/// Convert hex key to nsec (Bech32 format)
/// </summary>
public static string? ConvertHexToNsec(string hexKey)
{
return NostrConverter.ToNsec(hexKey);
}

/// <summary>
/// Convert hex key to note (Bech32 format)
/// </summary>
public static string? ConvertHexToNote(string hexKey)
{
return NostrConverter.ToNote(hexKey);
}
}
}

0 comments on commit 6ca7b02

Please sign in to comment.