Skip to content

Commit

Permalink
fix for issues 166 and 139
Browse files Browse the repository at this point in the history
  • Loading branch information
itailiors committed Dec 6, 2024
1 parent 21eab5a commit 04a9487
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 9 deletions.
18 changes: 17 additions & 1 deletion src/Angor/Client/Pages/Create.razor
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
@inject IRelayService _RelayService
@inject ILogger<Create> _Logger;
@inject IHtmlStripperService HtmlStripperService;
@inject IFounderKeyService FounderKeyService


@inject IFounderTransactionActions _founderTransactionActions
<NotificationComponent @ref="notificationComponent"/>
Expand Down Expand Up @@ -847,6 +849,21 @@

try
{
// Check if the FounderKey is in use and get the total keys used
var keyCheckResult = await FounderKeyService.CheckFounderKeyAsync(project.ProjectInfo.FounderKey);

if (keyCheckResult.IsKeyInUse)
{
notificationComponent.ShowErrorMessage("The FounderKey is already in use. Please use a different key.");
return;
}

if (keyCheckResult.TotalKeysInUse >= 14)
{
notificationComponent.ShowErrorMessage("You cannot create more than 15 projects.");
return;
}

var words = await passwordComponent.GetWalletAsync();
var accountInfo = storage.GetAccountInfo(network.Name);

Expand All @@ -863,7 +880,6 @@
}
catch (Exception e)
{
_Logger.LogError(e, e.Message);
notificationComponent.ShowErrorMessage(e.Message,e);
}
finally
Expand Down
30 changes: 22 additions & 8 deletions src/Angor/Client/Pages/Founder.razor
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@

</div>
</div>
<button class="btn btn-border" @onclick="NavigateToCreateProject" disabled="@scanningForProjects">
<i>
<Icon IconName="add"></Icon>
</i>
<span class="nav-link-text ms-1">
@(scanningForProjects ? "Scanning..." : "Create Project")
</span>
</button>
<div class="tooltip-container" data-bs-toggle="tooltip" title="@GetCreateButtonTooltip()">
<button
class="btn btn-border"
@onclick="NavigateToCreateProject"
disabled="@scanningForProjects || @(founderProjects.Count >= 14)">
<i>
<Icon IconName="add"></Icon>
</i>
<span class="nav-link-text ms-1">
@(scanningForProjects ? "Scanning..." : "Create Project")
</span>
</button>
</div>

</div>
<p class="mb-0 font-weight-normal text-sm mt-4">
Expand Down Expand Up @@ -214,5 +219,14 @@

NavigationManager.NavigateTo("/create");
}
private string GetCreateButtonTooltip()
{
if (founderProjects.Count >= 15)
return "You have reached the maximum number of projects. Please manage your existing projects.";
if (scanningForProjects)
return "Scanning in progress...";
return "Create a new project.";
}


}
80 changes: 80 additions & 0 deletions src/Angor/Client/Services/FounderKeyService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Angor.Shared.Models;
using Angor.Shared.Services;
using Nostr.Client.Messages;
using Angor.Client.Storage;

public class FounderKeyService : IFounderKeyService
{
private readonly IWalletStorage _walletStorage;
private readonly IRelayService _relayService;
private readonly ISerializer _serializer;




public FounderKeyService(
IWalletStorage walletStorage,
IIndexerService indexerService,
IRelayService relayService,
IClientStorage storage,
ISerializer serializer)
{
_walletStorage = walletStorage;
_relayService = relayService;
_serializer = serializer;
}

public async Task<bool> IsFounderKeyInUseAsync(string founderKey)
{
var keys = _walletStorage.GetFounderKeys();
var inUseKeys = new HashSet<string>();

await _relayService.RequestProjectCreateEventsByPubKeyAsync(
keys.Keys.Select(k => k.NostrPubKey).ToArray(),
eventMessage =>
{
if (eventMessage.Kind == NostrKind.ApplicationSpecificData)
{
var projectInfo = _serializer.Deserialize<ProjectInfo>(eventMessage.Content);
inUseKeys.Add(projectInfo.FounderKey);
}
},
null);

return inUseKeys.Contains(founderKey);
}

public async Task<FounderKeyCheckResult> CheckFounderKeyAsync(string founderKey)
{
var keys = _walletStorage.GetFounderKeys();
var usedKeys = new HashSet<string>();

// Perform the relay scan to get project data
await _relayService.RequestProjectCreateEventsByPubKeyAsync(
keys.Keys.Select(k => k.NostrPubKey).ToArray(),
eventMessage =>
{
if (eventMessage.Kind == NostrKind.ApplicationSpecificData)
{
var projectInfo = _serializer.Deserialize<ProjectInfo>(eventMessage.Content);
usedKeys.Add(projectInfo.FounderKey);
}
},
null);

return new FounderKeyCheckResult
{
IsKeyInUse = usedKeys.Contains(founderKey),
TotalKeysInUse = usedKeys.Count
};
}


}


public class FounderKeyCheckResult
{
public bool IsKeyInUse { get; set; }
public int TotalKeysInUse { get; set; }
}
7 changes: 7 additions & 0 deletions src/Angor/Client/Services/IFounderKeyService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public interface IFounderKeyService
{
Task<bool> IsFounderKeyInUseAsync(string founderKey);

Task<FounderKeyCheckResult> CheckFounderKeyAsync(string founderKey);

}
2 changes: 2 additions & 0 deletions src/Angor/Shared/Services/IRelayService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ public interface IRelayService

string SendDirectMessagesForPubKeyAsync(string senderNosterPrivateKey, string nostrPubKey, string encryptedMessage,
Action<NostrOkResponse> onResponseAction);
Task RequestProjectCreateEventsByPubKeyAsync(string[] nPubs, Action<NostrEvent> onResponseAction, Action? onEoseAction);

}
52 changes: 52 additions & 0 deletions src/Angor/Shared/Services/RelayService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,58 @@ public void RequestProjectCreateEventsByPubKey(Action<NostrEvent> onResponseActi
Kinds = [NostrKind.ApplicationSpecificData, NostrKind.Metadata],
}));
}

// add the same as function ^^ as async
public async Task RequestProjectCreateEventsByPubKeyAsync(
string[] nPubs,
Action<NostrEvent> onResponseAction,
Action? onEoseAction)
{
var subscriptionKey = Guid.NewGuid().ToString().Replace("-", "");

var nostrClient = _communicationFactory.GetOrCreateClient(_networkService);

if (!_subscriptionsHandling.RelaySubscriptionAdded(subscriptionKey))
{
var subscription = nostrClient.Streams.EventStream
.Where(_ => _.Subscription == subscriptionKey)
.Where(_ => _.Event is not null)
.Select(_ => _.Event)
.Subscribe(onResponseAction!);

_subscriptionsHandling.TryAddRelaySubscription(subscriptionKey, subscription);
}

if (onEoseAction != null)
{
_subscriptionsHandling.TryAddEoseAction(subscriptionKey, onEoseAction);
}

var tcs = new TaskCompletionSource();

// Handle end-of-stream asynchronously
if (onEoseAction == null)
{
_subscriptionsHandling.TryAddEoseAction(subscriptionKey, () => tcs.SetResult());
}
else
{
_subscriptionsHandling.TryAddEoseAction(subscriptionKey, () =>
{
onEoseAction();
tcs.SetResult();
});
}

nostrClient.Send(new NostrRequest(subscriptionKey, new NostrFilter
{
Authors = nPubs,
Kinds = new[] { NostrKind.ApplicationSpecificData, NostrKind.Metadata },
}));

await tcs.Task; // Wait for the end of stream
}


public Task LookupSignaturesDirectMessagesForPubKeyAsync(string nostrPubKey, DateTime? since, int? limit, Action<NostrEvent> onResponseAction)
{
Expand Down

0 comments on commit 04a9487

Please sign in to comment.