-
Notifications
You must be signed in to change notification settings - Fork 33
Add Pending Investments to the Backend API #365
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
42 changes: 42 additions & 0 deletions
42
src/Angor/Avalonia/Angor.Contexts.Funding.Tests/NostrDecrypterTests.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,42 @@ | ||
using Angor.Contexts.Funding.Founder.Operations; | ||
using Angor.Contexts.Funding.Projects.Domain; | ||
using Angor.Contexts.Funding.Shared; | ||
using Angor.Contexts.Funding.Tests.TestDoubles; | ||
using Angor.Shared; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Serilog; | ||
using Xunit.Abstractions; | ||
|
||
namespace Angor.Contexts.Funding.Tests; | ||
|
||
public class NostrDecrypterTests(ITestOutputHelper output) | ||
{ | ||
[Fact] | ||
public async Task Decrypt_Nostr_message() | ||
{ | ||
// Arrange | ||
var sut = CreateSut(); | ||
var nostrMessage = new NostrMessage("SOME ID", "c57cce49441740454e54fee15131b9699d54d4928a5eaa0756214dc21961cf1b", "gYpNKJuzweBOfsOXOOInoyEXI+jlOxs2dQ//Yk3CSpkvUQPs6od9eq/HCUGBPetHlTBlmlJheVNYzl+u+4NcShzgV2kmOb7rbH88UUCZXRyXdxwwnGU8rvM4BKNc/WqS3Rsyny+hDpy/YpEpsSMH46kTAqPm6+PjyyIoTGNMqAui95RdTNMHXju6hLXse6kTL93aD8xpCsLVZ773PaJxtO8qzVmCJ9Ton+TMkD3ujxcUAv/Dy03vRQWp96iNSfsBif6OUlenGPk5YUzgKscjNVVxOgLCB7hdlM9NQiTDB3EKabl6p7L9Dg77UftRcrHwYE44QYtgOjTggNRtoWYhAxgITp4IHtIOLJRIBexlxrY+2R8Zfkd1w0qvTm41zi9eITB0/rJEE/u8jMFJLVxDMLcaGVWuj1/9NlcZ9jTpf5ANOr6VnWDi2JRUsfJXDxUNyYSQttoclZVYD1gLjv/+ags+aBQzpiz5XOAC1NVOouKTE4Wj4b9MLvMJLMfyjtmBPBc++7uy0dOX6YVQqorOzNtk6NkOQFPDdCZK5zmQB2twWwwo6TlzKUT3n3msoeHi7AuBLAxoD7v2hmY61SMt7Z8tM4kOmaxMF+ed1ZKrD1gyfWniOD/4GXj+lovTPw4vOZRLmfYTTCKJROB/qBheH5tyuNahn4FiIWYxATTBO3jJoKw9evQdf8haDptwlPTZUwlf3C+T0nf+9Z+bXSWaRPsORQzNgWYnRys/G4OpodXiVFwdH+6rYeXDMXAiCMEDXBBfT2e5QZxVuJuSbhTW75jNvDEaHrg12UxvA+7nqCy4rpHC3nDQaABEprXdYK6bANqIWSsNCsmUhSjLfpH+gljJ5iiH+yE6kf/tGGNnSS4pKa9cPSMbSB68eIWwUeaNRRBtcJkvr42K35jUE2vEQhqs3ojQlMItgy4/YvcXP6+DMzGZbxdDWO8u0xsE0qNpkCavwi/+UgVsYj5dEK32nt8kiBryr6/UJyWprOa8lUQ=?iv=+uu8GD5eAnBhjP3FjCVeSQ==", DateTime.Now); | ||
|
||
// Act | ||
var clientKeyResult = await sut.Decrypt(Guid.NewGuid(), new ProjectId("angor1qatlv9htzte8vtddgyxpgt78ruyzaj57n4l7k46"), nostrMessage); | ||
|
||
// Assert | ||
Assert.True(clientKeyResult.IsSuccess); | ||
Assert.NotEmpty(clientKeyResult.Value); | ||
} | ||
|
||
|
||
private NostrDecrypter CreateSut() | ||
{ | ||
var serviceCollection = new ServiceCollection(); | ||
|
||
var logger = new LoggerConfiguration().WriteTo.TestOutput(output).CreateLogger(); | ||
FundingContextServices.Register(serviceCollection, logger); | ||
serviceCollection.AddSingleton<ISeedwordsProvider>(sp => new TestingSeedwordsProvider("oven suggest panda hip orange cheap kite focus cross never tornado forget", "", sp.GetRequiredService<IDerivationOperations>())); | ||
|
||
var serviceProvider = serviceCollection.BuildServiceProvider(); | ||
|
||
return ActivatorUtilities.CreateInstance<NostrDecrypter>(serviceProvider); | ||
} | ||
} |
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 Unic
6D40
ode characters
73 changes: 73 additions & 0 deletions
73
src/Angor/Avalonia/Angor.Contexts.Funding/Founder/Operations/GetPendingInvestments.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 System.Reactive.Disposables; | ||
using System.Reactive.Linq; | ||
using Angor.Contexts.Funding.Projects.Domain; | ||
using Angor.Shared; | ||
using Angor.Shared.Models; | ||
using Angor.Shared.Services; | ||
using CSharpFunctionalExtensions; | ||
using MediatR; | ||
|
||
namespace Angor.Contexts.Funding.Founder.Operations; | ||
|
||
public class GetPendingInvestments | ||
{ | ||
public class GetPendingInvestmentsRequest(Guid walletId, ProjectId projectId) : IRequest<Result<IEnumerable<PendingInvestmentDto>>> | ||
{ | ||
public Guid WalletId { get; } = walletId; | ||
public ProjectId ProjectId { get; } = projectId; | ||
} | ||
|
||
public class GetPendingInvestmentsHandler(IProjectRepository projectRepository, | ||
ISignService signService, | ||
INostrDecrypter nostrDecrypter, | ||
INetworkConfiguration networkConfiguration, | ||
ISerializer serializer) : IRequestHandler<GetPendingInvestmentsRequest, Result<IEnumerable<PendingInvestmentDto>>> | ||
{ | ||
public async Task<Result<IEnumerable<PendingInvestmentDto>>> Handle(GetPendingInvestmentsRequest request, CancellationToken cancellationToken) | ||
{ | ||
var project = await projectRepository.Get(request.ProjectId); | ||
if (project.IsFailure) | ||
{ | ||
return Result.Failure<IEnumerable<PendingInvestmentDto>>(project.Error); | ||
} | ||
|
||
var nostrPubKey = project.Value.NostrPubKey; | ||
var investingMessages = InvestmentMessages(nostrPubKey); | ||
var pendingInvestmentResults = await investingMessages.SelectMany(nostrMessage => DecryptInvestmentMessage(request.WalletId, project, nostrMessage)).ToList(); | ||
|
||
return pendingInvestmentResults.Combine(); | ||
} | ||
|
||
private Task<Result<PendingInvestmentDto>> DecryptInvestmentMessage(Guid walletId, Result<Project> project, NostrMessage nostrMessage) | ||
{ | ||
return from decrypted in nostrDecrypter.Decrypt(walletId, project.Value.Id, nostrMessage) | ||
from signRecoveryRequest in Result.Try(() => serializer.Deserialize<SignRecoveryRequest>(decrypted)) | ||
select new PendingInvestmentDto(nostrMessage.Created, GetAmount(signRecoveryRequest), nostrMessage.InvestorNostrPubKey); | ||
} | ||
|
||
private IObservable<NostrMessage> InvestmentMessages(string nostrPubKey) | ||
{ | ||
return Observable.Create<NostrMessage>(observer => | ||
{ | ||
signService.LookupInvestmentRequestsAsync(nostrPubKey, null, null, | ||
(id, pubKey, content, created) => observer.OnNext(new NostrMessage(id, pubKey, content, created)), | ||
observer.OnCompleted | ||
); | ||
|
||
return Disposable.Empty; | ||
}); | ||
} | ||
|
||
private decimal GetAmount(SignRecoveryRequest signRecoveryRequest) | ||
{ | ||
var investorTrx = networkConfiguration.GetNetwork().CreateTransaction(signRecoveryRequest.InvestmentTransactionHex); | ||
|
||
return investorTrx.Outputs.AsIndexedOutputs() | ||
.Skip(2) | ||
.Take(investorTrx.Outputs.Count - 3) | ||
.Sum(x => x.TxOut.Value.Satoshi); | ||
} | ||
} | ||
|
||
public record PendingInvestmentDto(DateTime Created, decimal Amount, string InvestorNostrPubKey); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
using Angor.Contexts.Funding.Founder.Operations; | ||
using Angor.Contexts.Funding.Investor.Dtos; | ||
using Angor.Contexts.Funding.Investor.Operations; | ||
using Angor.Contexts.Funding.Projects.Domain; | ||
|
@@ -8,6 +9,7 @@ namespace Angor.Contexts.Funding.Investor; | |
public interface IInvestmentAppService | ||
{ | ||
Task<Result<IEnumerable<InvestmentDto>>> GetInvestments(ProjectId projectId); | ||
Task<Result<CreateInvestment.Draft>> CreateDraft(Guid sourceWalletId, ProjectId projectId, Amount amount); | ||
Task<Result<Guid>> RequestInvestment(Guid sourceWalletId, ProjectId projectId, CreateInvestment.Draft draft); | ||
Task<Result<CreateInvestment.Draft>> CreateInvestmentDraft(Guid sourceWalletId, ProjectId projectId, Amount amount); | ||
Task<Result<Guid>> Invest(Guid sourceWalletId, ProjectId projectId, CreateInvestment.Draft draft); | ||
Task<Result<IEnumerable<GetPendingInvestments.PendingInvestmentDto>>> GetPendingInvestments(Guid walletId, ProjectId projectId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name should be single because the project id is only ever for 1 investment at the time |
||
} |
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
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
10 changes: 10 additions & 0 deletions
10
src/Angor/Avalonia/Angor.Contexts.Funding/Shared/INostrDecrypter.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,10 @@ | ||
using Angor.Contexts.Funding.Founder.Operations; | ||
using Angor.Contexts.Funding.Projects.Domain; | ||
using CSharpFunctionalExtensions; | ||
|
||
namespace Angor.Contexts.Funding; | ||
|
||
public interface INostrDecrypter | ||
{ | ||
Task<Result<string>> Decrypt(Guid walletId, ProjectId projectId, NostrMessage nostrMessage); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Angor/Avalonia/Angor.Contexts.Funding/Shared/NostrDecrypter.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,27 @@ | ||
using Angor.Client.Services; | ||
using Angor.Contests.CrossCutting; | ||
using Angor.Contexts.Funding.Founder.Operations; | ||
using Angor.Contexts.Funding.Projects.Domain; | ||
using Angor.Shared; | ||
using Blockcore.NBitcoin; | ||
using Blockcore.NBitcoin.DataEncoders; | ||
using CSharpFunctionalExtensions; | ||
|
||
namespace Angor.Contexts.Funding.Shared; | ||
|
||
public class NostrDecrypter(IDerivationOperations derivationOperations, IEncryptionService encryptionService, ISeedwordsProvider provider, IProjectRepository projectRepository) : INostrDecrypter | ||
{ | ||
public Task<Result<string>> Decrypt(Guid walletId, ProjectId projectId, NostrMessage nostrMessage) | ||
{ | ||
return from sensitiveData in provider.GetSensitiveData(walletId) | ||
from project in projectRepository.Get(projectId) | ||
from nostrPrivateKey in Result.Try(() => derivationOperations.DeriveProjectNostrPrivateKeyAsync(sensitiveData.ToWalletWords(), project.FounderKey)) | ||
from decrypted in Result.Try(() => | ||
{ | ||
var bytes = nostrPrivateKey.ToBytes(); | ||
var hex = Encoders.Hex.EncodeData(bytes); | ||
return encryptionService.DecryptNostrContentAsync(hex, nostrMessage.InvestorNostrPubKey, nostrMessage.Content); | ||
}) | ||
select decrypted; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/Angor/Avalonia/Angor.Contexts.Funding/Shared/NostrMessage.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,3 @@ | ||
namespace Angor.Contexts.Funding.Founder.Operations; | ||
|
||
public record NostrMessage(string Id, string InvestorNostrPubKey, string Content, DateTime Created); |
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.
To be honest I am not crazy about multiple classes in one file, it means I can't see the logic and structure when I look at the solution files, how many of these IRequests do we have so far?