Add some comments

This commit is contained in:
nicolas.dorier
2018-02-19 11:31:34 +09:00
parent 35f669aa15
commit af94de93d1
4 changed files with 22 additions and 17 deletions

View File

@@ -765,8 +765,8 @@ namespace BTCPayServer.Tests
private static bool IsMapped(Invoice invoice, ApplicationDbContext ctx) private static bool IsMapped(Invoice invoice, ApplicationDbContext ctx)
{ {
var h = BitcoinAddress.Create(invoice.BitcoinAddress).ScriptPubKey.Hash; var h = BitcoinAddress.Create(invoice.BitcoinAddress).ScriptPubKey.Hash.ToString();
return ctx.AddressInvoices.FirstOrDefault(i => i.InvoiceDataId == invoice.Id && i.GetHash() == h) != null; return ctx.AddressInvoices.FirstOrDefault(i => i.InvoiceDataId == invoice.Id && i.GetAddress() == h) != null;
} }
private void Eventually(Action act) private void Eventually(Action act)

View File

@@ -21,27 +21,29 @@ namespace BTCPayServer.Data
#pragma warning disable CS0618 #pragma warning disable CS0618
public ScriptId GetHash() public string GetAddress()
{ {
if (Address == null) if (Address == null)
return null; return null;
var index = Address.IndexOf("#", StringComparison.InvariantCulture); var index = Address.LastIndexOf("#", StringComparison.InvariantCulture);
if (index == -1) if (index == -1)
return new ScriptId(Address); return Address;
return new ScriptId(Address.Substring(0, index)); 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; return this;
} }
public CryptoDataId GetCryptoDataId() public CryptoDataId GetCryptoDataId()
{ {
if (Address == null) if (Address == null)
return 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) if (index == -1)
return CryptoDataId.Parse("BTC"); return CryptoDataId.Parse("BTC");
/////////////////////////
return CryptoDataId.Parse(Address.Substring(index + 1)); return CryptoDataId.Parse(Address.Substring(index + 1));
} }
#pragma warning restore CS0618 #pragma warning restore CS0618

View File

@@ -33,11 +33,13 @@ namespace BTCPayServer.Payments.Bitcoin
DepositAddress = BitcoinAddress.Create(newPaymentDestination, DepositAddress.Network); DepositAddress = BitcoinAddress.Create(newPaymentDestination, DepositAddress.Network);
} }
// Those properties are JsonIgnore because their data is inside CryptoData class for legacy reason
[JsonIgnore] [JsonIgnore]
public FeeRate FeeRate { get; set; } public FeeRate FeeRate { get; set; }
[JsonIgnore] [JsonIgnore]
public Money TxFee { get; set; } public Money TxFee { get; set; }
[JsonIgnore] [JsonIgnore]
public BitcoinAddress DepositAddress { get; set; } public BitcoinAddress DepositAddress { get; set; }
///////////////////////////////////////////////////////////////////////////////////////
} }
} }

View File

@@ -130,12 +130,12 @@ namespace BTCPayServer.Services.Invoices
throw new InvalidOperationException("CryptoCode unsupported"); throw new InvalidOperationException("CryptoCode unsupported");
var paymentDestination = cryptoData.GetPaymentMethod().GetPaymentDestination(); var paymentDestination = cryptoData.GetPaymentMethod().GetPaymentDestination();
ScriptId hash = GetAddressInvoiceHash(cryptoData); string address = GetDestination(cryptoData);
context.AddressInvoices.Add(new AddressInvoiceData() context.AddressInvoices.Add(new AddressInvoiceData()
{ {
InvoiceDataId = invoice.Id, InvoiceDataId = invoice.Id,
CreatedTime = DateTimeOffset.UtcNow, CreatedTime = DateTimeOffset.UtcNow,
}.SetHash(hash, cryptoData.GetId())); }.Set(address, cryptoData.GetId()));
context.HistoricalAddressInvoices.Add(new HistoricalAddressInvoiceData() context.HistoricalAddressInvoices.Add(new HistoricalAddressInvoiceData()
{ {
@@ -162,14 +162,15 @@ namespace BTCPayServer.Services.Invoices
return invoice; 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) 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<bool> NewAddress(string invoiceId, IPaymentMethod paymentMethod, BTCPayNetwork network) public async Task<bool> NewAddress(string invoiceId, IPaymentMethod paymentMethod, BTCPayNetwork network)
@@ -208,7 +209,7 @@ namespace BTCPayServer.Services.Invoices
InvoiceDataId = invoiceId, InvoiceDataId = invoiceId,
CreatedTime = DateTimeOffset.UtcNow CreatedTime = DateTimeOffset.UtcNow
} }
.SetHash(GetAddressInvoiceHash(currencyData), currencyData.GetId())); .Set(GetDestination(currencyData), currencyData.GetId()));
context.HistoricalAddressInvoices.Add(new HistoricalAddressInvoiceData() context.HistoricalAddressInvoices.Add(new HistoricalAddressInvoiceData()
{ {
InvoiceDataId = invoiceId, InvoiceDataId = invoiceId,
@@ -360,7 +361,7 @@ namespace BTCPayServer.Services.Invoices
} }
if (invoice.AddressInvoices != null) 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) if(invoice.Events != null)
{ {