Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Product>Branta</Product>
<Description>Easily verify payments, checkouts, and invoices.</Description>
<Version>0.4.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.3" />
</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);
}
}
6 changes: 3 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 @@ -45,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.

25 changes: 13 additions & 12 deletions BTCPayServer.Plugins.Branta/Models/BrantaSettingsViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BTCPayServer.Plugins.Branta.Classes;
using BTCPayServer.Plugins.Branta.Enums;
using Branta.Enums;
using Branta.Extensions;
using BTCPayServer.Plugins.Branta.Classes;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;

Expand Down Expand Up @@ -43,23 +44,23 @@ public string GetAPIKey()
return StagingEnabled ? StagingApiKey : ProductionApiKey;
}

public string GetBrantaServerUrl()
public BrantaServerBaseUrl GetBrantaServerUrl()
{
return GetBrantaServerUrl(StagingEnabled ? ServerEnvironment.Staging : ServerEnvironment.Production);
if (Debugger.IsAttached)
{
return BrantaServerBaseUrl.Localhost;
}

return StagingEnabled ? BrantaServerBaseUrl.Staging : BrantaServerBaseUrl.Production;
}

public static string GetBrantaServerUrl(ServerEnvironment environment)
public static string GetBrantaServerUrl(BrantaServerBaseUrl environment)
{
if (Debugger.IsAttached)
{
return "http://localhost:3000";
return BrantaServerBaseUrl.Localhost.GetUrl();
}

return environment switch
{
ServerEnvironment.Staging => "https://staging.branta.pro",
ServerEnvironment.Production => "https://guardrail.branta.pro",
_ => null
};
return environment.GetUrl();
}
}
45 changes: 28 additions & 17 deletions BTCPayServer.Plugins.Branta/Services/BrantaService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using BTCPayServer.Models.InvoicingModels;
using Branta.Classes;
using Branta.Enums;
using Branta.Exceptions;
using Branta.V2.Classes;
using Branta.V2.Models;
using BTCPayServer.Models.InvoicingModels;
using BTCPayServer.Payments;
using BTCPayServer.Plugins.Branta.Classes;
using BTCPayServer.Plugins.Branta.Interfaces;
Expand Down Expand Up @@ -101,7 +106,6 @@ private async Task<InvoiceData> CreateInvoiceAsync(InvoiceEntity btcPayInvoice,
var chainBtcId = PaymentTypes.CHAIN.GetPaymentMethodId("BTC");
var lnBtcId = PaymentTypes.LN.GetPaymentMethodId("BTC");

var secret = brantaSettings.EnableZeroKnowledge ? Guid.NewGuid().ToString() : null;
var payments = btcPayInvoice
.GetPaymentPrompts()
.Where(pp => pp.Destination != null)
Expand All @@ -115,15 +119,10 @@ private async Task<InvoiceData> CreateInvoiceAsync(InvoiceEntity btcPayInvoice,

return 3;
})
.Select(pp =>
.Select(pp => new Destination
{
var isZk = brantaSettings.EnableZeroKnowledge && pp.PaymentMethodId == PaymentMethodId.TryParse("BTC");

return new Destination()
{
Value = isZk ? Helper.Encrypt(pp.Destination, secret.ToString()) : pp.Destination,
Zk = isZk
};
Value = pp.Destination,
IsZk = brantaSettings.EnableZeroKnowledge && pp.PaymentMethodId == PaymentMethodId.TryParse("BTC")
})
.ToList();

Expand All @@ -135,9 +134,7 @@ private async Task<InvoiceData> CreateInvoiceAsync(InvoiceEntity btcPayInvoice,
.OrderBy(p => p.Value.Length)
.First()
.Value,
Environment = brantaSettings.StagingEnabled ? Enums.ServerEnvironment.Staging : Enums.ServerEnvironment.Production,
StoreId = btcPayInvoice.StoreId,
ZeroKnowledgeSecret = secret,
Environment = brantaSettings.StagingEnabled ? BrantaServerBaseUrl.Staging : BrantaServerBaseUrl.Production,
PluginVersion = Helper.GetVersion()
};

Expand All @@ -153,18 +150,32 @@ private async Task<InvoiceData> CreateInvoiceAsync(InvoiceEntity btcPayInvoice,

try
{
var ttl = (btcPayInvoice.ExpirationTime.AddMinutes(brantaSettings.TTL) - btcPayInvoice.InvoiceTime).TotalSeconds;
var ttl = (int)(btcPayInvoice.ExpirationTime.AddMinutes(brantaSettings.TTL) - btcPayInvoice.InvoiceTime).TotalSeconds;
invoiceData.ExpirationDate = now.AddSeconds(ttl);

var paymentRequest = new Classes.PaymentRequest()
var paymentRequest = new Payment()
{
Destinations = payments,
Description = brantaSettings.PostDescriptionEnabled ? GetDescription(btcPayInvoice) : null,
Ttl = ttl.ToString(),
TTL = ttl,
BtcPayServerPluginVersion = Helper.GetVersion()
};

await brantaClient.PostPaymentAsync(paymentRequest, brantaSettings);
var options = new BrantaClientOptions()
{
BaseUrl = brantaSettings.GetBrantaServerUrl(),
DefaultApiKey = brantaSettings.GetAPIKey()
};

if (brantaSettings.EnableZeroKnowledge == true)
{
var (_, secret) = await brantaClient.AddZKPaymentAsync(paymentRequest, options);
invoiceData.ZeroKnowledgeSecret = secret;
}
else
{
await brantaClient.AddPaymentAsync(paymentRequest, options);
}

invoiceData.Status = Enums.InvoiceDataStatus.Success;
}
Expand Down
5 changes: 3 additions & 2 deletions BTCPayServer.Plugins.Branta/Views/UIBranta/EditBranta.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@using BTCPayServer.Plugins.Branta.Enums
@using BTCPayServer.Plugins.Branta.Models
@using BTCPayServer.Plugins.Branta.Views
@using Branta.Enums

@model BTCPayServer.Plugins.Branta.Models.BrantaSettingsViewModel

Expand Down Expand Up @@ -123,7 +124,7 @@
</div>
</div>
<span asp-validation-for="Settings.ProductionApiKey" class="text-danger"></span>
<p class="text-secondary mt-2">Visit your Branta <a href="@BrantaSettings.GetBrantaServerUrl(ServerEnvironment.Production)/session/new" target="_blank">Dashboard</a> to create an API Key.</p>
<p class="text-secondary mt-2">Visit your Branta <a href="@BrantaSettings.GetBrantaServerUrl(BrantaServerBaseUrl.Production)/session/new" target="_blank">Dashboard</a> to create an API Key.</p>
</div>

<button class="accordion-button collapsed only-for-js ms-0 d-inline-block" type="button" data-bs-toggle="collapse" data-bs-target="#advanced-settings" aria-expanded="false" aria-controls="advanced-settings">
Expand Down Expand Up @@ -178,7 +179,7 @@
</div>
</div>
<span asp-validation-for="Settings.StagingApiKey" class="text-danger"></span>
<p class="text-secondary mt-2">Visit your Branta <a href="@BrantaSettings.GetBrantaServerUrl(ServerEnvironment.Staging)/session/new" target="_blank">Staging Dashboard</a> to create an API Key.</p>
<p class="text-secondary mt-2">Visit your Branta <a href="@BrantaSettings.GetBrantaServerUrl(BrantaServerBaseUrl.Staging)/session/new" target="_blank">Staging Dashboard</a> to create an API Key.</p>
</div>
</div>

Expand Down
Loading