diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..cd967fc --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.idea +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7ab637e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +**/bin/**/* +**/obj +.idea diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..2425aab --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "submodules/walletwasabi"] + path = submodules/walletwasabi + url = https://github.com/kukks/walletwasabi +[submodule "submodules/btcpayserver"] + path = submodules/btcpayserver + url = https://github.com/btcpayserver/btcpayserver diff --git a/.run/BTCPayServer_ Altcoins-HTTPS.run.xml b/.run/BTCPayServer_ Altcoins-HTTPS.run.xml new file mode 100644 index 0000000..5516a3a --- /dev/null +++ b/.run/BTCPayServer_ Altcoins-HTTPS.run.xml @@ -0,0 +1,19 @@ + + + + \ No newline at end of file diff --git a/BTCPayServerPlugins.sln b/BTCPayServerPlugins.sln new file mode 100644 index 0000000..9bb8691 --- /dev/null +++ b/BTCPayServerPlugins.sln @@ -0,0 +1,117 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WalletWasabi", "submodules\walletwasabi\WalletWasabi\WalletWasabi.csproj", "{D1D1116C-38F9-4EA3-AC65-A75FEA82E5C8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BTCPayServer", "submodules\btcpayserver\BTCPayServer\BTCPayServer.csproj", "{B19C9F52-DC47-466D-8B5C-2D202B7B003F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BTCPayServer.Plugins.BitcoinWhitepaper", "Plugins\BTCPayServer.Plugins.BitcoinWhitepaper\BTCPayServer.Plugins.BitcoinWhitepaper.csproj", "{AD9635BB-C70E-4676-BB04-900D51B01666}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BTCPayServer.Abstractions", "submodules\btcpayserver\BTCPayServer.Abstractions\BTCPayServer.Abstractions.csproj", "{8F158B88-0FEE-44FF-8552-7C0F17D5C508}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BTCPayServer.Client", "submodules\btcpayserver\BTCPayServer.Client\BTCPayServer.Client.csproj", "{DF85EFA4-0EF5-4A99-853F-E6F9C88E3F8C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BTCPayServer.Data", "submodules\btcpayserver\BTCPayServer.Data\BTCPayServer.Data.csproj", "{2C5C4DF9-BA1F-4671-9F24-B22D7C9C3D21}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BTCPayServer.Rating", "submodules\btcpayserver\BTCPayServer.Rating\BTCPayServer.Rating.csproj", "{D7E7309D-C4F4-496A-B2C8-BC5D3991B9C0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BTCPayServer.Common", "submodules\btcpayserver\BTCPayServer.Common\BTCPayServer.Common.csproj", "{3F2E0BA0-9EA7-490F-894D-F9703F35B174}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BTCPayServer.Plugins.Custodians.FakeCustodian", "submodules\btcpayserver\Plugins\BTCPayServer.Plugins.Custodians.FakeCustodian\BTCPayServer.Plugins.Custodians.FakeCustodian.csproj", "{CCDE6E23-60B7-4B12-95AA-91CB40DBC3BD}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BTCPay", "BTCPay", "{9E04ECE9-E304-4FF2-9CBC-83256E6C6962}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfigBuilder", "ConfigBuilder\ConfigBuilder.csproj", "{6295533A-F941-40CA-B889-FE6C0432ED53}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BTCPayServer.Plugins.FixedFloat", "Plugins\BTCPayServer.Plugins.FixedFloat\BTCPayServer.Plugins.FixedFloat.csproj", "{58863D86-3C78-4BEC-ACB6-2F82CC141210}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BTCPayServer.Plugins.LiquidPlus", "Plugins\BTCPayServer.Plugins.LiquidPlus\BTCPayServer.Plugins.LiquidPlus.csproj", "{B4E2ED08-4AD3-4648-8BDB-3107200460B9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BTCPayServer.Plugins.NFC", "Plugins\BTCPayServer.Plugins.NFC\BTCPayServer.Plugins.NFC.csproj", "{71885A5E-1B00-4676-9566-D81AAE37406C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BTCPayServer.Plugins.SideShift", "Plugins\BTCPayServer.Plugins.SideShift\BTCPayServer.Plugins.SideShift.csproj", "{5E1BAA06-7828-47BC-89D6-19C2A78EA427}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BTCPayServer.Plugins.TicketTailor", "Plugins\BTCPayServer.Plugins.TicketTailor\BTCPayServer.Plugins.TicketTailor.csproj", "{7AFC20EB-1696-47D7-8E57-822B05DD18F2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BTCPayServer.Plugins.Wabisabi", "Plugins\BTCPayServer.Plugins.Wabisabi\BTCPayServer.Plugins.Wabisabi.csproj", "{0D438B7D-F996-4BF3-8F54-02CB9DF120D8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D1D1116C-38F9-4EA3-AC65-A75FEA82E5C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D1D1116C-38F9-4EA3-AC65-A75FEA82E5C8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D1D1116C-38F9-4EA3-AC65-A75FEA82E5C8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D1D1116C-38F9-4EA3-AC65-A75FEA82E5C8}.Release|Any CPU.Build.0 = Release|Any CPU + {B19C9F52-DC47-466D-8B5C-2D202B7B003F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B19C9F52-DC47-466D-8B5C-2D202B7B003F}.Release|Any CPU.Build.0 = Release|Any CPU + {B19C9F52-DC47-466D-8B5C-2D202B7B003F}.Debug|Any CPU.ActiveCfg = Altcoins-Debug|Any CPU + {B19C9F52-DC47-466D-8B5C-2D202B7B003F}.Debug|Any CPU.Build.0 = Altcoins-Debug|Any CPU + {AD9635BB-C70E-4676-BB04-900D51B01666}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AD9635BB-C70E-4676-BB04-900D51B01666}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AD9635BB-C70E-4676-BB04-900D51B01666}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AD9635BB-C70E-4676-BB04-900D51B01666}.Release|Any CPU.Build.0 = Release|Any CPU + {8F158B88-0FEE-44FF-8552-7C0F17D5C508}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8F158B88-0FEE-44FF-8552-7C0F17D5C508}.Release|Any CPU.Build.0 = Release|Any CPU + {8F158B88-0FEE-44FF-8552-7C0F17D5C508}.Debug|Any CPU.ActiveCfg = Altcoins-Debug|Any CPU + {8F158B88-0FEE-44FF-8552-7C0F17D5C508}.Debug|Any CPU.Build.0 = Altcoins-Debug|Any CPU + {DF85EFA4-0EF5-4A99-853F-E6F9C88E3F8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DF85EFA4-0EF5-4A99-853F-E6F9C88E3F8C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DF85EFA4-0EF5-4A99-853F-E6F9C88E3F8C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DF85EFA4-0EF5-4A99-853F-E6F9C88E3F8C}.Release|Any CPU.Build.0 = Release|Any CPU + {2C5C4DF9-BA1F-4671-9F24-B22D7C9C3D21}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2C5C4DF9-BA1F-4671-9F24-B22D7C9C3D21}.Release|Any CPU.Build.0 = Release|Any CPU + {2C5C4DF9-BA1F-4671-9F24-B22D7C9C3D21}.Debug|Any CPU.ActiveCfg = Altcoins-Debug|Any CPU + {2C5C4DF9-BA1F-4671-9F24-B22D7C9C3D21}.Debug|Any CPU.Build.0 = Altcoins-Debug|Any CPU + {D7E7309D-C4F4-496A-B2C8-BC5D3991B9C0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D7E7309D-C4F4-496A-B2C8-BC5D3991B9C0}.Release|Any CPU.Build.0 = Release|Any CPU + {D7E7309D-C4F4-496A-B2C8-BC5D3991B9C0}.Debug|Any CPU.ActiveCfg = Altcoins-Release|Any CPU + {D7E7309D-C4F4-496A-B2C8-BC5D3991B9C0}.Debug|Any CPU.Build.0 = Altcoins-Release|Any CPU + {3F2E0BA0-9EA7-490F-894D-F9703F35B174}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3F2E0BA0-9EA7-490F-894D-F9703F35B174}.Release|Any CPU.Build.0 = Release|Any CPU + {3F2E0BA0-9EA7-490F-894D-F9703F35B174}.Debug|Any CPU.ActiveCfg = Altcoins-Debug|Any CPU + {3F2E0BA0-9EA7-490F-894D-F9703F35B174}.Debug|Any CPU.Build.0 = Altcoins-Debug|Any CPU + {CCDE6E23-60B7-4B12-95AA-91CB40DBC3BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CCDE6E23-60B7-4B12-95AA-91CB40DBC3BD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CCDE6E23-60B7-4B12-95AA-91CB40DBC3BD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CCDE6E23-60B7-4B12-95AA-91CB40DBC3BD}.Release|Any CPU.Build.0 = Release|Any CPU + {6295533A-F941-40CA-B889-FE6C0432ED53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6295533A-F941-40CA-B889-FE6C0432ED53}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6295533A-F941-40CA-B889-FE6C0432ED53}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6295533A-F941-40CA-B889-FE6C0432ED53}.Release|Any CPU.Build.0 = Release|Any CPU + {58863D86-3C78-4BEC-ACB6-2F82CC141210}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {58863D86-3C78-4BEC-ACB6-2F82CC141210}.Debug|Any CPU.Build.0 = Debug|Any CPU + {58863D86-3C78-4BEC-ACB6-2F82CC141210}.Release|Any CPU.ActiveCfg = Release|Any CPU + {58863D86-3C78-4BEC-ACB6-2F82CC141210}.Release|Any CPU.Build.0 = Release|Any CPU + {B4E2ED08-4AD3-4648-8BDB-3107200460B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B4E2ED08-4AD3-4648-8BDB-3107200460B9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B4E2ED08-4AD3-4648-8BDB-3107200460B9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B4E2ED08-4AD3-4648-8BDB-3107200460B9}.Release|Any CPU.Build.0 = Release|Any CPU + {71885A5E-1B00-4676-9566-D81AAE37406C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {71885A5E-1B00-4676-9566-D81AAE37406C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {71885A5E-1B00-4676-9566-D81AAE37406C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {71885A5E-1B00-4676-9566-D81AAE37406C}.Release|Any CPU.Build.0 = Release|Any CPU + {5E1BAA06-7828-47BC-89D6-19C2A78EA427}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5E1BAA06-7828-47BC-89D6-19C2A78EA427}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5E1BAA06-7828-47BC-89D6-19C2A78EA427}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5E1BAA06-7828-47BC-89D6-19C2A78EA427}.Release|Any CPU.Build.0 = Release|Any CPU + {7AFC20EB-1696-47D7-8E57-822B05DD18F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7AFC20EB-1696-47D7-8E57-822B05DD18F2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7AFC20EB-1696-47D7-8E57-822B05DD18F2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7AFC20EB-1696-47D7-8E57-822B05DD18F2}.Release|Any CPU.Build.0 = Release|Any CPU + {0D438B7D-F996-4BF3-8F54-02CB9DF120D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0D438B7D-F996-4BF3-8F54-02CB9DF120D8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0D438B7D-F996-4BF3-8F54-02CB9DF120D8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0D438B7D-F996-4BF3-8F54-02CB9DF120D8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {B19C9F52-DC47-466D-8B5C-2D202B7B003F} = {9E04ECE9-E304-4FF2-9CBC-83256E6C6962} + {DF85EFA4-0EF5-4A99-853F-E6F9C88E3F8C} = {9E04ECE9-E304-4FF2-9CBC-83256E6C6962} + {8F158B88-0FEE-44FF-8552-7C0F17D5C508} = {9E04ECE9-E304-4FF2-9CBC-83256E6C6962} + {D7E7309D-C4F4-496A-B2C8-BC5D3991B9C0} = {9E04ECE9-E304-4FF2-9CBC-83256E6C6962} + {3F2E0BA0-9EA7-490F-894D-F9703F35B174} = {9E04ECE9-E304-4FF2-9CBC-83256E6C6962} + {CCDE6E23-60B7-4B12-95AA-91CB40DBC3BD} = {9E04ECE9-E304-4FF2-9CBC-83256E6C6962} + {2C5C4DF9-BA1F-4671-9F24-B22D7C9C3D21} = {9E04ECE9-E304-4FF2-9CBC-83256E6C6962} + EndGlobalSection +EndGlobal diff --git a/ConfigBuilder/ConfigBuilder.csproj b/ConfigBuilder/ConfigBuilder.csproj new file mode 100644 index 0000000..ec44a9e --- /dev/null +++ b/ConfigBuilder/ConfigBuilder.csproj @@ -0,0 +1,17 @@ + + + + Exe + net6.0 + enable + enable + Linux + + + + + .dockerignore + + + + diff --git a/ConfigBuilder/Dockerfile b/ConfigBuilder/Dockerfile new file mode 100644 index 0000000..0083922 --- /dev/null +++ b/ConfigBuilder/Dockerfile @@ -0,0 +1,18 @@ +FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base +WORKDIR /app + +FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +WORKDIR /src +COPY ["ConfigBuilder/ConfigBuilder.csproj", "ConfigBuilder/"] +RUN dotnet restore "ConfigBuilder/ConfigBuilder.csproj" +COPY . . +WORKDIR "/src/ConfigBuilder" +RUN dotnet build "ConfigBuilder.csproj" -c Release -o /app/build + +FROM build AS publish +RUN dotnet publish "ConfigBuilder.csproj" -c Release -o /app/publish + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "ConfigBuilder.dll"] diff --git a/ConfigBuilder/Program.cs b/ConfigBuilder/Program.cs new file mode 100644 index 0000000..a884bfc --- /dev/null +++ b/ConfigBuilder/Program.cs @@ -0,0 +1,12 @@ +using System.Text.Json; + +var plugins = Directory.GetDirectories("../../../../Plugins"); +Console.WriteLine(string.Join(',',plugins)); +var p = string.Join(';', plugins.Select(s => $"{Path.GetFullPath(s)}/bin/Debug/net6.0/{Path.GetFileName(s)}.dll" ));; +var fileContents = $"{{ \"DEBUG_PLUGINS\": \"{p}\"}}"; +var content = JsonSerializer.Serialize(new +{ + DEBUG_PLUGINS = p +}); + +await File.WriteAllTextAsync("../../../../submodules/BTCPayServer/BTCPayServer/appsettings.dev.json", content); \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.AOPP/AOPPController.cs b/Plugins/BTCPayServer.Plugins.AOPP/AOPPController.cs new file mode 100644 index 0000000..0e034a9 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/AOPPController.cs @@ -0,0 +1,222 @@ +using System; +using System.IO; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using System.Web; +using BTCPayServer.Abstractions.Constants; +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Client; +using BTCPayServer.Client.Models; +using BTCPayServer.Common; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using NBitcoin; +using NBitcoin.Crypto; +using NBitcoin.Protocol; +using NBXplorer; +using Newtonsoft.Json; + +namespace BTCPayServer.Plugins.AOPP +{ + [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Route("plugins/{storeId}/AOPP")] + public class AOPPController : Controller + { + private readonly BTCPayServerClient _btcPayServerClient; + private readonly AOPPService _AOPPService; + + public AOPPController(BTCPayServerClient btcPayServerClient, AOPPService AOPPService) + { + _btcPayServerClient = btcPayServerClient; + _AOPPService = AOPPService; + } + + [HttpGet("")] + public async Task UpdateAOPPSettings(string storeId) + { + var store = await _btcPayServerClient.GetStore(storeId); + + UpdateAOPPSettingsViewModel vm = new UpdateAOPPSettingsViewModel(); + vm.StoreName = store.Name; + AOPPSettings AOPP = null; + try + { + AOPP = await _AOPPService.GetAOPPForStore(storeId); + } + catch (Exception) + { + // ignored + } + + SetExistingValues(AOPP, vm); + return View(vm); + } + + private void SetExistingValues(AOPPSettings existing, UpdateAOPPSettingsViewModel vm) + { + if (existing == null) + return; + vm.Enabled = existing.Enabled; + } + + [HttpPost("")] + public async Task UpdateAOPPSettings(string storeId, UpdateAOPPSettingsViewModel vm, + string command) + { + if (vm.Enabled) + { + if (!ModelState.IsValid) + { + return View(vm); + } + } + + var AOPPSettings = new AOPPSettings() + { + Enabled = vm.Enabled, + }; + + switch (command) + { + case "save": + await _AOPPService.SetAOPPForStore(storeId, AOPPSettings); + TempData["SuccessMessage"] = "AOPP settings modified"; + return RedirectToAction(nameof(UpdateAOPPSettings), new {storeId}); + + default: + return View(vm); + } + } + + + + internal static String BITCOIN_SIGNED_MESSAGE_HEADER = "Bitcoin Signed Message:\n"; + internal static byte[] BITCOIN_SIGNED_MESSAGE_HEADER_BYTES = Encoding.UTF8.GetBytes(BITCOIN_SIGNED_MESSAGE_HEADER); + + //http://bitcoinj.googlecode.com/git-history/keychain/core/src/main/java/com/google/bitcoin/core/Utils.java + private static byte[] FormatMessageForSigning(byte[] messageBytes) + { + MemoryStream ms = new MemoryStream(); + + ms.WriteByte((byte)BITCOIN_SIGNED_MESSAGE_HEADER_BYTES.Length); + ms.Write(BITCOIN_SIGNED_MESSAGE_HEADER_BYTES, 0, BITCOIN_SIGNED_MESSAGE_HEADER_BYTES.Length); + + VarInt size = new VarInt((ulong)messageBytes.Length); + ms.Write(size.ToBytes(), 0, size.ToBytes().Length); + ms.Write(messageBytes, 0, messageBytes.Length); + return ms.ToArray(); + } + + public class AoppRequest + { + public Uri aopp { get; set; } + } + + [HttpPost] + [Route("{invoiceId}")] + [AllowAnonymous] + public async Task AOPPExecute(string storeId, string invoiceId, + [FromBody] AoppRequest request , + [FromServices] IHttpClientFactory httpClientFactory, + [FromServices] BTCPayNetworkProvider btcPayNetworkProvider, + [FromServices] IExplorerClientProvider explorerClientProvider, + [FromServices] BTCPayServerClient btcPayServerClient, + [FromServices] IBTCPayServerClientFactory btcPayServerClientFactory) + { + try + { + var client = await btcPayServerClientFactory.Create(null, new[] {storeId}); + + var invoice = await client.GetInvoice(storeId, invoiceId); + if (invoice.Status is not InvoiceStatus.New) + { + return NotFound(); + } + + var qs = HttpUtility.ParseQueryString(request.aopp.Query); + var asset = qs.Get("asset"); + var network = btcPayNetworkProvider.GetNetwork(asset); + + + + var invoicePaymentMethods = await client.GetInvoicePaymentMethods(storeId, invoiceId); + + var pm = invoicePaymentMethods.FirstOrDefault(model => + model.PaymentMethod.Equals(asset, StringComparison.InvariantCultureIgnoreCase)); + if (pm is null) + { + return NotFound(); + } + var supported = (await client.GetStoreOnChainPaymentMethods(storeId)) + .FirstOrDefault(settings => settings.CryptoCode.Equals(asset, StringComparison.InvariantCultureIgnoreCase)); +; + var msg = qs.Get("msg"); + var format = qs.Get("format"); + var callback = new Uri(qs.Get("callback")!, UriKind.Absolute); + ScriptType? expectedType = null; + switch (format) + { + case "p2pkh": + expectedType = ScriptType.P2PKH; + break; + case "p2wpkh": + expectedType = ScriptType.P2WPKH; + break; + case "p2sh": + expectedType = ScriptType.P2SH; + break; + case "p2tr": + expectedType = ScriptType.Taproot; + break; + case "any": + break; + } + + var address = BitcoinAddress.Create(pm.Destination, network.NBitcoinNetwork); + if (expectedType is not null && !address.ScriptPubKey + .IsScriptType(expectedType.Value)) + { + return BadRequest(); + } + var derivatonScheme = + network.NBXplorerNetwork.DerivationStrategyFactory.Parse(supported.DerivationScheme); + var explorerClient = explorerClientProvider.GetExplorerClient(network); + var extKeyStr = await explorerClient.GetMetadataAsync( + derivatonScheme, + WellknownMetadataKeys.AccountHDKey); + if (extKeyStr == null) + { + return BadRequest(); + } + + var accountKey = ExtKey.Parse(extKeyStr, network.NBitcoinNetwork); + + var keyInfo = await explorerClient.GetKeyInformationAsync(derivatonScheme, address.ScriptPubKey); + var privateKey = accountKey.Derive(keyInfo.KeyPath).PrivateKey; + + var messageBytes = Encoding.UTF8.GetBytes(msg); + byte[] data = FormatMessageForSigning(messageBytes); + var hash = Hashes.DoubleSHA256(data); + var sig = Convert.ToBase64String(privateKey.SignCompact(hash, true).Signature); + + var response = new + { + version = 0, + address = pm.Destination, + signature = sig + }; + using var httpClient = httpClientFactory.CreateClient(); + await httpClient.PostAsync(callback, + new StringContent(JsonConvert.SerializeObject(response), Encoding.UTF8, "application/json")); + return Ok(); + } + catch (Exception e) + { + return BadRequest(new {ErrorMessage = e.Message}); + } + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.AOPP/AOPPPlugin.cs b/Plugins/BTCPayServer.Plugins.AOPP/AOPPPlugin.cs new file mode 100644 index 0000000..e62dace --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/AOPPPlugin.cs @@ -0,0 +1,42 @@ +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Models; +using BTCPayServer.Abstractions.Services; +using Microsoft.Extensions.DependencyInjection; + +namespace BTCPayServer.Plugins.AOPP +{ + public class AOPPPlugin : BaseBTCPayServerPlugin + { + public override string Identifier => "BTCPayServer.Plugins.AOPP"; + public override string Name => "AOPP"; + + + public override IBTCPayServerPlugin.PluginDependency[] Dependencies { get; } = + { + new() { Identifier = nameof(BTCPayServer), Condition = ">=1.6.0.0" } + }; + + public override string Description => + "Allows you to support the AOPP protocol in invoices to allow customers to bypass stupid KYC rules."; + + public override void Execute(IServiceCollection applicationBuilder) + { + applicationBuilder.AddSingleton(); + applicationBuilder.AddSingleton(new UIExtension("AOPP/StoreIntegrationAOPPOption", + "store-integrations-list")); + applicationBuilder.AddSingleton(new UIExtension("AOPP/CheckoutContentExtension", + "checkout-bitcoin-post-content")); + applicationBuilder.AddSingleton(new UIExtension("AOPP/CheckoutContentExtension", + "checkout-lightning-post-content")); + applicationBuilder.AddSingleton(new UIExtension("AOPP/CheckoutTabExtension", + "checkout-bitcoin-post-tabs")); + applicationBuilder.AddSingleton(new UIExtension("AOPP/CheckoutTabExtension", + "checkout-lightning-post-tabs")); + applicationBuilder.AddSingleton(new UIExtension("AOPP/CheckoutEnd", + "checkout-end")); + applicationBuilder.AddSingleton(new UIExtension("AOPP/AOPPNav", + "store-integrations-nav")); + base.Execute(applicationBuilder); + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.AOPP/AOPPService.cs b/Plugins/BTCPayServer.Plugins.AOPP/AOPPService.cs new file mode 100644 index 0000000..87ba10b --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/AOPPService.cs @@ -0,0 +1,51 @@ +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Contracts; +using Microsoft.Extensions.Caching.Memory; + +namespace BTCPayServer.Plugins.AOPP +{ + public class AOPPService + { + private readonly ISettingsRepository _settingsRepository; + private readonly IMemoryCache _memoryCache; + private readonly IStoreRepository _storeRepository; + + public AOPPService(ISettingsRepository settingsRepository, IMemoryCache memoryCache, + IStoreRepository storeRepository) + { + _settingsRepository = settingsRepository; + _memoryCache = memoryCache; + _storeRepository = storeRepository; + } + + + public async Task GetAOPPForStore(string storeId) + { + var k = $"{nameof(AOPPSettings)}_{storeId}"; + return await _memoryCache.GetOrCreateAsync(k, async _ => + { + var res = await _storeRepository.GetSettingAsync(storeId, + nameof(AOPPSettings)); + if (res is not null) return res; + res = await _settingsRepository.GetSettingAsync(k); + + if (res is not null) + { + await SetAOPPForStore(storeId, res); + } + + await _settingsRepository.UpdateSetting(null, k); + return res; + }); + } + + public async Task SetAOPPForStore(string storeId, AOPPSettings AOPPSettings) + { + var k = $"{nameof(AOPPSettings)}_{storeId}"; + await _storeRepository.UpdateSetting(storeId, nameof(AOPPSettings), AOPPSettings); + _memoryCache.Set(k, AOPPSettings); + } + + + } +} diff --git a/Plugins/BTCPayServer.Plugins.AOPP/AOPPSettings.cs b/Plugins/BTCPayServer.Plugins.AOPP/AOPPSettings.cs new file mode 100644 index 0000000..02677f0 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/AOPPSettings.cs @@ -0,0 +1,7 @@ +namespace BTCPayServer.Plugins.AOPP +{ + public class AOPPSettings + { + public bool Enabled { get; set; } + } +} diff --git a/Plugins/BTCPayServer.Plugins.AOPP/BTCPayServer.Plugins.AOPP.csproj b/Plugins/BTCPayServer.Plugins.AOPP/BTCPayServer.Plugins.AOPP.csproj new file mode 100644 index 0000000..2b90ff4 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/BTCPayServer.Plugins.AOPP.csproj @@ -0,0 +1,19 @@ + + + net6.0 + true + false + true + 1.0.1 + + + + + + + + + + + + diff --git a/Plugins/BTCPayServer.Plugins.AOPP/Pack.ps1 b/Plugins/BTCPayServer.Plugins.AOPP/Pack.ps1 new file mode 100644 index 0000000..933a81a --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/Pack.ps1 @@ -0,0 +1,2 @@ +dotnet publish -c Altcoins-Release -o bin/publish/BTCPayServer.Plugins.AOPP +dotnet run -p ../../BTCPayServer.PluginPacker bin/publish/BTCPayServer.Plugins.AOPP BTCPayServer.Plugins.AOPP ../packed diff --git a/Plugins/BTCPayServer.Plugins.AOPP/Resources/js/aoppComponent.js b/Plugins/BTCPayServer.Plugins.AOPP/Resources/js/aoppComponent.js new file mode 100644 index 0000000..49920e9 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/Resources/js/aoppComponent.js @@ -0,0 +1,36 @@ +Vue.component("AOPP", { + props: ["srvModel"], + methods: { + onaoppChange: function(){ + this.aoppAddressInputDirty = true; + this.aoppAddressInputInvalid = false; + }, + onSubmit : function(){ + var self = this; + if (this.aoppAddressInput && this.aoppAddressInput.startsWith("aopp:?")) { + this.aoppAddressFormSubmitting = true; + // Push the email to a server, once the reception is confirmed move on + $.ajax({ + url: "/plugins/"+this.srvModel.storeId+"/AOPP/" +this.srvModel.invoiceId, + type: "POST", + data: JSON.stringify({ aopp: this.aoppAddressInput }), + contentType: "application/json; charset=utf-8" + }) + .done(function () { + }).always(function () { + self.aoppAddressFormSubmitting = false; + }); + } else { + this.aoppAddressInputInvalid = true; + } + } + }, + data: function () { + return { + aoppAddressInput: "", + aoppAddressInputDirty: false, + aoppAddressInputInvalid: false, + aoppAddressFormSubmitting: false + } + } +}); diff --git a/Plugins/BTCPayServer.Plugins.AOPP/UpdateAOPPSettingsViewModel.cs b/Plugins/BTCPayServer.Plugins.AOPP/UpdateAOPPSettingsViewModel.cs new file mode 100644 index 0000000..81bdb75 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/UpdateAOPPSettingsViewModel.cs @@ -0,0 +1,8 @@ +namespace BTCPayServer.Plugins.AOPP +{ + public class UpdateAOPPSettingsViewModel + { + public bool Enabled { get; set; } + public string StoreName { get; set; } + } +} diff --git a/Plugins/BTCPayServer.Plugins.AOPP/Views/AOPP/UpdateAOPPSettings.cshtml b/Plugins/BTCPayServer.Plugins.AOPP/Views/AOPP/UpdateAOPPSettings.cshtml new file mode 100644 index 0000000..4ef5676 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/Views/AOPP/UpdateAOPPSettings.cshtml @@ -0,0 +1,23 @@ +@model BTCPayServer.Plugins.AOPP.UpdateAOPPSettingsViewModel +@{ + Layout = "../Shared/_NavLayout.cshtml"; + ViewData["NavPartialName"] = "../UIStores/_Nav"; +} + +

@ViewData["PageTitle"]

+ +
+
+
+
+ + +
+ +
+
+
+ +@section PageFootContent { + +} diff --git a/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/AOPPNav.cshtml b/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/AOPPNav.cshtml new file mode 100644 index 0000000..9fb0c6e --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/AOPPNav.cshtml @@ -0,0 +1,18 @@ +@using BTCPayServer.Abstractions.Contracts +@using BTCPayServer.Plugins.AOPP +@inject IScopeProvider ScopeProvider +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + var isActive = !string.IsNullOrEmpty(storeId) && ViewContext.RouteData.Values.TryGetValue("Controller", out var controller) && controller is not null && + nameof(AOPPController).StartsWith(controller.ToString(), StringComparison.InvariantCultureIgnoreCase); +} +@if (!string.IsNullOrEmpty(storeId)) +{ + +} diff --git a/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/CheckoutContentExtension.cshtml b/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/CheckoutContentExtension.cshtml new file mode 100644 index 0000000..681f992 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/CheckoutContentExtension.cshtml @@ -0,0 +1,40 @@ +@using BTCPayServer.Plugins.AOPP +@using Newtonsoft.Json +@using Newtonsoft.Json.Linq +@inject AOPPService AOPPService +@{ + var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value(); + var settings = await AOPPService.GetAOPPForStore(storeId); + if (settings?.Enabled is true) + { +
+ +
+
+ {{$t("AOPP")}} +
+
+ + If you are sending funds from an exchange that requiores that you "verify" the withdrawal access, you can use this tool to bypass this madness. You even earn bonus points if they try to pass that data over to a chain surveillance service, by poisoning their clusters. + + {{$t("Please enter a valid aopp address")}} +
+
+ + + + +
+
+
+
+ } +} diff --git a/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/CheckoutEnd.cshtml b/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/CheckoutEnd.cshtml new file mode 100644 index 0000000..e3666e4 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/CheckoutEnd.cshtml @@ -0,0 +1,13 @@ +@using BTCPayServer.Plugins.AOPP +@using Newtonsoft.Json +@using Newtonsoft.Json.Linq +@inject AOPPService AOPPService +@{ + + var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value(); + var settings = await AOPPService.GetAOPPForStore(storeId); + if (settings?.Enabled is true) + { + + } +} diff --git a/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/CheckoutTabExtension.cshtml b/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/CheckoutTabExtension.cshtml new file mode 100644 index 0000000..4749b25 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/CheckoutTabExtension.cshtml @@ -0,0 +1,14 @@ +@using BTCPayServer.Plugins.AOPP +@using Newtonsoft.Json +@using Newtonsoft.Json.Linq +@inject AOPPService AOPPService +@{ + var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value(); + var settings = await AOPPService.GetAOPPForStore(storeId); + if (settings?.Enabled is true) + { +
+ {{$t("AOPP")}} +
+ } +} diff --git a/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/StoreIntegrationAOPPOption.cshtml b/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/StoreIntegrationAOPPOption.cshtml new file mode 100644 index 0000000..0571ce9 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/Views/Shared/AOPP/StoreIntegrationAOPPOption.cshtml @@ -0,0 +1,57 @@ +@using BTCPayServer.Abstractions.Contracts +@using BTCPayServer.Plugins.AOPP +@using Microsoft.AspNetCore.Mvc.TagHelpers +@inject AOPPService AOPPService +@inject IScopeProvider ScopeProvider +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + AOPPSettings settings = null; + if (!string.IsNullOrEmpty(storeId)) + { + try + { + settings = await AOPPService.GetAOPPForStore(storeId); + } + catch (Exception) + { + } + } +} +@if (!string.IsNullOrEmpty(storeId)) +{ +
  • +
    + + + AOPP + + + Allows your customers to pay with altcoins that are not supported by BTCPay Server. + + + + @if (settings?.Enabled is true) + { + + + Enabled + + | + + Modify + + } + else + { + + + Disabled + + + Setup + + } + +
    +
  • +} diff --git a/Plugins/BTCPayServer.Plugins.AOPP/Views/_ViewImports.cshtml b/Plugins/BTCPayServer.Plugins.AOPP/Views/_ViewImports.cshtml new file mode 100644 index 0000000..afa82bb --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.AOPP/Views/_ViewImports.cshtml @@ -0,0 +1 @@ +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/Plugins/BTCPayServer.Plugins.BitcoinWhitepaper/BTCPayServer.Plugins.BitcoinWhitepaper.csproj b/Plugins/BTCPayServer.Plugins.BitcoinWhitepaper/BTCPayServer.Plugins.BitcoinWhitepaper.csproj new file mode 100644 index 0000000..7157900 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.BitcoinWhitepaper/BTCPayServer.Plugins.BitcoinWhitepaper.csproj @@ -0,0 +1,36 @@ + + + net6.0 + 10 + + + + + Bitcoin Whitepaper + This makes the Bitcoin whitepaper available on your BTCPay Server. + Kukks + 1.0.2 + + + + + true + false + true + + + + + + StaticWebAssetsEnabled=false + false + runtime;native;build;buildTransitive;contentFiles + + + + + + + + + \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.BitcoinWhitepaper/BitcoinWhitepaperPlugin.cs b/Plugins/BTCPayServer.Plugins.BitcoinWhitepaper/BitcoinWhitepaperPlugin.cs new file mode 100644 index 0000000..c8e2365 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.BitcoinWhitepaper/BitcoinWhitepaperPlugin.cs @@ -0,0 +1,17 @@ +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Models; + +namespace BTCPayServer.Plugins.BitcoinWhitepaper +{ + public class BitcoinWhitepaperPlugin: BaseBTCPayServerPlugin + { + public override string Identifier { get; } = "BTCPayServer.Plugins.BitcoinWhitepaper"; + public override string Name { get; } = "Bitcoin Whitepaper"; + public override string Description { get; } = "This makes the Bitcoin whitepaper available on your BTCPay Server."; + + public override IBTCPayServerPlugin.PluginDependency[] Dependencies { get; } = + { + new() { Identifier = nameof(BTCPayServer), Condition = ">=1.4.0.0" } + }; + } +} diff --git a/Plugins/BTCPayServer.Plugins.BitcoinWhitepaper/bitcoin.pdf b/Plugins/BTCPayServer.Plugins.BitcoinWhitepaper/bitcoin.pdf new file mode 100644 index 0000000..1e19b73 Binary files /dev/null and b/Plugins/BTCPayServer.Plugins.BitcoinWhitepaper/bitcoin.pdf differ diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/BTCPayServer.Plugins.FixedFloat.csproj b/Plugins/BTCPayServer.Plugins.FixedFloat/BTCPayServer.Plugins.FixedFloat.csproj new file mode 100644 index 0000000..dc94ba4 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/BTCPayServer.Plugins.FixedFloat.csproj @@ -0,0 +1,40 @@ + + + + net6.0 + 10 + + + + + FixedFloat + Allows you to embed a FixedFloat conversion screen to allow customers to pay with altcoins. + Kukks + 1.0.6 + + + + + true + false + true + + + + + + StaticWebAssetsEnabled=false + false + runtime;native;build;buildTransitive;contentFiles + + + + + + + + + + + + diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/FixedFloatController.cs b/Plugins/BTCPayServer.Plugins.FixedFloat/FixedFloatController.cs new file mode 100644 index 0000000..5e37eda --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/FixedFloatController.cs @@ -0,0 +1,81 @@ +using System; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Constants; +using BTCPayServer.Client; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace BTCPayServer.Plugins.FixedFloat +{ + [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Route("plugins/{storeId}/FixedFloat")] + public class FixedFloatController : Controller + { + private readonly BTCPayServerClient _btcPayServerClient; + private readonly FixedFloatService _FixedFloatService; + + public FixedFloatController(BTCPayServerClient btcPayServerClient, FixedFloatService FixedFloatService) + { + _btcPayServerClient = btcPayServerClient; + _FixedFloatService = FixedFloatService; + } + + [HttpGet("")] + public async Task UpdateFixedFloatSettings(string storeId) + { + var store = await _btcPayServerClient.GetStore(storeId); + + UpdateFixedFloatSettingsViewModel vm = new UpdateFixedFloatSettingsViewModel(); + vm.StoreName = store.Name; + FixedFloatSettings FixedFloat = null; + try + { + FixedFloat = await _FixedFloatService.GetFixedFloatForStore(storeId); + } + catch (Exception) + { + // ignored + } + + SetExistingValues(FixedFloat, vm); + return View(vm); + } + + private void SetExistingValues(FixedFloatSettings existing, UpdateFixedFloatSettingsViewModel vm) + { + if (existing == null) + return; + vm.Enabled = existing.Enabled; + } + + [HttpPost("")] + public async Task UpdateFixedFloatSettings(string storeId, UpdateFixedFloatSettingsViewModel vm, + string command) + { + if (vm.Enabled) + { + if (!ModelState.IsValid) + { + return View(vm); + } + } + + var FixedFloatSettings = new FixedFloatSettings() + { + Enabled = vm.Enabled, + }; + + switch (command) + { + case "save": + await _FixedFloatService.SetFixedFloatForStore(storeId, FixedFloatSettings); + TempData["SuccessMessage"] = "FixedFloat settings modified"; + return RedirectToAction(nameof(UpdateFixedFloatSettings), new {storeId}); + + default: + return View(vm); + } + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/FixedFloatPlugin.cs b/Plugins/BTCPayServer.Plugins.FixedFloat/FixedFloatPlugin.cs new file mode 100644 index 0000000..f448995 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/FixedFloatPlugin.cs @@ -0,0 +1,48 @@ +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Models; +using BTCPayServer.Abstractions.Services; +using Microsoft.Extensions.DependencyInjection; + +namespace BTCPayServer.Plugins.FixedFloat +{ + public class FixedFloatPlugin : BaseBTCPayServerPlugin + { + public override string Identifier => "BTCPayServer.Plugins.FixedFloat"; + public override string Name => "FixedFloat"; + + public override IBTCPayServerPlugin.PluginDependency[] Dependencies { get; } = + { + new() { Identifier = nameof(BTCPayServer), Condition = ">=1.7.0.0" } + }; + + public override string Description => + "Allows you to embed a FixedFloat conversion screen to allow customers to pay with altcoins."; + + public override void Execute(IServiceCollection applicationBuilder) + { + applicationBuilder.AddSingleton(); + applicationBuilder.AddSingleton(new UIExtension("FixedFloat/FixedFloatNav", + "store-integrations-nav")); + applicationBuilder.AddSingleton(new UIExtension("FixedFloat/StoreIntegrationFixedFloatOption", + "store-integrations-list")); + // Checkout v2 + applicationBuilder.AddSingleton(new UIExtension("FixedFloat/CheckoutPaymentMethodExtension", + "checkout-payment-method")); + applicationBuilder.AddSingleton(new UIExtension("FixedFloat/CheckoutPaymentExtension", + "checkout-payment")); + // Checkout Classic + applicationBuilder.AddSingleton(new UIExtension("FixedFloat/CheckoutContentExtension", + "checkout-bitcoin-post-content")); + applicationBuilder.AddSingleton(new UIExtension("FixedFloat/CheckoutContentExtension", + "checkout-lightning-post-content")); + applicationBuilder.AddSingleton(new UIExtension("FixedFloat/CheckoutTabExtension", + "checkout-bitcoin-post-tabs")); + applicationBuilder.AddSingleton(new UIExtension("FixedFloat/CheckoutTabExtension", + "checkout-lightning-post-tabs")); + applicationBuilder.AddSingleton(new UIExtension("FixedFloat/CheckoutEnd", + "checkout-end")); + base.Execute(applicationBuilder); + } + } + +} diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/FixedFloatService.cs b/Plugins/BTCPayServer.Plugins.FixedFloat/FixedFloatService.cs new file mode 100644 index 0000000..9940349 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/FixedFloatService.cs @@ -0,0 +1,46 @@ +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Contracts; +using Microsoft.Extensions.Caching.Memory; + +namespace BTCPayServer.Plugins.FixedFloat +{ + public class FixedFloatService + { + private readonly ISettingsRepository _settingsRepository; + private readonly IStoreRepository _storeRepository; + private readonly IMemoryCache _memoryCache; + + public FixedFloatService(ISettingsRepository settingsRepository, IStoreRepository storeRepository, IMemoryCache memoryCache) + { + _settingsRepository = settingsRepository; + _storeRepository = storeRepository; + _memoryCache = memoryCache; + } + public async Task GetFixedFloatForStore(string storeId) + { + var k = $"{nameof(FixedFloatSettings)}_{storeId}"; + return await _memoryCache.GetOrCreateAsync(k, async _ => + { + var res = await _storeRepository.GetSettingAsync(storeId, + nameof(FixedFloatSettings)); + if (res is not null) return res; + res = await _settingsRepository.GetSettingAsync(k); + + if (res is not null) + { + await SetFixedFloatForStore(storeId, res); + } + + await _settingsRepository.UpdateSetting(null, k); + return res; + }); + } + + public async Task SetFixedFloatForStore(string storeId, FixedFloatSettings fixedFloatSettings) + { + var k = $"{nameof(FixedFloatSettings)}_{storeId}"; + await _storeRepository.UpdateSetting(storeId, nameof(FixedFloatSettings), fixedFloatSettings); + _memoryCache.Set(k, fixedFloatSettings); + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/FixedFloatSettings.cs b/Plugins/BTCPayServer.Plugins.FixedFloat/FixedFloatSettings.cs new file mode 100644 index 0000000..f521771 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/FixedFloatSettings.cs @@ -0,0 +1,8 @@ +namespace BTCPayServer.Plugins.FixedFloat +{ + public class FixedFloatSettings + { + public bool Enabled { get; set; } + public decimal AmountMarkupPercentage { get; set; } = 0; + } +} diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/Pack.ps1 b/Plugins/BTCPayServer.Plugins.FixedFloat/Pack.ps1 new file mode 100644 index 0000000..6454eb8 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/Pack.ps1 @@ -0,0 +1,2 @@ +dotnet publish -c Altcoins-Release -o bin/publish/BTCPayServer.Plugins.FixedFloat +dotnet run -p ../../BTCPayServer.PluginPacker bin/publish/BTCPayServer.Plugins.FixedFloat BTCPayServer.Plugins.FixedFloat ../packed diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/Resources/assets/ff.png b/Plugins/BTCPayServer.Plugins.FixedFloat/Resources/assets/ff.png new file mode 100644 index 0000000..2385931 Binary files /dev/null and b/Plugins/BTCPayServer.Plugins.FixedFloat/Resources/assets/ff.png differ diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/Resources/js/fixedFloatComponent.js b/Plugins/BTCPayServer.Plugins.FixedFloat/Resources/js/fixedFloatComponent.js new file mode 100644 index 0000000..fd72222 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/Resources/js/fixedFloatComponent.js @@ -0,0 +1,35 @@ +Vue.component("fixed-float", { + props: ["toCurrency", "toCurrencyDue", "toCurrencyAddress"], + data() { + return { + shown: false, + }; + }, + computed: { + url() { + let settleMethodId = ""; + if ( + this.toCurrency.endsWith("LightningLike") || + this.toCurrency.endsWith("LNURLPay") + ) { + settleMethodId = "BTCLN"; + } else { + settleMethodId = this.toCurrency + .replace("_BTCLike", "") + .replace("_MoneroLike", "") + .replace("_ZcashLike", "") + .toUpperCase(); + } + const topup = this.$parent.srvModel.isUnsetTopUp; + return ( + "https://widget.fixedfloat.com/?" + + `to=${settleMethodId}` + + "&lockReceive=true&ref=fkbyt39c" + + `&address=${this.toCurrencyAddress}` + + (topup + ? "" + : `&lockType=true&hideType=true&lockAmount=true&toAmount=${this.toCurrencyDue}`) + ); + }, + }, +}); diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/UpdateFixedFloatSettingsViewModel.cs b/Plugins/BTCPayServer.Plugins.FixedFloat/UpdateFixedFloatSettingsViewModel.cs new file mode 100644 index 0000000..d27636a --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/UpdateFixedFloatSettingsViewModel.cs @@ -0,0 +1,8 @@ +namespace BTCPayServer.Plugins.FixedFloat +{ + public class UpdateFixedFloatSettingsViewModel + { + public bool Enabled { get; set; } + public string StoreName { get; set; } + } +} diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/Views/FixedFloat/UpdateFixedFloatSettings.cshtml b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/FixedFloat/UpdateFixedFloatSettings.cshtml new file mode 100644 index 0000000..85cd6ad --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/FixedFloat/UpdateFixedFloatSettings.cshtml @@ -0,0 +1,28 @@ +@using BTCPayServer.Abstractions.Extensions +@using Microsoft.AspNetCore.Mvc.TagHelpers +@model BTCPayServer.Plugins.FixedFloat.UpdateFixedFloatSettingsViewModel +@{ + ViewData.SetActivePage("FixedFloat", "FixedFloat", "FixedFloat"); +} + + + +

    @ViewData["Title"]

    + + + +
    +
    +
    +
    +
    + + +
    +
    + +
    +
    +
    diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutContentExtension.cshtml b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutContentExtension.cshtml new file mode 100644 index 0000000..13f46c3 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutContentExtension.cshtml @@ -0,0 +1,19 @@ +@using BTCPayServer.Plugins.FixedFloat +@using Newtonsoft.Json +@using Newtonsoft.Json.Linq +@inject FixedFloatService FixedFloatService +@{ + var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value(); + var settings = await FixedFloatService.GetFixedFloatForStore(storeId); + if (settings?.Enabled is true) + { +
    + + + +
    + } +} diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutEnd.cshtml b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutEnd.cshtml new file mode 100644 index 0000000..1796f6e --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutEnd.cshtml @@ -0,0 +1,12 @@ +@using BTCPayServer.Plugins.FixedFloat +@using Newtonsoft.Json +@using Newtonsoft.Json.Linq +@inject FixedFloatService FixedFloatService +@{ + var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value(); + var settings = await FixedFloatService.GetFixedFloatForStore(storeId); + if (settings?.Enabled is true) + { + + } +} diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutPaymentExtension.cshtml b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutPaymentExtension.cshtml new file mode 100644 index 0000000..2c2e853 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutPaymentExtension.cshtml @@ -0,0 +1,46 @@ +@using BTCPayServer.Plugins.FixedFloat +@using Newtonsoft.Json +@using Newtonsoft.Json.Linq +@inject FixedFloatService FixedFloatService +@{ + var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value(); + var settings = await FixedFloatService.GetFixedFloatForStore(storeId); +} +@if (settings?.Enabled is true) +{ + + +} diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutPaymentMethodExtension.cshtml b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutPaymentMethodExtension.cshtml new file mode 100644 index 0000000..af5b384 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutPaymentMethodExtension.cshtml @@ -0,0 +1,16 @@ +@using BTCPayServer.Plugins.FixedFloat +@using Newtonsoft.Json +@using Newtonsoft.Json.Linq +@inject FixedFloatService FixedFloatService +@{ + const string id = "FixedFloat"; + var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value(); + var settings = await FixedFloatService.GetFixedFloatForStore(storeId); + + if (settings?.Enabled is true) + { + + @id + + } +} diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutTabExtension.cshtml b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutTabExtension.cshtml new file mode 100644 index 0000000..bbb3923 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/CheckoutTabExtension.cshtml @@ -0,0 +1,14 @@ +@using BTCPayServer.Plugins.FixedFloat +@using Newtonsoft.Json +@using Newtonsoft.Json.Linq +@inject FixedFloatService FixedFloatService +@{ + var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value(); + var settings = await FixedFloatService.GetFixedFloatForStore(storeId); + if (settings?.Enabled is true) + { +
    + {{$t("Altcoins (FixedFloat)")}} +
    + } +} diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/FixedFloatNav.cshtml b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/FixedFloatNav.cshtml new file mode 100644 index 0000000..02aa4b6 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/FixedFloatNav.cshtml @@ -0,0 +1,16 @@ +@using BTCPayServer.Abstractions.Contracts +@using BTCPayServer.Abstractions.Extensions +@using Microsoft.AspNetCore.Mvc.TagHelpers +@inject IScopeProvider ScopeProvider +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); +} +@if (!string.IsNullOrEmpty(storeId)) +{ + +} diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/StoreIntegrationFixedFloatOption.cshtml b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/StoreIntegrationFixedFloatOption.cshtml new file mode 100644 index 0000000..00d847a --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/Shared/FixedFloat/StoreIntegrationFixedFloatOption.cshtml @@ -0,0 +1,58 @@ +@using BTCPayServer.Abstractions.Contracts +@using BTCPayServer.Plugins.FixedFloat +@using Microsoft.AspNetCore.Routing +@inject FixedFloatService FixedFloatService +@inject IScopeProvider ScopeProvider +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + + FixedFloatSettings settings = null; + if (!string.IsNullOrEmpty(storeId)) + { + try + { + settings = await FixedFloatService.GetFixedFloatForStore(storeId); + } + catch (Exception) + { + } + } +} +@if (!string.IsNullOrEmpty(storeId)) +{ +
  • +
    + + + FixedFloat + + + Allows your customers to pay with altcoins that are not supported by BTCPay Server. + + + + @if (settings?.Enabled is true) + { + + + Enabled + + | + + Modify + + } + else + { + + + Disabled + + + Setup + + } + +
    +
  • +} diff --git a/Plugins/BTCPayServer.Plugins.FixedFloat/Views/_ViewImports.cshtml b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/_ViewImports.cshtml new file mode 100644 index 0000000..52e6837 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FixedFloat/Views/_ViewImports.cshtml @@ -0,0 +1,2 @@ +@addTagHelper *, BTCPayServer.Abstractions +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/Plugins/BTCPayServer.Plugins.FujiOracle/BTCPayServer.Plugins.FujiOracle.csproj b/Plugins/BTCPayServer.Plugins.FujiOracle/BTCPayServer.Plugins.FujiOracle.csproj new file mode 100644 index 0000000..24991ee --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FujiOracle/BTCPayServer.Plugins.FujiOracle.csproj @@ -0,0 +1,18 @@ + + + net6.0 + true + false + true + 1.0.0 + + + + + + + + + + + diff --git a/Plugins/BTCPayServer.Plugins.FujiOracle/FujiOracleController.cs b/Plugins/BTCPayServer.Plugins.FujiOracle/FujiOracleController.cs new file mode 100644 index 0000000..25b304f --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FujiOracle/FujiOracleController.cs @@ -0,0 +1,330 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Constants; +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Extensions; +using BTCPayServer.Client; +using BTCPayServer.Client.Models; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using Microsoft.VisualBasic; +using NBitcoin; +using NBitcoin.Crypto; +using NBitcoin.DataEncoders; +using NBitcoin.Secp256k1; +using Newtonsoft.Json.Linq; +using Key = NBitcoin.Key; + +namespace BTCPayServer.Plugins.FujiOracle +{ + [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Route("plugins/{storeId}/FujiOracle")] + public class FujiOracleController : Controller + { + + + + private async Task CreateClient(string storeId) + { + return await _btcPayServerClientFactory.Create(null, new[] {storeId}, new DefaultHttpContext() + { + Request = + { + Scheme = "https", + Host = Request.Host, + Path = Request.Path, + PathBase = Request.PathBase + } + }); + } + + + private readonly IHttpClientFactory _httpClientFactory; + private readonly FujiOracleService _FujiOracleService; + private readonly IBTCPayServerClientFactory _btcPayServerClientFactory; + + public FujiOracleController(IHttpClientFactory httpClientFactory, + FujiOracleService FujiOracleService, + IBTCPayServerClientFactory btcPayServerClientFactory) + { + _httpClientFactory = httpClientFactory; + _FujiOracleService = FujiOracleService; + _btcPayServerClientFactory = btcPayServerClientFactory; + } + + [HttpGet("update")] + public async Task UpdateFujiOracleSettings(string storeId) + { + var + FujiOracle = (await _FujiOracleService.GetFujiOracleForStore(storeId)) ?? new(); + + + return View(FujiOracle); + } + + + + [HttpPost("update")] + public async Task UpdateFujiOracleSettings(string storeId, + FujiOracleSettings vm, + string command) + { + if (command == "generate") + { + ModelState.Clear(); + + if (ECPrivKey.TryCreate(new ReadOnlySpan(RandomNumberGenerator.GetBytes(32)), out var key)) + { + vm.Key = key.ToHex(); + } + return View(vm); + } + + if (command == "add-pair") + { + vm.Pairs ??= new List(); + vm.Pairs.Add(""); + return View(vm); + } + + if (command.StartsWith("remove-pair")) + { + var i = int.Parse(command.Substring(command.IndexOf(":", StringComparison.InvariantCultureIgnoreCase) + + 1)); + vm.Pairs.RemoveAt(i); + return View(vm); + } + + + var validPairsToQuery = ""; + for (var i = 0; i < vm.Pairs.Count; i++) + { + string vmPair = vm.Pairs[i]; + if (string.IsNullOrWhiteSpace(vmPair)) + { + + ModelState.AddModelError($"{vm.Pairs}[{i}]", + $"Remove invalid"); + continue; + } + + var split = vmPair.Split("_", StringSplitOptions.RemoveEmptyEntries); + if (split.Length != 2) + { + ModelState.AddModelError($"{vm.Pairs}[{i}]", + $"Invalid format, needs to be BTC_USD format"); + continue; + } + + validPairsToQuery += "," + vmPair; + } + + validPairsToQuery = validPairsToQuery.TrimStart(','); + if (!string.IsNullOrEmpty(validPairsToQuery)) + { + try + { + var url = Request.GetAbsoluteUri(Url.Action("GetRates2", + "BitpayRate", new {storeId, currencyPairs = validPairsToQuery})); + var resp = JArray.Parse(await _httpClientFactory.CreateClient().GetStringAsync(url)); + + for (var i = 0; i < vm.Pairs.Count; i++) + { + + string vmPair = vm.Pairs[i]; + if (!resp.Any(token => token["currencyPair"].Value() == vmPair)) + { + ModelState.AddModelError($"{vm.Pairs}[{i}]", + $"You store could not resolve pair {vmPair}"); + } + } + } + catch (Exception e) + { + } + + } + + if (string.IsNullOrEmpty(vm.Key) && vm.Enabled) + { + + ModelState.AddModelError(nameof(vm.Enabled), + $"Cannot enable without a key"); + } + + if (!string.IsNullOrEmpty(vm.Key)) + { + try + { + if (HexEncoder.IsWellFormed(vm.Key)) + { + ECPrivKey.Create(Encoders.Hex.DecodeData(vm.Key)); + } + } + catch (Exception e) + { + + ModelState.AddModelError(nameof(vm.Enabled), + $"Key was invalid"); + } + } + if (!ModelState.IsValid) + { + return View(vm); + } + + + switch (command?.ToLowerInvariant()) + { + case "save": + await _FujiOracleService.SetFujiOracleForStore(storeId, vm); + TempData["SuccessMessage"] = "FujiOracle settings modified"; + return RedirectToAction(nameof(UpdateFujiOracleSettings), new {storeId}); + + default: + return View(vm); + } + } + + [AllowAnonymous] + [HttpGet("")] + public async Task GetOracleInfo(string storeId) + { + var oracle = await _FujiOracleService.GetFujiOracleForStore(storeId); + if (oracle is null || !oracle.Enabled || oracle.Key is null) + { + return NotFound(); + } + + return Ok(new + { + publicKey = new Key(Encoders.Hex.DecodeData(oracle.Key)).PubKey.ToHex(), + availableTickers = oracle.Pairs.ToArray() + }); + } + [AllowAnonymous] + [HttpGet("{pair}")] + public async Task GetOracleAttestation(string storeId, string pair) + { + var oracle = await _FujiOracleService.GetFujiOracleForStore(storeId); + if (oracle is null || !oracle.Enabled || oracle.Key is null || !oracle.Pairs.Contains(pair)) + { + return NotFound(); + } + var url = Request.GetAbsoluteUri(Url.Action("GetRates2", + "BitpayRate", new {storeId, currencyPairs = pair})); + var resp = JArray.Parse(await _httpClientFactory.CreateClient().GetStringAsync(url)).First(); + var ts = DateTimeOffset.Now.ToUnixTimeSeconds(); + var rate =(long)decimal.Truncate( resp["rate"].Value()); + var messageBytes = BitConverter.GetBytes(ts).Concat(BitConverter.GetBytes(rate)).ToArray(); + using var sha256Hash = System.Security.Cryptography.SHA256.Create(); + var messageHash = sha256Hash.ComputeHash(messageBytes); + var key = Extensions.ParseKey(oracle.Key); + var buf = new byte[64]; + key.SignBIP340(messageHash).WriteToSpan(buf); + var sig = buf.ToHex(); + + return Ok(new + { + timestamp = ts.ToString(), + lastPrice = rate.ToString(), + attestation = new { + signature= sig, + message= messageBytes.ToHex(), + messageHash= messageHash.ToHex() + }, + }); + } + } + + public static class Extensions + { + public static ECPrivKey ParseKey(string key) + { + return ECPrivKey.Create(key.DecodHexData()); + } + + public static byte[] DecodHexData(this string encoded) + { + if (encoded == null) + throw new ArgumentNullException(nameof(encoded)); + if (encoded.Length % 2 == 1) + throw new FormatException("Invalid Hex String"); + + var result = new byte[encoded.Length / 2]; + for (int i = 0, j = 0; i < encoded.Length; i += 2, j++) + { + var a = IsDigit(encoded[i]); + var b = IsDigit(encoded[i + 1]); + if (a == -1 || b == -1) + throw new FormatException("Invalid Hex String"); + result[j] = (byte)(((uint)a << 4) | (uint)b); + } + + return result; + } + + public static int IsDigit(this char c) + { + if ('0' <= c && c <= '9') + { + return c - '0'; + } + else if ('a' <= c && c <= 'f') + { + return c - 'a' + 10; + } + else if ('A' <= c && c <= 'F') + { + return c - 'A' + 10; + } + else + { + return -1; + } + } + + public static string ToHex(this byte[] bytes) + { + var builder = new StringBuilder(); + foreach (var t in bytes) + { + builder.Append(t.ToHex()); + } + + return builder.ToString(); + } + + private static string ToHex(this byte b) + { + return b.ToString("x2"); + } + + public static string ToHex(this Span bytes) + { + var builder = new StringBuilder(); + foreach (var t in bytes) + { + builder.Append(t.ToHex()); + } + + return builder.ToString(); + } + + public static string ToHex(this ECPrivKey key) + { + Span output = new(new byte[32]); + key.WriteToSpan(output); + return output.ToHex(); + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.FujiOracle/FujiOraclePlugin.cs b/Plugins/BTCPayServer.Plugins.FujiOracle/FujiOraclePlugin.cs new file mode 100644 index 0000000..4fdd2fb --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FujiOracle/FujiOraclePlugin.cs @@ -0,0 +1,32 @@ +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Models; +using BTCPayServer.Abstractions.Services; +using Microsoft.Extensions.DependencyInjection; + +namespace BTCPayServer.Plugins.FujiOracle +{ + public class FujiOraclePlugin : BaseBTCPayServerPlugin + { + public override string Identifier => "BTCPayServer.Plugins.FujiOracle"; + public override string Name => "Fuji Oracle"; + + + public override IBTCPayServerPlugin.PluginDependency[] Dependencies { get; } = + { + new() { Identifier = nameof(BTCPayServer), Condition = ">=1.6.0.0" } + }; + + public override string Description => + "Allows you to become an oracle for the fuji.money platform"; + + public override void Execute(IServiceCollection applicationBuilder) + { + applicationBuilder.AddSingleton(); + applicationBuilder.AddSingleton(new UIExtension("FujiOracle/StoreIntegrationFujiOracleOption", + "store-integrations-list")); + applicationBuilder.AddSingleton(new UIExtension("FujiOracle/FujiOracleNav", + "store-integrations-nav")); + base.Execute(applicationBuilder); + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.FujiOracle/FujiOracleService.cs b/Plugins/BTCPayServer.Plugins.FujiOracle/FujiOracleService.cs new file mode 100644 index 0000000..c2ef3fe --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FujiOracle/FujiOracleService.cs @@ -0,0 +1,37 @@ +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Contracts; +using Microsoft.Extensions.Caching.Memory; + +namespace BTCPayServer.Plugins.FujiOracle +{ + public class FujiOracleService + { + private readonly IMemoryCache _memoryCache; + private readonly IStoreRepository _storeRepository; + + public FujiOracleService(IMemoryCache memoryCache, IStoreRepository storeRepository) + { + _memoryCache = memoryCache; + _storeRepository = storeRepository; + } + + public async Task GetFujiOracleForStore(string storeId) + { + var k = $"{nameof(FujiOracleSettings)}_{storeId}"; + return await _memoryCache.GetOrCreateAsync(k, async _ => + { + var res = await _storeRepository.GetSettingAsync(storeId, + nameof(FujiOracleSettings)); + return res; + }); + } + + public async Task SetFujiOracleForStore(string storeId, FujiOracleSettings FujiOracleSettings) + { + var k = $"{nameof(FujiOracleSettings)}_{storeId}"; + + await _storeRepository.UpdateSetting(storeId, nameof(FujiOracleSettings), FujiOracleSettings); + _memoryCache.Set(k, FujiOracleSettings); + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.FujiOracle/FujiOracleSettings.cs b/Plugins/BTCPayServer.Plugins.FujiOracle/FujiOracleSettings.cs new file mode 100644 index 0000000..e883021 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FujiOracle/FujiOracleSettings.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace BTCPayServer.Plugins.FujiOracle +{ + public class FujiOracleSettings + { + public bool Enabled { get; set; } + public string Key { get; set; } + public List Pairs { get; set; } = new(); + } +} diff --git a/Plugins/BTCPayServer.Plugins.FujiOracle/Pack.ps1 b/Plugins/BTCPayServer.Plugins.FujiOracle/Pack.ps1 new file mode 100644 index 0000000..9b6d2a6 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FujiOracle/Pack.ps1 @@ -0,0 +1,2 @@ +dotnet publish -c Altcoins-Release -o bin/publish/BTCPayServer.Plugins.FujiOracle +dotnet run -p ../../BTCPayServer.PluginPacker bin/publish/BTCPayServer.Plugins.FujiOracle BTCPayServer.Plugins.FujiOracle ../packed diff --git a/Plugins/BTCPayServer.Plugins.FujiOracle/Views/FujiOracle/UpdateFujioracleSettings.cshtml b/Plugins/BTCPayServer.Plugins.FujiOracle/Views/FujiOracle/UpdateFujioracleSettings.cshtml new file mode 100644 index 0000000..d3b51bf --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FujiOracle/Views/FujiOracle/UpdateFujioracleSettings.cshtml @@ -0,0 +1,55 @@ +@using BTCPayServer.Abstractions.Extensions +@using Microsoft.AspNetCore.Mvc.TagHelpers +@using BTCPayServer.Abstractions.Contracts +@model BTCPayServer.Plugins.FujiOracle.FujiOracleSettings +@inject IScopeProvider ScopeProvider +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + Layout = "../Shared/_NavLayout.cshtml"; + ViewData["NavPartialName"] = "../UIStores/_Nav"; + ViewData.SetActivePage("Fuji Oracle", "Update Fuji Oracle Settings", null); + +} + +

    @ViewData["Title"]

    +
    +
    +
    +
    + +
    + + +
    + + +
    + + +
    +
    + + +
    + +
    +
    + Pairs +
    + + @for (var index = 0; index < Model.Pairs.Count; index++) + { +
    + + + +
    + } +
    +
    + +
    +
    +
    +
    + diff --git a/Plugins/BTCPayServer.Plugins.FujiOracle/Views/Shared/FujiOracle/FujiOracleNav.cshtml b/Plugins/BTCPayServer.Plugins.FujiOracle/Views/Shared/FujiOracle/FujiOracleNav.cshtml new file mode 100644 index 0000000..ca944fc --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FujiOracle/Views/Shared/FujiOracle/FujiOracleNav.cshtml @@ -0,0 +1,19 @@ +@inject IScopeProvider ScopeProvider +@using BTCPayServer.Abstractions.Contracts +@using BTCPayServer.Plugins.FujiOracle +@using Microsoft.AspNetCore.Mvc.TagHelpers +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + var isActive = !string.IsNullOrEmpty(storeId) && ViewContext.RouteData.Values.TryGetValue("Controller", out var controller) && controller is not null && + nameof(FujiOracleController).StartsWith(controller?.ToString(), StringComparison.InvariantCultureIgnoreCase); +} +@if (!string.IsNullOrEmpty(storeId)) +{ + +} diff --git a/Plugins/BTCPayServer.Plugins.FujiOracle/Views/Shared/FujiOracle/StoreIntegrationFujiOracleOption.cshtml b/Plugins/BTCPayServer.Plugins.FujiOracle/Views/Shared/FujiOracle/StoreIntegrationFujiOracleOption.cshtml new file mode 100644 index 0000000..bf00734 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FujiOracle/Views/Shared/FujiOracle/StoreIntegrationFujiOracleOption.cshtml @@ -0,0 +1,58 @@ + +@using BTCPayServer.Abstractions.Contracts +@using BTCPayServer.Plugins.FujiOracle +@inject IScopeProvider ScopeProvider +@inject FujiOracleService FujiOracleService +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + + FujiOracleSettings settings = null; + if (!string.IsNullOrEmpty(storeId)) + { + try + { + settings = await FujiOracleService.GetFujiOracleForStore(storeId); + } + catch (Exception) + { + } + } +} +@if (!string.IsNullOrEmpty(storeId)) +{ +
  • +
    + + + Ticket Tailor + + + Sell tickets on Ticket Tailor using BTCPay Server + + + + @if (settings?.Enabled is true) + { + + + Active + + | + + Modify + + } + else + { + + + Disabled + + + Setup + + } + +
    +
  • +} diff --git a/Plugins/BTCPayServer.Plugins.FujiOracle/Views/_ViewImports.cshtml b/Plugins/BTCPayServer.Plugins.FujiOracle/Views/_ViewImports.cshtml new file mode 100644 index 0000000..04173ab --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.FujiOracle/Views/_ViewImports.cshtml @@ -0,0 +1,4 @@ +@using BTCPayServer.Abstractions.Services + +@inject Safe Safe +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/Plugins/BTCPayServer.Plugins.LSP/BTCPayServer.Plugins.LSP.csproj b/Plugins/BTCPayServer.Plugins.LSP/BTCPayServer.Plugins.LSP.csproj new file mode 100644 index 0000000..b48d219 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LSP/BTCPayServer.Plugins.LSP.csproj @@ -0,0 +1,19 @@ + + + net6.0 + true + false + true + 1.0.0 + + + + + + + + + + + + diff --git a/Plugins/BTCPayServer.Plugins.LSP/LSPController.cs b/Plugins/BTCPayServer.Plugins.LSP/LSPController.cs new file mode 100644 index 0000000..0a043be --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LSP/LSPController.cs @@ -0,0 +1,301 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Constants; +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Extensions; +using BTCPayServer.Client; +using BTCPayServer.Client.Models; +using BTCPayServer.Lightning; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; +using NBitcoin; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Plugins.LSP +{ + [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Route("plugins/{storeId}/LSP")] + public class LSPController : Controller + { + [AllowAnonymous] + [HttpGet("")] + public async Task View(string storeId) + { + var config = await _LSPService.GetLSPForStore(storeId); + try + { + if (config?.Enabled is true) + { + return View(new LSPViewModel() {Settings = config}); + } + } + catch (Exception e) + { + // ignored + } + + return NotFound(); + } + + + [AllowAnonymous] + [HttpPost("")] + public async Task Purchase(string storeId, string email, uint inbound, bool privateChannel) + { + var config = await _LSPService.GetLSPForStore(storeId); + try + { + if (config?.Enabled is not true || string.IsNullOrEmpty(email) || inbound < config.Minimum || + inbound > config.Maximum) + { + return RedirectToAction("View", new {storeId}); + } + + var price = Math.Ceiling((config.FeePerSat == 0 ? 0 : (config.FeePerSat * inbound)) + config.BaseFee); + var btcpayClient = await CreateClient(storeId); + var redirectUrl = Request.GetAbsoluteUri(Url.Action("Connect", + "LSP", new {storeId, invoiceId = "kukkskukkskukks"})); + redirectUrl = redirectUrl.Replace("kukkskukkskukks", "{InvoiceId}"); + var inv = await btcpayClient.CreateInvoice(storeId, + new CreateInvoiceRequest() + { + Amount = price, + Currency = "sats", + Type = InvoiceType.Standard, + AdditionalSearchTerms = new[] {"LSP"}, + Checkout = + { + RequiresRefundEmail = true, + RedirectAutomatically = price > 0, + RedirectURL = redirectUrl, + }, + Metadata = JObject.FromObject(new + { + buyerEmail = email, + privateChannel, + inbound, + config.BaseFee, + config.FeePerSat, + orderId = "LSP" + }) + }); + + while (inv.Amount == 0 && inv.Status == InvoiceStatus.New) + { + if (inv.Status == InvoiceStatus.New) + inv = await btcpayClient.GetInvoice(inv.StoreId, inv.Id); + } + + if (inv.Status == InvoiceStatus.Settled) + return RedirectToAction("Connect", new {storeId, invoiceId = inv.Id}); + return Redirect(inv.CheckoutLink); + } + catch (Exception e) + { + } + + return RedirectToAction("View", new {storeId}); + } + + + [AllowAnonymous] + [HttpGet("connect")] + public async Task Connect(string storeId, string invoiceId) + { + var btcpayClient = await CreateClient(storeId); + try + { + var config = await _LSPService.GetLSPForStore(storeId); + var result = new LSPConnectPage() {InvoiceId = invoiceId, Settings = config}; + var invoice = await btcpayClient.GetInvoice(storeId, invoiceId); + result.Status = invoice.Status; + if (invoice.Status != InvoiceStatus.Settled) return View(result); + if (invoice.Metadata.TryGetValue("lsp-channel-complete", out _)) + { + return Redirect(invoice.CheckoutLink); + } + + + result.Invoice = invoice; + result.LNURL = LNURL.LNURL.EncodeUri(new Uri(Request.GetAbsoluteUri(Url.Action( + "LNURLChannelRequest", + "LSP", new {storeId, invoiceId}))), "channelRequest", true).ToString(); + + return View(result); + } + catch (Exception e) + { + return NotFound(); + } + } + + private async Task CreateClient(string storeId) + { + return await _btcPayServerClientFactory.Create(null, new[] {storeId}, + new DefaultHttpContext() + { + Request = + { + Scheme = "https", Host = Request.Host, Path = Request.Path, PathBase = Request.PathBase + } + }); + } + + public class LSPConnectPage + { + public string LNURL; + public string InvoiceId { get; set; } + public InvoiceStatus Status { get; set; } + public LSPSettings Settings { get; set; } + public InvoiceData Invoice { get; set; } + } + + private readonly LSPService _LSPService; + private readonly IBTCPayServerClientFactory _btcPayServerClientFactory; + + public LSPController(IHttpClientFactory httpClientFactory, + LSPService LSPService, + IBTCPayServerClientFactory btcPayServerClientFactory) + { + _LSPService = LSPService; + _btcPayServerClientFactory = btcPayServerClientFactory; + } + + [HttpGet("update")] + public async Task UpdateLSPSettings(string storeId) + { + LSPSettings vm = null; + try + { + vm = await _LSPService.GetLSPForStore(storeId); + } + catch (Exception) + { + // ignored + } + + vm ??= new(); + + return View(vm); + } + + [HttpPost("update")] + public async Task UpdateLSPSettings(string storeId, + LSPSettings vm, + string command) + { + if (!ModelState.IsValid) + { + return View(vm); + } + + + switch (command?.ToLowerInvariant()) + { + case "save": + await _LSPService.SetLSPForStore(storeId, vm); + TempData["SuccessMessage"] = "LSP settings modified"; + return RedirectToAction(nameof(UpdateLSPSettings), new {storeId}); + + default: + return View(vm); + } + } + + + [AllowAnonymous] + [HttpGet("lnurlc-callback")] + public async Task LNURLChannelRequestCallback(string storeId, string k1, string remoteId) + { + if (!NodeInfo.TryParse(remoteId, out var remoteNode)) + { + return BadRequest(); + } + var btcPayClient = await CreateClient(storeId); + var invoice = await btcPayClient.GetInvoice(storeId, k1); + if (invoice?.Status != InvoiceStatus.Settled || invoice.Metadata.TryGetValue("lsp-channel-complete", out _)) + { + return NotFound(); + } + var settings = await _LSPService.GetLSPForStore(storeId); + if (settings?.Enabled is not true) + { + return BadRequest(); + } + if (!invoice.Metadata.TryGetValue("posData", out var posData)) + { + posData = JToken.Parse("{}"); + } + + var inbound = invoice.Metadata["inbound"].Value(); + try + { + await btcPayClient.ConnectToLightningNode(storeId, "BTC", new ConnectToNodeRequest(remoteNode)); + posData["LSP"] = JToken.FromObject(new Dictionary()); + posData["LSP"]["Remote Node"] = remoteId; + await btcPayClient.OpenLightningChannel(storeId, "BTC", new OpenLightningChannelRequest() + { + ChannelAmount = new Money(inbound, MoneyUnit.Satoshi), + + NodeURI = remoteNode + }); + posData["LSP"]["Channel Status"] = "Opened"; + invoice.Metadata["posData"] = posData; + invoice.Metadata["lsp-channel-complete"] = true; + await btcPayClient.UpdateInvoice(storeId, invoice.Id, + new UpdateInvoiceRequest() {Metadata = invoice.Metadata}); + return Ok(new LNURL.LNUrlStatusResponse() + { + Status = "OK" + }); + } + catch (Exception e) + { + posData["Error"] = + $"Channel could not be created. You should refund customer.{Environment.NewLine}{e.Message}"; + invoice.Metadata["posData"] = posData; + await btcPayClient.UpdateInvoice(storeId, invoice.Id, + new UpdateInvoiceRequest() {Metadata = invoice.Metadata}); + return Ok(new LNURL.LNUrlStatusResponse() + { + Status = "ERROR", Reason = $"Channel could not be created to {remoteId}" + }); + + } + } + + [AllowAnonymous] + [HttpGet("{invoiceId}/lnurlc")] + public async Task LNURLChannelRequest(string storeId, string invoiceId, string nodeUri) + { + var btcPayClient = await CreateClient(storeId); + var invoice = await btcPayClient.GetInvoice(storeId, invoiceId); + if (invoice?.Status != InvoiceStatus.Settled || invoice.Metadata.TryGetValue("lsp-channel-complete", out _)) + { + return NotFound(); + } + var settings = await _LSPService.GetLSPForStore(storeId); + if (settings?.Enabled is not true) + { + return BadRequest(); + } + return Ok(new LNURL.LNURLChannelRequest() + { + Tag = "channelRequest", + K1 = invoiceId, + Callback = new Uri(Request.GetAbsoluteUri(Url.Action("LNURLChannelRequestCallback", + "LSP", new {storeId}))), + Uri = nodeUri is null + ? (await btcPayClient.GetLightningNodeInfo(storeId, "BTC")).NodeURIs + .OrderBy(nodeInfo => nodeInfo.IsTor).First() + : NodeInfo.Parse(nodeUri) + }); + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.LSP/LSPPlugin.cs b/Plugins/BTCPayServer.Plugins.LSP/LSPPlugin.cs new file mode 100644 index 0000000..7d49164 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LSP/LSPPlugin.cs @@ -0,0 +1,31 @@ +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Models; +using BTCPayServer.Abstractions.Services; +using Microsoft.Extensions.DependencyInjection; + +namespace BTCPayServer.Plugins.LSP +{ + public class LSPPlugin : BaseBTCPayServerPlugin + { + public override string Identifier => "BTCPayServer.Plugins.LSP"; + public override string Name => "LSP"; + + public override IBTCPayServerPlugin.PluginDependency[] Dependencies { get; } = + { + new() {Identifier = nameof(BTCPayServer), Condition = ">=1.6.0.0"} + }; + + public override string Description => + "Allows you to become an LSP selling lightning channels with inbound liquidity"; + + public override void Execute(IServiceCollection applicationBuilder) + { + applicationBuilder.AddSingleton(); + applicationBuilder.AddSingleton(new UIExtension("LSP/StoreIntegrationLSPOption", + "store-integrations-list")); + applicationBuilder.AddSingleton(new UIExtension("LSP/LSPNav", + "store-integrations-nav")); + base.Execute(applicationBuilder); + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.LSP/LSPService.cs b/Plugins/BTCPayServer.Plugins.LSP/LSPService.cs new file mode 100644 index 0000000..8467cb0 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LSP/LSPService.cs @@ -0,0 +1,37 @@ +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Contracts; +using Microsoft.Extensions.Caching.Memory; + +namespace BTCPayServer.Plugins.LSP; + +public class LSPService +{ + private readonly IMemoryCache _memoryCache; + private readonly IStoreRepository _storeRepository; + + public LSPService(IMemoryCache memoryCache, + IStoreRepository storeRepository) + { + _memoryCache = memoryCache; + _storeRepository = storeRepository; + } + + + public async Task GetLSPForStore(string storeId) + { + var k = $"{nameof(LSPSettings)}_{storeId}"; + return await _memoryCache.GetOrCreateAsync(k, async _ => + { + var res = await _storeRepository.GetSettingAsync(storeId, + nameof(LSPSettings)); + return res; + }); + } + + public async Task SetLSPForStore(string storeId, LSPSettings lspSettings) + { + var k = $"{nameof(LSPSettings)}_{storeId}"; + await _storeRepository.UpdateSetting(storeId, nameof(LSPSettings), lspSettings); + _memoryCache.Set(k, lspSettings); + } +} diff --git a/Plugins/BTCPayServer.Plugins.LSP/Pack.ps1 b/Plugins/BTCPayServer.Plugins.LSP/Pack.ps1 new file mode 100644 index 0000000..2b22d27 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LSP/Pack.ps1 @@ -0,0 +1,2 @@ +dotnet publish -c Altcoins-Release -o bin/publish/BTCPayServer.Plugins.LSP +dotnet run -p ../../BTCPayServer.PluginPacker bin/publish/BTCPayServer.Plugins.LSP BTCPayServer.Plugins.LSP ../packed diff --git a/Plugins/BTCPayServer.Plugins.LSP/UpdateLSPViewModel.cs b/Plugins/BTCPayServer.Plugins.LSP/UpdateLSPViewModel.cs new file mode 100644 index 0000000..c8455ff --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LSP/UpdateLSPViewModel.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc.Rendering; + +namespace BTCPayServer.Plugins.LSP; + +public class LSPSettings +{ + public bool Enabled { get; set; } = true; + public long Minimum { get; set; } = 100000; + public long Maximum { get; set; } = 10000000; + public decimal FeePerSat { get; set; } = 0.01m; + public long BaseFee { get; set; } = 0; + public string CustomCSS { get; set; } + public string Title { get; set; } = "Lightning Liquidity Peddler"; + public string Description { get; set; } = "

    Get an inbound channel

    This will open a public channel to your node.

    "; +} + +public class LSPViewModel +{ + public LSPSettings Settings { get; set; } +} diff --git a/Plugins/BTCPayServer.Plugins.LSP/Views/LSP/Connect.cshtml b/Plugins/BTCPayServer.Plugins.LSP/Views/LSP/Connect.cshtml new file mode 100644 index 0000000..b1db246 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LSP/Views/LSP/Connect.cshtml @@ -0,0 +1,73 @@ +@using BTCPayServer.Client.Models +@model BTCPayServer.Plugins.LSP.LSPController.LSPConnectPage +@inject ContentSecurityPolicies contentSecurityPolicies +@using BTCPayServer.Security +@using NBitcoin +@{ + var nonce = RandomUtils.GetUInt256().ToString().Substring(0, 32); + contentSecurityPolicies.Add("script-src", $"'nonce-{nonce}'"); + contentSecurityPolicies.AllowUnsafeHashes(); + Layout = "_LayoutSimple"; + var reloadPage = false; +} + + +
    +
    + +
    + +

    Thank you!

    +
    + @if (Model.Status == InvoiceStatus.Processing) + { + reloadPage = true; +
    + The invoice has detected a payment but is still waiting to be settled. This page will refresh periodically until it is settled. +
    + } + else if (Model.Status != InvoiceStatus.Settled) + { +
    + The invoice is not settled. +
    + } + else + { + Model.Invoice.Metadata.TryGetValue("inbound", out var inbound); +
    +
    + @await Component.InvokeAsync("QRCode", new {data = Model.LNURL.ToUpperInvariant()}) +
    + +

    Scan this QR with your wallet to proceed with opening the channel.

    + Open in wallet +

    Opening a channel of at least @inbound.ToString() sats.

    +
    + } +
    +
    + Powered by BTCPay Server +
    +
    +
    +
    + +@if (reloadPage) +{ + +} diff --git a/Plugins/BTCPayServer.Plugins.LSP/Views/LSP/UpdateLSPSettings.cshtml b/Plugins/BTCPayServer.Plugins.LSP/Views/LSP/UpdateLSPSettings.cshtml new file mode 100644 index 0000000..d13b797 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LSP/Views/LSP/UpdateLSPSettings.cshtml @@ -0,0 +1,78 @@ +@using BTCPayServer.Abstractions.Extensions +@using Microsoft.AspNetCore.Mvc.TagHelpers +@using BTCPayServer.Abstractions.Contracts +@model BTCPayServer.Plugins.LSP.LSPSettings +@inject IScopeProvider ScopeProvider +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + Layout = "../Shared/_NavLayout.cshtml"; + ViewData["NavPartialName"] = "../UIStores/_Nav"; + ViewData.SetActivePage("LSP", "Update Store LSP Settings", null); +} + +

    @ViewData["Title"]

    +
    +
    +
    +
    +
    + + + +
    +
    + + + +
    + +
    + + + +
    +
    + + + +
    +
    +
    + + + +
    +
    + + + +
    +
    + + + +
    +
    + + + +
    +
    +
    + + @if (this.ViewContext.ModelState.IsValid && Model.Enabled) + { + + Purchase page + + } +
    +
    +
    +
    + +@section PageFootContent { + + + +} diff --git a/Plugins/BTCPayServer.Plugins.LSP/Views/LSP/View.cshtml b/Plugins/BTCPayServer.Plugins.LSP/Views/LSP/View.cshtml new file mode 100644 index 0000000..da04647 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LSP/Views/LSP/View.cshtml @@ -0,0 +1,88 @@ +@using Microsoft.AspNetCore.Routing +@using BTCPayServer.Plugins.LSP +@model BTCPayServer.Plugins.LSP.LSPViewModel +@inject ContentSecurityPolicies contentSecurityPolicies +@inject IScopeProvider ScopeProvider +@using BTCPayServer.Security +@using NBitcoin +@using BTCPayServer.Abstractions.Contracts +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + var nonce = RandomUtils.GetUInt256().ToString().Substring(0, 32); + contentSecurityPolicies.Add("script-src", $"'nonce-{nonce}'"); + contentSecurityPolicies.AllowUnsafeHashes(); + Layout = "_LayoutSimple"; + +} + + + +
    +
    + + +

    @Model.Settings.Title

    + @if (!string.IsNullOrEmpty(Model.Settings.Description)) + { +
    +
    @Safe.Raw(Model.Settings.Description)
    +
    + } +
    +
    + +
    +
    + + +
    +
    +
    +
    + + +
    +
    + +
    +

    Base fee: @Model.Settings.BaseFee sats, fee per inbound sat: @Model.Settings.FeePerSat sats

    +

    +
    +
    + +
    +
    +
    + + +
    +
    + Powered by BTCPay Server +
    +
    +
    +
    diff --git a/Plugins/BTCPayServer.Plugins.LSP/Views/Shared/LSP/LSPNav.cshtml b/Plugins/BTCPayServer.Plugins.LSP/Views/Shared/LSP/LSPNav.cshtml new file mode 100644 index 0000000..7edfce4 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LSP/Views/Shared/LSP/LSPNav.cshtml @@ -0,0 +1,19 @@ +@using BTCPayServer.Plugins.LSP +@using Microsoft.AspNetCore.Mvc.TagHelpers +@using BTCPayServer.Abstractions.Contracts +@inject IScopeProvider ScopeProvider +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + var isActive = !string.IsNullOrEmpty(storeId) && ViewContext.RouteData.Values.TryGetValue("Controller", out var controller) && controller is not null && + nameof(LSPController).StartsWith(controller?.ToString(), StringComparison.InvariantCultureIgnoreCase); +} +@if (!string.IsNullOrEmpty(storeId)) +{ + +} diff --git a/Plugins/BTCPayServer.Plugins.LSP/Views/Shared/LSP/StoreIntegrationLSPOption.cshtml b/Plugins/BTCPayServer.Plugins.LSP/Views/Shared/LSP/StoreIntegrationLSPOption.cshtml new file mode 100644 index 0000000..f245538 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LSP/Views/Shared/LSP/StoreIntegrationLSPOption.cshtml @@ -0,0 +1,61 @@ +@using BTCPayServer.Client +@using BTCPayServer.Plugins.LSP +@using Microsoft.AspNetCore.Mvc.TagHelpers +@using Microsoft.AspNetCore.Routing +@using BTCPayServer.Abstractions.Contracts +@inject BTCPayServerClient BTCPayServerClient +@inject LSPService LSPService +@inject IScopeProvider ScopeProvider +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + + LSPSettings settings = null; + if (!string.IsNullOrEmpty(storeId)) + { + try + { + settings = await LSPService.GetLSPForStore(storeId); + } + catch (Exception) + { + } + } +} +@if (!string.IsNullOrEmpty(storeId)) +{ +
  • +
    + + + LSP + + + Sell lightning channel inbound liquidity using BTCPay Server + + + + @if (settings?.Enabled is true) + { + + + Active + + | + + Modify + + } + else + { + + + Disabled + + + Setup + + } + +
    +
  • +} diff --git a/Plugins/BTCPayServer.Plugins.LSP/Views/_ViewImports.cshtml b/Plugins/BTCPayServer.Plugins.LSP/Views/_ViewImports.cshtml new file mode 100644 index 0000000..7ece6c4 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LSP/Views/_ViewImports.cshtml @@ -0,0 +1,4 @@ +@using BTCPayServer.Abstractions.Services +@using BTCPayServer.Abstractions.Extensions +@inject Safe Safe +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/Plugins/BTCPayServer.Plugins.LiquidPlus/BTCPayServer.Plugins.LiquidPlus.csproj b/Plugins/BTCPayServer.Plugins.LiquidPlus/BTCPayServer.Plugins.LiquidPlus.csproj new file mode 100644 index 0000000..207b665 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LiquidPlus/BTCPayServer.Plugins.LiquidPlus.csproj @@ -0,0 +1,39 @@ + + + net6.0 + 10 + + + + + "Liquid+ + Enhanced support for the liquid network. + Kukks + 1.0.8 + + + + + true + false + true + + + + + + StaticWebAssetsEnabled=false + false + runtime;native;build;buildTransitive;contentFiles + + + + + + + + + + + + diff --git a/Plugins/BTCPayServer.Plugins.LiquidPlus/Controllers/CustomLiquidAssetsController.cs b/Plugins/BTCPayServer.Plugins.LiquidPlus/Controllers/CustomLiquidAssetsController.cs new file mode 100644 index 0000000..7913e2f --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LiquidPlus/Controllers/CustomLiquidAssetsController.cs @@ -0,0 +1,114 @@ +using System; +using System.Globalization; +using System.Net.Http; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Constants; +using BTCPayServer.Plugins.LiquidPlus.Models; +using BTCPayServer.Plugins.LiquidPlus.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using NBitcoin; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Plugins.LiquidPlus.Controllers +{ + [Route("plugins/liquid/admin-settings")] + [Authorize(Policy = BTCPayServer.Client.Policies.CanModifyServerSettings, + AuthenticationSchemes = AuthenticationSchemes.Cookie)] + public class CustomLiquidAssetsController : Controller + { + private readonly CustomLiquidAssetsRepository _liquidAssetsRepository; + private readonly IHttpClientFactory _httpClientFactory; + + public CustomLiquidAssetsController(CustomLiquidAssetsRepository liquidAssetsRepository, + IHttpClientFactory httpClientFactory) + { + _liquidAssetsRepository = liquidAssetsRepository; + _httpClientFactory = httpClientFactory; + } + + [HttpGet("")] + public IActionResult Assets() + { + return View(new CustomLiquidAssetsViewModel() + { + Items = (_liquidAssetsRepository.Get()).Items, + PendingChanges = _liquidAssetsRepository.ChangesPending + }); + } + + [HttpPost("")] + public async Task Assets(CustomLiquidAssetsViewModel model, string command = null, + string import = null) + { + if (command == "add") + { + ModelState.Clear(); + model.Items.Add(new CustomLiquidAssetsSettings.LiquidAssetConfiguration()); + return View(model); + } + + if (command?.StartsWith("remove", StringComparison.InvariantCultureIgnoreCase) is true) + { + ModelState.Clear(); + var index = int.Parse( + command.Substring(command.IndexOf(":", StringComparison.InvariantCultureIgnoreCase) + 1), + CultureInfo.InvariantCulture); + model.Items.RemoveAt(index); + return View(model); + } + + if (import != null) + { + try + { + if (!uint256.TryParse(import, out var _)) + { + TempData["ErrorMessage"] = + "Asset Id to import was invalid."; + return View(model); + } + + var data = JObject.Parse(await _httpClientFactory.CreateClient() + .GetStringAsync($"https://blockstream.info/liquid/api/asset/{import}")); + + model.Items.Add(new CustomLiquidAssetsSettings.LiquidAssetConfiguration() + { + DisplayName = data["name"].Value(), + Divisibility = data["precision"].Value(), + AssetId = data["asset_id"].Value(), + CryptoCode = data["ticker"].Value().Replace("-", "").Replace("_", "") + }); + } + catch (Exception) + { + TempData["ErrorMessage"] = + "Asset Id to import was invalid."; + return View(model); + } + } + + for (int i = 0; i < model.Items.Count; i++) + { + if (!string.IsNullOrEmpty(model.Items[i].AssetId) && + !uint256.TryParse(model.Items[i].AssetId, out var x)) + { + var inputName = + string.Format(CultureInfo.InvariantCulture, "Items[{0}].", + i.ToString(CultureInfo.InvariantCulture)) + + nameof(CustomLiquidAssetsSettings.LiquidAssetConfiguration.AssetId); + + ModelState.AddModelError(inputName, "Invalid asset id format"); + } + } + + if (!ModelState.IsValid) + { + return View(model); + } + + await _liquidAssetsRepository.Set(model); + return RedirectToAction(nameof(Assets)); + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.LiquidPlus/Controllers/StoreLiquidController.cs b/Plugins/BTCPayServer.Plugins.LiquidPlus/Controllers/StoreLiquidController.cs new file mode 100644 index 0000000..cac0edd --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LiquidPlus/Controllers/StoreLiquidController.cs @@ -0,0 +1,264 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Constants; +using BTCPayServer.Abstractions.Extensions; +using BTCPayServer.Abstractions.Models; +using BTCPayServer.Client; +using BTCPayServer.Common; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using NBitcoin; +using NBXplorer; + +namespace BTCPayServer.Plugins.LiquidPlus.Controllers +{ + [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [AutoValidateAntiforgeryToken] + public class StoreLiquidController : Controller + { + private readonly BTCPayNetworkProvider _btcPayNetworkProvider; + private readonly BTCPayServerClient _client; + private readonly IExplorerClientProvider _explorerClientProvider; + + public StoreLiquidController(BTCPayNetworkProvider btcPayNetworkProvider, + BTCPayServerClient client, IExplorerClientProvider explorerClientProvider) + { + _btcPayNetworkProvider = btcPayNetworkProvider; + _client = client; + _explorerClientProvider = explorerClientProvider; + } + + [HttpGet("stores/{storeId}/liquid")] + public async Task GenerateLiquidScript(string storeId, Dictionary bitcoinExtKeys = null) + { + Dictionary generated = new Dictionary(); + var allNetworks = _btcPayNetworkProvider.GetAll().OfType() + .GroupBy(network => network.NetworkCryptoCode); + var allNetworkCodes = allNetworks + .SelectMany(networks => networks.Select(network => network.CryptoCode.ToUpperInvariant())) + .ToArray() + .Distinct(); + Dictionary privKeys = bitcoinExtKeys ?? new Dictionary(); + + + var paymentMethods = (await _client.GetStoreOnChainPaymentMethods(storeId)) + .Where(settings => allNetworkCodes.Contains(settings.CryptoCode)) + .GroupBy(data => _btcPayNetworkProvider.GetNetwork(data.CryptoCode).NetworkCryptoCode); + + if (paymentMethods.Any() is false) + { + TempData.SetStatusMessageModel(new StatusMessageModel() + { + Severity = StatusMessageModel.StatusSeverity.Info, + Message = "There are no wallets configured that use Liquid or an elements side-chain." + }); + return View(new GenerateLiquidImportScripts()); + } + + foreach (var der in paymentMethods) + { + var network = _btcPayNetworkProvider.GetNetwork(der.Key); + var nbxnet = network.NBXplorerNetwork; + + var sb = new StringBuilder(); + + var explorerClient = _explorerClientProvider.GetExplorerClient(der.Key); + var status = await explorerClient.GetStatusAsync(); + if (status.BitcoinStatus is null) + { + sb.AppendLine($"{der.Key} node is not available. Try again later."); + generated.Add(der.Key, sb.ToString()); + continue; + } + var derivationSchemesForNetwork = der.GroupBy(data => data.DerivationScheme); + + foreach (var paymentMethodDerivationScheme in derivationSchemesForNetwork) + { + var derivatonScheme = + nbxnet.DerivationStrategyFactory.Parse(paymentMethodDerivationScheme.Key); + var sameWalletCryptoCodes = paymentMethodDerivationScheme.Select(data => data.CryptoCode).ToArray(); + var matchedExistingKey = privKeys.Where(pair => sameWalletCryptoCodes.Contains(pair.Key)); + BitcoinExtKey key = null; + if (matchedExistingKey.Any()) + { + key = matchedExistingKey.First().Value; + } + else + { + + key = await explorerClient.GetMetadataAsync(derivatonScheme, + WellknownMetadataKeys.AccountHDKey); + } + + if (key != null) + { + + foreach (var paymentMethodData in paymentMethodDerivationScheme) + { + privKeys.TryAdd(paymentMethodData.CryptoCode, key); + } + } + + var utxos = await explorerClient.GetUTXOsAsync(derivatonScheme, CancellationToken.None); + + foreach (var utxo in utxos.GetUnspentUTXOs()) + { + var addr = nbxnet.CreateAddress(derivatonScheme, utxo.KeyPath, utxo.ScriptPubKey); + + if (key is null) + { + sb.AppendLine( + $"elements-cli importaddress \"{addr}\" \"{utxo.KeyPath} from {derivatonScheme}\" false"); + } + else + { + sb.AppendLine( + $"elements-cli importprivkey \"{key.Derive(utxo.KeyPath).PrivateKey.GetWif(nbxnet.NBitcoinNetwork)}\" \"{utxo.KeyPath} from {derivatonScheme}\" false"); + } + + if (!derivatonScheme.Unblinded()) + { + var blindingKey = + NBXplorerNetworkProvider.LiquidNBXplorerNetwork.GenerateBlindingKey( + derivatonScheme, utxo.KeyPath, utxo.ScriptPubKey, nbxnet.NBitcoinNetwork); + sb.AppendLine($"elements-cli importblindingkey {addr} {blindingKey.ToHex()}"); + } + } + } + + if (sb.Length > 0) + { + sb.AppendLine("elements-cli stop"); + sb.AppendLine("elementsd -rescan"); + + } + generated.Add(der.Key, sb.ToString()); + } + + return View(new GenerateLiquidImportScripts() + { + Wallets = paymentMethods.SelectMany(settings => + settings.Select(data => + new GenerateLiquidImportScripts.GenerateLiquidImportScriptWalletKeyVm() + { + CryptoCode = data.CryptoCode, + KeyPresent = privKeys.ContainsKey(data.CryptoCode), + ManualKey = null + }).ToArray()).ToArray(), + Scripts = generated + }); + } + + + [HttpPost("stores/{storeId}/liquid")] + public async Task GenerateLiquidScript(string storeId, GenerateLiquidImportScripts vm) + { + Dictionary privKeys = new Dictionary(); + for (var index = 0; index < vm.Wallets.Length; index++) + { + var wallet = vm.Wallets[index]; + if (string.IsNullOrEmpty(wallet.ManualKey)) + continue; + + var n = + _btcPayNetworkProvider.GetNetwork(wallet.CryptoCode); + ExtKey extKey = null; + try + { + var mnemonic = new Mnemonic(wallet.ManualKey); + extKey = mnemonic.DeriveExtKey(); + } + catch (Exception) + { + } + + if (extKey == null) + { + try + { + extKey = ExtKey.Parse(wallet.ManualKey, n.NBitcoinNetwork); + } + catch (Exception) + { + } + } + + if (extKey == null) + { + vm.AddModelError(scripts => scripts.Wallets[index].ManualKey, + "Invalid key (must be seed or root xprv or account xprv)", this); + continue; + } + + + + var der = n.NBXplorerNetwork.DerivationStrategyFactory.Parse( + (await _client.GetStoreOnChainPaymentMethod(storeId, wallet.CryptoCode)).DerivationScheme); + if (der.GetExtPubKeys().Count() > 1) + { + vm.AddModelError(scripts => scripts.Wallets[index].ManualKey, "cannot handle multsig", this); + continue; + } + + var first = der + .GetExtPubKeys().First(); + if (first != extKey.Neuter()) + { + KeyPath kp = null; + switch (der.ScriptPubKeyType()) + { + case ScriptPubKeyType.Legacy: + kp = new KeyPath($"m/44'/{n.CoinType}/0'"); + break; + case ScriptPubKeyType.Segwit: + + kp = new KeyPath($"m/84'/{n.CoinType}/0'"); + break; + case ScriptPubKeyType.SegwitP2SH: + kp = new KeyPath($"m/49'/{n.CoinType}/0'"); + break; + default: + vm.AddModelError(scripts => scripts.Wallets[index].ManualKey, "cannot handle wallet type", + this); + continue; + } + + extKey = extKey.Derive(kp); + if (first != extKey.Neuter()) + { + vm.AddModelError(scripts => scripts.Wallets[index].ManualKey, "key did not match", this); + continue; + } + } + + privKeys.TryAdd(wallet.CryptoCode, extKey.GetWif(n.NBitcoinNetwork)); + } + + if (!ModelState.IsValid) + { + return View(vm); + } + + return await GenerateLiquidScript(storeId, privKeys); + } +public class GenerateLiquidImportScripts + { + public class GenerateLiquidImportScriptWalletKeyVm + { + public string CryptoCode { get; set; } + public bool KeyPresent { get; set; } + public string ManualKey { get; set; } + } + + public GenerateLiquidImportScriptWalletKeyVm[] Wallets { get; set; } = + Array.Empty(); + + public Dictionary Scripts { get; set; } = new Dictionary(); + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.LiquidPlus/LiquidPlusPlugin.cs b/Plugins/BTCPayServer.Plugins.LiquidPlus/LiquidPlusPlugin.cs new file mode 100644 index 0000000..01b0921 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LiquidPlus/LiquidPlusPlugin.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Models; +using BTCPayServer.Abstractions.Services; +using BTCPayServer.Plugins.LiquidPlus.Services; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Logging; +using NBitcoin; + +namespace BTCPayServer.Plugins.LiquidPlus +{ + public class LiquidPlusPlugin : BaseBTCPayServerPlugin + { + public override string Identifier { get; } = "BTCPayServer.Plugins.LiquidPlus"; + public override string Name { get; } = "Liquid+"; + + public override IBTCPayServerPlugin.PluginDependency[] Dependencies { get; } = + { + new IBTCPayServerPlugin.PluginDependency() { Identifier = nameof(BTCPayServer), Condition = ">=1.7.0.0" } + }; + public override string Description { get; } = "Enhanced support for the liquid network."; + + public override void Execute(IServiceCollection services) + { + services.AddSingleton(new UIExtension("LiquidNav", "store-integrations-nav")); + services.AddSingleton(new UIExtension("CustomLiquidAssetsNavExtension", "server-nav")); + services.AddSingleton(new UIExtension("StoreNavLiquidExtension", "store-nav")); + services.AddSingleton(); + + var originalImplementationFactory = services.Single(descriptor => + descriptor.Lifetime == ServiceLifetime.Singleton && + descriptor.ServiceType == typeof(BTCPayNetworkProvider)); + services.Replace(ServiceDescriptor.Singleton(provider => + { + var _customLiquidAssetsRepository = provider.GetService(); + var _logger = provider.GetService>(); + var networkProvider = + (originalImplementationFactory.ImplementationInstance ?? + originalImplementationFactory.ImplementationFactory.Invoke(provider)) as BTCPayNetworkProvider; + if (networkProvider.Support("LBTC")) + { + var settings = _customLiquidAssetsRepository.Get(); + var template = networkProvider.GetNetwork("LBTC"); + var additionalNetworks = settings.Items.Select(configuration => new ElementsBTCPayNetwork() + { + CryptoCode = configuration.CryptoCode + .Replace("-", "") + .Replace("_", ""), + DefaultRateRules = configuration.DefaultRateRules ?? Array.Empty(), + AssetId = uint256.Parse(configuration.AssetId), + Divisibility = configuration.Divisibility, + DisplayName = configuration.DisplayName, + CryptoImagePath = configuration.CryptoImagePath, + NetworkCryptoCode = template.NetworkCryptoCode, + DefaultSettings = template.DefaultSettings, + ElectrumMapping = template.ElectrumMapping, + BlockExplorerLink = template.BlockExplorerLink, + ReadonlyWallet = template.ReadonlyWallet, + SupportLightning = false, + SupportPayJoin = false, + ShowSyncSummary = false, + WalletSupported = template.WalletSupported, + LightningImagePath = "", + NBXplorerNetwork = template.NBXplorerNetwork, + CoinType = template.CoinType, + VaultSupported = template.VaultSupported, + MaxTrackedConfirmation = template.MaxTrackedConfirmation, + BlockExplorerLinkDefault = template.BlockExplorerLinkDefault, + SupportRBF = template.SupportRBF + }); + var newCryptoCodes = settings.Items.Select(configuration => configuration.CryptoCode).ToArray(); + _logger.LogInformation($"Loaded {newCryptoCodes.Length} " + + $"{(!newCryptoCodes.Any()?string.Empty: $"({string.Join(',', newCryptoCodes)})")} additional liquid assets"); + var newSupportedChains = networkProvider.GetAll().Select(b => b.CryptoCode).Concat(newCryptoCodes).ToArray(); + return new BTCPayNetworkProviderOverride(networkProvider.NetworkType, additionalNetworks).Filter(newSupportedChains); + } + + return networkProvider; + })); + } + } + + public class BTCPayNetworkProviderOverride : BTCPayNetworkProvider + { + public BTCPayNetworkProviderOverride(ChainName networkType, + IEnumerable elementsBTCPayNetworks) : base(networkType) + { + foreach (ElementsBTCPayNetwork elementsBTCPayNetwork in elementsBTCPayNetworks) + { + _Networks.TryAdd(elementsBTCPayNetwork.CryptoCode.ToUpperInvariant(), elementsBTCPayNetwork); + } + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.LiquidPlus/Models/CustomLiquidAssetsSettings.cs b/Plugins/BTCPayServer.Plugins.LiquidPlus/Models/CustomLiquidAssetsSettings.cs new file mode 100644 index 0000000..fd17ddd --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LiquidPlus/Models/CustomLiquidAssetsSettings.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; + +namespace BTCPayServer.Plugins.LiquidPlus.Models +{ + public class CustomLiquidAssetsSettings + { + public List Items { get; set; } = new List(); + + public class LiquidAssetConfiguration + { + [Required] public string AssetId { get; set; } + + [Range(0, double.PositiveInfinity)] public int Divisibility { get; set; } = 8; + + [Required] + [Display(Name = "Display name")] + public string DisplayName { get; set; } + + [Display(Name = "Checkout icon url")] public string CryptoImagePath { get; set; } + + [Required] + [Display(Name = "Currency code")] + public string CryptoCode { get; set; } + + public string[] DefaultRateRules { get; set; } + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.LiquidPlus/Models/CustomLiquidAssetsViewModel.cs b/Plugins/BTCPayServer.Plugins.LiquidPlus/Models/CustomLiquidAssetsViewModel.cs new file mode 100644 index 0000000..978db40 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LiquidPlus/Models/CustomLiquidAssetsViewModel.cs @@ -0,0 +1,7 @@ +namespace BTCPayServer.Plugins.LiquidPlus.Models +{ + public class CustomLiquidAssetsViewModel: CustomLiquidAssetsSettings + { + public bool PendingChanges { get; set; } + } +} diff --git a/Plugins/BTCPayServer.Plugins.LiquidPlus/Pack.ps1 b/Plugins/BTCPayServer.Plugins.LiquidPlus/Pack.ps1 new file mode 100644 index 0000000..8f67aa9 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LiquidPlus/Pack.ps1 @@ -0,0 +1,2 @@ +dotnet publish -c Altcoins-Release -o bin/publish/BTCPayServer.Plugins.LiquidPlus +dotnet run -p ../../BTCPayServer.PluginPacker bin/publish/BTCPayServer.Plugins.LiquidPlus BTCPayServer.Plugins.LiquidPlus ../packed diff --git a/Plugins/BTCPayServer.Plugins.LiquidPlus/Services/CustomLiquidAssetsRepository.cs b/Plugins/BTCPayServer.Plugins.LiquidPlus/Services/CustomLiquidAssetsRepository.cs new file mode 100644 index 0000000..67a096a --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LiquidPlus/Services/CustomLiquidAssetsRepository.cs @@ -0,0 +1,60 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using BTCPayServer.Configuration; +using BTCPayServer.Plugins.LiquidPlus.Models; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Plugins.LiquidPlus.Services +{ + public class CustomLiquidAssetsRepository + { + private readonly ILogger _logger; + private readonly IOptions _options; + private string File => Path.Combine(_options.Value.DataDir, "custom-liquid-assets.json"); + + public CustomLiquidAssetsRepository(ILogger logger, IOptions options) + { + _logger = logger; + _options = options; + } + + public CustomLiquidAssetsSettings Get() + { + try + { + if (System.IO.File.Exists(File)) + { + return JObject.Parse(System.IO.File.ReadAllText(File)).ToObject(); + } + } + + catch (Exception e) + { + _logger.LogError(e, "could not parse custom liquid assets file"); + } + + return new CustomLiquidAssetsSettings(); + } + + public async Task Set(CustomLiquidAssetsSettings settings) + { + try + { + await System.IO.File.WriteAllTextAsync(File, JObject.FromObject(settings).ToString(Formatting.Indented)); + + ChangesPending = true; + } + + catch (Exception e) + { + _logger.LogError(e, "could not write custom liquid assets file"); + } + } + + public bool ChangesPending { get; private set; } + } +} diff --git a/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/CustomLiquidAssets/Assets.cshtml b/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/CustomLiquidAssets/Assets.cshtml new file mode 100644 index 0000000..ed8d296 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/CustomLiquidAssets/Assets.cshtml @@ -0,0 +1,79 @@ +@inject ContentSecurityPolicies contentSecurityPolicies +@using BTCPayServer.Security +@using NBitcoin +@model BTCPayServer.Plugins.LiquidPlus.Models.CustomLiquidAssetsViewModel +@{ + Layout = "../Shared/_NavLayout.cshtml"; + ViewData["NavPartialName"] = "../UIServer/_Nav"; + ViewData["Title"] = "Custom Liquid Assets"; + var nonce = RandomUtils.GetUInt256().ToString().Substring(0, 32); + contentSecurityPolicies.Add("script-src", $"'nonce-{nonce}'"); +} +@if (Model.PendingChanges) +{ +
    There are saved changes to the custom liquid assets that have not yet been applied. Restart BTCPay Server to load these changes.
    +} + +
    +
    + @if (!Model.Items.Any()) + { +

    No custom assets set up

    + } + @for (var index = 0; index < Model.Items.Count; index++) + { +
    +
    +
    + + + +
    +
    + + + +
    +
    + + + +
    +
    + + + +
    +
    + + + +
    +
    + +
    + } + +
    + +
    + + + + +
    + +
    diff --git a/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/Shared/CustomLiquidAssetsNavExtension.cshtml b/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/Shared/CustomLiquidAssetsNavExtension.cshtml new file mode 100644 index 0000000..7a47cb8 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/Shared/CustomLiquidAssetsNavExtension.cshtml @@ -0,0 +1,7 @@ +@using BTCPayServer.Plugins.LiquidPlus.Controllers +@{ + var isActive = ViewContext.RouteData.Values.TryGetValue("Controller", out var controller) && controller is not null && + nameof(CustomLiquidAssetsController).StartsWith(controller?.ToString(), StringComparison.InvariantCultureIgnoreCase); +} + +Liquid Assets diff --git a/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/Shared/LiquidNav.cshtml b/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/Shared/LiquidNav.cshtml new file mode 100644 index 0000000..17c9e01 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/Shared/LiquidNav.cshtml @@ -0,0 +1,23 @@ +@using BTCPayServer +@using BTCPayServer.Abstractions.Contracts +@using BTCPayServer.Plugins.LiquidPlus.Controllers +@using Microsoft.AspNetCore.Routing + +@inject BTCPayNetworkProvider BTCPayNetworkProvider; + +@inject IScopeProvider ScopeProvider +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + var isActive = !string.IsNullOrEmpty(storeId) && ViewContext.RouteData.Values.TryGetValue("Controller", out var controller) && controller is not null && + nameof(StoreLiquidController).StartsWith(controller?.ToString(), StringComparison.InvariantCultureIgnoreCase); +} +@if (!string.IsNullOrEmpty(storeId) && (BTCPayNetworkProvider.GetAll().OfType().Any())) +{ + +} diff --git a/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/Shared/StoreNavLiquidExtension.cshtml b/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/Shared/StoreNavLiquidExtension.cshtml new file mode 100644 index 0000000..3750656 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/Shared/StoreNavLiquidExtension.cshtml @@ -0,0 +1,15 @@ +@using BTCPayServer +@using BTCPayServer.Abstractions.Contracts +@using BTCPayServer.Plugins.LiquidPlus.Controllers +@using Microsoft.AspNetCore.Routing +@inject BTCPayNetworkProvider BTCPayNetworkProvider; +@inject IScopeProvider ScopeProvider +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + var isActive = !string.IsNullOrEmpty(storeId) && ViewContext.RouteData.Values.TryGetValue("Controller", out var controller) && controller is not null && + nameof(StoreLiquidController).StartsWith(controller?.ToString(), StringComparison.InvariantCultureIgnoreCase); +} +@if (BTCPayNetworkProvider.GetAll().OfType().Any()) +{ + Liquid +} diff --git a/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/StoreLiquid/GenerateLiquidScript.cshtml b/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/StoreLiquid/GenerateLiquidScript.cshtml new file mode 100644 index 0000000..812da37 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/StoreLiquid/GenerateLiquidScript.cshtml @@ -0,0 +1,65 @@ + +@model BTCPayServer.Plugins.LiquidPlus.Controllers.StoreLiquidController.GenerateLiquidImportScripts +@{ + Layout = "../Shared/_NavLayout.cshtml"; + ViewData["NavPartialName"] = "../UIStores/_Nav"; + ViewBag.MainTitle = "Store settings"; + ViewData["title"] = "Liquid Import"; +} + +

    Liquid import

    +
    +
    +

    Generates commands to import your received liquid funds into an elements node

    +
    +
    +@if (Model.Wallets.Any()) +{ +
    +
      + +
    • Wallets
    • + + @for (var index = 0; index < Model.Wallets.Length; index++) + { + var x = Model.Wallets[index]; + + +
    • +
      + @x.CryptoCode + @if (!x.KeyPresent) + { + + } + else + { + Keys already available + } + +
      + + +
    • + } + @if (!Model.Wallets.All(vm => vm.KeyPresent)) + { +
    • + +
    • + } +
    • Scripts (per chain)
    • + @foreach (var script in Model.Scripts) + { +
    • @script.Key
    • +
    • + @if (string.IsNullOrEmpty(script.Value)) + { + Nothing to generate + } +
      @Html.Raw(script.Value)
      +
    • + } +
    +
    +} diff --git a/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/_ViewImports.cshtml b/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/_ViewImports.cshtml new file mode 100644 index 0000000..afa82bb --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.LiquidPlus/Views/_ViewImports.cshtml @@ -0,0 +1 @@ +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/Plugins/BTCPayServer.Plugins.NFC/BTCPayServer.Plugins.NFC.csproj b/Plugins/BTCPayServer.Plugins.NFC/BTCPayServer.Plugins.NFC.csproj new file mode 100644 index 0000000..94c029f --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.NFC/BTCPayServer.Plugins.NFC.csproj @@ -0,0 +1,40 @@ + + + + net6.0 + 10 + + + + + LNURL NFC Support + Allows you to support contactless card payments over NFC and LNURL Withdraw! + Kukks + 1.0.8 + + + + + true + false + true + + + + + + StaticWebAssetsEnabled=false + false + runtime;native;build;buildTransitive;contentFiles + + + + + + + + + + + + diff --git a/Plugins/BTCPayServer.Plugins.NFC/NFCController.cs b/Plugins/BTCPayServer.Plugins.NFC/NFCController.cs new file mode 100644 index 0000000..209b7da --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.NFC/NFCController.cs @@ -0,0 +1,99 @@ +using System; +using System.Net.Http; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Constants; +using BTCPayServer.Client; +using LNURL; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using NBitcoin; + +namespace BTCPayServer.Plugins.NFC +{ + [Route("plugins/NFC")] + public class NFCController : Controller + { + private readonly IHttpClientFactory _httpClientFactory; + + public NFCController(IHttpClientFactory httpClientFactory) + { + _httpClientFactory = httpClientFactory; + } + + public class SubmitRequest + { + public string Lnurl { get; set; } + public string Destination { get; set; } + } + + [AllowAnonymous] + public async Task SubmitLNURLWithdrawForInvoice([FromBody] SubmitRequest request) + { + Uri uri; + string tag; + try + { + uri = LNURL.LNURL.Parse(request.Lnurl, out tag); + if (uri is null) + { + return BadRequest("lnurl was malformed"); + } + } + catch (Exception e) + { + + return BadRequest(e.Message); + } + + + if (!string.IsNullOrEmpty(tag) && !tag.Equals("withdrawRequest")) + { + return BadRequest("lnurl was not lnurl-withdraw"); + } + + var httpClient = _httpClientFactory.CreateClient(uri.IsOnion() + ? "LightningLikePayoutHandlerOnionNamedClient" + : "LightningLikePayoutHandlerClearnetNamedClient"); + var info = (await + LNURL.LNURL.FetchInformation(uri, "withdrawRequest", httpClient)) as LNURLWithdrawRequest; + if (info is null) + { + return BadRequest("Could not fetch info from lnurl-withdraw "); + } + + httpClient = _httpClientFactory.CreateClient(info.Callback.IsOnion() + ? "LightningLikePayoutHandlerOnionNamedClient" + : "LightningLikePayoutHandlerClearnetNamedClient"); + + try + { + var destinationuri = LNURL.LNURL.Parse(request.Destination, out string _); + + var destinfo = (await + LNURL.LNURL.FetchInformation(destinationuri, "payRequest", httpClient)) as LNURLPayRequest; + + if (destinfo is null) + { + return BadRequest("Could not fetch bolt11 invoice to pay to."); + } + + httpClient = _httpClientFactory.CreateClient(destinfo.Callback.IsOnion() + ? "LightningLikePayoutHandlerOnionNamedClient" + : "LightningLikePayoutHandlerClearnetNamedClient"); + var destCallback = await destinfo.SendRequest(destinfo.MinSendable, Network.Main, httpClient); + request.Destination = destCallback.Pr; + } + catch (Exception e) + { + } + + var result = await info.SendRequest(request.Destination, httpClient); + if (result.Status.Equals("ok", StringComparison.InvariantCultureIgnoreCase)) + { + return Ok(result.Reason); + } + + return BadRequest(result.Reason); + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.NFC/NFCPlugin.cs b/Plugins/BTCPayServer.Plugins.NFC/NFCPlugin.cs new file mode 100644 index 0000000..85aabea --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.NFC/NFCPlugin.cs @@ -0,0 +1,31 @@ +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Models; +using BTCPayServer.Abstractions.Services; +using Microsoft.Extensions.DependencyInjection; + +namespace BTCPayServer.Plugins.FixedFloat +{ + public class NFCPlugin : BaseBTCPayServerPlugin + { + public override string Identifier => "BTCPayServer.Plugins.NFC"; + public override string Name => "LNURL NFC Support"; + + + public override IBTCPayServerPlugin.PluginDependency[] Dependencies { get; } = + { + new() { Identifier = nameof(BTCPayServer), Condition = ">1.7.1.0" } + }; + + public override string Description => + "Allows you to support contactless card payments over NFC and LNURL Withdraw!"; + + public override void Execute(IServiceCollection applicationBuilder) + { + applicationBuilder.AddSingleton(new UIExtension("NFC/CheckoutEnd", + "checkout-end")); + applicationBuilder.AddSingleton(new UIExtension("NFC/LightningCheckoutPostContent", + "checkout-lightning-post-content")); + base.Execute(applicationBuilder); + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.NFC/Pack.ps1 b/Plugins/BTCPayServer.Plugins.NFC/Pack.ps1 new file mode 100644 index 0000000..14449fb --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.NFC/Pack.ps1 @@ -0,0 +1,2 @@ +dotnet publish -c Altcoins-Release -o bin/publish/BTCPayServer.Plugins.NFC +dotnet run -p ../../BTCPayServer.PluginPacker bin/publish/BTCPayServer.Plugins.NFC BTCPayServer.Plugins.NFC ../packed diff --git a/Plugins/BTCPayServer.Plugins.NFC/Resources/js/lnurlwnfc.js b/Plugins/BTCPayServer.Plugins.NFC/Resources/js/lnurlwnfc.js new file mode 100644 index 0000000..1f981f5 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.NFC/Resources/js/lnurlwnfc.js @@ -0,0 +1,80 @@ +Vue.component("LNURLWithdrawContactless", { + data: function () { + return { + supported: ('NDEFReader' in window && window.self === window.top), + scanning: false, + submitting: false, + readerAbortController: null, + } + }, + methods: { + startScan: async function () { + try { + if (this.scanning || this.submitting) { + return; + } + const self = this; + self.submitting = false; + self.scanning = true; + if (!this.supported) { + const result = prompt("enter lnurl withdraw"); + if (result) { + self.sendData.bind(self)(result); + return; + } + self.scanning = false; + } + ndef = new NDEFReader() + self.readerAbortController = new AbortController() + await ndef.scan({signal: self.readerAbortController.signal}) + + ndef.addEventListener('readingerror', () => { + self.scanning = false; + self.readerAbortController.abort() + }) + + ndef.addEventListener('reading', ({message, serialNumber}) => { + //Decode NDEF data from tag + const record = message.records[0] + const textDecoder = new TextDecoder('utf-8') + const lnurl = textDecoder.decode(record.data) + + //User feedback, show loader icon + self.scanning = false; + self.sendData.bind(self)(lnurl); + + }) + } catch(e) { + self.scanning = false; + self.submitting = false; + } + }, + sendData: function (lnurl) { + + this.submitting = true; + //Post LNURLW data to server + var xhr = new XMLHttpRequest() + xhr.open('POST', window.lnurlWithdrawSubmitUrl, true) + xhr.setRequestHeader('Content-Type', 'application/json') + xhr.send(JSON.stringify({lnurl, destination: this.$parent.srvModel.btcAddress})) + const self = this; + //User feedback, reset on failure + xhr.onload = function () { + if (xhr.readyState === xhr.DONE) { + console.log(xhr.response); + console.log(xhr.responseText); + self.scanning = false; + self.submitting = false; + + if(self.readerAbortController) { + self.readerAbortController.abort() + } + + if(xhr.response){ + alert(xhr.response) + } + } + } + } + } +}); diff --git a/Plugins/BTCPayServer.Plugins.NFC/Views/Shared/NFC/CheckoutEnd.cshtml b/Plugins/BTCPayServer.Plugins.NFC/Views/Shared/NFC/CheckoutEnd.cshtml new file mode 100644 index 0000000..e01fd0f --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.NFC/Views/Shared/NFC/CheckoutEnd.cshtml @@ -0,0 +1,14 @@ +@inject ContentSecurityPolicies contentSecurityPolicies +@using BTCPayServer.Abstractions.Extensions +@using BTCPayServer.Security +@using NBitcoin +@{ + var nonce = RandomUtils.GetUInt256().ToString().Substring(0, 32); + contentSecurityPolicies.Add("script-src", $"'nonce-{nonce}'"); + var url = Context.Request.GetAbsoluteUri(Url.Action("SubmitLNURLWithdrawForInvoice", "NFC")); +} + + + diff --git a/Plugins/BTCPayServer.Plugins.NFC/Views/Shared/NFC/LightningCheckoutPostContent.cshtml b/Plugins/BTCPayServer.Plugins.NFC/Views/Shared/NFC/LightningCheckoutPostContent.cshtml new file mode 100644 index 0000000..1450249 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.NFC/Views/Shared/NFC/LightningCheckoutPostContent.cshtml @@ -0,0 +1,12 @@ + + + + + + diff --git a/Plugins/BTCPayServer.Plugins.NFC/Views/_ViewImports.cshtml b/Plugins/BTCPayServer.Plugins.NFC/Views/_ViewImports.cshtml new file mode 100644 index 0000000..afa82bb --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.NFC/Views/_ViewImports.cshtml @@ -0,0 +1 @@ +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/Plugins/BTCPayServer.Plugins.RockstarStylist/BTCPayServer.Plugins.RockstarStylist.csproj b/Plugins/BTCPayServer.Plugins.RockstarStylist/BTCPayServer.Plugins.RockstarStylist.csproj new file mode 100644 index 0000000..63d8a3c --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.RockstarStylist/BTCPayServer.Plugins.RockstarStylist.csproj @@ -0,0 +1,18 @@ + + + net6.0 + true + false + true + 1.0.2 + + + + + + + + + <_ContentIncludedByDefault Remove="Views\TestExtension\Index.cshtml" /> + + diff --git a/Plugins/BTCPayServer.Plugins.RockstarStylist/Pack.ps1 b/Plugins/BTCPayServer.Plugins.RockstarStylist/Pack.ps1 new file mode 100644 index 0000000..306c0a0 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.RockstarStylist/Pack.ps1 @@ -0,0 +1,2 @@ +dotnet publish -c Altcoins-Release -o bin/publish/BTCPayServer.Plugins.RockstarStylist +dotnet run -p ../../BTCPayServer.PluginPacker bin/publish/BTCPayServer.Plugins.RockstarStylist BTCPayServer.Plugins.RockstarStylist ../packed diff --git a/Plugins/BTCPayServer.Plugins.RockstarStylist/RockstarStyleProvider.cs b/Plugins/BTCPayServer.Plugins.RockstarStylist/RockstarStyleProvider.cs new file mode 100644 index 0000000..beedf3a --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.RockstarStylist/RockstarStyleProvider.cs @@ -0,0 +1,37 @@ +using System.Linq; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Plugins.RockstarStylist +{ + public class RockstarStyleProvider + { + private HttpClient _githubClient; + + public RockstarStyleProvider(IHttpClientFactory httpClientFactory) + { + _githubClient = httpClientFactory.CreateClient(); + _githubClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("btcpayserver", "1")); + } + + public async Task Get() + { + var response = JArray.Parse(await _githubClient.GetStringAsync("https://api.github.com/repos/btcpayserver/BTCPayThemes/contents")); + return response.Where(token => token.Value("type") == "dir").Select(token => new RockstarStyle() + { + StyleName = token.Value("name"), + CssUrl = $"https://btcpayserver.github.io/BTCPayThemes/{token.Value("name")}/btcpay-checkout.custom.css", + PreviewUrl = $"https://btcpayserver.github.io/BTCPayThemes/{token.Value("name")}" + }).ToArray(); + } + } + + public class RockstarStyle + { + public string StyleName { get; set; } + public string CssUrl { get; set; } + public string PreviewUrl { get; set; } + } +} diff --git a/Plugins/BTCPayServer.Plugins.RockstarStylist/RockstarStylistPlugin.cs b/Plugins/BTCPayServer.Plugins.RockstarStylist/RockstarStylistPlugin.cs new file mode 100644 index 0000000..0af3880 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.RockstarStylist/RockstarStylistPlugin.cs @@ -0,0 +1,27 @@ +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Models; +using BTCPayServer.Abstractions.Services; +using Microsoft.Extensions.DependencyInjection; + +namespace BTCPayServer.Plugins.RockstarStylist +{ + public class RockstarStylistPlugin : BaseBTCPayServerPlugin + { + public override string Identifier { get; } = "BTCPayServer.Plugins.RockstarStylist"; + public override string Name { get; } = "Rockstar hairstylist"; + public override string Description { get; } = "Allows your checkout to get a rockstar approved makeover"; + + public override void Execute(IServiceCollection services) + { + services.AddSingleton(new UIExtension("InvoiceCheckoutThemeOptions", + "invoice-checkout-theme-options")); + services.AddSingleton(); + } + + + public override IBTCPayServerPlugin.PluginDependency[] Dependencies { get; } = + { + new IBTCPayServerPlugin.PluginDependency() { Identifier = nameof(BTCPayServer), Condition = ">=1.4.6.0" } + }; + } +} diff --git a/Plugins/BTCPayServer.Plugins.RockstarStylist/Views/Shared/InvoiceCheckoutThemeOptions.cshtml b/Plugins/BTCPayServer.Plugins.RockstarStylist/Views/Shared/InvoiceCheckoutThemeOptions.cshtml new file mode 100644 index 0000000..e536781 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.RockstarStylist/Views/Shared/InvoiceCheckoutThemeOptions.cshtml @@ -0,0 +1,26 @@ +@using BTCPayServer.Plugins.RockstarStylist +@using BTCPayServer.Security +@using NBitcoin +@inject ContentSecurityPolicies contentSecurityPolicies +@inject RockstarStyleProvider RockstarStyleProvider +@{ + var nonce = RandomUtils.GetUInt256().ToString().Substring(0, 32); + contentSecurityPolicies.Add("script-src", $"'nonce-{nonce}'"); + var themes = await RockstarStyleProvider.Get(); +} +@foreach (var theme in themes) +{ + +} + + diff --git a/Plugins/BTCPayServer.Plugins.RockstarStylist/Views/_ViewImports.cshtml b/Plugins/BTCPayServer.Plugins.RockstarStylist/Views/_ViewImports.cshtml new file mode 100644 index 0000000..afa82bb --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.RockstarStylist/Views/_ViewImports.cshtml @@ -0,0 +1 @@ +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/Plugins/BTCPayServer.Plugins.SideShift/BTCPayServer.Plugins.SideShift.csproj b/Plugins/BTCPayServer.Plugins.SideShift/BTCPayServer.Plugins.SideShift.csproj new file mode 100644 index 0000000..d9964c2 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/BTCPayServer.Plugins.SideShift.csproj @@ -0,0 +1,40 @@ + + + + net6.0 + 10 + + + + + SideShift + Allows you to embed a SideShift conversion screen to allow customers to pay with altcoins. + Kukks + 1.0.9 + + + + + true + false + true + + + + + + StaticWebAssetsEnabled=false + false + runtime;native;build;buildTransitive;contentFiles + + + + + + + + + + + + diff --git a/Plugins/BTCPayServer.Plugins.SideShift/Pack.ps1 b/Plugins/BTCPayServer.Plugins.SideShift/Pack.ps1 new file mode 100644 index 0000000..04e0c96 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/Pack.ps1 @@ -0,0 +1,2 @@ +dotnet publish -c Altcoins-Release -o bin/publish/BTCPayServer.Plugins.SideShift +dotnet run -p ../../BTCPayServer.PluginPacker bin/publish/BTCPayServer.Plugins.SideShift BTCPayServer.Plugins.SideShift ../packed diff --git a/Plugins/BTCPayServer.Plugins.SideShift/Resources/assets/sideshift.svg b/Plugins/BTCPayServer.Plugins.SideShift/Resources/assets/sideshift.svg new file mode 100644 index 0000000..7ecaaaa --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/Resources/assets/sideshift.svg @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/Plugins/BTCPayServer.Plugins.SideShift/Resources/js/sideShiftComponent.js b/Plugins/BTCPayServer.Plugins.SideShift/Resources/js/sideShiftComponent.js new file mode 100644 index 0000000..8eecf6e --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/Resources/js/sideShiftComponent.js @@ -0,0 +1,38 @@ +Vue.component("side-shift", { + props: ["toCurrency", "toCurrencyDue", "toCurrencyAddress"], + methods: { + openDialog: function (e) { + if (e && e.preventDefault) { + e.preventDefault(); + } + let settleMethodId = ""; + let amount = !this.$parent.srvModel.isUnsetTopUp + ? this.toCurrencyDue + : undefined; + if (this.toCurrency.toLowerCase() === "lbtc") { + settleMethodId = "liquid"; + } else if (this.toCurrency.toLowerCase() === "usdt") { + settleMethodId = "usdtla"; + } else if ( + this.toCurrency.endsWith("LightningLike") || + this.toCurrency.endsWith("LNURLPay") + ) { + settleMethodId = "ln"; + } else { + settleMethodId = this.toCurrency + .replace("_BTCLike", "") + .replace("_MoneroLike", "") + .replace("_ZcashLike", "") + .toLowerCase(); + } + window.__SIDESHIFT__ = { + parentAffiliateId: "qg0OrfHJV", + defaultSettleMethodId: settleMethodId, + settleAddress: this.toCurrencyAddress, + settleAmount: amount, + type: !this.$parent.srvModel.isUnsetTopUp ? "fixed" : undefined, + }; + window.sideshift.show(); + }, + }, +}); diff --git a/Plugins/BTCPayServer.Plugins.SideShift/SideShiftController.cs b/Plugins/BTCPayServer.Plugins.SideShift/SideShiftController.cs new file mode 100644 index 0000000..15bb439 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/SideShiftController.cs @@ -0,0 +1,82 @@ +using System; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Constants; +using BTCPayServer.Client; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Plugins.SideShift +{ + [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Route("plugins/{storeId}/SideShift")] + public class SideShiftController : Controller + { + private readonly BTCPayServerClient _btcPayServerClient; + private readonly SideShiftService _sideShiftService; + + public SideShiftController(BTCPayServerClient btcPayServerClient, SideShiftService sideShiftService) + { + _btcPayServerClient = btcPayServerClient; + _sideShiftService = sideShiftService; + } + + [HttpGet("")] + public async Task UpdateSideShiftSettings(string storeId) + { + var store = await _btcPayServerClient.GetStore(storeId); + + UpdateSideShiftSettingsViewModel vm = new UpdateSideShiftSettingsViewModel(); + vm.StoreName = store.Name; + SideShiftSettings SideShift = null; + try + { + SideShift = await _sideShiftService.GetSideShiftForStore(storeId); + } + catch (Exception) + { + // ignored + } + + SetExistingValues(SideShift, vm); + return View(vm); + } + + private void SetExistingValues(SideShiftSettings existing, UpdateSideShiftSettingsViewModel vm) + { + if (existing == null) + return; + vm.Enabled = existing.Enabled; + } + + [HttpPost("")] + public async Task UpdateSideShiftSettings(string storeId, UpdateSideShiftSettingsViewModel vm, + string command) + { + if (vm.Enabled) + { + if (!ModelState.IsValid) + { + return View(vm); + } + } + + var sideShiftSettings = new SideShiftSettings() + { + Enabled = vm.Enabled, + }; + + switch (command) + { + case "save": + await _sideShiftService.SetSideShiftForStore(storeId, sideShiftSettings); + TempData["SuccessMessage"] = "SideShift settings modified"; + return RedirectToAction(nameof(UpdateSideShiftSettings), new {storeId}); + + default: + return View(vm); + } + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.SideShift/SideShiftPlugin.cs b/Plugins/BTCPayServer.Plugins.SideShift/SideShiftPlugin.cs new file mode 100644 index 0000000..3b23787 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/SideShiftPlugin.cs @@ -0,0 +1,48 @@ +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Models; +using BTCPayServer.Abstractions.Services; +using Microsoft.Extensions.DependencyInjection; + +namespace BTCPayServer.Plugins.SideShift +{ + public class SideShiftPlugin : BaseBTCPayServerPlugin + { + public override string Identifier => "BTCPayServer.Plugins.SideShift"; + public override string Name => "SideShift"; + + + public override IBTCPayServerPlugin.PluginDependency[] Dependencies { get; } = + { + new IBTCPayServerPlugin.PluginDependency() { Identifier = nameof(BTCPayServer), Condition = ">=1.7.0.0" } + }; + + public override string Description => + "Allows you to embed a SideShift conversion screen to allow customers to pay with altcoins."; + + public override void Execute(IServiceCollection applicationBuilder) + { + applicationBuilder.AddSingleton(); + applicationBuilder.AddSingleton(new UIExtension("SideShift/SideShiftNav", + "store-integrations-nav")); + applicationBuilder.AddSingleton(new UIExtension("SideShift/StoreIntegrationSideShiftOption", + "store-integrations-list")); + // Checkout v2 + applicationBuilder.AddSingleton(new UIExtension("SideShift/CheckoutPaymentMethodExtension", + "checkout-payment-method")); + applicationBuilder.AddSingleton(new UIExtension("SideShift/CheckoutPaymentExtension", + "checkout-payment")); + // Checkout Classic + applicationBuilder.AddSingleton(new UIExtension("SideShift/CheckoutContentExtension", + "checkout-bitcoin-post-content")); + applicationBuilder.AddSingleton(new UIExtension("SideShift/CheckoutContentExtension", + "checkout-lightning-post-content")); + applicationBuilder.AddSingleton(new UIExtension("SideShift/CheckoutTabExtension", + "checkout-bitcoin-post-tabs")); + applicationBuilder.AddSingleton(new UIExtension("SideShift/CheckoutTabExtension", + "checkout-lightning-post-tabs")); + applicationBuilder.AddSingleton(new UIExtension("SideShift/CheckoutEnd", + "checkout-end")); + base.Execute(applicationBuilder); + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.SideShift/SideShiftService.cs b/Plugins/BTCPayServer.Plugins.SideShift/SideShiftService.cs new file mode 100644 index 0000000..668afaa --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/SideShiftService.cs @@ -0,0 +1,49 @@ +using System; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Client; +using Microsoft.Extensions.Caching.Memory; + +namespace BTCPayServer.Plugins.SideShift +{ + public class SideShiftService + { + private readonly ISettingsRepository _settingsRepository; + private readonly IMemoryCache _memoryCache; + private readonly IStoreRepository _storeRepository; + + public SideShiftService(ISettingsRepository settingsRepository, IMemoryCache memoryCache, IStoreRepository storeRepository) + { + _settingsRepository = settingsRepository; + _memoryCache = memoryCache; + _storeRepository = storeRepository; + } + + public async Task GetSideShiftForStore(string storeId) + { + var k = $"{nameof(SideShiftSettings)}_{storeId}"; + return await _memoryCache.GetOrCreateAsync(k, async _ => + { + var res = await _storeRepository.GetSettingAsync(storeId, + nameof(SideShiftSettings)); + if (res is not null) return res; + res = await _settingsRepository.GetSettingAsync(k); + + if (res is not null) + { + await SetSideShiftForStore(storeId, res); + } + + await _settingsRepository.UpdateSetting(null, k); + return res; + }); + } + + public async Task SetSideShiftForStore(string storeId, SideShiftSettings SideShiftSettings) + { + var k = $"{nameof(SideShiftSettings)}_{storeId}"; + await _storeRepository.UpdateSetting(storeId, nameof(SideShiftSettings), SideShiftSettings); + _memoryCache.Set(k, SideShiftSettings); + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.SideShift/SideShiftSettings.cs b/Plugins/BTCPayServer.Plugins.SideShift/SideShiftSettings.cs new file mode 100644 index 0000000..448e15c --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/SideShiftSettings.cs @@ -0,0 +1,8 @@ +namespace BTCPayServer.Plugins.SideShift +{ + public class SideShiftSettings + { + public bool Enabled { get; set; } + public decimal AmountMarkupPercentage { get; set; } = 0; + } +} diff --git a/Plugins/BTCPayServer.Plugins.SideShift/UpdateSideShiftSettingsViewModel.cs b/Plugins/BTCPayServer.Plugins.SideShift/UpdateSideShiftSettingsViewModel.cs new file mode 100644 index 0000000..86b2418 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/UpdateSideShiftSettingsViewModel.cs @@ -0,0 +1,8 @@ +namespace BTCPayServer.Plugins.SideShift +{ + public class UpdateSideShiftSettingsViewModel + { + public bool Enabled { get; set; } + public string StoreName { get; set; } + } +} diff --git a/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutContentExtension.cshtml b/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutContentExtension.cshtml new file mode 100644 index 0000000..58f5d6d --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutContentExtension.cshtml @@ -0,0 +1,26 @@ +@using BTCPayServer.Plugins.SideShift +@using Newtonsoft.Json +@using Newtonsoft.Json.Linq +@inject SideShiftService SideShiftService +@{ + var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value(); + var settings = await SideShiftService.GetSideShiftForStore(storeId); + if (settings?.Enabled is true) + { +
    +
    + + {{$t("ConversionTab_BodyTop", srvModel)}} +

    + {{$t("ConversionTab_BodyDesc", srvModel)}} +
    +
    + + {{$t("Pay with SideShift")}} + +
    + } +} diff --git a/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutEnd.cshtml b/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutEnd.cshtml new file mode 100644 index 0000000..46b0ee4 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutEnd.cshtml @@ -0,0 +1,16 @@ +@using BTCPayServer.Plugins.SideShift +@using Newtonsoft.Json +@using Newtonsoft.Json.Linq +@inject BTCPayServer.Security.ContentSecurityPolicies csp +@inject SideShiftService SideShiftService +@{ + var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value(); + var settings = await SideShiftService.GetSideShiftForStore(storeId); + if (settings?.Enabled is true) + { + csp.Add("script-src", "https://sideshift.ai"); + csp.Add("script-src", "*.sideshift.ai"); + + + } +} diff --git a/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutPaymentExtension.cshtml b/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutPaymentExtension.cshtml new file mode 100644 index 0000000..c19952c --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutPaymentExtension.cshtml @@ -0,0 +1,69 @@ +@using BTCPayServer.Plugins.SideShift +@using Newtonsoft.Json +@using Newtonsoft.Json.Linq +@inject BTCPayServer.Security.ContentSecurityPolicies csp +@inject SideShiftService SideShiftService +@{ + var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value(); + var settings = await SideShiftService.GetSideShiftForStore(storeId); +} +@if (settings?.Enabled is true) +{ + csp.Add("script-src", "https://sideshift.ai"); + csp.Add("script-src", "*.sideshift.ai"); + + + + +} diff --git a/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutPaymentMethodExtension.cshtml b/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutPaymentMethodExtension.cshtml new file mode 100644 index 0000000..a177fdf --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutPaymentMethodExtension.cshtml @@ -0,0 +1,15 @@ +@using BTCPayServer.Plugins.SideShift +@using Newtonsoft.Json +@using Newtonsoft.Json.Linq +@inject SideShiftService SideShiftService +@{ + const string id = "SideShift"; + var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value(); + var settings = await SideShiftService.GetSideShiftForStore(storeId); + if (settings?.Enabled is true) + { + + @id + + } +} diff --git a/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutTabExtension.cshtml b/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutTabExtension.cshtml new file mode 100644 index 0000000..74a4d8d --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/CheckoutTabExtension.cshtml @@ -0,0 +1,14 @@ +@using BTCPayServer.Plugins.SideShift +@using Newtonsoft.Json +@using Newtonsoft.Json.Linq +@inject SideShiftService SideShiftService +@{ + var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value(); + var settings = await SideShiftService.GetSideShiftForStore(storeId); + if (settings?.Enabled is true) + { +
    + {{$t("Altcoins (SideShift)")}} +
    + } +} diff --git a/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/SideShiftNav.cshtml b/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/SideShiftNav.cshtml new file mode 100644 index 0000000..409b21d --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/SideShiftNav.cshtml @@ -0,0 +1,16 @@ +@using BTCPayServer.Abstractions.Contracts +@using BTCPayServer.Abstractions.Extensions +@using Microsoft.AspNetCore.Mvc.TagHelpers +@inject IScopeProvider ScopeProvider +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); +} +@if (!string.IsNullOrEmpty(storeId)) +{ + +} diff --git a/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/StoreIntegrationSideShiftOption.cshtml b/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/StoreIntegrationSideShiftOption.cshtml new file mode 100644 index 0000000..9127400 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/Views/Shared/SideShift/StoreIntegrationSideShiftOption.cshtml @@ -0,0 +1,59 @@ +@using BTCPayServer.Abstractions.Contracts +@using BTCPayServer.Plugins.SideShift +@using Microsoft.AspNetCore.Mvc.TagHelpers +@using Microsoft.AspNetCore.Routing +@inject SideShiftService SideShiftService +@inject IScopeProvider ScopeProvider +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + + SideShiftSettings settings = null; + if (!string.IsNullOrEmpty(storeId)) + { + try + { + settings = await SideShiftService.GetSideShiftForStore(storeId); + } + catch (Exception) + { + } + } +} +@if (!string.IsNullOrEmpty(storeId)) +{ +
  • +
    + + + SideShift + + + Allows your customers to pay with altcoins that are not supported by BTCPay Server. + + + + @if (settings?.Enabled is true) + { + + + Enabled + + | + + Modify + + } + else + { + + + Disabled + + + Setup + + } + +
    +
  • +} diff --git a/Plugins/BTCPayServer.Plugins.SideShift/Views/SideShift/UpdateSideShiftSettings.cshtml b/Plugins/BTCPayServer.Plugins.SideShift/Views/SideShift/UpdateSideShiftSettings.cshtml new file mode 100644 index 0000000..613aab3 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/Views/SideShift/UpdateSideShiftSettings.cshtml @@ -0,0 +1,28 @@ +@using BTCPayServer.Abstractions.Extensions +@using Microsoft.AspNetCore.Mvc.TagHelpers +@model BTCPayServer.Plugins.SideShift.UpdateSideShiftSettingsViewModel +@{ + ViewData.SetActivePage("SideShift", "SideShift", "SideShift"); +} + + + +

    @ViewData["Title"]

    + + + +
    +
    +
    +
    +
    + + +
    +
    + +
    +
    +
    diff --git a/Plugins/BTCPayServer.Plugins.SideShift/Views/_ViewImports.cshtml b/Plugins/BTCPayServer.Plugins.SideShift/Views/_ViewImports.cshtml new file mode 100644 index 0000000..52e6837 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.SideShift/Views/_ViewImports.cshtml @@ -0,0 +1,2 @@ +@addTagHelper *, BTCPayServer.Abstractions +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/BTCPayServer.Plugins.TicketTailor.csproj b/Plugins/BTCPayServer.Plugins.TicketTailor/BTCPayServer.Plugins.TicketTailor.csproj new file mode 100644 index 0000000..62ab7ad --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.TicketTailor/BTCPayServer.Plugins.TicketTailor.csproj @@ -0,0 +1,40 @@ + + + + net6.0 + 10 + + + + + TicketTailor + Allows you to integrate with TicketTailor.com to sell tickets for Bitcoin + Kukks + 1.0.5 + + + + + true + false + true + + + + + + StaticWebAssetsEnabled=false + false + runtime;native;build;buildTransitive;contentFiles + + + + + + + + + + + + diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/Pack.ps1 b/Plugins/BTCPayServer.Plugins.TicketTailor/Pack.ps1 new file mode 100644 index 0000000..21394c5 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.TicketTailor/Pack.ps1 @@ -0,0 +1,2 @@ +dotnet publish -c Altcoins-Release -o bin/publish/BTCPayServer.Plugins.TicketTailor +dotnet run -p ../../BTCPayServer.PluginPacker bin/publish/BTCPayServer.Plugins.TicketTailor BTCPayServer.Plugins.TicketTailor ../packed diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/Resources/assets/tt.png b/Plugins/BTCPayServer.Plugins.TicketTailor/Resources/assets/tt.png new file mode 100644 index 0000000..1199f94 Binary files /dev/null and b/Plugins/BTCPayServer.Plugins.TicketTailor/Resources/assets/tt.png differ diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorClient.cs b/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorClient.cs new file mode 100644 index 0000000..83c1a68 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorClient.cs @@ -0,0 +1,266 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Net.Http.Json; +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using NBitcoin.DataEncoders; +using NBitcoin.Logging; + +namespace BTCPayServer.Plugins.TicketTailor; + +public class TicketTailorClient : IDisposable +{ + private readonly HttpClient _httpClient; + + public TicketTailorClient(IHttpClientFactory httpClientFactory, string apiKey) + { + _httpClient = httpClientFactory.CreateClient(); + _httpClient.BaseAddress = new Uri("https://api.tickettailor.com"); + _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + _httpClient.DefaultRequestHeaders.Authorization = + new AuthenticationHeaderValue("Basic", Encoders.Base64.EncodeData(Encoding.ASCII.GetBytes(apiKey))); + } + + public async Task GetEvents() + { + return (await _httpClient.GetFromJsonAsync>("/v1/events"))?.Data; + } + + public async Task GetEvent(string id) + { + return await _httpClient.GetFromJsonAsync($"/v1/events/{id}"); + } + + public async Task<(IssuedTicket, string)> CreateTicket(IssueTicketRequest request) + { + var data = JsonSerializer.SerializeToElement(request).EnumerateObject().Select(property => + new KeyValuePair(property.Name, property.Value.GetString())).Where(pair =>pair.Value != null); + + + var response = await _httpClient.PostAsync($"/v1/issued_tickets", new FormUrlEncodedContent(data.ToArray())); + if (!response.IsSuccessStatusCode) + { + var error = await response.Content.ReadAsStringAsync(); + return (null, error); + } + return (await response.Content.ReadFromJsonAsync(), null); + } + + + public async Task GetTicket(string id) + { + return await _httpClient.GetFromJsonAsync($"/v1/issued_tickets/{id}"); + } + + public class DataHolder + { + [JsonPropertyName("data")] public T Data { get; set; } + } + + + public void Dispose() + { + _httpClient?.Dispose(); + } + + public class IssueTicketRequest + { + [JsonPropertyName("event_id")] public string EventId { get; set; } + [JsonPropertyName("ticket_type_id")] public string TicketTypeId { get; set; } + [JsonPropertyName("email")] public string Email { get; set; } + [JsonPropertyName("full_name")] public string FullName { get; set; } + [JsonPropertyName("reference")] public string Reference { get; set; } + [JsonPropertyName("barcode")] public string BarCode { get; set; } + } + + + public class EventEnd + { + [JsonPropertyName("date")] public string Date { get; set; } + + [JsonPropertyName("formatted")] public string Formatted { get; set; } + + [JsonPropertyName("iso")] public DateTime Iso { get; set; } + + [JsonPropertyName("time")] public string Time { get; set; } + + [JsonPropertyName("timezone")] public string Timezone { get; set; } + + [JsonPropertyName("unix")] public int Unix { get; set; } + } + + public class Images + { + [JsonPropertyName("header")] public string Header { get; set; } + + [JsonPropertyName("thumbnail")] public string Thumbnail { get; set; } + } + + public class PaymentMethod + { + [JsonPropertyName("external_id")] public string ExternalId { get; set; } + + [JsonPropertyName("id")] public string Id { get; set; } + + [JsonPropertyName("instructions")] public string Instructions { get; set; } + + [JsonPropertyName("name")] public string Name { get; set; } + + [JsonPropertyName("type")] public string Type { get; set; } + } + + public class Start + { + [JsonPropertyName("date")] public string Date { get; set; } + + [JsonPropertyName("formatted")] public string Formatted { get; set; } + + [JsonPropertyName("iso")] public DateTime Iso { get; set; } + + [JsonPropertyName("time")] public string Time { get; set; } + + [JsonPropertyName("timezone")] public string Timezone { get; set; } + + [JsonPropertyName("unix")] public int Unix { get; set; } + } + + public class TicketGroup + { + [JsonPropertyName("id")] public string Id { get; set; } + + [JsonPropertyName("max_per_order")] public object MaxPerOrder { get; set; } + + [JsonPropertyName("name")] public string Name { get; set; } + + [JsonPropertyName("sort_order")] public int SortOrder { get; set; } + + [JsonPropertyName("ticket_ids")] public List TicketIds { get; set; } + } + + public class TicketType + { + [JsonPropertyName("object")] public string Object { get; set; } + + [JsonPropertyName("id")] public string Id { get; set; } + + [JsonPropertyName("access_code")] public object AccessCode { get; set; } + + [JsonPropertyName("booking_fee")] public int BookingFee { get; set; } + + [JsonPropertyName("description")] public string Description { get; set; } + + [JsonPropertyName("group_id")] public string GroupId { get; set; } + + [JsonPropertyName("max_per_order")] public int MaxPerOrder { get; set; } + + [JsonPropertyName("min_per_order")] public int MinPerOrder { get; set; } + + [JsonPropertyName("name")] public string Name { get; set; } + + [JsonPropertyName("price")] public decimal Price { get; set; } + + [JsonPropertyName("status")] public string Status { get; set; } + + [JsonPropertyName("sort_order")] public int SortOrder { get; set; } + + [JsonPropertyName("type")] public string Type { get; set; } + + [JsonPropertyName("quantity")] public int Quantity { get; set; } + + [JsonPropertyName("quantity_held")] public int QuantityHeld { get; set; } + + [JsonPropertyName("quantity_issued")] public int QuantityIssued { get; set; } + + [JsonPropertyName("quantity_total")] public int QuantityTotal { get; set; } + } + + public class Venue + { + [JsonPropertyName("name")] public string Name { get; set; } + + [JsonPropertyName("postal_code")] public string PostalCode { get; set; } + } + + public class IssuedTicket + { + [JsonPropertyName("id")] public string Id { get; set; } + + [JsonPropertyName("reference")] public string Reference { get; set; } + [JsonPropertyName("description")] public string Description { get; set; } + [JsonPropertyName("status")] public string Status { get; set; } + + [JsonPropertyName("full_name")] public string FullName { get; set; } + + + [JsonPropertyName("qr_code_url")] public string QrCodeUrl { get; set; } + [JsonPropertyName("barcode_url")] public string BarcodeUrl { get; set; } + [JsonPropertyName("barcode")] public string Barcode { get; set; } + [JsonPropertyName("ticket_type_id")] public string TicketTypeId { get; set; } + } + + public class Event + { + [JsonPropertyName("object")] public string Object { get; set; } + + [JsonPropertyName("id")] public string Id { get; set; } + + [JsonPropertyName("access_code")] public object AccessCode { get; set; } + + [JsonPropertyName("call_to_action")] public string CallToAction { get; set; } + + [JsonPropertyName("created_at")] public int CreatedAt { get; set; } + + [JsonPropertyName("currency")] public string Currency { get; set; } + + [JsonPropertyName("description")] public string Description { get; set; } + + [JsonPropertyName("end")] public EventEnd EventEnd { get; set; } + + [JsonPropertyName("hidden")] public string Hidden { get; set; } + + [JsonPropertyName("images")] public Images Images { get; set; } + + [JsonPropertyName("name")] public string Title { get; set; } + + [JsonPropertyName("online_event")] public string OnlineEvent { get; set; } + + [JsonPropertyName("payment_methods")] public List PaymentMethods { get; set; } + + [JsonPropertyName("private")] public string Private { get; set; } + + [JsonPropertyName("start")] public Start Start { get; set; } + + [JsonPropertyName("status")] public string Status { get; set; } + + [JsonPropertyName("ticket_groups")] public List TicketGroups { get; set; } + + [JsonPropertyName("ticket_types")] public List TicketTypes { get; set; } + + [JsonPropertyName("tickets_available")] + public string TicketsAvailable { get; set; } + + [JsonPropertyName("timezone")] public string Timezone { get; set; } + + [JsonPropertyName("total_holds")] public int TotalHolds { get; set; } + + [JsonPropertyName("total_issued_tickets")] + public int TotalIssuedTickets { get; set; } + + [JsonPropertyName("total_orders")] public int TotalOrders { get; set; } + + [JsonPropertyName("unavailable")] public string Unavailable { get; set; } + + [JsonPropertyName("unavailable_status")] + public object UnavailableStatus { get; set; } + + [JsonPropertyName("url")] public string Url { get; set; } + + [JsonPropertyName("venue")] public Venue Venue { get; set; } + } +} diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorController.cs b/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorController.cs new file mode 100644 index 0000000..8045f99 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorController.cs @@ -0,0 +1,503 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Json; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using AngleSharp; +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Extensions; +using BTCPayServer.Client; +using BTCPayServer.Client.Models; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Configuration; +using NBitcoin; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using AuthenticationSchemes = BTCPayServer.Abstractions.Constants.AuthenticationSchemes; +using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration; + +namespace BTCPayServer.Plugins.TicketTailor +{ + [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Route("plugins/{storeId}/TicketTailor")] + public class TicketTailorController : Controller + { + [AllowAnonymous] + [HttpGet("")] + public async Task View(string storeId) + { + var config = await _ticketTailorService.GetTicketTailorForStore(storeId); + try + { + if (config?.ApiKey is not null && config?.EventId is not null) + { + var client = new TicketTailorClient(_httpClientFactory, config.ApiKey); + var evt = await client.GetEvent(config.EventId); + if (evt is null) + { + return NotFound(); + } + + return View(new TicketTailorViewModel() {Event = evt, Settings = config}); + } + } + catch (Exception e) + { + } + + return NotFound(); + } + + + [AllowAnonymous] + [HttpPost("")] + public async Task Purchase(string storeId, string ticketTypeId, string firstName, + string lastName, string email) + { + var config = await _ticketTailorService.GetTicketTailorForStore(storeId); + try + { + if (config?.ApiKey is not null && config?.EventId is not null) + { + var client = new TicketTailorClient(_httpClientFactory, config.ApiKey); + var evt = await client.GetEvent(config.EventId); + if (evt is null || (!config.BypassAvailabilityCheck && (evt.Unavailable == "true" || evt.TicketsAvailable == "false"))) + { + return NotFound(); + } + + var ticketType = evt.TicketTypes.FirstOrDefault(type => type.Id == ticketTypeId); + var specificTicket = + config.SpecificTickets?.SingleOrDefault(ticket => ticketType?.Id == ticket.TicketTypeId); + if (ticketType is not null && specificTicket is not null) + { + ticketType.Price = specificTicket.Price.GetValueOrDefault(ticketType.Price); + } + + if (ticketType is null || (specificTicket is null && ticketType.Status != "on_sale") || + ticketType.Quantity <= 0) + { + return NotFound(); + } + + var btcpayClient = await CreateClient(storeId); + var redirectUrl = Request.GetAbsoluteUri(Url.Action("Receipt", + "TicketTailor", new {storeId, invoiceId = "kukkskukkskukks"})); + redirectUrl = redirectUrl.Replace("kukkskukkskukks", "{InvoiceId}"); + var inv = await btcpayClient.CreateInvoice(storeId, + new CreateInvoiceRequest() + { + Amount = ticketType.Price, + Currency = evt.Currency, + Type = InvoiceType.Standard, + AdditionalSearchTerms = new[] {"tickettailor", ticketTypeId, evt.Id}, + Checkout = + { + RequiresRefundEmail = true, + RedirectAutomatically = ticketType.Price > 0, + RedirectURL = redirectUrl, + }, + Receipt = new InvoiceDataBase.ReceiptOptions() + { + Enabled = false + }, + Metadata = JObject.FromObject(new + { + buyerName = $"{firstName} {lastName}", buyerEmail = email, ticketTypeId,orderId="tickettailor" + }) + }); + + while (inv.Amount == 0 && inv.Status == InvoiceStatus.New) + { + if (inv.Status == InvoiceStatus.New) + inv = await btcpayClient.GetInvoice(inv.StoreId, inv.Id); + } + + if (inv.Status == InvoiceStatus.Settled) + return RedirectToAction("Receipt", new {storeId, invoiceId = inv.Id}); + return Redirect(inv.CheckoutLink); + } + } + catch (Exception e) + { + } + + return RedirectToAction("View", new {storeId}); + } + + + [AllowAnonymous] + [HttpGet("receipt")] + public async Task Receipt(string storeId, string invoiceId) + { + var btcpayClient = await CreateClient(storeId); + try + { + var result = new TicketReceiptPage() {InvoiceId = invoiceId}; + var invoice = await btcpayClient.GetInvoice(storeId, invoiceId); + result.Status = invoice.Status; + if (invoice.Status == InvoiceStatus.Settled) + { + + if (invoice.Metadata.TryGetValue("ticketId", out var ticketId)) + { + await SetTicketTailorTicketResult(storeId, result, ticketId); + } + else + { + invoice = await _ticketTailorService.Handle(invoice.Id, storeId, Request.GetAbsoluteRootUri()); + if (invoice.Metadata.TryGetValue("ticketId", out ticketId)) + { + await SetTicketTailorTicketResult(storeId, result, ticketId); + } + } + } + + return View(result); + } + catch (Exception e) + { + return NotFound(); + } + } + + private async Task SetTicketTailorTicketResult(string storeId, TicketReceiptPage result, JToken ticketId) + { + var settings = await _ticketTailorService.GetTicketTailorForStore(storeId); + var client = new TicketTailorClient(_httpClientFactory, settings.ApiKey); + result.Ticket = await client.GetTicket(ticketId.ToString()); + var evt = await client.GetEvent(settings.EventId); + result.Event = evt; + result.TicketType = + evt.TicketTypes.FirstOrDefault(type => type.Id == result.Ticket.TicketTypeId); + result.Settings = settings; + } + + private async Task CreateClient(string storeId) + { + return await _btcPayServerClientFactory.Create(null, new[] {storeId}, new DefaultHttpContext() + { + Request = + { + Scheme = "https", + Host = Request.Host, + Path = Request.Path, + PathBase = Request.PathBase + } + }); + } + + public class TicketReceiptPage + { + public string InvoiceId { get; set; } + public InvoiceStatus Status { get; set; } + public TicketTailorClient.IssuedTicket Ticket { get; set; } + public TicketTailorClient.Event Event { get; set; } + public TicketTailorClient.TicketType TicketType { get; set; } + public TicketTailorSettings Settings { get; set; } + } + + + private readonly IHttpClientFactory _httpClientFactory; + private readonly TicketTailorService _ticketTailorService; + private readonly IBTCPayServerClientFactory _btcPayServerClientFactory; + private readonly IConfiguration _configuration; + private readonly LinkGenerator _linkGenerator; + + public TicketTailorController(IHttpClientFactory httpClientFactory, + TicketTailorService ticketTailorService, + IBTCPayServerClientFactory btcPayServerClientFactory, + IConfiguration configuration, + LinkGenerator linkGenerator ) + { + + _httpClientFactory = httpClientFactory; + _ticketTailorService = ticketTailorService; + _btcPayServerClientFactory = btcPayServerClientFactory; + _configuration = configuration; + _linkGenerator = linkGenerator; + } + + [HttpGet("update")] + public async Task UpdateTicketTailorSettings(string storeId) + { + UpdateTicketTailorSettingsViewModel vm = new(); + TicketTailorSettings TicketTailor = null; + try + { + TicketTailor = await _ticketTailorService.GetTicketTailorForStore(storeId); + if (TicketTailor is not null) + { + vm.ApiKey = TicketTailor.ApiKey; + vm.EventId = TicketTailor.EventId; + vm.ShowDescription = TicketTailor.ShowDescription; + vm.BypassAvailabilityCheck = TicketTailor.BypassAvailabilityCheck; + vm.CustomCSS = TicketTailor.CustomCSS; + vm.SpecificTickets = TicketTailor.SpecificTickets; + } + } + catch (Exception) + { + // ignored + } + + vm = await SetValues(vm); + + return View(vm); + } + + private async Task SetValues(UpdateTicketTailorSettingsViewModel vm) + { + try + { + if (!string.IsNullOrEmpty(vm.ApiKey)) + { + TicketTailorClient.Event? evt = null; + var client = new TicketTailorClient(_httpClientFactory, vm.ApiKey); + var evts = await client.GetEvents(); + if (vm.EventId is not null && evts.All(e => e.Id != vm.EventId)) + { + vm.EventId = null; + vm.SpecificTickets = new List(); + } + else + { + if (vm.EventId is null) + { + vm.SpecificTickets = new List(); + } + else + { + evt = evts.SingleOrDefault(e => e.Id == vm.EventId); + } + } + + evts = evts.Prepend(new TicketTailorClient.Event() {Id = null, Title = "Select an event"}) + .ToArray(); + vm.Events = new SelectList(evts, nameof(TicketTailorClient.Event.Id), + nameof(TicketTailorClient.Event.Title), vm.EventId); + + if (vm.EventId is not null) + { + vm.TicketTypes = evt?.TicketTypes?.ToArray(); + } + } + } + catch (Exception e) + { + ModelState.AddModelError(nameof(vm.ApiKey), "Api key did not work."); + } + + return vm; + } + + + [HttpPost("update")] + public async Task UpdateTicketTailorSettings(string storeId, + UpdateTicketTailorSettingsViewModel vm, + string command, + [FromServices] BTCPayServerClient btcPayServerClient) + { + vm = await SetValues(vm); + + if (command == "add-specific-ticket" && vm.NewSpecificTicket is not null) + { + vm.SpecificTickets ??= new List(); + vm.SpecificTickets.Add(new() {TicketTypeId = vm.NewSpecificTicket}); + vm.NewSpecificTicket = null; + return View(vm); + } + + if (command.StartsWith("remove-specific-ticket")) + { + var i = int.Parse(command.Substring(command.IndexOf(":", StringComparison.InvariantCultureIgnoreCase) + + 1)); + vm.SpecificTickets.RemoveAt(i); + return View(vm); + } + + if (!ModelState.IsValid) + { + return View(vm); + } + ModelState.Clear(); + var settings = new TicketTailorSettings() + { + ApiKey = vm.ApiKey, + EventId = vm.EventId, + ShowDescription = vm.ShowDescription, + CustomCSS = vm.CustomCSS, + SpecificTickets = vm.SpecificTickets, + BypassAvailabilityCheck = vm.BypassAvailabilityCheck + }; + + var bindAddress = _configuration.GetValue("bind", IPAddress.Loopback); + if (Equals(bindAddress, IPAddress.Any)) + { + bindAddress = IPAddress.Loopback; + } + if (Equals(bindAddress, IPAddress.IPv6Any)) + { + bindAddress = IPAddress.IPv6Loopback; + } + int bindPort = _configuration.GetValue("port", 443); + + string rootPath = _configuration.GetValue("rootpath", "/"); + string attempt1 = null; + if (bindAddress is not null) + { + attempt1 = _linkGenerator.GetUriByAction("Callback", + "TicketTailor", new {storeId,test= true}, "https", new HostString(bindAddress?.ToString(), bindPort), + new PathString(rootPath)); + } + + var attempt2 = Request.GetAbsoluteUri(Url.Action("Callback", + "TicketTailor", new {storeId, test= true})); + + + HttpRequestMessage Create(string uri) + { + return new HttpRequestMessage(HttpMethod.Post, uri) + { + Content = new StringContent( + JsonConvert.SerializeObject(new WebhookInvoiceEvent(WebhookEventType.InvoiceSettled)) + ,Encoding.UTF8, + "application/json"), + + }; + } + + HttpClient CreateClient(string uri) + { + var link = new Uri(uri); + if (link.IsLoopback) + { + return _httpClientFactory.CreateClient("greenfield-webhook.loopback"); + + }else if (link.Host.EndsWith("onion")) + { + return _httpClientFactory.CreateClient("greenfield-webhook.onion"); + } + else + { + return _httpClientFactory.CreateClient("greenfield-webhook.clearnet"); + } + } + + + HttpResponseMessage result = null; + if (attempt1 is not null) + { + + try + { + + result = await CreateClient(attempt1).SendAsync(Create(attempt1), CancellationToken.None); + } + catch (Exception e) + { + + } + } + + string webhookUrl = null; + if (result?.IsSuccessStatusCode is true) + { + webhookUrl = _linkGenerator.GetUriByAction("Callback", + "TicketTailor", new {storeId}, "http", new HostString(bindAddress.ToString(), bindPort), + new PathString(rootPath));; + } + else + { + try + { + result = null; + result = await CreateClient(attempt2).SendAsync(Create(attempt2), CancellationToken.None); + } + catch (Exception e) + { + } + if (result?.IsSuccessStatusCode is true) + { + webhookUrl = Request.GetAbsoluteUri(Url.Action("Callback", + "TicketTailor", new {storeId}));; + } + + } + + if (webhookUrl is null) + { + ModelState.AddModelError("", $"{attempt1} or {attempt2} was not reachable by BTCPayServer."); + + return View(vm); + + }else if (vm.ApiKey is not null && vm.EventId is not null) + { + var webhooks = await btcPayServerClient.GetWebhooks(storeId); + var webhook = webhooks.FirstOrDefault(data => data.Enabled && data.Url == webhookUrl && (data.AuthorizedEvents.Everything || data.AuthorizedEvents.SpecificEvents.Contains(WebhookEventType.InvoiceSettled))); + if (webhook is null) + { + await CreateWebhook(storeId, btcPayServerClient, webhookUrl); + } + } + + switch (command?.ToLowerInvariant()) + { + case "save": + await _ticketTailorService.SetTicketTailorForStore(storeId, settings); + TempData["SuccessMessage"] = "TicketTailor settings modified"; + return RedirectToAction(nameof(UpdateTicketTailorSettings), new {storeId}); + + default: + return View(vm); + } + } + + private static async Task CreateWebhook(string storeId, BTCPayServerClient btcPayServerClient, + string webhookUrl) + { + var wh = await btcPayServerClient.CreateWebhook(storeId, + new CreateStoreWebhookRequest() + { + Enabled = true, + Url = webhookUrl, + AuthorizedEvents = new StoreWebhookBaseData.AuthorizedEventsData() + { + Everything = false, + SpecificEvents = new[] {WebhookEventType.InvoiceSettled} + }, + AutomaticRedelivery = true + }); + return wh.Id; + } + + [AllowAnonymous] + [HttpPost("callback")] + public async Task Callback(string storeId, [FromBody] WebhookInvoiceSettledEvent response, [FromQuery ]bool test) + { + if (test) + { + return Ok(); + } + if (response.StoreId != storeId && response.Type != WebhookEventType.InvoiceSettled) + { + return BadRequest(); + } + + await _ticketTailorService.Handle(response.InvoiceId, response.StoreId, Request.GetAbsoluteRootUri()); + + return Ok(); + } + + + } +} diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorPlugin.cs b/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorPlugin.cs new file mode 100644 index 0000000..7751cb7 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorPlugin.cs @@ -0,0 +1,33 @@ +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Models; +using BTCPayServer.Abstractions.Services; +using Microsoft.Extensions.DependencyInjection; + +namespace BTCPayServer.Plugins.TicketTailor +{ + public class TicketTailorPlugin : BaseBTCPayServerPlugin + { + public override string Identifier => "BTCPayServer.Plugins.TicketTailor"; + public override string Name => "TicketTailor"; + + + public override IBTCPayServerPlugin.PluginDependency[] Dependencies { get; } = + { + new() { Identifier = nameof(BTCPayServer), Condition = ">=1.6.0.0" } + }; + + public override string Description => + "Allows you to integrate with TicketTailor.com to sell tickets for Bitcoin"; + + public override void Execute(IServiceCollection applicationBuilder) + { + applicationBuilder.AddSingleton(); + applicationBuilder.AddHostedService(s=>s.GetRequiredService()); + applicationBuilder.AddSingleton(new UIExtension("TicketTailor/StoreIntegrationTicketTailorOption", + "store-integrations-list")); + applicationBuilder.AddSingleton(new UIExtension("TicketTailor/TicketTailorNav", + "store-integrations-nav")); + base.Execute(applicationBuilder); + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorService.cs b/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorService.cs new file mode 100644 index 0000000..30c961c --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorService.cs @@ -0,0 +1,236 @@ +using System; +using System.Net.Http; +using System.Threading; +using System.Threading.Channels; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Client; +using BTCPayServer.Client.Models; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Plugins.TicketTailor; + +public class TicketTailorService : IHostedService +{ + private readonly ISettingsRepository _settingsRepository; + private readonly IMemoryCache _memoryCache; + private readonly IHttpClientFactory _httpClientFactory; + private readonly IStoreRepository _storeRepository; + private readonly ILogger _logger; + private readonly IBTCPayServerClientFactory _btcPayServerClientFactory; + private readonly LinkGenerator _linkGenerator; + + public TicketTailorService(ISettingsRepository settingsRepository, IMemoryCache memoryCache, + IHttpClientFactory httpClientFactory, + IStoreRepository storeRepository, ILogger logger, + IBTCPayServerClientFactory btcPayServerClientFactory, LinkGenerator linkGenerator) + { + _settingsRepository = settingsRepository; + _memoryCache = memoryCache; + _httpClientFactory = httpClientFactory; + _storeRepository = storeRepository; + _logger = logger; + _btcPayServerClientFactory = btcPayServerClientFactory; + _linkGenerator = linkGenerator; + } + + + public async Task GetTicketTailorForStore(string storeId) + { + var k = $"{nameof(TicketTailorSettings)}_{storeId}"; + return await _memoryCache.GetOrCreateAsync(k, async _ => + { + var res = await _storeRepository.GetSettingAsync(storeId, + nameof(TicketTailorSettings)); + if (res is not null) return res; + res = await _settingsRepository.GetSettingAsync(k); + + if (res is not null) + { + await SetTicketTailorForStore(storeId, res); + } + + await _settingsRepository.UpdateSetting(null, k); + return res; + }); + } + + public async Task SetTicketTailorForStore(string storeId, TicketTailorSettings TicketTailorSettings) + { + var k = $"{nameof(TicketTailorSettings)}_{storeId}"; + await _storeRepository.UpdateSetting(storeId, nameof(TicketTailorSettings), TicketTailorSettings); + _memoryCache.Set(k, TicketTailorSettings); + } + + + public Task Handle(string invoiceId, string storeId, Uri host) + { + var tcs = new TaskCompletionSource(); + _events.Writer.TryWrite(new IssueTicket() {Task = tcs, InvoiceId = invoiceId, StoreId = storeId, Host = host}); + return tcs.Task; + } + + internal class IssueTicket + { + public string InvoiceId { get; set; } + public string StoreId { get; set; } + public TaskCompletionSource Task { get; set; } + public Uri Host { get; set; } + } + + + readonly Channel _events = Channel.CreateUnbounded(); + + public Task StartAsync(CancellationToken cancellationToken) + { + _ = ProcessEvents(cancellationToken); + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + + public async Task CreateClient(string storeId, Uri host) + { + return await _btcPayServerClientFactory.Create(null, new []{storeId}, new DefaultHttpContext() + { + Request = + { + Scheme = host.Scheme, + Host = new HostString(host.Host), + Path = new PathString(host.AbsolutePath), + PathBase = new PathString(), + } + }); + } + + private async Task ProcessEvents(CancellationToken cancellationToken) + { + while (await _events.Reader.WaitToReadAsync(cancellationToken)) + { + if (!_events.Reader.TryRead(out var evt)) continue; + + async Task HandleIssueTicketError(JToken posData, string e, InvoiceData invoiceData, + BTCPayServerClient btcPayClient) + { + posData["Error"] = + $"Ticket could not be created. You should refund customer.{Environment.NewLine}{e}"; + invoiceData.Metadata["posData"] = posData; + await btcPayClient.UpdateInvoice(evt.StoreId, invoiceData.Id, + new UpdateInvoiceRequest() {Metadata = invoiceData.Metadata}, cancellationToken); + try + { + await btcPayClient.MarkInvoiceStatus(evt.StoreId, invoiceData.Id, + new MarkInvoiceStatusRequest() {Status = InvoiceStatus.Invalid}, cancellationToken); + } + catch (Exception exception) + { + _logger.LogError(exception, $"Failed to update invoice {invoiceData.Id} status from {invoiceData.Status} to Invalid after failing to issue ticket from ticket tailor"); + } + } + + InvoiceData invoice = null; + try + { + var settings = await GetTicketTailorForStore(evt.StoreId); + if (settings is null || settings.ApiKey is null) + { + evt.Task.SetResult(null); + continue; + } + + var btcPayClient = await CreateClient(evt.StoreId, evt.Host); + invoice = await btcPayClient.GetInvoice(evt.StoreId, evt.InvoiceId, cancellationToken); + if (invoice.Status != InvoiceStatus.Settled) + { + evt.Task.SetResult(null); + continue; + } + + if (invoice.Metadata.ContainsKey("ticketId")) + { + evt.Task.SetResult(null); + continue; + } + + var ticketTypeId = invoice.Metadata["ticketTypeId"].ToString(); + var email = invoice.Metadata["buyerEmail"].ToString(); + var name = invoice.Metadata["buyerName"]?.ToString(); + invoice.Metadata.TryGetValue("posData", out var posData); + posData ??= new JObject(); + var client = new TicketTailorClient(_httpClientFactory, settings.ApiKey); + try + { + var ticketResult = await client.CreateTicket(new TicketTailorClient.IssueTicketRequest() + { + Reference = invoice.Id, + Email = email, + EventId = settings.EventId, + TicketTypeId = ticketTypeId, + FullName = name, + }); + + if (ticketResult.Item2 is not null) + { + await HandleIssueTicketError(posData, ticketResult.Item2, invoice, btcPayClient); + + continue; + } + + var ticket = ticketResult.Item1; + invoice.Metadata["ticketId"] = ticket.Id; + invoice.Metadata["orderId"] = $"tickettailor_{ticket.Id}"; + + posData["Ticket Code"] = ticket.Barcode; + posData["Ticket Id"] = ticket.Id; + invoice.Metadata["posData"] = posData; + await btcPayClient.UpdateInvoice(evt.StoreId, invoice.Id, + new UpdateInvoiceRequest() {Metadata = invoice.Metadata}, cancellationToken); + + var url = + _linkGenerator.GetUriByAction("Receipt", + "TicketTailor", + new {evt.StoreId, invoiceId = invoice.Id}, + evt.Host.Scheme, + new HostString(evt.Host.Host), + evt.Host.AbsolutePath); + + try + { + await btcPayClient.SendEmail(evt.StoreId, + new SendEmailRequest() + { + Subject = "Your ticket is available now.", + Email = email, + Body = + $"Your payment has been settled and the event ticket has been issued successfully. Please go to {url}" + }, cancellationToken); + } + catch (Exception e) + { + // ignored + } + } + catch (Exception e) + { + await HandleIssueTicketError(posData, e.Message, invoice, btcPayClient); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to issue ticket"); + } + finally + { + evt.Task.SetResult(invoice); + } + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorSettings.cs b/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorSettings.cs new file mode 100644 index 0000000..d797ff9 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.TicketTailor/TicketTailorSettings.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +namespace BTCPayServer.Plugins.TicketTailor +{ + public class TicketTailorSettings + { + public string ApiKey { get; set; } + public string EventId { get; set; } + + public bool ShowDescription { get; set; } + public string CustomCSS { get; set; } + public List SpecificTickets { get; set; } + public bool BypassAvailabilityCheck { get; set; } + } +} diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/UpdateTicketTailorSettingsViewModel.cs b/Plugins/BTCPayServer.Plugins.TicketTailor/UpdateTicketTailorSettingsViewModel.cs new file mode 100644 index 0000000..e4e85fc --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.TicketTailor/UpdateTicketTailorSettingsViewModel.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc.Rendering; + +namespace BTCPayServer.Plugins.TicketTailor; + +public class UpdateTicketTailorSettingsViewModel +{ + public string NewSpecificTicket { get; set; } + public string ApiKey { get; set; } + public SelectList Events { get; set; } + public string EventId { get; set; } + public bool ShowDescription { get; set; } + public string CustomCSS { get; set; } + public TicketTailorClient.TicketType[] TicketTypes { get; set; } + + public List SpecificTickets { get; set; } + public bool BypassAvailabilityCheck { get; set; } +} + +public class SpecificTicket +{ + public string TicketTypeId { get; set; } + public decimal? Price { get; set; } + public string Name { get; set; } + public string Description { get; set; } + public bool Hidden { get; set; } +} + +public class TicketTailorViewModel +{ + public TicketTailorClient.Event Event { get; set; } + public TicketTailorSettings Settings { get; set; } +} diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/Views/Shared/TicketTailor/StoreIntegrationTicketTailorOption.cshtml b/Plugins/BTCPayServer.Plugins.TicketTailor/Views/Shared/TicketTailor/StoreIntegrationTicketTailorOption.cshtml new file mode 100644 index 0000000..8c426e3 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.TicketTailor/Views/Shared/TicketTailor/StoreIntegrationTicketTailorOption.cshtml @@ -0,0 +1,59 @@ +@using BTCPayServer.Client +@using BTCPayServer.Plugins.TicketTailor +@using Microsoft.AspNetCore.Routing +@using BTCPayServer.Abstractions.Contracts +@inject IScopeProvider ScopeProvider +@inject TicketTailorService TicketTailorService +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + + TicketTailorSettings settings = null; + if (!string.IsNullOrEmpty(storeId)) + { + try + { + settings = await TicketTailorService.GetTicketTailorForStore(storeId); + } + catch (Exception) + { + } + } +} +@if (!string.IsNullOrEmpty(storeId)) +{ +
  • +
    + + + Ticket Tailor + + + Sell tickets on Ticket Tailor using BTCPay Server + + + + @if (settings?.ApiKey is not null) + { + + + Active + + | + + Modify + + } + else + { + + + Disabled + + + Setup + + } + +
    +
  • +} diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/Views/Shared/TicketTailor/TicketTailorNav.cshtml b/Plugins/BTCPayServer.Plugins.TicketTailor/Views/Shared/TicketTailor/TicketTailorNav.cshtml new file mode 100644 index 0000000..f8e77f6 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.TicketTailor/Views/Shared/TicketTailor/TicketTailorNav.cshtml @@ -0,0 +1,17 @@ +@using BTCPayServer.Plugins.TicketTailor +@inject IScopeProvider ScopeProvider +@using BTCPayServer.Abstractions.Contracts +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + var isActive = !string.IsNullOrEmpty(storeId) && ViewContext.RouteData.Values.TryGetValue("Controller", out var controller) && controller is not null && + nameof(TicketTailorController).StartsWith(controller?.ToString(), StringComparison.InvariantCultureIgnoreCase); +} +@if (!string.IsNullOrEmpty(storeId)) +{ + +} diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/Views/TicketTailor/Receipt.cshtml b/Plugins/BTCPayServer.Plugins.TicketTailor/Views/TicketTailor/Receipt.cshtml new file mode 100644 index 0000000..77ab3fb --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.TicketTailor/Views/TicketTailor/Receipt.cshtml @@ -0,0 +1,133 @@ +@using BTCPayServer.Client.Models +@model BTCPayServer.Plugins.TicketTailor.TicketTailorController.TicketReceiptPage +@inject ContentSecurityPolicies contentSecurityPolicies +@using BTCPayServer.Security +@using NBitcoin +@{ + var nonce = RandomUtils.GetUInt256().ToString().Substring(0, 32); + contentSecurityPolicies.Add("script-src", $"'nonce-{nonce}'"); + contentSecurityPolicies.AllowUnsafeHashes(); + Layout = "_LayoutSimple"; + var reloadPage = false; +} + +
    +
    + +
    + +

    Ticket Receipt

    + +
    + @if (Model.Status == InvoiceStatus.Processing) + { + reloadPage = true; +
    + The invoice has detected a payment but is still waiting to be settled. This page will refresh periodically until it is settled. + +
    + } + else if (Model.Status != InvoiceStatus.Settled) + { +
    + The invoice is not settled. +
    + } + else if (Model.Ticket is null) + { + + reloadPage = true; +
    + The invoice is settled but the ticket has not been issued yet. This page will refresh periodically until it is issued. +
    + } + else + { + var specificTicketName = Model.Settings?.SpecificTickets?.FirstOrDefault(ticket => ticket.TicketTypeId == Model.TicketType.Id)?.Name ?? Model.TicketType.Name; + +
    +
    +
    +

    Ticket Details

    +
    +
    + Please ensure you can see this QR barcode +
    +
    + Please ensure you can see this barcode +
    +
    +
    +
    @Model.Ticket.Barcode
    TICKET CODE
    +
    + @if (!string.IsNullOrEmpty(Model.Ticket.Reference)) + { +
    +
    @Model.Ticket.Reference
    REFERENCE
    +
    + } + + @if (!string.IsNullOrEmpty(Model.Ticket.FullName)) + { +
    +
    @Model.Ticket.FullName
    ATTENDEE NAME
    +
    + } +
    +
    @specificTicketName
    TICKET TYPE
    +
    +
    +
    +
    +
    +

    Event Details

    +
    +
    @Model.Event.Title
    EVENT
    +
    +
    +
    + @Model.Event.Url +
    EVENT URL
    +
    +
    +
    @Model.Event.Start.Formatted - @Model.Event.EventEnd.Formatted
    +
    Date
    +
    +
    +
    @Model.Event.Venue.Name
    +
    Venue
    +
    +
    +
    +
    + } +
    +
    + Powered by BTCPay Server +
    +
    +
    +
    + +@if (reloadPage) +{ + +} + diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/Views/TicketTailor/UpdateTicketTailorSettings.cshtml b/Plugins/BTCPayServer.Plugins.TicketTailor/Views/TicketTailor/UpdateTicketTailorSettings.cshtml new file mode 100644 index 0000000..fa0e93a --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.TicketTailor/Views/TicketTailor/UpdateTicketTailorSettings.cshtml @@ -0,0 +1,134 @@ +@using BTCPayServer.Abstractions.Extensions +@using BTCPayServer.Client +@using BTCPayServer.Plugins.TicketTailor +@using Microsoft.AspNetCore.Mvc.TagHelpers +@using Microsoft.AspNetCore.Routing +@using BTCPayServer.Abstractions.Contracts +@model BTCPayServer.Plugins.TicketTailor.UpdateTicketTailorSettingsViewModel +@inject IScopeProvider ScopeProvider +@{ + var storeId = ScopeProvider.GetCurrentStoreId(); + Layout = "../Shared/_NavLayout.cshtml"; + ViewData["NavPartialName"] = "../UIStores/_Nav"; + ViewData.SetActivePage("TicketTailor", "Update Store TicketTailor Settings", null); + Model.SpecificTickets ??= new List(); + +} + +

    @ViewData["Title"]

    +@if (ViewContext.ModelState.IsValid && Model.EventId is not null) +{ +
    + Please ensure that emails in your store are configured if you wish to send the tickets via email to customers as TicketTailor does not handle it for tickets issued via its API. +
    + Ensure that the url used for btcpayserver is accessible publicly. +
    +} +
    +
    +
    +
    + +
    + + + +
    + + @if (Model.Events is not null) + { +
    + + +
    + } + + @if (Model.EventId is not null) + { +
    + + + +
    +
    + + + +
    + +
    + + + +
    + + @if (Model.TicketTypes?.Any() is true) + { + var uniqueRemaining = Model.TicketTypes.Where(type => Model.SpecificTickets?.Any(ticket => ticket.TicketTypeId == type.Id) is false); +
    +
    Specific tickets
    +

    Specific tickets allow you to override explicitly which tickets you wish to allow to sell, and also override the price, name, and description of these tickets.

    + @if (uniqueRemaining.Any()) + { + SelectList sl = new SelectList(uniqueRemaining, nameof(TicketTailorClient.TicketType.Id), nameof(TicketTailorClient.TicketType.Name)); +
    + +
    + + + +
    +
    + } + @for (var index = 0; index < Model.SpecificTickets.Count; index++) + { + var specific = Model.SpecificTickets[index]; + var ticketType = Model.TicketTypes.SingleOrDefault(type => type.Id == specific.TicketTypeId); + if (ticketType is null) + { + continue; + } +
    +
    + @ticketType.Name +
    +
    +
    +
    + + + +
    +
    + +
    + +
    +
    + + + +
    +
    + +
    + } +
    + } + } +
    + + @if (this.ViewContext.ModelState.IsValid && Model.EventId is not null) + { + + Ticket purchase page + + } +
    +
    +
    +
    + + diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/Views/TicketTailor/View.cshtml b/Plugins/BTCPayServer.Plugins.TicketTailor/Views/TicketTailor/View.cshtml new file mode 100644 index 0000000..5f66d28 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.TicketTailor/Views/TicketTailor/View.cshtml @@ -0,0 +1,163 @@ +@using Microsoft.AspNetCore.Routing +@using BTCPayServer.Plugins.TicketTailor +@model BTCPayServer.Plugins.TicketTailor.TicketTailorViewModel + +@inject BTCPayServer.Security.ContentSecurityPolicies csp; +@{ + Layout = "_LayoutSimple"; + var available = Model.Settings.BypassAvailabilityCheck || (Model.Event.Unavailable != "true" && Model.Event.TicketsAvailable == "true"); + Model.Settings.SpecificTickets ??= new List(); +} + +
    +
    + + +

    @Model.Event.Title

    +

    @Model.Event.Start.Formatted - @Model.Event.EventEnd.Formatted

    + @if (Model.Settings.ShowDescription && !string.IsNullOrEmpty(Model.Event.Description)) + { +
    +
    @Safe.Raw(Model.Event.Description)
    +
    + } +
    +
    + +
    +
    + + +
    +
    + +
    +
    +
    +
    + + +
    +
    +
    +
    + + +
    +
    +
    +
    + + @for (int x = 0; x < Model.Event.TicketTypes.Count; x++) + { + var item = Model.Event.TicketTypes[x]; + var availableToShow = new[] {"on_sale", "sold_out", "unavailable"}.Contains(item.Status); + var specific = false; + + if (Model.Settings.SpecificTickets?.Any() is true) + { + var matched = Model.Settings.SpecificTickets.FirstOrDefault(ticket => ticket.TicketTypeId == item.Id); + if (matched is null || matched.Hidden) + { + continue; + } + if (matched.Price is not null) + { + item.Price = matched.Price.Value; + } + if (!string.IsNullOrEmpty(matched.Name)) + { + item.Name = matched.Name; + } + if (!string.IsNullOrEmpty(matched.Description)) + { + item.Description = matched.Description; + } + availableToShow = true; + specific = true; + } + if (!availableToShow) + { + continue; + } +
    + + @{ CardBody(item.Name, item.Description); } + +
    + } +
    +
    + + +
    + +
    + Powered by BTCPay Server +
    +
    +
    +
    + +@functions { + + + private void CardBody(string title, string description) + { +
    +
    @title
    + @if (!String.IsNullOrWhiteSpace(description)) + { +

    @Html.Raw(description)

    + } +
    + } + +} diff --git a/Plugins/BTCPayServer.Plugins.TicketTailor/Views/_ViewImports.cshtml b/Plugins/BTCPayServer.Plugins.TicketTailor/Views/_ViewImports.cshtml new file mode 100644 index 0000000..04173ab --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.TicketTailor/Views/_ViewImports.cshtml @@ -0,0 +1,4 @@ +@using BTCPayServer.Abstractions.Services + +@inject Safe Safe +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/Plugins/BTCPayServer.Plugins.Wabisabi/BTCPayCoinjoinCoinSelector.cs b/Plugins/BTCPayServer.Plugins.Wabisabi/BTCPayCoinjoinCoinSelector.cs new file mode 100644 index 0000000..9933efa --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Wabisabi/BTCPayCoinjoinCoinSelector.cs @@ -0,0 +1,406 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BTCPayServer.Client.Models; +using Microsoft.Extensions.Logging; +using NBitcoin; +using WalletWasabi.Blockchain.TransactionOutputs; +using WalletWasabi.Crypto.Randomness; +using WalletWasabi.Extensions; +using WalletWasabi.WabiSabi.Backend.Rounds; +using WalletWasabi.WabiSabi.Client; +using WalletWasabi.Wallets; + +namespace BTCPayServer.Plugins.Wabisabi; + +public class BTCPayCoinjoinCoinSelector : IRoundCoinSelector +{ + private readonly BTCPayWallet _wallet; + private readonly ILogger _logger; + + public BTCPayCoinjoinCoinSelector(BTCPayWallet wallet, ILogger logger) + { + _wallet = wallet; + _logger = logger; + } + + public async Task> SelectCoinsAsync(IEnumerable coinCandidates, + UtxoSelectionParameters utxoSelectionParameters, + Money liquidityClue, SecureRandom secureRandom) + { + coinCandidates = + coinCandidates + .Where(coin => utxoSelectionParameters.AllowedInputScriptTypes.Contains(coin.ScriptType)) + .Where(coin => utxoSelectionParameters.AllowedInputAmounts.Contains(coin.Amount)) + .Where(coin => + { + var effV = coin.EffectiveValue(utxoSelectionParameters.MiningFeeRate, + utxoSelectionParameters.CoordinationFeeRate); + var percentageLeft = (effV.ToDecimal(MoneyUnit.BTC) / coin.Amount.ToDecimal(MoneyUnit.BTC)); + // filter out low value coins where 50% of the value would be eaten up by fees + return effV > 0 && percentageLeft >= 0.5m; + }); + var payments = + _wallet.BatchPayments + ? await _wallet.DestinationProvider.GetPendingPaymentsAsync(utxoSelectionParameters) + : Array.Empty(); + var minCoins = new Dictionary(); + if (_wallet.RedCoinIsolation) + { + minCoins.Add(AnonsetType.Red, 1); + } + + var solution = SelectCoinsInternal(utxoSelectionParameters, coinCandidates, payments, + Random.Shared.Next(10, 31), + minCoins, + new Dictionary() {{AnonsetType.Red, 1}, {AnonsetType.Orange, 1}, {AnonsetType.Green, 1}}, + _wallet.ConsolidationMode, liquidityClue); + _logger.LogInformation(solution.ToString()); + // SubsetSolution bestSolution = null; + // for (int i = 0; i < 100; i++) + // { + // var minCoins = new Dictionary(); + // if (_wallet.RedCoinIsolation) + // { + // minCoins.Add(AnonsetType.Red, 1); + // } + // var solution = SelectCoinsInternal(utxoSelectionParameters, coinCandidates, payments, Random.Shared.Next(10,31), + // minCoins, new Dictionary() + // { + // + // {AnonsetType.Red, 1}, + // {AnonsetType.Orange, 1}, + // {AnonsetType.Green, 1} + // },_wallet.ConsolidationMode, liquidityClue); + // if (bestSolution is null || solution.Score() > bestSolution.Score()) + // { + // bestSolution = solution; + // } + // } + // _logger.LogInformation(bestSolution.ToString()); + // return bestSolution.Coins.ToImmutableList(); + return solution.Coins.ToImmutableList(); + } + + private SubsetSolution SelectCoinsInternal(UtxoSelectionParameters utxoSelectionParameters, + IEnumerable coins, IEnumerable pendingPayments, + int maxCoins, + Dictionary maxPerType, Dictionary idealMinimumPerType, + bool consolidationMode, Money liquidityClue) + { + var stopwatch = Stopwatch.StartNew(); + + // Sort the coins by their anon score and then by descending order their value, and then slightly randomize in 2 ways: + //attempt to shift coins that comes from the same tx AND also attempt to shift coins based on percentage probability + var remainingCoins = SlightlyShiftOrder(RandomizeCoins( + coins.OrderBy(coin => coin.CoinColor(_wallet.AnonymitySetTarget)).ThenByDescending(x => + x.EffectiveValue(utxoSelectionParameters.MiningFeeRate, + utxoSelectionParameters.CoordinationFeeRate)) + .ToList(), liquidityClue), 10); + var remainingPendingPayments = new List(pendingPayments); + var solution = new SubsetSolution(remainingPendingPayments.Count, _wallet.AnonymitySetTarget, + utxoSelectionParameters); + + if (remainingCoins.All(coin => coin.CoinColor(_wallet.AnonymitySetTarget) == AnonsetType.Green) && + !remainingPendingPayments.Any()) + { + // var decidedAmt = Random.Shared.Next(10, maxCoins); + // // all the coins are mixed and we have no payments to do.. + // //if we are trying to reduce our utxoset, and we + // if (consolidationMode && remainingCoins.Count >= decidedAmt) + // { + // + // for (int i = 0; i < decidedAmt; i++) + // { + // + // var anonsetOrderedCoin = + // remainingCoins.OrderBy(coin => coin.AnonymitySet).BiasedRandomElement(70); + // solution.Coins.Add(anonsetOrderedCoin); + // remainingCoins.Remove(anonsetOrderedCoin); + // } + // } + // else + // { + //still good to have a chance to proceed with a join to reduce timing analysis + + var rand = Random.Shared.Next(1, 101); + if (rand > 5) + { + _logger.LogInformation($"All coins are private and we have no pending payments. Skipping join."); + return solution; + } + + _logger.LogInformation( + "All coins are private and we have no pending payments but will join just to reduce timing analysis"); + //} + } + + while (remainingCoins.Any()) + { + var coinColorCount = solution.SortedCoins.ToDictionary(pair => pair.Key, pair => pair.Value.Length); + + var predicate = new Func(_ => true); + foreach (var coinColor in idealMinimumPerType.ToShuffled()) + { + if (coinColor.Value != 0) + { + coinColorCount.TryGetValue(coinColor.Key, out var currentCoinColorCount); + if (currentCoinColorCount < coinColor.Value) + { + predicate = coin1 => coin1.CoinColor(_wallet.AnonymitySetTarget) == coinColor.Key; + break; + } + } + else + { + //if the ideal amount = 0, then we should de-prioritize. + predicate = coin1 => coin1.CoinColor(_wallet.AnonymitySetTarget) != coinColor.Key; + break; + } + } + + var coin = remainingCoins.FirstOrDefault(predicate) ?? remainingCoins.First(); + var color = coin.CoinColor(_wallet.AnonymitySetTarget); + // If the selected coins list is at its maximum size, break out of the loop + if (solution.Coins.Count == maxCoins) + { + break; + } + + remainingCoins.Remove(coin); + if (maxPerType.TryGetValue(color, out var maxColor) && + solution.Coins.Count(coin1 => coin1.CoinColor(_wallet.AnonymitySetTarget) == color) == maxColor) + { + continue; + } + + solution.Coins.Add(coin); + + // Loop through the pending payments and handle each payment by subtracting the payment amount from the total value of the selected coins + var potentialPayments = remainingPendingPayments + .Where(payment => + payment.ToTxOut().EffectiveCost(utxoSelectionParameters.MiningFeeRate).ToDecimal(MoneyUnit.BTC) <= + solution.LeftoverValue).ToShuffled(); + + while (potentialPayments.Any()) + { + var payment = potentialPayments.First(); + solution.HandledPayments.Add(payment); + remainingPendingPayments.Remove(payment); + potentialPayments = remainingPendingPayments.Where(payment => + payment.ToTxOut().EffectiveCost(utxoSelectionParameters.MiningFeeRate).ToDecimal(MoneyUnit.BTC) <= + solution.LeftoverValue).ToShuffled(); + } + + if (!remainingPendingPayments.Any()) + { + //if we're in consolidation mode, we should use more than one coin at the very least + if (solution.Coins.Count == 1 && consolidationMode) + { + continue; + } + + var rand = Random.Shared.Next(1, 101); + //let's check how many coins we are allowed to add max and how many we added, and use that percentage as the random chance of not adding it. + // if max coins = 20, and current coins = 5 then 5/20 = 0.25 * 100 = 25 + var maxCoinCapacityPercentage = Math.Floor((solution.Coins.Count / (decimal)maxCoins) * 100); + //aggressively attempt to reach max coin target if consolidation mode is on + var chance = consolidationMode ? 90 : 100 - maxCoinCapacityPercentage; + _logger.LogDebug( + $"coin selection: no payms left but at {solution.Coins.Count()} coins. random chance to add another coin if: {chance} <= {rand} (random 0-100) "); + if (chance <= rand) + { + break; + } + } + } + + stopwatch.Stop(); + solution.TimeElapsed = stopwatch.Elapsed; + return solution; + } + + static List SlightlyShiftOrder(List list, int chanceOfShiftPercentage) + { + // Create a random number generator + var rand = new Random(); + List workingList = new List(list); +// Loop through the coins and determine whether to swap the positions of two consecutive coins in the list + for (int i = 0; i < workingList.Count() - 1; i++) + { + // If a random number between 0 and 1 is less than or equal to 0.1, swap the positions of the current and next coins in the list + if (rand.NextDouble() <= ((double)chanceOfShiftPercentage / 100)) + { + // Swap the positions of the current and next coins in the list + (workingList[i], workingList[i + 1]) = (workingList[i + 1], workingList[i]); + } + } + + return workingList; + } + + private List RandomizeCoins(List coins, Money liquidityClue) + { + var remainingCoins = new List(coins); + var workingList = new List(); + while (remainingCoins.Any()) + { + var currentCoin = remainingCoins.First(); + remainingCoins.RemoveAt(0); + var lastCoin = workingList.LastOrDefault(); + if (lastCoin is null || currentCoin.CoinColor(_wallet.AnonymitySetTarget) == AnonsetType.Green || + !remainingCoins.Any() || + (remainingCoins.Count == 1 && remainingCoins.First().TransactionId == currentCoin.TransactionId) || + lastCoin.TransactionId != currentCoin.TransactionId || + liquidityClue <= currentCoin.Amount || + Random.Shared.Next(0, 10) < 5) + { + workingList.Add(currentCoin); + } + else + { + remainingCoins.Insert(1, currentCoin); + } + } + + + return workingList.ToList(); + } +} + +public static class SmartCoinExtensions +{ + public static AnonsetType CoinColor(this SmartCoin coin, int anonsetTarget) + { + return coin.AnonymitySet <= 1 ? AnonsetType.Red : + coin.AnonymitySet >= anonsetTarget ? AnonsetType.Green : AnonsetType.Orange; + } +} + +public enum AnonsetType +{ + Red, + Orange, + Green +} + +public class SubsetSolution : IEquatable +{ + private readonly UtxoSelectionParameters _utxoSelectionParameters; + + public SubsetSolution(int totalPaymentsGross, int anonsetTarget, UtxoSelectionParameters utxoSelectionParameters) + { + _utxoSelectionParameters = utxoSelectionParameters; + TotalPaymentsGross = totalPaymentsGross; + AnonsetTarget = anonsetTarget; + } + + public TimeSpan TimeElapsed { get; set; } + public List Coins { get; set; } = new(); + public List HandledPayments { get; set; } = new(); + + public decimal TotalValue => Coins.Sum(coin => + coin.EffectiveValue(_utxoSelectionParameters.MiningFeeRate, _utxoSelectionParameters.CoordinationFeeRate) + .ToDecimal(MoneyUnit.BTC)); + + public Dictionary SortedCoins => + Coins.GroupBy(coin => coin.CoinColor(AnonsetTarget)).ToDictionary(coins => coins.Key, coins => coins.ToArray()); + + public int TotalPaymentsGross { get; } + public int AnonsetTarget { get; } + + public decimal TotalPaymentCost => HandledPayments.Sum(payment => + payment.ToTxOut().EffectiveCost(_utxoSelectionParameters.MiningFeeRate).ToDecimal(MoneyUnit.BTC)); + + public decimal LeftoverValue => TotalValue - TotalPaymentCost; + + public decimal Score() + { + var score = 0m; + + decimal ComputeCoinScore(List coins) + { + var w = 0m; + foreach (var smartCoin in coins) + { + var val = smartCoin.EffectiveValue(_utxoSelectionParameters.MiningFeeRate, + _utxoSelectionParameters.CoordinationFeeRate).ToDecimal(MoneyUnit.BTC); + if (smartCoin.AnonymitySet <= 0) + { + w += val; + } + else + { + w += val / (decimal)smartCoin.AnonymitySet; + } + } + + return w; // / (coins.Count == 0 ? 1 : coins.Count); + } + + decimal ComputePaymentScore(List pendingPayments) + { + return TotalPaymentsGross == 0 ? 100 : (pendingPayments.Count / (decimal)TotalPaymentsGross) * 100; + } + + score += ComputeCoinScore(Coins); + score += ComputePaymentScore(HandledPayments); + + return score; + } + + + public string GetId() + { + return string.Join("-", + Coins.OrderBy(coin => coin.Outpoint).Select(coin => coin.Outpoint.ToString()) + .Concat(HandledPayments.OrderBy(arg => arg.Value).Select(p => p.Value.ToString()))); + } + + public override string ToString() + { + var sb = new StringBuilder(); + if (!Coins.Any()) + { + return "Solution yielded no selection of coins"; + } + + var sc = SortedCoins; + sc.TryGetValue(AnonsetType.Green, out var gcoins); + sc.TryGetValue(AnonsetType.Orange, out var ocoins); + sc.TryGetValue(AnonsetType.Red, out var rcoins); + sb.AppendLine( + $"Solution total coins:{Coins.Count} R:{rcoins?.Length ?? 0} O:{ocoins?.Length ?? 0} G:{gcoins?.Length ?? 0} AL:{GetAnonLoss(Coins)} total value: {TotalValue} total payments:{TotalPaymentCost}/{TotalPaymentsGross} leftover: {LeftoverValue} score: {Score()} Compute time: {TimeElapsed} "); + sb.AppendLine( + $"Used coins: {string.Join(", ", Coins.Select(coin => coin.Outpoint + " " + coin.Amount.ToString() + " A" + coin.AnonymitySet))}"); + if (HandledPayments.Any()) + sb.AppendLine($"handled payments: {string.Join(", ", HandledPayments.Select(p => p.Value))} "); + return sb.ToString(); + } + + public bool Equals(SubsetSolution? other) + { + return GetId() == other?.GetId(); + } + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((SubsetSolution)obj); + } + + private static decimal GetAnonLoss(IEnumerable coins) + where TCoin : SmartCoin + { + double minimumAnonScore = coins.Min(x => x.AnonymitySet); + var rawSum = coins.Sum(x => x.Amount); + return coins.Sum(x => + ((decimal)x.AnonymitySet - (decimal)minimumAnonScore) * x.Amount.ToDecimal(MoneyUnit.BTC)) / rawSum; + } +} diff --git a/Plugins/BTCPayServer.Plugins.Wabisabi/BTCPayKeyChain.cs b/Plugins/BTCPayServer.Plugins.Wabisabi/BTCPayKeyChain.cs new file mode 100644 index 0000000..e99826d --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Wabisabi/BTCPayKeyChain.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using NBitcoin; +using NBXplorer; +using NBXplorer.DerivationStrategy; +using WalletWasabi.Blockchain.Keys; +using WalletWasabi.Crypto; +using WalletWasabi.Extensions; +using WalletWasabi.WabiSabi.Client; + +namespace BTCPayServer.Plugins.Wabisabi; + +public class BTCPayKeyChain : IKeyChain +{ + private readonly ExplorerClient _explorerClient; + private readonly DerivationStrategyBase _derivationStrategy; + private readonly ExtKey _masterKey; + private readonly ExtKey _accountKey; + + public bool KeysAvailable => _masterKey is not null && _accountKey is not null; + + public BTCPayKeyChain(ExplorerClient explorerClient, DerivationStrategyBase derivationStrategy, ExtKey masterKey, + ExtKey accountKey) + { + _explorerClient = explorerClient; + _derivationStrategy = derivationStrategy; + _masterKey = masterKey; + _accountKey = accountKey; + } + + + public OwnershipProof GetOwnershipProof(IDestination destination, CoinJoinInputCommitmentData committedData) + { + return NBitcoinExtensions.GetOwnershipProof(_masterKey.PrivateKey, GetBitcoinSecret(destination.ScriptPubKey), + destination.ScriptPubKey, committedData); + } + + public Transaction Sign(Transaction transaction, Coin coin, PrecomputedTransactionData precomputeTransactionData) + { + transaction = transaction.Clone(); + + if (transaction.Inputs.Count == 0) + { + throw new ArgumentException("No inputs to sign.", nameof(transaction)); + } + + var txInput = transaction.Inputs.AsIndexedInputs().FirstOrDefault(input => input.PrevOut == coin.Outpoint); + + if (txInput is null) + { + throw new InvalidOperationException("Missing input."); + } + + + BitcoinSecret secret = GetBitcoinSecret(coin.ScriptPubKey); + + TransactionBuilder builder = Network.Main.CreateTransactionBuilder(); + builder.AddKeys(secret); + builder.AddCoins(coin); + builder.SetSigningOptions(new SigningOptions(TaprootSigHash.All, + (TaprootReadyPrecomputedTransactionData)precomputeTransactionData)); + builder.SignTransactionInPlace(transaction); + + return transaction; + } + + public void TrySetScriptStates(KeyState state, IEnumerable diff --git a/Plugins/BTCPayServer.Plugins.Wabisabi/Views/WabisabiStore/UpdateWabisabiStoreSettings.cshtml b/Plugins/BTCPayServer.Plugins.Wabisabi/Views/WabisabiStore/UpdateWabisabiStoreSettings.cshtml new file mode 100644 index 0000000..d668792 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Wabisabi/Views/WabisabiStore/UpdateWabisabiStoreSettings.cshtml @@ -0,0 +1,322 @@ +@using BTCPayServer.Plugins.Wabisabi +@using BTCPayServer.Abstractions.Extensions +@using BTCPayServer.Abstractions.Contracts +@using BTCPayServer.Security +@using NBitcoin +@using System.Security.Claims +@using BTCPayServer +@using BTCPayServer.Client +@using BTCPayServer.Common +@using Microsoft.AspNetCore.Mvc.TagHelpers +@using WalletWasabi.Backend.Controllers +@model BTCPayServer.Plugins.Wabisabi.WabisabiStoreSettings +@inject ContentSecurityPolicies contentSecurityPolicies +@inject WabisabiCoordinatorClientInstanceManager WabisabiCoordinatorClientInstanceManager +@inject IScopeProvider _scopeProvider +@inject IExplorerClientProvider ExplorerClientProvider; +@inject IBTCPayServerClientFactory ClientFactory +@inject WalletProvider WalletProvider +@{ + var storeId = _scopeProvider.GetCurrentStoreId(); + Layout = "../Shared/_NavLayout.cshtml"; + ViewData["NavPartialName"] = "../UIStores/_Nav"; + ViewData.SetActivePage("Plugins", "BTCPayServer.Views.Stores.StoreNavPages", "Wabisabi coinjoin support", storeId); + var nonce = RandomUtils.GetUInt256().ToString().Substring(0, 32); + contentSecurityPolicies.Add("script-src", $"'nonce-{nonce}'"); + contentSecurityPolicies.AllowUnsafeHashes(); + var explorerClient = ExplorerClientProvider.GetExplorerClient("BTC"); + var userid = Context.User.Claims.Single(claim => claim.Type == ClaimTypes.NameIdentifier).Value; + var anyEnabled = Model.Settings.Any(settings => settings.Enabled); + var Client = await ClientFactory.Create(userid, storeId); + ScriptPubKeyType? scriptType = null; + try + { + var pm = await Client.GetStoreOnChainPaymentMethod(storeId, "BTC"); + scriptType = explorerClient.Network.DerivationStrategyFactory.Parse(pm.DerivationScheme).ScriptPubKeyType(); + } + catch (Exception e) + { + } + Client = await ClientFactory.Create(userid); + var stores = (await Client.GetStores()) + .Where(data => data.Id != storeId) + .ToDictionary(s => s.Id, async s => + { + try + { + var sclient = await ClientFactory.Create(userid, s.Id, storeId); + var pm = await sclient.GetStoreOnChainPaymentMethod(s.Id, "BTC"); + if (explorerClient.Network.DerivationStrategyFactory.Parse(pm.DerivationScheme).ScriptPubKeyType() != scriptType) + { + return null; + } + return s.Name; + } + catch (Exception e) + { + return null; + } + }); + await Task.WhenAll(stores.Values); + var selectStores = + stores.Where(pair => pair.Value.Result is not null) + .Select(pair => new SelectListItem(pair.Value.Result, pair.Key, Model.MixToOtherWallet == pair.Key)).Prepend(new SelectListItem("None", "")); +} + + +

    Coinjoin configuration

    + +
    + @{ + var wallet = await WalletProvider.GetWalletAsync(storeId); + if (wallet is BTCPayWallet btcPayWallet) + { + @if (btcPayWallet.OnChainPaymentMethodData?.Enabled is not true) + { + + } + else if (!((BTCPayKeyChain) wallet.KeyChain).KeysAvailable) + { + + } + } + } + + +
    +
    +
    +
    + + +

    I just want to coinjoin.

    +
    +
    +
    +
    + + +

    The world is broken and I need to be vigilant about my bitcoin practices.

    +
    +
    +
    +
    +
    + + + + +

    Scores your coinjoined utxos based on how many other utxos in the coinjoin (and other previous coinjoin rounds) had the same value.
    Anonset score computation is not an exact science, and when using coordinators with massive liquidity, is not that important as all rounds (past, present, future) contribute to your privacy.

    +
    +
    + + +

    Feed as many coins to the coinjoin as possible.

    +
    +
    + + +

    Only allow a single non-private coin into a coinjoin.

    +
    +
    + + +

    Batch your pending payments (on-chain payouts awaiting payment) inside coinjoins.

    +
    +
    + + +

    Send coins that have been created in a coinjoin in a standard denomination to another wallet

    +
    + +
    +
    Only mix coins with these labels
    + @if (Model.InputLabelsAllowed?.Any() is not true) + { +
    No label filter applied
    + } + else + { + @for (var xIndex = 0; xIndex < Model.InputLabelsAllowed.Count; xIndex++) + { +
    +
    + + +
    +
    + } + } +
    + +
    +
    +
    +
    Only mix coins without these labels
    + @if (Model.InputLabelsExcluded?.Any() is not true) + { +
    No label filter applied
    + } + else + { + @for (var xIndex = 0; xIndex < Model.InputLabelsExcluded.Count; xIndex++) + { +
    + +
    + + +
    +
    + } + } +
    + +
    +
    + +
    +
    + + @for (var index = 0; index < Model.Settings.Count; index++) + { + + var s = Model.Settings[index]; + + if (! WabisabiCoordinatorClientInstanceManager.HostedServices.TryGetValue(s.Coordinator, out var coordinator)) + { + continue; + } +
    +
    +
    +
    + +

    @coordinator.CoordinatorDisplayName

    + +
    + + @coordinator.Coordinator +
    + @if (!coordinator.WasabiCoordinatorStatusFetcher.Connected) + { +

    Coordinator Status: Not connected

    + } + else + { +

    + Coordinator Status: Connected + + T&C + +

    + } +
    +
    +
    + + +
    + @if (coordinator.CoordinatorName != "local" && coordinator.CoordinatorName != "zksnacks") + { + + } +
    + + +
    + } + @if (ViewBag.DiscoveredCoordinators is List discoveredCoordinators) + { + foreach (var coordinator in discoveredCoordinators) + { + +
    +
    +
    +
    + +

    @coordinator.Name

    + +
    + + @coordinator.Uri +
    +
    + +
    +
    + + +
    + + } + } + + Coordinator runner + + Enable Discrete payments - Coming soon + + + + + +@section PageFootContent { + +} + + diff --git a/Plugins/BTCPayServer.Plugins.Wabisabi/Views/_ViewImports.cshtml b/Plugins/BTCPayServer.Plugins.Wabisabi/Views/_ViewImports.cshtml new file mode 100644 index 0000000..04173ab --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Wabisabi/Views/_ViewImports.cshtml @@ -0,0 +1,4 @@ +@using BTCPayServer.Abstractions.Services + +@inject Safe Safe +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiCoordinatorClientInstance.cs b/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiCoordinatorClientInstance.cs new file mode 100644 index 0000000..361ee49 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiCoordinatorClientInstance.cs @@ -0,0 +1,228 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Threading; +using System.Threading.Tasks; +using BTCPayServer.Payments.PayJoin; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using NBitcoin; +using WalletWasabi.Services; +using WalletWasabi.Tor.Socks5.Pool.Circuits; +using WalletWasabi.Userfacing; +using WalletWasabi.WabiSabi.Client; +using WalletWasabi.WabiSabi.Client.RoundStateAwaiters; +using WalletWasabi.WabiSabi.Client.StatusChangedEvents; +using WalletWasabi.WebClients.Wasabi; + +namespace BTCPayServer.Plugins.Wabisabi; + +public class WabisabiCoordinatorClientInstanceManager:IHostedService +{ + private readonly IServiceProvider _provider; + private readonly WalletProvider _walletProvider; + public Dictionary HostedServices { get; set; } = new(); + + public WabisabiCoordinatorClientInstanceManager(IServiceProvider provider, WalletProvider walletProvider ) + { + _provider = provider; + _walletProvider = walletProvider; + _walletProvider.WalletUnloaded += WalletProviderOnWalletUnloaded; + + } + + private void WalletProviderOnWalletUnloaded(object sender, WalletProvider.WalletUnloadEventArgs e) + { + _ =StopWallet(e.Wallet); + } + + private bool started = false; + public LocalisedUTXOLocker UTXOLocker; + + public async Task StartAsync(CancellationToken cancellationToken) + { + started = true; + foreach (KeyValuePair coordinatorManager in HostedServices) + { + await coordinatorManager.Value.StartAsync(cancellationToken); + } + } + + public async Task StopAsync(CancellationToken cancellationToken) + { + foreach (KeyValuePair coordinatorManager in HostedServices) + { + await coordinatorManager.Value.StopAsync(cancellationToken); + } + } + + public async Task StopWallet(string name) + { + foreach (var servicesValue in HostedServices.Values) + { + await servicesValue.StopWallet(name); + } + } + + + + public void AddCoordinator(string displayName, string name, + Func fetcher, string termsConditions = null) + { + if (HostedServices.ContainsKey(name)) + { + return; + } + var instance = new WabisabiCoordinatorClientInstance( + displayName, + name, fetcher.Invoke(_provider), _provider.GetService(), _provider, UTXOLocker, + _provider.GetService()); + if (HostedServices.TryAdd(instance.CoordinatorName, instance)) + { + if(started) + _ = instance.StartAsync(CancellationToken.None); + } + } + + public async Task RemoveCoordinator(string name) + { + if (!HostedServices.TryGetValue(name, out var s)) + { + return; + } + + await s.StopAsync(CancellationToken.None); + HostedServices.Remove(name); + } +} + +public class WabisabiCoordinatorClientInstance +{ + private readonly IUTXOLocker _utxoLocker; + private readonly ILogger _logger; + public string CoordinatorDisplayName { get; } + public string CoordinatorName { get; set; } + public Uri Coordinator { get; set; } + public WalletProvider WalletProvider { get; } + public HttpClientFactory WasabiHttpClientFactory { get; set; } + public RoundStateUpdater RoundStateUpdater { get; set; } + public WasabiCoordinatorStatusFetcher WasabiCoordinatorStatusFetcher { get; set; } + public CoinJoinManager CoinJoinManager { get; set; } + + public WabisabiCoordinatorClientInstance(string coordinatorDisplayName, + string coordinatorName, + Uri coordinator, + ILoggerFactory loggerFactory, + IServiceProvider serviceProvider, + IUTXOLocker utxoLocker, + WalletProvider walletProvider) + { + _utxoLocker = utxoLocker; + var config = serviceProvider.GetService(); + var socksEndpoint = config.GetValue("socksendpoint"); + EndPointParser.TryParse(socksEndpoint,9050, out var torEndpoint); + if (torEndpoint is not null && torEndpoint is DnsEndPoint dnsEndPoint) + { + torEndpoint = new IPEndPoint(Dns.GetHostAddresses(dnsEndPoint.Host).First(), dnsEndPoint.Port); + } + CoordinatorDisplayName = coordinatorDisplayName; + CoordinatorName = coordinatorName; + Coordinator = coordinator; + WalletProvider = walletProvider; + _logger = loggerFactory.CreateLogger(coordinatorName); + WasabiHttpClientFactory = new HttpClientFactory(torEndpoint, () => Coordinator); + var roundStateUpdaterCircuit = new PersonCircuit(); + var roundStateUpdaterHttpClient = + WasabiHttpClientFactory.NewHttpClient(Mode.SingleCircuitPerLifetime, roundStateUpdaterCircuit); + var sharedWabisabiClient = new WabiSabiHttpApiClient(roundStateUpdaterHttpClient); + WasabiCoordinatorStatusFetcher = new WasabiCoordinatorStatusFetcher(sharedWabisabiClient, _logger); + + RoundStateUpdater = new RoundStateUpdater(TimeSpan.FromSeconds(5),sharedWabisabiClient, WasabiCoordinatorStatusFetcher); + CoinJoinManager = new CoinJoinManager(coordinatorName,WalletProvider, RoundStateUpdater, WasabiHttpClientFactory, + WasabiCoordinatorStatusFetcher, "CoinJoinCoordinatorIdentifier"); + CoinJoinManager.StatusChanged += OnStatusChanged; + CoinJoinManager.OnBan += (sender, args) => + { + WalletProvider.OnBan(coordinatorName, args); + }; + + } + + public async Task StopWallet(string walletName) + { + await CoinJoinManager.StopAsyncByName(walletName, CancellationToken.None); + } + + private void OnStatusChanged(object sender, StatusChangedEventArgs e) + { + + switch (e) + { + case CoinJoinStatusEventArgs coinJoinStatusEventArgs: + _logger.LogInformation(coinJoinStatusEventArgs.CoinJoinProgressEventArgs.GetType().ToString() + " :" + + e.Wallet.WalletName); + break; + case CompletedEventArgs completedEventArgs: + + var result = completedEventArgs.CoinJoinResult; + + if (completedEventArgs.CompletionStatus == CompletionStatus.Success) + { + Task.Run(async () => + { + + var wallet = (BTCPayWallet) e.Wallet; + await wallet.RegisterCoinjoinTransaction(result, CoordinatorName); + + }); + } + else + { + Task.Run(async () => + { + // _logger.LogInformation("unlocking coins because round failed"); + await _utxoLocker.TryUnlock( + result.RegisteredCoins.Select(coin => coin.Outpoint).ToArray()); + }); + break; + } + _logger.LogInformation("Coinjoin complete! :" + + e.Wallet.WalletName); + break; + case LoadedEventArgs loadedEventArgs: + var stopWhenAllMixed = !((BTCPayWallet)loadedEventArgs.Wallet).BatchPayments; + _ = CoinJoinManager.StartAsync(loadedEventArgs.Wallet, stopWhenAllMixed, false, CancellationToken.None); + _logger.LogInformation( "Loaded wallet :" + e.Wallet.WalletName + $"stopWhenAllMixed: {stopWhenAllMixed}"); + break; + case StartErrorEventArgs errorArgs: + _logger.LogInformation("Could not start wallet for coinjoin:" + errorArgs.Error.ToString() + " :" + e.Wallet.WalletName); + break; + case StoppedEventArgs stoppedEventArgs: + _logger.LogInformation("Stopped wallet for coinjoin: " + stoppedEventArgs.Reason + " :" + e.Wallet.WalletName); + break; + default: + _logger.LogInformation(e.GetType() + " :" + e.Wallet.WalletName); + break; + } + } + + public Task StartAsync(CancellationToken cancellationToken) + { + + RoundStateUpdater.StartAsync(cancellationToken); + WasabiCoordinatorStatusFetcher.StartAsync(cancellationToken); + CoinJoinManager.StartAsync(cancellationToken); + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) + { + RoundStateUpdater.StopAsync(cancellationToken); + WasabiCoordinatorStatusFetcher.StopAsync(cancellationToken); + CoinJoinManager.StopAsync(cancellationToken); + return Task.CompletedTask; + } +} diff --git a/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiPlugin.cs b/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiPlugin.cs new file mode 100644 index 0000000..36910b9 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiPlugin.cs @@ -0,0 +1,119 @@ +using System; +using System.Buffers; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Models; +using BTCPayServer.Abstractions.Services; +using BTCPayServer.Common; +using BTCPayServer.Payments.PayJoin; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.AspNetCore.Mvc.Formatters; +using Microsoft.AspNetCore.Mvc.Infrastructure; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.ObjectPool; +using Microsoft.Extensions.Options; +using NBitcoin; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using WalletWasabi.Backend.Controllers; +using WalletWasabi.Logging; +using WalletWasabi.WabiSabi.Client; +using WalletWasabi.WabiSabi.Models.Serialization; +using LogLevel = WalletWasabi.Logging.LogLevel; + +namespace BTCPayServer.Plugins.Wabisabi; + +public class WabisabiPlugin : BaseBTCPayServerPlugin +{ + public override string Identifier => "BTCPayServer.Plugins.Wabisabi"; + public override string Name => "Coinjoin"; + + + public override IBTCPayServerPlugin.PluginDependency[] Dependencies { get; } = + { + new() {Identifier = nameof(BTCPayServer), Condition = ">=1.7.3.0"} + }; + + public override string Description => + "Allows you to integrate your btcpayserver store with coinjoins."; + + + public override void Execute(IServiceCollection applicationBuilder) + { + var utxoLocker = new LocalisedUTXOLocker(); + applicationBuilder.AddSingleton( + provider => + { + var res = ActivatorUtilities.CreateInstance(provider); + res.UTXOLocker = utxoLocker; + res.AddCoordinator("zkSNACKS Coordinator", "zksnacks", provider => + { + var chain = provider.GetService().GetExplorerClient("BTC").Network + .NBitcoinNetwork.ChainName; + if (chain == ChainName.Mainnet) + { + return new Uri("https://wasabiwallet.io/"); + } + + if (chain == ChainName.Testnet) + { + return new Uri("https://wasabiwallet.co/"); + } + + return new Uri("http://localhost:37127"); + }); + return res; + }); + applicationBuilder.AddHostedService(provider => + provider.GetRequiredService()); + applicationBuilder.AddSingleton(); + applicationBuilder.AddSingleton(provider => new( + provider.GetRequiredService(), + provider.GetRequiredService(), + provider.GetRequiredService(), + provider.GetRequiredService(), + utxoLocker + )); + applicationBuilder.AddWabisabiCoordinator(); + applicationBuilder.AddSingleton(provider => provider.GetRequiredService()); + applicationBuilder.AddHostedService(provider => provider.GetRequiredService()); + ; + applicationBuilder.AddSingleton(new UIExtension("Wabisabi/StoreIntegrationWabisabiOption", + "store-integrations-list")); + applicationBuilder.AddSingleton(new UIExtension("Wabisabi/WabisabiNav", + "store-integrations-nav")); + applicationBuilder.AddSingleton(new UIExtension("Wabisabi/WabisabiDashboard", + "dashboard")); + + Logger.SetMinimumLevel(LogLevel.Info); + Logger.SetModes(LogMode.DotNetLoggers); + + + base.Execute(applicationBuilder); + } + + + public override void Execute(IApplicationBuilder applicationBuilder, + IServiceProvider applicationBuilderApplicationServices) + { + Task.Run(async () => + { + var walletProvider = + (WalletProvider)applicationBuilderApplicationServices.GetRequiredService(); + await walletProvider.ResetWabisabiStuckPayouts(); + }); + + Logger.DotnetLogger = applicationBuilderApplicationServices.GetService>(); + base.Execute(applicationBuilder, applicationBuilderApplicationServices); + } +} diff --git a/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiService.cs b/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiService.cs new file mode 100644 index 0000000..889cea7 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiService.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Client; +using Microsoft.Extensions.Caching.Memory; +using WalletWasabi.WabiSabi.Client; + +namespace BTCPayServer.Plugins.Wabisabi +{ + public class WabisabiService + { + private readonly IStoreRepository _storeRepository; + private readonly WabisabiCoordinatorClientInstanceManager _coordinatorClientInstanceManager; + private readonly WalletProvider _walletProvider; + private string[] _ids => _coordinatorClientInstanceManager.HostedServices.Keys.ToArray(); + + public WabisabiService( IStoreRepository storeRepository, + WabisabiCoordinatorClientInstanceManager coordinatorClientInstanceManager, + WalletProvider walletProvider) + { + _storeRepository = storeRepository; + _coordinatorClientInstanceManager = coordinatorClientInstanceManager; + _walletProvider = walletProvider; + } + + public async Task GetWabisabiForStore(string storeId) + { + + var res = await _storeRepository.GetSettingAsync(storeId, nameof(WabisabiStoreSettings)); + res ??= new WabisabiStoreSettings(); + res.Settings = res.Settings.Where(settings => _ids.Contains(settings.Coordinator)).ToList(); + foreach (var wabisabiCoordinatorManager in _coordinatorClientInstanceManager.HostedServices) + { + if (res.Settings.All(settings => settings.Coordinator != wabisabiCoordinatorManager.Key)) + { + res.Settings.Add(new WabisabiStoreCoordinatorSettings() + { + Coordinator = wabisabiCoordinatorManager.Key, + }); + } + } + + return res; + } + + public async Task SetWabisabiForStore(string storeId, WabisabiStoreSettings wabisabiSettings) + { + + foreach (var setting in wabisabiSettings.Settings) + { + if (setting.Enabled) continue; + if(_coordinatorClientInstanceManager.HostedServices.TryGetValue(setting.Coordinator, out var coordinator)) + _ = coordinator.StopWallet(storeId); + } + + if (wabisabiSettings.Settings.All(settings => !settings.Enabled)) + { + + await _storeRepository.UpdateSetting(storeId, nameof(WabisabiStoreSettings), null!); + } + else + { + await _storeRepository.UpdateSetting(storeId, nameof(WabisabiStoreSettings), wabisabiSettings!); + } + + await _walletProvider.SettingsUpdated(storeId, wabisabiSettings); + + } + } + +} diff --git a/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiStore.Wabisabi.csproj b/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiStore.Wabisabi.csproj new file mode 100644 index 0000000..acf3e39 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiStore.Wabisabi.csproj @@ -0,0 +1,41 @@ + + + net6.0 + true + false + true + 0.0.10 + + + + + Debug;Release;Altcoins-Release;Altcoins-Debug + AnyCPU + + + + + true + true + + + $(DefineConstants);DEBUG + true + + + + + + + + + + + + + + + + + + diff --git a/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiStoreController.cs b/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiStoreController.cs new file mode 100644 index 0000000..46a584f --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiStoreController.cs @@ -0,0 +1,375 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Security.Claims; +using System.Threading; +using System.Threading.Tasks; +using AngleSharp.Dom.Events; +using BTCPayServer.Abstractions.Constants; +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Client; +using BTCPayServer.Client.Models; +using BTCPayServer.Common; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using NBitcoin; +using NBitcoin.Payment; +using NBXplorer; +using Newtonsoft.Json.Linq; +using NNostr.Client; +using WalletWasabi.Backend.Controllers; +using WalletWasabi.Blockchain.TransactionBuilding; +using WalletWasabi.Blockchain.TransactionOutputs; + +namespace BTCPayServer.Plugins.Wabisabi +{ + [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)] + [Route("plugins/{storeId}/Wabisabi")] + public partial class WabisabiStoreController : Controller + { + private readonly WabisabiService _WabisabiService; + private readonly WalletProvider _walletProvider; + private readonly IBTCPayServerClientFactory _btcPayServerClientFactory; + private readonly IExplorerClientProvider _explorerClientProvider; + private readonly WabisabiCoordinatorService _wabisabiCoordinatorService; + private readonly WabisabiCoordinatorClientInstanceManager _instanceManager; + + public WabisabiStoreController(WabisabiService WabisabiService, WalletProvider walletProvider, + IBTCPayServerClientFactory btcPayServerClientFactory, + IExplorerClientProvider explorerClientProvider, + WabisabiCoordinatorService wabisabiCoordinatorService, + WabisabiCoordinatorClientInstanceManager instanceManager) + { + _WabisabiService = WabisabiService; + _walletProvider = walletProvider; + _btcPayServerClientFactory = btcPayServerClientFactory; + _explorerClientProvider = explorerClientProvider; + _wabisabiCoordinatorService = wabisabiCoordinatorService; + _instanceManager = instanceManager; + } + + [HttpGet("")] + public async Task UpdateWabisabiStoreSettings(string storeId) + { + WabisabiStoreSettings Wabisabi = null; + try + { + Wabisabi = await _WabisabiService.GetWabisabiForStore(storeId); + } + catch (Exception) + { + // ignored + } + + return View(Wabisabi); + } + + public const int coordinatorEventKind = 15750; + + [HttpPost("")] + public async Task UpdateWabisabiStoreSettings(string storeId, WabisabiStoreSettings vm, + string command) + { + var pieces = command.Split(":"); + var actualCommand = pieces[0]; + var commandIndex = pieces.Length > 1 ? pieces[1] : null; + var coordinator = pieces.Length > 2 ? pieces[2] : null; + var coord = vm.Settings.SingleOrDefault(settings => settings.Coordinator == coordinator); + ModelState.Clear(); + + WabisabiCoordinatorSettings coordSettings; + switch (actualCommand) + { + case "discover": + coordSettings = await _wabisabiCoordinatorService.GetSettings(); + var relay = commandIndex ?? + (await _wabisabiCoordinatorService.GetSettings())?.NostrRelay.ToString(); + + if (Uri.TryCreate(relay, UriKind.Absolute, out var relayUri)) + { + using var nostrClient = new NostrClient(relayUri); + await nostrClient.CreateSubscription("nostr-wabisabi-coordinators", + new[] + { + new NostrSubscriptionFilter() + { + Kinds = new[] {coordinatorEventKind}, + Since = DateTimeOffset.UtcNow.Subtract(TimeSpan.FromHours(1)), + } + }); + var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1)); + await nostrClient.ConnectAndWaitUntilConnected(cts.Token); + _ = nostrClient.ListenForMessages(); + var result = new List(); + var tcs = new TaskCompletionSource(); + Stopwatch stopwatch = new(); + stopwatch.Start(); + nostrClient.MessageReceived += (sender, s) => + { + if (JArray.Parse(s).FirstOrDefault()?.Value() == "EOSE") + { + tcs.SetResult(); + } + }; + nostrClient.EventsReceived += (sender, tuple) => + { + stopwatch.Restart(); + result.AddRange(tuple.events); + }; + while (!tcs.Task.IsCompleted && !cts.IsCancellationRequested && + stopwatch.ElapsedMilliseconds < 10000) + { + await Task.Delay(1000, cts.Token); + } + + nostrClient.Dispose(); + + var network = _explorerClientProvider.GetExplorerClient("BTC").Network.NBitcoinNetwork.Name + .ToLower(); + ViewBag.DiscoveredCoordinators = result.Where(@event => + @event.CreatedAt < DateTimeOffset.UtcNow.AddMinutes(15) && + @event.Verify() && + @event.Tags.Any(tag => + tag.TagIdentifier == "uri" && + tag.Data.Any(s => Uri.IsWellFormedUriString(s, UriKind.Absolute))) && + @event.Tags.Any(tag => + tag.TagIdentifier == "network" && tag.Data.FirstOrDefault() == network) + ).Select(@event => new DiscoveredCoordinator() + { + Name = @event.PublicKey, + Uri = new Uri(@event.GetTaggedData("uri") + .First(s => Uri.IsWellFormedUriString(s, UriKind.Absolute))) + }).Where(discoveredCoordinator => string.IsNullOrEmpty(coordSettings.NostrIdentity) || discoveredCoordinator.Name != coordSettings.PubKey?.ToHex()).ToList(); + } + else + { + TempData["ErrorMessage"] = $"No relay uri was provided"; + } + + return View(vm); + case "add-coordinator": + var name = commandIndex; + var uri = coordinator; + + coordSettings = await _wabisabiCoordinatorService.GetSettings(); + if (coordSettings.DiscoveredCoordinators.All(discoveredCoordinator => + discoveredCoordinator.Name != name)) + { + coordSettings.DiscoveredCoordinators.Add(new DiscoveredCoordinator() {Name = name,}); + await _wabisabiCoordinatorService.UpdateSettings(coordSettings); + _instanceManager.AddCoordinator($"nostr[{name}]", name, provider => new Uri(uri)); + + TempData["SuccessMessage"] = $"Coordinator {commandIndex} added and started"; + return RedirectToAction(nameof(UpdateWabisabiStoreSettings), new {storeId}); + } + + else + { + TempData["ErrorMessage"] = + $"Coordinator {commandIndex} could not be added because the name was not unique"; + return View(vm); + } + + break; + case "remove-coordinator": + coordSettings = await _wabisabiCoordinatorService.GetSettings(); + if (coordSettings.DiscoveredCoordinators.RemoveAll(discoveredCoordinator => + discoveredCoordinator.Name == commandIndex) > 0) + { + TempData["SuccessMessage"] = $"Coordinator {commandIndex} stopped and removed"; + await _wabisabiCoordinatorService.UpdateSettings(coordSettings); + await _instanceManager.RemoveCoordinator(commandIndex); + return RedirectToAction(nameof(UpdateWabisabiStoreSettings), new {storeId}); + } + else + { + TempData["ErrorMessage"] = + $"Coordinator {commandIndex} could not be removed because it was not found"; + } + + return View(vm); + break; + case "check": + await _walletProvider.Check(storeId, CancellationToken.None); + TempData["SuccessMessage"] = "Store wallet re-checked"; + return RedirectToAction(nameof(UpdateWabisabiStoreSettings), new {storeId}); + case "exclude-label-add": + vm.InputLabelsExcluded.Add(""); + return View(vm); + + case "exclude-label-remove": + vm.InputLabelsExcluded.Remove(commandIndex); + return View(vm); + case "include-label-add": + vm.InputLabelsAllowed.Add(""); + return View(vm); + case "include-label-remove": + vm.InputLabelsAllowed.Remove(commandIndex); + return View(vm); + + case "save": + foreach (WabisabiStoreCoordinatorSettings settings in vm.Settings) + { + vm.InputLabelsAllowed = vm.InputLabelsAllowed.Where(s => !string.IsNullOrEmpty(s)).Distinct() + .ToList(); + vm.InputLabelsExcluded = vm.InputLabelsExcluded.Where(s => !string.IsNullOrEmpty(s)).Distinct() + .ToList(); + } + + await _WabisabiService.SetWabisabiForStore(storeId, vm); + TempData["SuccessMessage"] = "Wabisabi settings modified"; + return RedirectToAction(nameof(UpdateWabisabiStoreSettings), new {storeId}); + + default: + return View(vm); + } + } + + [HttpGet("spend")] + public async Task Spend(string storeId) + { + if ((await _walletProvider.GetWalletAsync(storeId)) is not BTCPayWallet wallet) + { + return NotFound(); + } + + return View(new SpendViewModel() { }); + } + + [HttpPost("spend")] + public async Task Spend(string storeId, SpendViewModel spendViewModel, string command) + { + if ((await _walletProvider.GetWalletAsync(storeId)) is not BTCPayWallet wallet) + { + return NotFound(); + } + + var n = _explorerClientProvider.GetExplorerClient("BTC").Network.NBitcoinNetwork; + if (string.IsNullOrEmpty(spendViewModel.Destination)) + { + ModelState.AddModelError(nameof(spendViewModel.Destination), + "A destination is required"); + } + else + { + try + { + BitcoinAddress.Create(spendViewModel.Destination, n); + } + catch (Exception e) + { + try + { + new BitcoinUrlBuilder(spendViewModel.Destination, n); + } + catch (Exception exception) + { + ModelState.AddModelError(nameof(spendViewModel.Destination), + "A destination must be a bitcoin address or a bip21 payment link"); + } + } + } + + if (spendViewModel.Amount is null) + { + try + { + spendViewModel.Amount = + new BitcoinUrlBuilder(spendViewModel.Destination, n).Amount.ToDecimal(MoneyUnit.BTC); + } + catch (Exception e) + { + ModelState.AddModelError(nameof(spendViewModel.Amount), + "An amount was not specified and the destination did not have an amount specified"); + } + } + + if (!ModelState.IsValid) + { + return View(); + } + + if (command == "payout") + { + var client = await _btcPayServerClientFactory.Create(null, storeId); + await client.CreatePayout(storeId, + new CreatePayoutThroughStoreRequest() + { + Approved = true, Amount = spendViewModel.Amount, Destination = spendViewModel.Destination + }); + + TempData["SuccessMessage"] = + "The payment has been scheduled. If payment batching is enabled in the coinjoin settings, and the coordinator supports sending that amount and that address type, it will be batched."; + + return RedirectToAction("UpdateWabisabiStoreSettings", new {storeId}); + } + + var coins = await wallet.GetAllCoins(); + if (command == "compute-with-selection") + { + if (spendViewModel.SelectedCoins?.Any() is true) + { + coins = (CoinsView)coins.FilterBy(coin => + spendViewModel.SelectedCoins.Contains(coin.Outpoint.ToString())); + } + } + + if (command == "compute-with-selection" || command == "compute") + { + if (spendViewModel.Amount is null) + { + spendViewModel.Amount = + new BitcoinUrlBuilder(spendViewModel.Destination, n).Amount.ToDecimal(MoneyUnit.BTC); + } + + var defaultCoinSelector = new DefaultCoinSelector(); + var defaultSelection = + (defaultCoinSelector.Select(coins.Select(coin => coin.Coin).ToArray(), + new Money((decimal)spendViewModel.Amount, MoneyUnit.BTC)) ?? Array.Empty()) + .ToArray(); + var selector = new SmartCoinSelector(coins.ToList()); + var smartSelection = selector.Select(defaultSelection, + new Money((decimal)spendViewModel.Amount, MoneyUnit.BTC)); + spendViewModel.SelectedCoins = smartSelection.Select(coin => coin.Outpoint.ToString()).ToArray(); + return View(spendViewModel); + } + + if (command == "send") + { + var userid = HttpContext.User.Claims.Single(claim => claim.Type == ClaimTypes.NameIdentifier).Value; + var client = await _btcPayServerClientFactory.Create(userid, storeId); + var tx = await client.CreateOnChainTransaction(storeId, "BTC", + new CreateOnChainTransactionRequest() + { + SelectedInputs = spendViewModel.SelectedCoins?.Select(OutPoint.Parse).ToList(), + Destinations = + new List() + { + new CreateOnChainTransactionRequest.CreateOnChainTransactionRequestDestination() + { + Destination = spendViewModel.Destination, Amount = spendViewModel.Amount + } + } + }); + + TempData["SuccessMessage"] = + $"The tx {tx.TransactionHash} has been broadcast."; + + return RedirectToAction("UpdateWabisabiStoreSettings", new {storeId}); + } + + return View(spendViewModel); + } + + + public class SpendViewModel + { + public string Destination { get; set; } + public decimal? Amount { get; set; } + public string[] SelectedCoins { get; set; } + } + } +} diff --git a/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiStoreSettings.cs b/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiStoreSettings.cs new file mode 100644 index 0000000..5e79a57 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Wabisabi/WabisabiStoreSettings.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; +using System.Linq; + +namespace BTCPayServer.Plugins.Wabisabi; + +public class WabisabiStoreSettings +{ + public List Settings { get; set; } = new(); + + + public string MixToOtherWallet { get; set; } + + public bool PlebMode { get; set; } = true; + + public List InputLabelsAllowed { get; set; } = new(); + public List InputLabelsExcluded { get; set; } = new(); + public bool ConsolidationMode { get; set; } = false; + public bool RedCoinIsolation { get; set; } = false; + public int AnonymitySetTarget { get; set; } = 5; + + public bool BatchPayments { get; set; } = true; + + +} + +public class WabisabiStoreCoordinatorSettings +{ + public string Coordinator { get; set; } + public bool Enabled { get; set; } = false; + + +} diff --git a/Plugins/BTCPayServer.Plugins.Wabisabi/WalletProvider.cs b/Plugins/BTCPayServer.Plugins.Wabisabi/WalletProvider.cs new file mode 100644 index 0000000..886102a --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Wabisabi/WalletProvider.cs @@ -0,0 +1,257 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Client.Models; +using BTCPayServer.Common; +using BTCPayServer.Payments.PayJoin; +using Microsoft.Extensions.Logging; +using NBitcoin; +using NBXplorer; +using WalletWasabi.Bases; +using WalletWasabi.Blockchain.TransactionOutputs; +using WalletWasabi.WabiSabi.Client; +using WalletWasabi.Wallets; + +namespace BTCPayServer.Plugins.Wabisabi; + +public class WalletProvider : PeriodicRunner,IWalletProvider +{ + private Dictionary? _cachedSettings; + private readonly IBTCPayServerClientFactory _btcPayServerClientFactory; + private readonly IExplorerClientProvider _explorerClientProvider; + public IUTXOLocker UtxoLocker { get; set; } + private readonly ILoggerFactory _loggerFactory; + + public WalletProvider(IStoreRepository storeRepository, IBTCPayServerClientFactory btcPayServerClientFactory, + IExplorerClientProvider explorerClientProvider, ILoggerFactory loggerFactory, IUTXOLocker utxoLocker ) : base(TimeSpan.FromMinutes(5)) + { + UtxoLocker = utxoLocker; + _btcPayServerClientFactory = btcPayServerClientFactory; + _explorerClientProvider = explorerClientProvider; + _loggerFactory = loggerFactory; + initialLoad = Task.Run(async () => + { + _cachedSettings = + await storeRepository.GetSettingsAsync(nameof(WabisabiStoreSettings)); + }); + } + + public readonly ConcurrentDictionary> LoadedWallets = new(); + public ConcurrentDictionary> BannedCoins = new(); + + + public class WalletUnloadEventArgs : EventArgs + { + public string Wallet { get; } + + public WalletUnloadEventArgs(string wallet) + { + Wallet = wallet; + } + } + + public event EventHandler? WalletUnloaded; + public async Task GetWalletAsync(string name) + { + await initialLoad; + return await LoadedWallets.GetOrAddAsync(name, async s => + { + + if (!_cachedSettings.TryGetValue(name, out var wabisabiStoreSettings)) + { + return null; + } + + var client = await _btcPayServerClientFactory.Create(null, name); + var pm = await client.GetStoreOnChainPaymentMethod(name, "BTC"); + var explorerClient = _explorerClientProvider.GetExplorerClient("BTC"); + var derivationStrategy = + explorerClient.Network.DerivationStrategyFactory.Parse(pm.DerivationScheme); + + var masterKey = await explorerClient.GetMetadataAsync(derivationStrategy, + WellknownMetadataKeys.MasterHDKey); + var accountKey = await explorerClient.GetMetadataAsync(derivationStrategy, + WellknownMetadataKeys.AccountHDKey); + + var keychain = new BTCPayKeyChain(explorerClient, derivationStrategy, masterKey, accountKey); + + var destinationProvider = + new NBXInternalDestinationProvider(explorerClient, _btcPayServerClientFactory, derivationStrategy, client, name, + wabisabiStoreSettings); + + var smartifier = new Smartifier(explorerClient, derivationStrategy, _btcPayServerClientFactory, name, + CoinOnPropertyChanged); + + return (IWallet)new BTCPayWallet(pm, derivationStrategy, explorerClient, keychain, destinationProvider, + _btcPayServerClientFactory, name, wabisabiStoreSettings, UtxoLocker, + _loggerFactory, smartifier, BannedCoins); + + }); + + } + + private Task initialLoad = null; + public async Task> GetWalletsAsync() + { + var explorerClient = _explorerClientProvider.GetExplorerClient("BTC"); + var status = await explorerClient.GetStatusAsync(); + if (!status.IsFullySynched) + { + return Array.Empty(); + } + + await initialLoad; + return (await Task.WhenAll(_cachedSettings + .Select(pair => GetWalletAsync(pair.Key)))) + .Where(wallet => wallet is not null); + } + + private void CoinOnPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (sender is SmartCoin smartCoin) + { + if (e.PropertyName == nameof(SmartCoin.CoinJoinInProgress)) + { + // _logger.LogInformation($"{smartCoin.Outpoint}.CoinJoinInProgress = {smartCoin.CoinJoinInProgress}"); + if (UtxoLocker is not null) + { + _ = (smartCoin.CoinJoinInProgress + ? UtxoLocker.TryLock(smartCoin.Outpoint) + : UtxoLocker.TryUnlock(smartCoin.Outpoint)).ContinueWith(task => + { + // _logger.LogInformation( + // $"{(task.Result ? "Success" : "Fail")}: {(smartCoin.CoinJoinInProgress ? "" : "un")}locking coin for coinjoin: {smartCoin.Outpoint} "); + }); + } + } + } + } + + public async Task ResetWabisabiStuckPayouts() + { + var wallets = await GetWalletsAsync(); + foreach (BTCPayWallet wallet in wallets) + { + var client = await _btcPayServerClientFactory.Create(null, wallet.StoreId); + var payouts = await client.GetStorePayouts(wallet.StoreId); + var inProgressPayouts = payouts.Where(data => + data.State == PayoutState.InProgress && data.PaymentMethod == "BTC" && + data.PaymentProof?.Value("proofType") == "Wabisabi"); + foreach (PayoutData payout in inProgressPayouts) + { + try + { + var paymentproof = + payout.PaymentProof.ToObject(); + if (paymentproof.Candidates?.Any() is not true) + await client.MarkPayout(wallet.StoreId, payout.Id, + new MarkPayoutRequest() {State = PayoutState.AwaitingPayment}); + } + catch (Exception e) + { + } + } + } + } + + protected override async Task ActionAsync(CancellationToken cancel) + { + + var toCheck = LoadedWallets.Keys.ToList(); + while (toCheck.Any()) + { + var storeid = toCheck.First(); + await Check(storeid, cancel); + toCheck.Remove(storeid); + } + } + + public async Task Check(string storeId, CancellationToken cancellationToken) + { + var client = await _btcPayServerClientFactory.Create(null, storeId); + try + { + if (LoadedWallets.TryGetValue(storeId, out var currentWallet)) + { + var wallet = (BTCPayWallet)await currentWallet; + var kc = (BTCPayKeyChain)wallet.KeyChain; + var pm = await client.GetStoreOnChainPaymentMethod(storeId, "BTC", cancellationToken); + if (pm.DerivationScheme != wallet.OnChainPaymentMethodData.DerivationScheme) + { + await UnloadWallet(storeId); + } + else + { + wallet.OnChainPaymentMethodData = pm; + } + + if (!kc.KeysAvailable) + { + await UnloadWallet(storeId); + } + } + } + catch (Exception e) + { + await UnloadWallet(storeId); + } + } + + private async Task UnloadWallet(string name) + { + + LoadedWallets.TryRemove(name, out _); + WalletUnloaded?.Invoke(this, new WalletUnloadEventArgs(name)); + } + + public async Task SettingsUpdated(string storeId, WabisabiStoreSettings wabisabiSettings) + { + + if (wabisabiSettings.Settings.All(settings => !settings.Enabled)) + { + _cachedSettings?.Remove(storeId); + await UnloadWallet(storeId); + }else if (LoadedWallets.TryGetValue(storeId, out var existingWallet)) + { + + _cachedSettings.AddOrReplace(storeId, wabisabiSettings); + var btcpayWalet = (BTCPayWallet) await existingWallet; + if (btcpayWalet is null) + { + + LoadedWallets.TryRemove(storeId, out _); + var w = await GetWalletAsync(storeId); + if (w is null) + { + await UnloadWallet(storeId); + } + } + else + { + + btcpayWalet.WabisabiStoreSettings = wabisabiSettings; + } + } + else + { + _cachedSettings.AddOrReplace(storeId, wabisabiSettings); + await GetWalletAsync(storeId); + } + } + + public void OnBan(string coordinatorName, BannedCoinEventArgs args) + { + BannedCoins.AddOrUpdate(coordinatorName, + s => new Dictionary() {{args.Utxo, args.BannedTime}}, + (s, offsets) => + { + offsets.TryAdd(args.Utxo, args.BannedTime); + return offsets; + }); + } +} diff --git a/Plugins/BTCPayServer.Plugins.Wabisabi/WasabiCoordinatorStatusFetcher.cs b/Plugins/BTCPayServer.Plugins.Wabisabi/WasabiCoordinatorStatusFetcher.cs new file mode 100644 index 0000000..9f11697 --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Wabisabi/WasabiCoordinatorStatusFetcher.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Immutable; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using NBitcoin; +using WalletWasabi.Backend.Models.Responses; +using WalletWasabi.Bases; +using WalletWasabi.WabiSabi.Client; +using WalletWasabi.WabiSabi.Models; +using WalletWasabi.WebClients.Wasabi; + +namespace BTCPayServer.Plugins.Wabisabi; + +public class WasabiCoordinatorStatusFetcher : PeriodicRunner, IWasabiBackendStatusProvider +{ + private readonly WabiSabiHttpApiClient _wasabiClient; + private readonly ILogger _logger; + public bool Connected { get; set; } = false; + public WasabiCoordinatorStatusFetcher(WabiSabiHttpApiClient wasabiClient, ILogger logger) : + base(TimeSpan.FromSeconds(30)) + { + _wasabiClient = wasabiClient; + _logger = logger; + } + + protected override async Task ActionAsync(CancellationToken cancel) + { + try + { + await _wasabiClient.GetStatusAsync(new RoundStateRequest(ImmutableList.Empty), cancel); + if (!Connected) + { + _logger.LogInformation("Connected to coordinator" ); + } + + Connected = true; + } + catch (Exception e) + { + Connected = false; + _logger.LogError(e, "Could not connect to the coordinator "); + throw; + } + } +} diff --git a/submodules/btcpayserver b/submodules/btcpayserver new file mode 160000 index 0000000..068b717 --- /dev/null +++ b/submodules/btcpayserver @@ -0,0 +1 @@ +Subproject commit 068b717a7530a6f904502f41678df40a97d63b03 diff --git a/submodules/walletwasabi b/submodules/walletwasabi new file mode 160000 index 0000000..0fb49c8 --- /dev/null +++ b/submodules/walletwasabi @@ -0,0 +1 @@ +Subproject commit 0fb49c8b6145acf1bd2402b765ed1edcd1e8a895