Files
btcpayserver/BTCPayServer/VaultClient.cs
Nicolas Dorier 2f26979ed7 Refactor vault (#6678)
* Use Blazor for the Vault code

* Put elements in different file

* Controller abstraction

* Break into VaultElement
2025-04-21 17:09:46 +09:00

57 lines
1.9 KiB
C#

#nullable enable
using System;
using System.Linq;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.JSInterop;
using Newtonsoft.Json.Linq;
namespace BTCPayServer;
public class VaultClient(IJSRuntime js, string serviceUri)
{
public class VaultNotConnectedException() : Exception("Vault not connected");
public class VaultException(string message) : Exception(message);
public async Task<VaultPermissionResult> AskPermission(CancellationToken cancellationToken)
{
return await js.InvokeAsync<VaultPermissionResult>("vault.askVaultPermission", cancellationToken, serviceUri);
}
public async Task<JToken?> SendVaultRequest(string? path, JObject? body, CancellationToken cancellationToken)
{
var isAbsolute = path is not null && Uri.IsWellFormedUriString(path, UriKind.Absolute);
var query = new JsonObject()
{
["uri"] = isAbsolute ? path : serviceUri + path
};
if (body is not null)
query["body"] = JsonObject.Parse(body.ToString());
var resp = await js.InvokeAsync<SendRequestResponse>("vault.sendRequest", cancellationToken, query);
if (resp.HttpCode is not { } p)
throw new VaultNotConnectedException();
if (p != 200)
throw new VaultException($"Unexpected response code from vault {p}");
return (resp.Body)?.ToJsonString() is { } str ? JToken.Parse(str) : null;
}
public class SendRequestResponse
{
public int? HttpCode { get; set; }
public JsonNode? Body { get; set; }
}
public class HwiResponse
{
public int HttpCode { get; set; }
public string? Error { get; set; }
public JsonNode? Body { get; set; }
}
}
public class VaultPermissionResult
{
public int HttpCode { get; set; }
public string? Browser { get; set; }
}