From ff572eef7f79fa13e5ef8e898079757a2085ddd7 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Thu, 17 Nov 2022 10:24:49 +0900 Subject: [PATCH] Use constants rather than magic strings in transaction attachments --- BTCPayServer.Data/Data/WalletObjectData.cs | 7 +++++++ BTCPayServer/Controllers/UIWalletsController.cs | 14 +++++++------- BTCPayServer/Services/Attachment.cs | 17 +++++++++-------- BTCPayServer/Services/WalletRepository.cs | 16 ---------------- 4 files changed, 23 insertions(+), 31 deletions(-) diff --git a/BTCPayServer.Data/Data/WalletObjectData.cs b/BTCPayServer.Data/Data/WalletObjectData.cs index c2d447e4c..ddb0dc50d 100644 --- a/BTCPayServer.Data/Data/WalletObjectData.cs +++ b/BTCPayServer.Data/Data/WalletObjectData.cs @@ -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; } diff --git a/BTCPayServer/Controllers/UIWalletsController.cs b/BTCPayServer/Controllers/UIWalletsController.cs index 38b8d51a3..488f79076 100644 --- a/BTCPayServer/Controllers/UIWalletsController.cs +++ b/BTCPayServer/Controllers/UIWalletsController.cs @@ -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."; } diff --git a/BTCPayServer/Services/Attachment.cs b/BTCPayServer/Services/Attachment.cs index d6d771ce5..bd3ca7b5e 100644 --- a/BTCPayServer/Services/Attachment.cs +++ b/BTCPayServer/Services/Attachment.cs @@ -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 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); } } } diff --git a/BTCPayServer/Services/WalletRepository.cs b/BTCPayServer/Services/WalletRepository.cs index 7f5c3ef0a..ab647c7d6 100644 --- a/BTCPayServer/Services/WalletRepository.cs +++ b/BTCPayServer/Services/WalletRepository.cs @@ -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 });