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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build & Test

on:
pull_request:
branches: [main, develop]
branches: [main, develop, release/**]

jobs:
build-and-test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public async Task CreateInvoiceIfNotExists_CreatesZeroKnowledgeInvoice()
}

[Fact]
public async Task CreateInvoiceIfNotExists_ProperlyAddsQueryParamsForLightningOnly()
public async Task CreateInvoiceIfNotExists_DoesNotAddZeroKnowledgeParamsBolt11()
{
var invoice = CreateInvoice("BTC-Lightning");
var checkoutModel = CreateCheckoutModel(invoice);
Expand All @@ -187,8 +187,8 @@ public async Task CreateInvoiceIfNotExists_ProperlyAddsQueryParamsForLightningOn

await _brantaService.CreateInvoiceIfNotExistsAsync(checkoutModel);

Assert.Contains("?branta_id", checkoutModel.InvoiceBitcoinUrlQR);
Assert.Contains("&branta_secret", checkoutModel.InvoiceBitcoinUrlQR);
Assert.DoesNotContain("?branta_id", checkoutModel.InvoiceBitcoinUrlQR);
Assert.DoesNotContain("&branta_secret", checkoutModel.InvoiceBitcoinUrlQR);
}

[Fact]
Expand All @@ -205,7 +205,6 @@ public async Task CreateInvoiceIfNotExists_ShouldNotSetZeroKnowledgeIfRequestUns
Assert.DoesNotContain("&branta_secret", checkoutModel.InvoiceBitcoinUrlQR);
}


private InvoiceData GetSavedInvoiceData()
{
return _invoiceServiceMock.Invocations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PropertyGroup>
<Product>Branta</Product>
<Description>Easily verify payments, checkouts, and invoices.</Description>
<Version>0.3.0</Version>
<Version>0.4.0</Version>
</PropertyGroup>

<!-- Plugin development properties -->
Expand Down
31 changes: 21 additions & 10 deletions BTCPayServer.Plugins.Branta/Classes/BrantaClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using BTCPayServer.Plugins.Branta.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
Expand All @@ -10,11 +12,17 @@ namespace BTCPayServer.Plugins.Branta.Classes;

public class BrantaClient(IHttpClientFactory httpClientFactory)
{
public static readonly string PaymentVersion = "v1";
public static readonly string PaymentVersion = "v2";

private JsonSerializerSettings _jsonSettings = new()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};

public async Task PostPaymentAsync(PaymentRequest paymentRequest, BrantaSettings brantaSettings)
{
var json = JsonConvert.SerializeObject(paymentRequest);

var json = JsonConvert.SerializeObject(paymentRequest, _jsonSettings);
var content = new StringContent(json, Encoding.UTF8, "application/json");

using var request = new HttpRequestMessage(HttpMethod.Post, $"{PaymentVersion}/payments")
Expand All @@ -38,15 +46,18 @@ public async Task PostPaymentAsync(PaymentRequest paymentRequest, BrantaSettings

public class PaymentRequest
{
public Payment payment { get; set; }
public List<Destination> Destinations { get; set; }

public string Description { get; set; }

public string Ttl { get; set; }

public string BtcPayServerPluginVersion { get; set; }
}

public class Payment
public class Destination
{
public string description { get; set; }
public string payment { get; set; }
public string[] alt_payments { get; set; }
public string ttl { get; set; }
public string btcPayServerPluginVersion { get; set; }
public bool zk { get; set; }
public string Value { get; set; }

public bool Zk { get; set; }
}
2 changes: 1 addition & 1 deletion BTCPayServer.Plugins.Branta/Classes/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static string GetVersion()

public static void SetZeroKnowledgeParams(this CheckoutModel model, string payment, string secret)
{
if (payment == null || secret == null)
if (payment == null || secret == null || model.PaymentMethodId != "BTC-CHAIN")
{
return;
}
Expand Down
30 changes: 17 additions & 13 deletions BTCPayServer.Plugins.Branta/Services/BrantaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,26 @@ private async Task<InvoiceData> CreateInvoiceAsync(InvoiceEntity btcPayInvoice,

return 3;
})
.Select(pp => pp.Destination)
.Select(d => brantaSettings.EnableZeroKnowledge ? Helper.Encrypt(d, secret.ToString()) : d)
.Select(pp =>
{
var isZk = brantaSettings.EnableZeroKnowledge && pp.PaymentMethodId == PaymentMethodId.TryParse("BTC");

return new Destination()
{
Value = isZk ? Helper.Encrypt(pp.Destination, secret.ToString()) : pp.Destination,
Zk = isZk
};
})
.ToList();

var invoiceData = new InvoiceData()
{
DateCreated = now,
InvoiceId = btcPayInvoice.Id,
PaymentId = payments
.OrderBy(p => p.Length)
.First(),
.OrderBy(p => p.Value.Length)
.First()
.Value,
Environment = brantaSettings.StagingEnabled ? Enums.ServerEnvironment.Staging : Enums.ServerEnvironment.Production,
StoreId = btcPayInvoice.StoreId,
ZeroKnowledgeSecret = secret
Expand All @@ -148,15 +157,10 @@ private async Task<InvoiceData> CreateInvoiceAsync(InvoiceEntity btcPayInvoice,

var paymentRequest = new Classes.PaymentRequest()
{
payment = new Payment
{
description = brantaSettings.PostDescriptionEnabled ? GetDescription(btcPayInvoice) : null,
payment = payments.First(),
alt_payments = [.. payments.Skip(1)],
ttl = ttl.ToString(),
btcPayServerPluginVersion = Helper.GetVersion(),
zk = brantaSettings.EnableZeroKnowledge
}
Destinations = payments,
Description = brantaSettings.PostDescriptionEnabled ? GetDescription(btcPayInvoice) : null,
Ttl = ttl.ToString(),
BtcPayServerPluginVersion = Helper.GetVersion()
};

await brantaClient.PostPaymentAsync(paymentRequest, brantaSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ else
<div class="form-group">
<label asp-for="Settings.EnableZeroKnowledge" class="form-label" style="padding-right: 10px;"></label>
<input asp-for="Settings.EnableZeroKnowledge" type="checkbox" class="btcpay-toggle" />
<span class="info" title="Branta will never know the addresses posted.">
<span class="info" title="Encrypt on-chain addresses before sending to Branta.">
<vc:icon symbol="info" />
</span>
</div>
Expand Down