-
Notifications
You must be signed in to change notification settings - Fork 39
Add cancellation and below-threshold investment notifications #626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
src/Angor/Avalonia/Angor.Sdk/Funding/Investor/InvestmentAppService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/Angor/Avalonia/Angor.Sdk/Funding/Investor/Operations/InvestmentNotification.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| namespace Angor.Sdk.Funding.Investor.Operations; | ||
|
|
||
| /// <summary> | ||
| /// Notification sent to founder when a below-threshold investment is published directly to the blockchain. | ||
| /// Contains only the essential information needed - the founder can fetch full details from the indexer. | ||
| /// </summary> | ||
| public class InvestmentNotification | ||
| { | ||
| /// <summary> | ||
| /// The project identifier this investment is for. | ||
| /// </summary> | ||
| public string ProjectIdentifier { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// The transaction ID of the published investment transaction. | ||
| /// </summary> | ||
| public string TransactionId { get; set; } = string.Empty; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Notification sent to founder when an investor cancels their investment request. | ||
| /// This is sent unencrypted as it only contains public identifiers. | ||
| /// </summary> | ||
| public class CancellationNotification | ||
| { | ||
| /// <summary> | ||
| /// The project identifier this cancellation is for. | ||
| /// </summary> | ||
| public string ProjectIdentifier { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// The Nostr event ID of the original investment request that is being cancelled. | ||
| /// </summary> | ||
| public string RequestEventId { get; set; } = string.Empty; | ||
| } | ||
|
|
73 changes: 73 additions & 0 deletions
73
src/Angor/Avalonia/Angor.Sdk/Funding/Investor/Operations/NotifyFounderOfCancellation.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| using Angor.Sdk.Common; | ||
| using Angor.Sdk.Funding.Services; | ||
| using Angor.Sdk.Funding.Shared; | ||
| using Angor.Shared; | ||
| using Angor.Shared.Services; | ||
| using Blockcore.NBitcoin; | ||
| using Blockcore.NBitcoin.DataEncoders; | ||
| using CSharpFunctionalExtensions; | ||
| using MediatR; | ||
|
|
||
| namespace Angor.Sdk.Funding.Investor.Operations; | ||
|
|
||
| public static class NotifyFounderOfCancellation | ||
| { | ||
| public record NotifyFounderOfCancellationRequest( | ||
| WalletId WalletId, | ||
| ProjectId ProjectId, | ||
| string RequestEventId) : IRequest<Result<NotifyFounderOfCancellationResponse>>; | ||
|
|
||
| public record NotifyFounderOfCancellationResponse(DateTime EventTime, string EventId); | ||
|
|
||
| public class NotifyFounderOfCancellationHandler( | ||
| IProjectService projectService, | ||
| ISeedwordsProvider seedwordsProvider, | ||
| IDerivationOperations derivationOperations, | ||
| ISerializer serializer, | ||
| ISignService signService) : IRequestHandler<NotifyFounderOfCancellationRequest, Result<NotifyFounderOfCancellationResponse>> | ||
| { | ||
| public async Task<Result<NotifyFounderOfCancellationResponse>> Handle( | ||
| NotifyFounderOfCancellationRequest request, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var projectResult = await projectService.GetAsync(request.ProjectId); | ||
| if (projectResult.IsFailure) | ||
| return Result.Failure<NotifyFounderOfCancellationResponse>(projectResult.Error); | ||
|
|
||
| var sensitiveDataResult = await seedwordsProvider.GetSensitiveData(request.WalletId.Value); | ||
| if (sensitiveDataResult.IsFailure) | ||
| return Result.Failure<NotifyFounderOfCancellationResponse>(sensitiveDataResult.Error); | ||
|
|
||
| var walletWords = sensitiveDataResult.Value.ToWalletWords(); | ||
| var project = projectResult.Value; | ||
|
|
||
| try | ||
| { | ||
| var investorNostrPrivateKey = await derivationOperations.DeriveProjectNostrPrivateKeyAsync( | ||
| walletWords, project.FounderKey); | ||
| var investorNostrPrivateKeyHex = Encoders.Hex.EncodeData(investorNostrPrivateKey.ToBytes()); | ||
|
|
||
| var notification = new CancellationNotification | ||
| { | ||
| ProjectIdentifier = request.ProjectId.Value, | ||
| RequestEventId = request.RequestEventId | ||
| }; | ||
|
|
||
| var content = serializer.Serialize(notification); | ||
|
|
||
| var (eventTime, eventId) = signService.NotifyInvestmentCancelled( | ||
| content, | ||
| investorNostrPrivateKeyHex, | ||
| project.NostrPubKey, | ||
| _ => { }); | ||
|
|
||
| return Result.Success(new NotifyFounderOfCancellationResponse(eventTime, eventId)); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return Result.Failure<NotifyFounderOfCancellationResponse>($"Error sending cancellation notification: {ex.Message}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
113 changes: 113 additions & 0 deletions
113
src/Angor/Avalonia/Angor.Sdk/Funding/Investor/Operations/NotifyFounderOfInvestment.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| using Angor.Sdk.Common; | ||
| using Angor.Sdk.Funding.Projects.Domain; | ||
| using Angor.Sdk.Funding.Services; | ||
| using Angor.Sdk.Funding.Shared; | ||
| using Angor.Sdk.Funding.Shared.TransactionDrafts; | ||
| using Angor.Shared; | ||
| using Angor.Shared.Models; | ||
| using Angor.Shared.Services; | ||
| using Blockcore.NBitcoin; | ||
| using Blockcore.NBitcoin.DataEncoders; | ||
| using CSharpFunctionalExtensions; | ||
| using MediatR; | ||
|
|
||
| namespace Angor.Sdk.Funding.Investor.Operations; | ||
|
|
||
| /// <summary> | ||
| /// Notifies the founder of a below-threshold investment that was published directly to the blockchain. | ||
| /// This allows the investment to appear in the founder's investment list with the investor's Nostr pubkey for chat functionality. | ||
| /// </summary> | ||
| public static class NotifyFounderOfInvestment | ||
| { | ||
| public record NotifyFounderOfInvestmentRequest( | ||
| WalletId WalletId, | ||
| ProjectId ProjectId, | ||
| InvestmentDraft Draft) : IRequest<Result<NotifyFounderOfInvestmentResponse>>; | ||
|
|
||
| public record NotifyFounderOfInvestmentResponse(DateTime EventTime, string EventId); | ||
|
|
||
| public class NotifyFounderOfInvestmentHandler( | ||
| IProjectService projectService, | ||
| ISeedwordsProvider seedwordsProvider, | ||
| IDerivationOperations derivationOperations, | ||
| IEncryptionService encryptionService, | ||
| ISerializer serializer, | ||
| ISignService signService) : IRequestHandler<NotifyFounderOfInvestmentRequest, Result<NotifyFounderOfInvestmentResponse>> | ||
| { | ||
| public async Task<Result<NotifyFounderOfInvestmentResponse>> Handle( | ||
| NotifyFounderOfInvestmentRequest request, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var projectResult = await projectService.GetAsync(request.ProjectId); | ||
| if (projectResult.IsFailure) | ||
| { | ||
| return Result.Failure<NotifyFounderOfInvestmentResponse>(projectResult.Error); | ||
| } | ||
|
|
||
| var sensitiveDataResult = await seedwordsProvider.GetSensitiveData(request.WalletId.Value); | ||
| if (sensitiveDataResult.IsFailure) | ||
| { | ||
| return Result.Failure<NotifyFounderOfInvestmentResponse>(sensitiveDataResult.Error); | ||
| } | ||
|
|
||
| var walletWords = sensitiveDataResult.Value.ToWalletWords(); | ||
| var project = projectResult.Value; | ||
|
|
||
| var notificationResult = await SendInvestmentNotification( | ||
| walletWords, | ||
| project, | ||
| request.ProjectId.Value, | ||
| request.Draft.TransactionId); | ||
|
|
||
| if (notificationResult.IsFailure) | ||
| { | ||
| return Result.Failure<NotifyFounderOfInvestmentResponse>(notificationResult.Error); | ||
| } | ||
|
|
||
| return Result.Success(new NotifyFounderOfInvestmentResponse( | ||
| notificationResult.Value.eventTime, | ||
| notificationResult.Value.eventId)); | ||
| } | ||
|
|
||
| private async Task<Result<(DateTime eventTime, string eventId)>> SendInvestmentNotification( | ||
| WalletWords walletWords, | ||
| Project project, | ||
| string projectIdentifier, | ||
| string transactionId) | ||
| { | ||
| try | ||
| { | ||
| var investorNostrPrivateKey = await derivationOperations.DeriveProjectNostrPrivateKeyAsync( | ||
| walletWords, | ||
| project.FounderKey); | ||
| var investorNostrPrivateKeyHex = Encoders.Hex.EncodeData(investorNostrPrivateKey.ToBytes()); | ||
|
|
||
| var notification = new InvestmentNotification | ||
| { | ||
| ProjectIdentifier = projectIdentifier, | ||
| TransactionId = transactionId | ||
| }; | ||
|
|
||
| var serializedNotification = serializer.Serialize(notification); | ||
|
|
||
| var encryptedContent = await encryptionService.EncryptNostrContentAsync( | ||
| investorNostrPrivateKeyHex, | ||
| project.NostrPubKey, | ||
| serializedNotification); | ||
|
|
||
| var (eventTime, eventId) = signService.NotifyInvestmentCompleted( | ||
| encryptedContent, | ||
| investorNostrPrivateKeyHex, | ||
| project.NostrPubKey, | ||
| _ => { }); | ||
|
|
||
| return Result.Success((eventTime, eventId)); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return Result.Failure<(DateTime, string)>($"Error sending investment notification: {ex.Message}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we have a general notification message and inside several options? looks to me like here we will have a different message type per notification