From 8f0d82d21994a3745c500fafc5cdd49b86a59231 Mon Sep 17 00:00:00 2001 From: Wouter Samaey Date: Mon, 29 Nov 2021 07:23:56 +0100 Subject: [PATCH] Cheat mode to mine blocks (#3163) --- .../Controllers/InvoiceController.Testing.cs | 45 +++++++++++++++++ .../Views/Invoice/Checkout-Testing.cshtml | 50 +++++++++++-------- 2 files changed, 74 insertions(+), 21 deletions(-) diff --git a/BTCPayServer/Controllers/InvoiceController.Testing.cs b/BTCPayServer/Controllers/InvoiceController.Testing.cs index fd878bee0..0f27e3861 100644 --- a/BTCPayServer/Controllers/InvoiceController.Testing.cs +++ b/BTCPayServer/Controllers/InvoiceController.Testing.cs @@ -17,6 +17,12 @@ namespace BTCPayServer.Controllers public Decimal Amount { get; set; } public string CryptoCode { get; set; } = "BTC"; } + + public class MineBlocksRequest + { + public int BlockCount { get; set; } + public string CryptoCode { get; set; } = "BTC"; + } [HttpPost] [Route("i/{invoiceId}/test-payment")] @@ -57,6 +63,45 @@ namespace BTCPayServer.Controllers }); } } + + [HttpPost] + [Route("i/{invoiceId}/mine-blocks")] + [CheatModeRoute] + public async Task MineBlock(string invoiceId, MineBlocksRequest request, [FromServices] Cheater cheater) + { + var invoice = await _InvoiceRepository.GetInvoice(invoiceId); + var store = await _StoreRepository.FindStore(invoice.StoreId); + + // TODO support altcoins, not just bitcoin + var network = _NetworkProvider.GetNetwork(request.CryptoCode); + var paymentMethodId = store.GetDefaultPaymentId() ?? store.GetEnabledPaymentIds(_NetworkProvider).FirstOrDefault(p => p.CryptoCode == request.CryptoCode && p.PaymentType == PaymentTypes.BTCLike); + var bitcoinAddressString = invoice.GetPaymentMethod(paymentMethodId).GetPaymentMethodDetails().GetPaymentDestination(); + var bitcoinAddressObj = BitcoinAddress.Create(bitcoinAddressString, network.NBitcoinNetwork); + + // Mine the blocks + try + { + if (request.BlockCount > 0) + { + cheater.CashCow.GenerateToAddress(request.BlockCount, bitcoinAddressObj); + return Ok(new + { + SuccessMessage = "Mined "+request.BlockCount+" blocks for " + bitcoinAddressObj + }); + } + return BadRequest(new + { + ErrorMessage = "Number of blocks should be > 0" + }); + } + catch (Exception e) + { + return BadRequest(new + { + ErrorMessage = e.Message + }); + } + } [HttpPost] [Route("i/{invoiceId}/expire")] diff --git a/BTCPayServer/Views/Invoice/Checkout-Testing.cshtml b/BTCPayServer/Views/Invoice/Checkout-Testing.cshtml index e8975121c..7ea86bce0 100644 --- a/BTCPayServer/Views/Invoice/Checkout-Testing.cshtml +++ b/BTCPayServer/Views/Invoice/Checkout-Testing.cshtml @@ -4,7 +4,7 @@
-
+
@@ -24,35 +24,39 @@ -
- - - -
*@ +
+ +
+ +
+ +
{{$t("Blocks")}}
+
+
+ +