2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2020-11-13 08:28:15 +01:00
|
|
|
using System.Collections.Generic;
|
2020-06-28 10:55:27 +02:00
|
|
|
using System.Linq;
|
2020-03-29 17:28:22 +02:00
|
|
|
using System.Net.Http;
|
2017-09-13 08:47:34 +02:00
|
|
|
using System.Text;
|
2020-11-13 08:28:15 +01:00
|
|
|
using System.Threading;
|
2017-09-13 08:47:34 +02:00
|
|
|
using System.Threading.Tasks;
|
2022-03-02 18:28:34 +01:00
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
2021-12-31 08:59:02 +01:00
|
|
|
using BTCPayServer.BIP78.Sender;
|
2020-03-16 08:36:55 +01:00
|
|
|
using BTCPayServer.Client;
|
2020-05-23 21:13:18 +02:00
|
|
|
using BTCPayServer.Client.Models;
|
2020-06-28 10:55:27 +02:00
|
|
|
using BTCPayServer.Controllers;
|
|
|
|
using BTCPayServer.Data;
|
2020-04-07 12:32:30 +02:00
|
|
|
using BTCPayServer.Events;
|
2020-06-28 10:55:27 +02:00
|
|
|
using BTCPayServer.Lightning;
|
|
|
|
using BTCPayServer.Lightning.CLightning;
|
|
|
|
using BTCPayServer.Models.AccountViewModels;
|
|
|
|
using BTCPayServer.Models.StoreViewModels;
|
2021-12-31 08:59:02 +01:00
|
|
|
using BTCPayServer.Payments.Lightning;
|
2021-03-01 14:44:53 +01:00
|
|
|
using BTCPayServer.Payments.PayJoin.Sender;
|
2020-03-29 17:28:22 +02:00
|
|
|
using BTCPayServer.Services;
|
|
|
|
using BTCPayServer.Services.Stores;
|
|
|
|
using BTCPayServer.Services.Wallets;
|
2020-06-28 10:55:27 +02:00
|
|
|
using BTCPayServer.Tests.Logging;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-12-11 05:41:39 +01:00
|
|
|
using Microsoft.CodeAnalysis.Operations;
|
2020-06-28 10:55:27 +02:00
|
|
|
using NBitcoin;
|
2020-03-29 17:28:22 +02:00
|
|
|
using NBitcoin.Payment;
|
2020-06-28 10:55:27 +02:00
|
|
|
using NBitpayClient;
|
|
|
|
using NBXplorer.DerivationStrategy;
|
|
|
|
using NBXplorer.Models;
|
2020-11-13 08:28:15 +01:00
|
|
|
using Newtonsoft.Json;
|
2020-03-29 17:28:22 +02:00
|
|
|
using Newtonsoft.Json.Linq;
|
2020-06-28 10:55:27 +02:00
|
|
|
using Xunit;
|
2020-11-13 08:28:15 +01:00
|
|
|
using Xunit.Sdk;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
|
|
namespace BTCPayServer.Tests
|
|
|
|
{
|
|
|
|
public class TestAccount
|
|
|
|
{
|
2020-06-29 05:07:48 +02:00
|
|
|
readonly ServerTester parent;
|
2020-03-29 17:28:22 +02:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
public TestAccount(ServerTester parent)
|
|
|
|
{
|
|
|
|
this.parent = parent;
|
|
|
|
BitPay = new Bitpay(new Key(), parent.PayTester.ServerUri);
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
2020-01-06 13:57:32 +01:00
|
|
|
public void GrantAccess(bool isAdmin = false)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2020-01-06 13:57:32 +01:00
|
|
|
GrantAccessAsync(isAdmin).GetAwaiter().GetResult();
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2017-10-11 05:20:44 +02:00
|
|
|
|
2020-03-19 11:11:15 +01:00
|
|
|
public async Task MakeAdmin(bool isAdmin = true)
|
2019-10-12 13:35:30 +02:00
|
|
|
{
|
|
|
|
var userManager = parent.PayTester.GetService<UserManager<ApplicationUser>>();
|
|
|
|
var u = await userManager.FindByIdAsync(UserId);
|
2020-03-19 11:11:15 +01:00
|
|
|
if (isAdmin)
|
|
|
|
await userManager.AddToRoleAsync(u, Roles.ServerAdmin);
|
|
|
|
else
|
|
|
|
await userManager.RemoveFromRoleAsync(u, Roles.ServerAdmin);
|
2020-02-19 09:35:23 +01:00
|
|
|
IsAdmin = true;
|
2019-10-12 13:35:30 +02:00
|
|
|
}
|
|
|
|
|
2020-03-25 16:57:54 +01:00
|
|
|
public Task<BTCPayServerClient> CreateClient()
|
2020-03-20 17:14:47 +01:00
|
|
|
{
|
2020-03-29 17:28:22 +02:00
|
|
|
return Task.FromResult(new BTCPayServerClient(parent.PayTester.ServerUri, RegisterDetails.Email,
|
|
|
|
RegisterDetails.Password));
|
2020-03-20 17:14:47 +01:00
|
|
|
}
|
|
|
|
|
2020-03-16 08:36:55 +01:00
|
|
|
public async Task<BTCPayServerClient> CreateClient(params string[] permissions)
|
|
|
|
{
|
2022-01-07 04:32:00 +01:00
|
|
|
var manageController = parent.PayTester.GetController<UIManageController>(UserId, StoreId, IsAdmin);
|
2020-03-16 08:36:55 +01:00
|
|
|
var x = Assert.IsType<RedirectToActionResult>(await manageController.AddApiKey(
|
2022-01-07 04:32:00 +01:00
|
|
|
new UIManageController.AddApiKeyViewModel()
|
2020-03-16 08:36:55 +01:00
|
|
|
{
|
2020-04-22 15:12:38 +02:00
|
|
|
PermissionValues = permissions.Select(s =>
|
2020-03-23 14:17:17 +01:00
|
|
|
{
|
2020-04-22 15:12:38 +02:00
|
|
|
Permission.TryParse(s, out var p);
|
|
|
|
return p;
|
|
|
|
}).GroupBy(permission => permission.Policy).Select(p =>
|
|
|
|
{
|
2020-06-07 16:17:48 +02:00
|
|
|
var stores = p.Where(permission => !string.IsNullOrEmpty(permission.Scope))
|
|
|
|
.Select(permission => permission.Scope).ToList();
|
2022-01-07 04:32:00 +01:00
|
|
|
return new UIManageController.AddApiKeyViewModel.PermissionValueItem()
|
2020-04-22 15:12:38 +02:00
|
|
|
{
|
|
|
|
Permission = p.Key,
|
|
|
|
Forbidden = false,
|
2022-01-07 04:32:00 +01:00
|
|
|
StoreMode = stores.Any() ? UIManageController.AddApiKeyViewModel.ApiKeyStoreMode.Specific : UIManageController.AddApiKeyViewModel.ApiKeyStoreMode.AllStores,
|
2020-04-22 15:12:38 +02:00
|
|
|
SpecificStores = stores,
|
|
|
|
Value = true
|
|
|
|
};
|
2020-03-23 14:17:17 +01:00
|
|
|
}).ToList()
|
2020-03-16 08:36:55 +01:00
|
|
|
}));
|
|
|
|
var statusMessage = manageController.TempData.GetStatusMessageModel();
|
|
|
|
Assert.NotNull(statusMessage);
|
2020-04-10 09:06:09 +02:00
|
|
|
var str = "<code class='alert-link'>";
|
|
|
|
var apiKey = statusMessage.Html.Substring(statusMessage.Html.IndexOf(str) + str.Length);
|
2020-03-16 08:36:55 +01:00
|
|
|
apiKey = apiKey.Substring(0, apiKey.IndexOf("</code>"));
|
|
|
|
return new BTCPayServerClient(parent.PayTester.ServerUri, apiKey);
|
|
|
|
}
|
|
|
|
|
2020-01-06 13:57:32 +01:00
|
|
|
public void Register(bool isAdmin = false)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2020-01-06 13:57:32 +01:00
|
|
|
RegisterAsync(isAdmin).GetAwaiter().GetResult();
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2020-03-29 17:28:22 +02:00
|
|
|
|
2020-01-06 13:57:32 +01:00
|
|
|
public async Task GrantAccessAsync(bool isAdmin = false)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2020-01-06 13:57:32 +01:00
|
|
|
await RegisterAsync(isAdmin);
|
2018-04-30 15:28:00 +02:00
|
|
|
await CreateStoreAsync();
|
2022-01-07 04:32:00 +01:00
|
|
|
var store = GetController<UIStoresController>();
|
2017-10-27 10:53:04 +02:00
|
|
|
var pairingCode = BitPay.RequestClientAuthorization("test", Facade.Merchant);
|
|
|
|
Assert.IsType<ViewResult>(await store.RequestPairing(pairingCode.ToString()));
|
|
|
|
await store.Pair(pairingCode.ToString(), StoreId);
|
|
|
|
}
|
2020-03-27 06:17:31 +01:00
|
|
|
|
|
|
|
public BTCPayServerClient CreateClientFromAPIKey(string apiKey)
|
|
|
|
{
|
|
|
|
return new BTCPayServerClient(parent.PayTester.ServerUri, apiKey);
|
|
|
|
}
|
|
|
|
|
2018-04-30 15:28:00 +02:00
|
|
|
public void CreateStore()
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2018-04-30 15:28:00 +02:00
|
|
|
CreateStoreAsync().GetAwaiter().GetResult();
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2018-01-13 14:01:09 +01:00
|
|
|
|
2021-06-18 03:25:17 +02:00
|
|
|
public async Task SetNetworkFeeMode(NetworkFeeMode mode)
|
2019-01-06 07:04:30 +01:00
|
|
|
{
|
2021-10-25 08:18:02 +02:00
|
|
|
await ModifyPayment(payment =>
|
2019-01-06 07:04:30 +01:00
|
|
|
{
|
2021-10-25 08:18:02 +02:00
|
|
|
payment.NetworkFeeMode = mode;
|
2019-01-06 07:04:30 +01:00
|
|
|
});
|
|
|
|
}
|
2020-03-29 17:28:22 +02:00
|
|
|
|
2022-01-20 12:52:31 +01:00
|
|
|
public async Task ModifyPayment(Action<GeneralSettingsViewModel> modify)
|
2019-01-04 16:37:09 +01:00
|
|
|
{
|
2022-01-07 04:32:00 +01:00
|
|
|
var storeController = GetController<UIStoresController>();
|
2022-01-20 12:52:31 +01:00
|
|
|
var response = storeController.GeneralSettings();
|
|
|
|
GeneralSettingsViewModel settings = (GeneralSettingsViewModel)((ViewResult)response).Model;
|
|
|
|
modify(settings);
|
|
|
|
await storeController.GeneralSettings(settings);
|
2021-10-29 08:25:43 +02:00
|
|
|
}
|
2021-12-31 08:59:02 +01:00
|
|
|
|
2021-10-29 08:25:43 +02:00
|
|
|
public async Task ModifyWalletSettings(Action<WalletSettingsViewModel> modify)
|
|
|
|
{
|
2022-01-07 04:32:00 +01:00
|
|
|
var storeController = GetController<UIStoresController>();
|
2021-10-29 08:25:43 +02:00
|
|
|
var response = await storeController.WalletSettings(StoreId, "BTC");
|
|
|
|
WalletSettingsViewModel walletSettings = (WalletSettingsViewModel)((ViewResult)response).Model;
|
|
|
|
modify(walletSettings);
|
|
|
|
storeController.UpdateWalletSettings(walletSettings).GetAwaiter().GetResult();
|
2019-01-04 16:37:09 +01:00
|
|
|
}
|
2021-12-31 08:59:02 +01:00
|
|
|
|
2021-11-11 06:30:19 +01:00
|
|
|
public async Task ModifyOnchainPaymentSettings(Action<WalletSettingsViewModel> modify)
|
|
|
|
{
|
2022-01-07 04:32:00 +01:00
|
|
|
var storeController = GetController<UIStoresController>();
|
2021-11-11 06:30:19 +01:00
|
|
|
var response = await storeController.WalletSettings(StoreId, "BTC");
|
|
|
|
WalletSettingsViewModel walletSettings = (WalletSettingsViewModel)((ViewResult)response).Model;
|
|
|
|
modify(walletSettings);
|
|
|
|
storeController.UpdatePaymentSettings(walletSettings).GetAwaiter().GetResult();
|
|
|
|
}
|
2019-01-04 16:37:09 +01:00
|
|
|
|
2018-04-30 15:00:43 +02:00
|
|
|
public T GetController<T>(bool setImplicitStore = true) where T : Controller
|
2018-04-03 09:53:55 +02:00
|
|
|
{
|
2019-10-12 13:35:30 +02:00
|
|
|
var controller = parent.PayTester.GetController<T>(UserId, setImplicitStore ? StoreId : null, IsAdmin);
|
|
|
|
return controller;
|
2018-04-03 09:53:55 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 15:28:00 +02:00
|
|
|
public async Task CreateStoreAsync()
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2020-05-16 22:07:24 +02:00
|
|
|
if (UserId is null)
|
|
|
|
{
|
|
|
|
await RegisterAsync();
|
|
|
|
}
|
2022-01-07 04:32:00 +01:00
|
|
|
var store = GetController<UIUserStoresController>();
|
2022-12-14 05:33:27 +01:00
|
|
|
await store.CreateStore(new CreateStoreViewModel { Name = "Test Store", PreferredExchange = "coingecko" });
|
2017-10-27 10:53:04 +02:00
|
|
|
StoreId = store.CreatedStoreId;
|
2018-07-19 12:31:17 +02:00
|
|
|
parent.Stores.Add(StoreId);
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2017-10-11 05:20:44 +02:00
|
|
|
|
2018-01-11 14:52:28 +01:00
|
|
|
public BTCPayNetwork SupportedNetwork { get; set; }
|
|
|
|
|
2020-04-07 12:32:30 +02:00
|
|
|
public WalletId RegisterDerivationScheme(string crytoCode, ScriptPubKeyType segwit = ScriptPubKeyType.Legacy, bool importKeysToNBX = false)
|
2018-01-11 09:29:48 +01:00
|
|
|
{
|
2020-02-19 09:35:23 +01:00
|
|
|
return RegisterDerivationSchemeAsync(crytoCode, segwit, importKeysToNBX).GetAwaiter().GetResult();
|
2018-01-11 09:29:48 +01:00
|
|
|
}
|
2020-03-29 17:28:22 +02:00
|
|
|
|
2020-04-07 12:32:30 +02:00
|
|
|
public async Task<WalletId> RegisterDerivationSchemeAsync(string cryptoCode, ScriptPubKeyType segwit = ScriptPubKeyType.Legacy,
|
2021-03-24 13:33:49 +01:00
|
|
|
bool importKeysToNBX = false, bool importsKeysToBitcoinCore = false)
|
2018-01-11 09:29:48 +01:00
|
|
|
{
|
2020-05-16 22:07:24 +02:00
|
|
|
if (StoreId is null)
|
|
|
|
await CreateStoreAsync();
|
2019-05-29 11:43:50 +02:00
|
|
|
SupportedNetwork = parent.NetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
|
2022-01-07 04:32:00 +01:00
|
|
|
var store = parent.PayTester.GetController<UIStoresController>(UserId, StoreId, true);
|
2021-06-17 09:02:47 +02:00
|
|
|
|
2021-06-18 03:25:17 +02:00
|
|
|
var generateRequest = new WalletSetupRequest
|
2020-02-19 09:35:23 +01:00
|
|
|
{
|
2020-04-07 12:32:30 +02:00
|
|
|
ScriptPubKeyType = segwit,
|
2020-04-05 11:34:46 +02:00
|
|
|
SavePrivateKeys = importKeysToNBX,
|
2021-03-24 13:33:49 +01:00
|
|
|
ImportKeysToRPC = importsKeysToBitcoinCore
|
2021-06-17 09:02:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
await store.GenerateWallet(StoreId, cryptoCode, WalletSetupMethod.HotWallet, generateRequest);
|
2021-06-17 11:27:17 +02:00
|
|
|
Assert.NotNull(store.GenerateWalletResponse);
|
|
|
|
GenerateWalletResponseV = store.GenerateWalletResponse;
|
2018-10-09 17:13:37 +02:00
|
|
|
return new WalletId(StoreId, cryptoCode);
|
2018-01-11 09:29:48 +01:00
|
|
|
}
|
|
|
|
|
2020-02-19 09:35:23 +01:00
|
|
|
public GenerateWalletResponse GenerateWalletResponseV { get; set; }
|
|
|
|
|
|
|
|
public DerivationStrategyBase DerivationScheme
|
|
|
|
{
|
2021-10-29 08:25:43 +02:00
|
|
|
get => GenerateWalletResponseV.DerivationScheme;
|
2020-02-19 09:35:23 +01:00
|
|
|
}
|
2017-11-06 09:31:02 +01:00
|
|
|
|
2020-01-06 13:57:32 +01:00
|
|
|
private async Task RegisterAsync(bool isAdmin = false)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2022-01-07 04:32:00 +01:00
|
|
|
var account = parent.PayTester.GetController<UIAccountController>();
|
2019-05-02 14:01:08 +02:00
|
|
|
RegisterDetails = new RegisterViewModel()
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2023-02-24 08:19:03 +01:00
|
|
|
Email = Utils.GenerateEmail(),
|
2023-03-20 02:46:46 +01:00
|
|
|
ConfirmPassword = Password,
|
|
|
|
Password = Password,
|
2020-01-06 13:57:32 +01:00
|
|
|
IsAdmin = isAdmin
|
2019-05-02 14:01:08 +02:00
|
|
|
};
|
|
|
|
await account.Register(RegisterDetails);
|
2021-11-10 04:28:54 +01:00
|
|
|
|
|
|
|
//this addresses an obscure issue where LockSubscription is unintentionally set to "true",
|
|
|
|
//resulting in a large number of tests failing.
|
2021-12-31 08:59:02 +01:00
|
|
|
if (account.RegisteredUserId == null)
|
2021-11-10 04:28:54 +01:00
|
|
|
{
|
|
|
|
var settings = parent.PayTester.GetService<SettingsRepository>();
|
|
|
|
var policies = await settings.GetSettingAsync<PoliciesSettings>() ?? new PoliciesSettings();
|
|
|
|
policies.LockSubscription = false;
|
|
|
|
await account.Register(RegisterDetails);
|
|
|
|
}
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
UserId = account.RegisteredUserId;
|
2022-02-15 16:19:52 +01:00
|
|
|
Email = RegisterDetails.Email;
|
2020-02-29 06:15:06 +01:00
|
|
|
IsAdmin = account.RegisteredAdmin;
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2023-03-20 02:46:46 +01:00
|
|
|
public string Password { get; set; } = "Kitten0@";
|
2017-09-13 08:47:34 +02:00
|
|
|
|
2020-03-29 17:28:22 +02:00
|
|
|
public RegisterViewModel RegisterDetails { get; set; }
|
2019-05-02 14:01:08 +02:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
public Bitpay BitPay
|
|
|
|
{
|
2020-03-29 17:28:22 +02:00
|
|
|
get;
|
|
|
|
set;
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2020-03-29 17:28:22 +02:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
public string UserId
|
|
|
|
{
|
2020-03-29 17:28:22 +02:00
|
|
|
get;
|
|
|
|
set;
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2023-01-06 14:18:07 +01:00
|
|
|
|
2022-02-15 16:19:52 +01:00
|
|
|
public string Email
|
|
|
|
{
|
|
|
|
get;
|
|
|
|
set;
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
public string StoreId
|
|
|
|
{
|
2020-03-29 17:28:22 +02:00
|
|
|
get;
|
|
|
|
set;
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2020-03-29 17:28:22 +02:00
|
|
|
|
2019-10-12 13:35:30 +02:00
|
|
|
public bool IsAdmin { get; internal set; }
|
2018-07-01 09:10:17 +02:00
|
|
|
|
2021-10-29 08:25:43 +02:00
|
|
|
public void RegisterLightningNode(string cryptoCode, LightningConnectionType connectionType, bool isMerchant = true)
|
2018-02-25 16:48:12 +01:00
|
|
|
{
|
2021-10-29 08:25:43 +02:00
|
|
|
RegisterLightningNodeAsync(cryptoCode, connectionType, isMerchant).GetAwaiter().GetResult();
|
2018-02-25 16:48:12 +01:00
|
|
|
}
|
2021-10-29 08:25:43 +02:00
|
|
|
public Task RegisterLightningNodeAsync(string cryptoCode, bool isMerchant = true, string storeId = null)
|
2021-03-02 03:11:58 +01:00
|
|
|
{
|
2021-10-29 08:25:43 +02:00
|
|
|
return RegisterLightningNodeAsync(cryptoCode, null, isMerchant, storeId);
|
2021-03-02 03:11:58 +01:00
|
|
|
}
|
2021-10-29 08:25:43 +02:00
|
|
|
public async Task RegisterLightningNodeAsync(string cryptoCode, LightningConnectionType? connectionType, bool isMerchant = true, string storeId = null)
|
2018-02-25 16:48:12 +01:00
|
|
|
{
|
2022-01-07 04:32:00 +01:00
|
|
|
var storeController = GetController<UIStoresController>();
|
2018-05-25 17:44:59 +02:00
|
|
|
|
2021-03-31 13:23:36 +02:00
|
|
|
var connectionString = parent.GetLightningConnectionString(connectionType, isMerchant);
|
|
|
|
var nodeType = connectionString == LightningSupportedPaymentMethod.InternalNode ? LightningNodeType.Internal : LightningNodeType.Custom;
|
2018-05-25 17:44:59 +02:00
|
|
|
|
2021-10-25 08:18:02 +02:00
|
|
|
var vm = new LightningNodeViewModel { ConnectionString = connectionString, LightningNodeType = nodeType, SkipPortTest = true };
|
2021-12-16 04:32:13 +01:00
|
|
|
await storeController.SetupLightningNode(storeId ?? StoreId,
|
|
|
|
vm, "save", cryptoCode);
|
|
|
|
if (storeController.ModelState.ErrorCount != 0)
|
|
|
|
Assert.False(true, storeController.ModelState.FirstOrDefault().Value.Errors[0].ErrorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task RegisterInternalLightningNodeAsync(string cryptoCode, string storeId = null)
|
|
|
|
{
|
2022-01-07 04:32:00 +01:00
|
|
|
var storeController = GetController<UIStoresController>();
|
2021-12-16 04:32:13 +01:00
|
|
|
var vm = new LightningNodeViewModel { ConnectionString = "", LightningNodeType = LightningNodeType.Internal, SkipPortTest = true };
|
2021-04-19 16:21:50 +02:00
|
|
|
await storeController.SetupLightningNode(storeId ?? StoreId,
|
2021-10-25 08:18:02 +02:00
|
|
|
vm, "save", cryptoCode);
|
2018-03-20 16:31:19 +01:00
|
|
|
if (storeController.ModelState.ErrorCount != 0)
|
|
|
|
Assert.False(true, storeController.ModelState.FirstOrDefault().Value.Errors[0].ErrorMessage);
|
2018-02-25 16:48:12 +01:00
|
|
|
}
|
2020-03-29 17:28:22 +02:00
|
|
|
|
|
|
|
public async Task<Coin> ReceiveUTXO(Money value, BTCPayNetwork network)
|
|
|
|
{
|
|
|
|
var cashCow = parent.ExplorerNode;
|
|
|
|
var btcPayWallet = parent.PayTester.GetService<BTCPayWalletProvider>().GetWallet(network);
|
|
|
|
var address = (await btcPayWallet.ReserveAddressAsync(this.DerivationScheme)).Address;
|
2020-04-07 12:32:30 +02:00
|
|
|
await parent.WaitForEvent<NewOnChainTransactionEvent>(async () =>
|
|
|
|
{
|
|
|
|
await cashCow.SendToAddressAsync(address, value);
|
|
|
|
});
|
2020-04-08 10:10:42 +02:00
|
|
|
int i = 0;
|
2020-06-28 10:55:27 +02:00
|
|
|
while (i < 30)
|
2020-04-08 10:10:42 +02:00
|
|
|
{
|
|
|
|
var result = (await btcPayWallet.GetUnspentCoins(DerivationScheme))
|
|
|
|
.FirstOrDefault(c => c.ScriptPubKey == address.ScriptPubKey)?.Coin;
|
|
|
|
if (result != null)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
await Task.Delay(1000);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
Assert.False(true);
|
2020-04-08 14:31:58 +02:00
|
|
|
return null;
|
2020-03-29 17:28:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<BitcoinAddress> GetNewAddress(BTCPayNetwork network)
|
|
|
|
{
|
|
|
|
var cashCow = parent.ExplorerNode;
|
|
|
|
var btcPayWallet = parent.PayTester.GetService<BTCPayWalletProvider>().GetWallet(network);
|
|
|
|
var address = (await btcPayWallet.ReserveAddressAsync(this.DerivationScheme)).Address;
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<PSBT> Sign(PSBT psbt)
|
|
|
|
{
|
|
|
|
var btcPayWallet = parent.PayTester.GetService<BTCPayWalletProvider>()
|
|
|
|
.GetWallet(psbt.Network.NetworkSet.CryptoCode);
|
|
|
|
var explorerClient = parent.PayTester.GetService<ExplorerClientProvider>()
|
|
|
|
.GetExplorerClient(psbt.Network.NetworkSet.CryptoCode);
|
|
|
|
psbt = (await explorerClient.UpdatePSBTAsync(new UpdatePSBTRequest()
|
|
|
|
{
|
2020-06-28 10:55:27 +02:00
|
|
|
DerivationScheme = DerivationScheme,
|
|
|
|
PSBT = psbt
|
2020-03-29 17:28:22 +02:00
|
|
|
})).PSBT;
|
|
|
|
return psbt.SignAll(this.DerivationScheme, GenerateWalletResponseV.AccountHDKey,
|
|
|
|
GenerateWalletResponseV.AccountKeyPath);
|
|
|
|
}
|
2021-11-22 09:16:08 +01:00
|
|
|
Logging.ILog TestLogs => this.parent.TestLogs;
|
2020-06-28 10:55:27 +02:00
|
|
|
public async Task<PSBT> SubmitPayjoin(Invoice invoice, PSBT psbt, string expectedError = null, bool senderError = false)
|
2020-03-29 17:28:22 +02:00
|
|
|
{
|
2020-06-17 14:43:56 +02:00
|
|
|
var endpoint = GetPayjoinBitcoinUrl(invoice, psbt.Network);
|
2020-04-07 12:32:30 +02:00
|
|
|
if (endpoint == null)
|
|
|
|
{
|
2020-05-19 13:55:42 +02:00
|
|
|
throw new InvalidOperationException("No payjoin endpoint for the invoice");
|
2020-04-07 12:32:30 +02:00
|
|
|
}
|
2020-03-29 17:28:22 +02:00
|
|
|
var pjClient = parent.PayTester.GetService<PayjoinClient>();
|
|
|
|
var storeRepository = parent.PayTester.GetService<StoreRepository>();
|
|
|
|
var store = await storeRepository.FindStore(StoreId);
|
|
|
|
var settings = store.GetSupportedPaymentMethods(parent.NetworkProvider).OfType<DerivationSchemeSettings>()
|
|
|
|
.First();
|
2021-11-22 09:16:08 +01:00
|
|
|
TestLogs.LogInformation($"Proposing {psbt.GetGlobalTransaction().GetHash()}");
|
2020-04-08 08:20:19 +02:00
|
|
|
if (expectedError is null && !senderError)
|
2020-03-29 17:28:22 +02:00
|
|
|
{
|
2021-03-01 14:44:53 +01:00
|
|
|
var proposed = await pjClient.RequestPayjoin(endpoint, new PayjoinWallet(settings), psbt, default);
|
2021-11-22 09:16:08 +01:00
|
|
|
TestLogs.LogInformation($"Proposed payjoin is {proposed.GetGlobalTransaction().GetHash()}");
|
2020-03-29 17:28:22 +02:00
|
|
|
Assert.NotNull(proposed);
|
|
|
|
return proposed;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-08 08:20:19 +02:00
|
|
|
if (senderError)
|
|
|
|
{
|
2021-03-01 14:44:53 +01:00
|
|
|
await Assert.ThrowsAsync<PayjoinSenderException>(async () => await pjClient.RequestPayjoin(endpoint, new PayjoinWallet(settings), psbt, default));
|
2020-04-08 08:20:19 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-01 14:44:53 +01:00
|
|
|
var ex = await Assert.ThrowsAsync<PayjoinReceiverException>(async () => await pjClient.RequestPayjoin(endpoint, new PayjoinWallet(settings), psbt, default));
|
2020-05-19 13:55:42 +02:00
|
|
|
var split = expectedError.Split('|');
|
|
|
|
Assert.Equal(split[0], ex.ErrorCode);
|
|
|
|
if (split.Length > 1)
|
|
|
|
Assert.Contains(split[1], ex.ReceiverMessage);
|
2020-04-08 08:20:19 +02:00
|
|
|
}
|
2020-03-29 17:28:22 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<Transaction> SubmitPayjoin(Invoice invoice, Transaction transaction, BTCPayNetwork network,
|
|
|
|
string expectedError = null)
|
|
|
|
{
|
|
|
|
var response =
|
|
|
|
await SubmitPayjoinCore(transaction.ToHex(), invoice, network.NBitcoinNetwork, expectedError);
|
|
|
|
if (response == null)
|
|
|
|
return null;
|
|
|
|
var signed = Transaction.Parse(await response.Content.ReadAsStringAsync(), network.NBitcoinNetwork);
|
|
|
|
return signed;
|
|
|
|
}
|
|
|
|
|
|
|
|
async Task<HttpResponseMessage> SubmitPayjoinCore(string content, Invoice invoice, Network network,
|
|
|
|
string expectedError)
|
|
|
|
{
|
2020-06-17 14:43:56 +02:00
|
|
|
var bip21 = GetPayjoinBitcoinUrl(invoice, network);
|
|
|
|
bip21.TryGetPayjoinEndpoint(out var endpoint);
|
2020-03-29 17:28:22 +02:00
|
|
|
var response = await parent.PayTester.HttpClient.PostAsync(endpoint,
|
|
|
|
new StringContent(content, Encoding.UTF8, "text/plain"));
|
|
|
|
if (expectedError != null)
|
|
|
|
{
|
2020-05-19 13:55:42 +02:00
|
|
|
var split = expectedError.Split('|');
|
2020-03-29 17:28:22 +02:00
|
|
|
Assert.False(response.IsSuccessStatusCode);
|
|
|
|
var error = JObject.Parse(await response.Content.ReadAsStringAsync());
|
2020-05-19 13:55:42 +02:00
|
|
|
if (split.Length > 0)
|
|
|
|
Assert.Equal(split[0], error["errorCode"].Value<string>());
|
|
|
|
if (split.Length > 1)
|
|
|
|
Assert.Contains(split[1], error["message"].Value<string>());
|
2020-03-29 17:28:22 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
{
|
|
|
|
var error = JObject.Parse(await response.Content.ReadAsStringAsync());
|
|
|
|
Assert.True(false,
|
|
|
|
$"Error: {error["errorCode"].Value<string>()}: {error["message"].Value<string>()}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2020-06-17 14:43:56 +02:00
|
|
|
public static BitcoinUrlBuilder GetPayjoinBitcoinUrl(Invoice invoice, Network network)
|
2020-03-29 17:28:22 +02:00
|
|
|
{
|
|
|
|
var parsedBip21 = new BitcoinUrlBuilder(
|
|
|
|
invoice.CryptoInfo.First(c => c.CryptoCode == network.NetworkSet.CryptoCode).PaymentUrls.BIP21,
|
|
|
|
network);
|
2020-06-17 14:43:56 +02:00
|
|
|
if (!parsedBip21.TryGetPayjoinEndpoint(out var endpoint))
|
|
|
|
return null;
|
|
|
|
return parsedBip21;
|
2020-03-29 17:28:22 +02:00
|
|
|
}
|
2020-11-13 08:28:15 +01:00
|
|
|
|
|
|
|
class WebhookListener : IDisposable
|
|
|
|
{
|
|
|
|
private Client.Models.StoreWebhookData _wh;
|
|
|
|
private FakeServer _server;
|
|
|
|
private readonly List<WebhookInvoiceEvent> _webhookEvents;
|
|
|
|
private CancellationTokenSource _cts;
|
|
|
|
public WebhookListener(Client.Models.StoreWebhookData wh, FakeServer server, List<WebhookInvoiceEvent> webhookEvents)
|
|
|
|
{
|
|
|
|
_wh = wh;
|
|
|
|
_server = server;
|
|
|
|
_webhookEvents = webhookEvents;
|
|
|
|
_cts = new CancellationTokenSource();
|
|
|
|
_ = Listen(_cts.Token);
|
|
|
|
}
|
|
|
|
|
|
|
|
async Task Listen(CancellationToken cancellation)
|
|
|
|
{
|
|
|
|
while (!cancellation.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
var req = await _server.GetNextRequest(cancellation);
|
|
|
|
var bytes = await req.Request.Body.ReadBytesAsync((int)req.Request.Headers.ContentLength);
|
|
|
|
var callback = Encoding.UTF8.GetString(bytes);
|
|
|
|
_webhookEvents.Add(JsonConvert.DeserializeObject<WebhookInvoiceEvent>(callback));
|
|
|
|
req.Response.StatusCode = 200;
|
|
|
|
_server.Done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
_cts.Cancel();
|
|
|
|
_server.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<WebhookInvoiceEvent> WebhookEvents { get; set; } = new List<WebhookInvoiceEvent>();
|
|
|
|
public TEvent AssertHasWebhookEvent<TEvent>(WebhookEventType eventType, Action<TEvent> assert) where TEvent : class
|
|
|
|
{
|
2020-12-11 05:41:39 +01:00
|
|
|
int retry = 0;
|
2021-12-31 08:59:02 +01:00
|
|
|
retry:
|
2020-11-13 08:28:15 +01:00
|
|
|
foreach (var evt in WebhookEvents)
|
|
|
|
{
|
|
|
|
if (evt.Type == eventType)
|
|
|
|
{
|
|
|
|
var typedEvt = evt.ReadAs<TEvent>();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(typedEvt);
|
|
|
|
return typedEvt;
|
|
|
|
}
|
2020-11-19 04:34:56 +01:00
|
|
|
catch (XunitException)
|
2020-11-13 08:28:15 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-11 05:41:39 +01:00
|
|
|
if (retry < 3)
|
|
|
|
{
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
retry++;
|
|
|
|
goto retry;
|
|
|
|
}
|
2020-11-13 08:28:15 +01:00
|
|
|
Assert.True(false, "No webhook event match the assertion");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
public async Task SetupWebhook()
|
|
|
|
{
|
|
|
|
FakeServer server = new FakeServer();
|
|
|
|
await server.Start();
|
|
|
|
var client = await CreateClient(Policies.CanModifyStoreWebhooks);
|
|
|
|
var wh = await client.CreateWebhook(StoreId, new CreateStoreWebhookRequest()
|
|
|
|
{
|
|
|
|
AutomaticRedelivery = false,
|
|
|
|
Url = server.ServerUri.AbsoluteUri
|
|
|
|
});
|
|
|
|
|
|
|
|
parent.Resources.Add(new WebhookListener(wh, server, WebhookEvents));
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task PayInvoice(string invoiceId)
|
|
|
|
{
|
|
|
|
var inv = await BitPay.GetInvoiceAsync(invoiceId);
|
|
|
|
var net = parent.ExplorerNode.Network;
|
|
|
|
this.parent.ExplorerNode.SendToAddress(BitcoinAddress.Create(inv.BitcoinAddress, net), inv.BtcDue);
|
|
|
|
await TestUtils.EventuallyAsync(async () =>
|
|
|
|
{
|
|
|
|
var localInvoice = await BitPay.GetInvoiceAsync(invoiceId, Facade.Merchant);
|
|
|
|
Assert.Equal("paid", localInvoice.Status);
|
|
|
|
});
|
|
|
|
}
|
2022-01-11 09:22:10 +01:00
|
|
|
|
|
|
|
public async Task AddGuest(string userId)
|
|
|
|
{
|
|
|
|
var repo = this.parent.PayTester.GetService<StoreRepository>();
|
|
|
|
await repo.AddStoreUser(StoreId, userId, "Guest");
|
|
|
|
}
|
2022-01-11 15:38:05 +01:00
|
|
|
public async Task AddOwner(string userId)
|
|
|
|
{
|
|
|
|
var repo = this.parent.PayTester.GetService<StoreRepository>();
|
|
|
|
await repo.AddStoreUser(StoreId, userId, "Owner");
|
|
|
|
}
|
2018-07-01 09:10:17 +02:00
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
}
|