Use constants rather than magic strings in transaction attachments

This commit is contained in:
nicolas.dorier 2022-11-17 10:24:49 +09:00
parent 2740dfea87
commit ff572eef7f
No known key found for this signature in database
GPG key ID: 6618763EF09186FE
4 changed files with 23 additions and 31 deletions

View file

@ -14,6 +14,13 @@ namespace BTCPayServer.Data
{
public const string Label = "label";
public const string Tx = "tx";
public const string Payjoin = "payjoin";
public const string Invoice = "invoice";
public const string PaymentRequest = "payment-request";
public const string App = "app";
public const string PayjoinExposed = "pj-exposed";
public const string Payout = "payout";
public const string PullPayment = "pull-payment";
}
public string WalletId { get; set; }
public string Type { get; set; }

View file

@ -1363,7 +1363,7 @@ namespace BTCPayServer.Controllers
TextColor = ColorPalette.Default.TextColor(color)
};
models.Add(tag.Type, model);
if (tag.Type == "payout")
if (tag.Type == WalletObjectData.Types.Payout)
{
var payoutsByPullPaymentId =
transactionInfo.Attachments.Where(t => t.Type == "payout")
@ -1381,28 +1381,28 @@ namespace BTCPayServer.Controllers
model.Link = _linkGenerator.PayoutLink(transactionInfo.WalletId.ToString(), null, PayoutState.Completed, Request.Scheme, Request.Host,
Request.PathBase);
}
else if (tag.Type == "payjoin")
else if (tag.Type == WalletObjectData.Types.Payjoin)
{
model.Tooltip = $"This UTXO was part of a PayJoin transaction.";
}
else if (tag.Type == "invoice")
else if (tag.Type == WalletObjectData.Types.Invoice)
{
model.Tooltip = $"Received through an invoice {tag.Id}";
model.Link = string.IsNullOrEmpty(tag.Id)
? null
: _linkGenerator.InvoiceLink(tag.Id, Request.Scheme, Request.Host, Request.PathBase);
}
else if (tag.Type == "payment-request")
else if (tag.Type == WalletObjectData.Types.PaymentRequest)
{
model.Tooltip = $"Received through a payment request {tag.Id}";
model.Link = _linkGenerator.PaymentRequestLink(tag.Id, Request.Scheme, Request.Host, Request.PathBase);
}
else if (tag.Type == "app")
else if (tag.Type == WalletObjectData.Types.App)
{
model.Tooltip = $"Received through an app {tag.Id}";
model.Link = _linkGenerator.AppLink(tag.Id, Request.Scheme, Request.Host, Request.PathBase);
}
else if (tag.Type == "pj-exposed")
else if (tag.Type == WalletObjectData.Types.PayjoinExposed)
{
if (tag.Id.Length != 0)
@ -1415,7 +1415,7 @@ namespace BTCPayServer.Controllers
model.Tooltip = $"This UTXO was exposed through a PayJoin proposal";
}
}
else if (tag.Type == "payjoin")
else if (tag.Type == WalletObjectData.Types.Payjoin)
{
model.Tooltip = $"This UTXO was part of a PayJoin transaction.";
}

View file

@ -1,6 +1,7 @@
#nullable enable
using System.Collections.Generic;
using BTCPayServer.Client.Models;
using BTCPayServer.Data;
using BTCPayServer.Services.Labels;
using Newtonsoft.Json.Linq;
@ -20,39 +21,39 @@ namespace BTCPayServer.Services
}
public static Attachment Payjoin()
{
return new Attachment("payjoin");
return new Attachment(WalletObjectData.Types.Payjoin);
}
public static Attachment Invoice(string invoice)
{
return new Attachment("invoice", invoice);
return new Attachment(WalletObjectData.Types.Invoice, invoice);
}
public static Attachment PaymentRequest(string paymentRequestId)
{
return new Attachment("payment-request", paymentRequestId);
return new Attachment(WalletObjectData.Types.PaymentRequest, paymentRequestId);
}
public static Attachment App(string appId)
{
return new Attachment("app", appId);
return new Attachment(WalletObjectData.Types.App, appId);
}
public static Attachment PayjoinExposed(string? invoice)
{
return new Attachment("pj-exposed", invoice);
return new Attachment(WalletObjectData.Types.PayjoinExposed, invoice);
}
public static IEnumerable<Attachment> Payout(string? pullPaymentId, string payoutId)
{
if (string.IsNullOrEmpty(pullPaymentId))
{
yield return new Attachment("payout", payoutId);
yield return new Attachment(WalletObjectData.Types.Payout, payoutId);
}
else
{
yield return new Attachment("payout", payoutId, new JObject()
yield return new Attachment(WalletObjectData.Types.Payout, payoutId, new JObject()
{
["pullPaymentId"] = pullPaymentId
});
yield return new Attachment("pull-payment", pullPaymentId);
yield return new Attachment(WalletObjectData.Types.PullPayment, pullPaymentId);
}
}
}

View file

@ -266,22 +266,6 @@ namespace BTCPayServer.Services
await EnsureWalletObjectLink(labelObjId, id);
}
}
public async Task AddWalletObjects(WalletObjectId id, params string[] labels)
{
ArgumentNullException.ThrowIfNull(id);
await EnsureWalletObject(id);
foreach (var l in labels.Select(l => l.Trim().Truncate(MaxLabelSize)))
{
var labelObjId = new WalletObjectId(id.WalletId, WalletObjectData.Types.Label, l);
await EnsureWalletObject(labelObjId, new JObject()
{
["color"] = ColorPalette.Default.DeterministicColor(l)
});
await EnsureWalletObjectLink(labelObjId, id);
}
}
public Task AddWalletTransactionAttachment(WalletId walletId, uint256 txId, Attachment attachment)
{
return AddWalletTransactionAttachment(walletId, txId, new[] { attachment });