Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions BTCPayServer.Plugins.Branta.Tests/Services/BrantaServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using BTCPayServer.Models.InvoicingModels;
using Branta.Classes;
using Branta.Enums;
using Branta.V2.Classes;
using Branta.V2.Models;
using BTCPayServer.Models.InvoicingModels;
using BTCPayServer.Payments;
using BTCPayServer.Plugins.Branta.Classes;
using BTCPayServer.Plugins.Branta.Data.Domain;
using BTCPayServer.Plugins.Branta.Enums;
using BTCPayServer.Plugins.Branta.Interfaces;
Expand All @@ -9,9 +12,11 @@
using BTCPayServer.Plugins.Branta.Tests.Classes;
using BTCPayServer.Services.Invoices;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using Moq.Protected;
using System.Net;
using System.Text.Json;

namespace BTCPayServer.Plugins.Branta.Tests.Services;

Expand Down Expand Up @@ -51,7 +56,19 @@ public BrantaServiceTests()
{
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.Created
StatusCode = HttpStatusCode.Created,
Content = new StringContent(
JsonSerializer.Serialize(new Payment()
{
Destinations = [
new Destination() {
Value = "pQerSFV+fievHP+guYoGJjx1CzFFrYWHAgWrLhn5473Z19M6+WMScLd1hsk808AEF/x+GpZKmNacFBf5BbQ=",
IsZk = true
}
]
}),
System.Text.Encoding.UTF8,
"application/json")
};
}
else
Expand All @@ -67,7 +84,14 @@ public BrantaServiceTests()
.Setup(x => x.CreateClient(It.IsAny<string>()))
.Returns(new HttpClient(_httpMessageHandlerMock.Object));

_brantaClientMock = new Mock<BrantaClient>(httpClientFactoryMock.Object);
var defaultOptions = new BrantaClientOptions
{
BaseUrl = BrantaServerBaseUrl.Localhost
};
var optionsMock = new Mock<IOptions<BrantaClientOptions>>();
optionsMock.Setup(x => x.Value).Returns(defaultOptions);

_brantaClientMock = new Mock<BrantaClient>(httpClientFactoryMock.Object, optionsMock.Object);

_brantaService = new BrantaService(
_loggerMock.Object,
Expand Down Expand Up @@ -162,7 +186,7 @@ public async Task CreateInvoiceIfNotExists_CreatesZeroKnowledgeInvoice()
var secret = TestHelper.GetSecret(result);
var value = TestHelper.GetValueFromZeroKnowledgeUrl(result);
Assert.NotNull(value);
var decryptedValue = TestHelper.Decrypt(value, secret);
var decryptedValue = TestHelper.Decrypt(value, "1234");
Assert.Equal(OnChainAddress, decryptedValue);

_invoiceServiceMock.Verify(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<PropertyGroup>
<Product>Branta</Product>
<Description>Easily verify payments, checkouts, and invoices.</Description>
<Version>0.4.0</Version>
<Version>0.5.0</Version>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

<!-- Plugin development properties -->
Expand Down Expand Up @@ -43,6 +44,11 @@
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.10.0" />
</ItemGroup>


<ItemGroup>
<PackageReference Include="Branta" Version="0.0.5" />
</ItemGroup>

<!-- If you reference another project, by default, the dlls won't be copied in the published plugin, you need <Private>true</Private> -->
<!--
<ItemGroup>
Expand Down
5 changes: 2 additions & 3 deletions BTCPayServer.Plugins.Branta/BrantaPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Branta.V2.Extensions;
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Abstractions.Models;
using BTCPayServer.Payments;
using BTCPayServer.Plugins.Branta.Classes;
using BTCPayServer.Plugins.Branta.Interfaces;
using BTCPayServer.Plugins.Branta.Services;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -27,12 +27,11 @@ public override void Execute(IServiceCollection services)
factory.ConfigureBuilder(o);
});

services.ConfigureBrantaServices();
services.AddScoped<IBrantaService, BrantaService>();
services.AddScoped<IBrantaSettingsService, BrantaSettingsService>();
services.AddScoped<IInvoiceService, InvoiceService>();
services.AddScoped<IInvoiceRepository, InvoiceRepositoryAdapter>();
services.AddHttpClient();
services.AddScoped<BrantaClient>();
services.AddScheduledTask<CleanUpInvoiceService>(TimeSpan.FromHours(24));

services.AddScoped<IGlobalCheckoutModelExtension, BrantaCheckoutModelExtension>();
Expand Down
63 changes: 0 additions & 63 deletions BTCPayServer.Plugins.Branta/Classes/BrantaClient.cs

This file was deleted.

7 changes: 0 additions & 7 deletions BTCPayServer.Plugins.Branta/Classes/BrantaPaymentException.cs

This file was deleted.

34 changes: 0 additions & 34 deletions BTCPayServer.Plugins.Branta/Classes/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using BTCPayServer.Models.InvoicingModels;
using System;
using System.Security.Cryptography;
using System.Text;

namespace BTCPayServer.Plugins.Branta.Classes;

Expand All @@ -23,35 +20,4 @@ public static void SetZeroKnowledgeParams(this CheckoutModel model, string payme
(model.InvoiceBitcoinUrlQR.Contains('?') ? "&" : "?") +
$"{Constants.PaymentId}={payment}&{Constants.ZeroKnowledgeSecret}={secret}";
}

public static string Encrypt(string value, string secret)
{
byte[] keyData;
using (var sha256 = SHA256.Create())
{
keyData = sha256.ComputeHash(Encoding.UTF8.GetBytes(secret));
}

byte[] iv = new byte[12];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(iv);
}

byte[] plaintext = Encoding.UTF8.GetBytes(value);
byte[] ciphertext = new byte[plaintext.Length];
byte[] tag = new byte[16];

using (AesGcm aesGcm = new(keyData, 16))
{
aesGcm.Encrypt(iv, plaintext, ciphertext, tag);
}

byte[] result = new byte[iv.Length + ciphertext.Length + tag.Length];
Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
Buffer.BlockCopy(ciphertext, 0, result, iv.Length, ciphertext.Length);
Buffer.BlockCopy(tag, 0, result, iv.Length + ciphertext.Length, tag.Length);

return Convert.ToBase64String(result);
}
}
8 changes: 5 additions & 3 deletions BTCPayServer.Plugins.Branta/Data/Domain/InvoiceData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BTCPayServer.Plugins.Branta.Classes;
using Branta.Enums;
using BTCPayServer.Plugins.Branta.Enums;
using BTCPayServer.Plugins.Branta.Models;
using System;
Expand All @@ -21,7 +21,7 @@ public class InvoiceData

public int ProcessingTime { get; set; }

public ServerEnvironment Environment { get; set; }
public BrantaServerBaseUrl Environment { get; set; }

public InvoiceDataStatus Status { get; set; }

Expand All @@ -31,6 +31,8 @@ public class InvoiceData

public string ZeroKnowledgeSecret { get; set; }

public string PluginVersion { get; set; }

public string GetVerifyLink()
{
if (ExpirationDate <= DateTime.UtcNow || Status != InvoiceDataStatus.Success)
Expand All @@ -43,6 +45,6 @@ public string GetVerifyLink()
var path = ZeroKnowledgeSecret != null ? "zk-verify" : "verify";
var secret = ZeroKnowledgeSecret != null ? $"#secret={ZeroKnowledgeSecret}" : "";

return $"{baseUrl}/{BrantaClient.PaymentVersion}/{path}/{Uri.EscapeDataString(PaymentId)}{secret}";
return $"{baseUrl}/v2/{path}/{Uri.EscapeDataString(PaymentId)}{secret}";
}
}
7 changes: 0 additions & 7 deletions BTCPayServer.Plugins.Branta/Enums/ServerEnvironment.cs

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace BTCPayServer.Plugins.Branta.Migrations
{
/// <inheritdoc />
public partial class AddPluginVersionToInvoiceData : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "PluginVersion",
schema: "BTCPayServer.Plugins.Branta",
table: "Invoice",
type: "text",
nullable: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "PluginVersion",
schema: "BTCPayServer.Plugins.Branta",
table: "Invoice");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<string>("PaymentId")
.HasColumnType("text");

b.Property<string>("PluginVersion")
.HasColumnType("text");

b.Property<int>("ProcessingTime")
.HasColumnType("integer");

Expand Down
Loading