diff --git a/BTCPayServer.Tests/UnitTest1.cs b/BTCPayServer.Tests/UnitTest1.cs index cccd168e2..cffe28e88 100644 --- a/BTCPayServer.Tests/UnitTest1.cs +++ b/BTCPayServer.Tests/UnitTest1.cs @@ -765,8 +765,8 @@ namespace BTCPayServer.Tests private static bool IsMapped(Invoice invoice, ApplicationDbContext ctx) { - var h = BitcoinAddress.Create(invoice.BitcoinAddress).ScriptPubKey.Hash; - return ctx.AddressInvoices.FirstOrDefault(i => i.InvoiceDataId == invoice.Id && i.GetHash() == h) != null; + var h = BitcoinAddress.Create(invoice.BitcoinAddress).ScriptPubKey.Hash.ToString(); + return ctx.AddressInvoices.FirstOrDefault(i => i.InvoiceDataId == invoice.Id && i.GetAddress() == h) != null; } private void Eventually(Action act) diff --git a/BTCPayServer/Data/AddressInvoiceData.cs b/BTCPayServer/Data/AddressInvoiceData.cs index 16179511f..7e0c8eb3b 100644 --- a/BTCPayServer/Data/AddressInvoiceData.cs +++ b/BTCPayServer/Data/AddressInvoiceData.cs @@ -21,27 +21,29 @@ namespace BTCPayServer.Data #pragma warning disable CS0618 - public ScriptId GetHash() + public string GetAddress() { if (Address == null) return null; - var index = Address.IndexOf("#", StringComparison.InvariantCulture); + var index = Address.LastIndexOf("#", StringComparison.InvariantCulture); if (index == -1) - return new ScriptId(Address); - return new ScriptId(Address.Substring(0, index)); + return Address; + return Address.Substring(0, index); } - public AddressInvoiceData SetHash(ScriptId scriptId, CryptoDataId cryptoDataId) + public AddressInvoiceData Set(string address, CryptoDataId cryptoDataId) { - Address = scriptId + "#" + cryptoDataId?.ToString(); + Address = address + "#" + cryptoDataId?.ToString(); return this; } public CryptoDataId GetCryptoDataId() { if (Address == null) return null; - var index = Address.IndexOf("#", StringComparison.InvariantCulture); + var index = Address.LastIndexOf("#", StringComparison.InvariantCulture); + // Legacy AddressInvoiceData does not have the CryptoDataId attached to the Address if (index == -1) return CryptoDataId.Parse("BTC"); + ///////////////////////// return CryptoDataId.Parse(Address.Substring(index + 1)); } #pragma warning restore CS0618 diff --git a/BTCPayServer/Payments/Bitcoin/BitcoinLikeOnChainPaymentMethod.cs b/BTCPayServer/Payments/Bitcoin/BitcoinLikeOnChainPaymentMethod.cs index 615608d7b..5d9657950 100644 --- a/BTCPayServer/Payments/Bitcoin/BitcoinLikeOnChainPaymentMethod.cs +++ b/BTCPayServer/Payments/Bitcoin/BitcoinLikeOnChainPaymentMethod.cs @@ -33,11 +33,13 @@ namespace BTCPayServer.Payments.Bitcoin DepositAddress = BitcoinAddress.Create(newPaymentDestination, DepositAddress.Network); } + // Those properties are JsonIgnore because their data is inside CryptoData class for legacy reason [JsonIgnore] public FeeRate FeeRate { get; set; } [JsonIgnore] public Money TxFee { get; set; } [JsonIgnore] public BitcoinAddress DepositAddress { get; set; } + /////////////////////////////////////////////////////////////////////////////////////// } } diff --git a/BTCPayServer/Services/Invoices/InvoiceRepository.cs b/BTCPayServer/Services/Invoices/InvoiceRepository.cs index c5347540b..8670c9010 100644 --- a/BTCPayServer/Services/Invoices/InvoiceRepository.cs +++ b/BTCPayServer/Services/Invoices/InvoiceRepository.cs @@ -130,12 +130,12 @@ namespace BTCPayServer.Services.Invoices throw new InvalidOperationException("CryptoCode unsupported"); var paymentDestination = cryptoData.GetPaymentMethod().GetPaymentDestination(); - ScriptId hash = GetAddressInvoiceHash(cryptoData); + string address = GetDestination(cryptoData); context.AddressInvoices.Add(new AddressInvoiceData() { InvoiceDataId = invoice.Id, CreatedTime = DateTimeOffset.UtcNow, - }.SetHash(hash, cryptoData.GetId())); + }.Set(address, cryptoData.GetId())); context.HistoricalAddressInvoices.Add(new HistoricalAddressInvoiceData() { @@ -162,14 +162,15 @@ namespace BTCPayServer.Services.Invoices return invoice; } - private static ScriptId GetAddressInvoiceHash(CryptoData cryptoData) + private static string GetDestination(CryptoData cryptoData) { - ScriptId hash = null; + // For legacy reason, BitcoinLikeOnChain is putting the hashes of addresses in database if (cryptoData.GetId().PaymentType == Payments.PaymentTypes.BTCLike) { - hash = ((Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod)cryptoData.GetPaymentMethod()).DepositAddress.ScriptPubKey.Hash; + return ((Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod)cryptoData.GetPaymentMethod()).DepositAddress.ScriptPubKey.Hash.ToString(); } - return hash; + /////////////// + return cryptoData.GetPaymentMethod().GetPaymentDestination(); } public async Task NewAddress(string invoiceId, IPaymentMethod paymentMethod, BTCPayNetwork network) @@ -208,7 +209,7 @@ namespace BTCPayServer.Services.Invoices InvoiceDataId = invoiceId, CreatedTime = DateTimeOffset.UtcNow } - .SetHash(GetAddressInvoiceHash(currencyData), currencyData.GetId())); + .Set(GetDestination(currencyData), currencyData.GetId())); context.HistoricalAddressInvoices.Add(new HistoricalAddressInvoiceData() { InvoiceDataId = invoiceId, @@ -360,7 +361,7 @@ namespace BTCPayServer.Services.Invoices } if (invoice.AddressInvoices != null) { - entity.AvailableAddressHashes = invoice.AddressInvoices.Select(a => a.GetHash() + a.GetCryptoDataId().ToString()).ToHashSet(); + entity.AvailableAddressHashes = invoice.AddressInvoices.Select(a => a.GetAddress() + a.GetCryptoDataId().ToString()).ToHashSet(); } if(invoice.Events != null) {