forked from MegalithicBTC/BTCPayserver-LSPS1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSPS1Controller.cs
More file actions
167 lines (146 loc) · 7.18 KB
/
LSPS1Controller.cs
File metadata and controls
167 lines (146 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using BTCPayServer.Abstractions.Constants;
using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Plugins.LSPS1.Models;
using BTCPayServer.Plugins.LSPS1.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
namespace BTCPayServer.Plugins.LSPS1.Controllers
{
[Route("stores/{storeId}/plugins/lsps1")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
public sealed class LSPS1Controller : Controller
{
private readonly LspProviderService _lspProviderService;
private readonly LightningNodeService _lightningNodeService;
private readonly ILogger<LSPS1Controller> _logger;
public LSPS1Controller(
LspProviderService lspProviderService,
LightningNodeService lightningNodeService,
ILogger<LSPS1Controller> logger)
{
_lspProviderService = lspProviderService;
_lightningNodeService = lightningNodeService;
_logger = logger;
}
[HttpGet("")]
public async Task<IActionResult> Index(string storeId)
{
_logger.LogInformation("LSPS1Controller: Index request for store {StoreId}", storeId);
// Get store for Lightning node information
var store = await _lightningNodeService.GetStore(storeId);
if (store == null)
{
_logger.LogWarning("LSPS1Controller: Store {StoreId} not found", storeId);
return NotFound();
}
_logger.LogInformation("LSPS1Controller: Found store {StoreId}, Name: {StoreName}", storeId, store.StoreName);
// Add this line to explicitly set the store in the HTTP context
HttpContext.SetStoreData(store);
// First check if a valid Lightning node configuration exists
string? nodePublicKey = await _lightningNodeService.GetNodePublicKey(store);
_logger.LogInformation("LSPS1Controller: Node public key result: {NodePublicKey}", nodePublicKey ?? "null");
bool userHasLightningNode = !string.IsNullOrEmpty(nodePublicKey);
_logger.LogInformation("LSPS1Controller: User has Lightning node: {UserHasLightningNode}", userHasLightningNode);
// Initialize variables - we don't connect to LSP yet until user requests it
bool userNodeIsConnectedToLsp = false;
bool userNodeFailedToConnectToLsp = false;
LspProvider? connectedLsp = null;
var vm = new PluginPageViewModel
{
StoreId = storeId,
AvailableLsps = _lspProviderService.GetAllLsps(),
ConnectedLsp = connectedLsp,
SelectedLspSlug = "megalith-lsp",
NodePublicKey = nodePublicKey ?? string.Empty,
UserHasLightningNode = userHasLightningNode,
UserNodeIsConnectedToLsp = userNodeIsConnectedToLsp,
UserNodeFailedToConnectToLsp = userNodeFailedToConnectToLsp
};
// Create a client data object with all the properties needed by JavaScript
var clientData = new
{
storeId = vm.StoreId,
userNodeIsConnectedToLsp = vm.UserNodeIsConnectedToLsp,
userNodeFailedToConnectToLsp = vm.UserNodeFailedToConnectToLsp,
selectedLspSlug = vm.SelectedLspSlug,
connectedLspName = vm.ConnectedLsp?.Name ?? string.Empty,
lspUrl = connectedLsp?.Url ?? string.Empty, // Add direct LSP URL for client-side API calls
nodePublicKey = vm.NodePublicKey,
userHasLightningNode = vm.UserHasLightningNode,
availableLsps = vm.AvailableLsps.Select(lsp => new
{
slug = lsp.Slug,
name = lsp.Name,
url = lsp.Url,
selected = lsp.Slug == vm.SelectedLspSlug
})
};
// Serialize the data for the client
ViewBag.ClientDataJson = JsonSerializer.Serialize(clientData, new JsonSerializerOptions
{
// Don't use PropertyNamingPolicy to preserve original property names (snake_case)
PropertyNamingPolicy = null,
WriteIndented = false
});
return View(vm);
}
[HttpPost("connect-node")]
public async Task<IActionResult> ConnectNode(string storeId, [FromBody] ConnectNodeRequest request)
{
if (request == null || string.IsNullOrEmpty(request.Uri))
{
return BadRequest(new { success = false, error = "No node URI provided" });
}
try
{
_logger.LogInformation("Connecting node for store {StoreId} to URI {Uri}", storeId, request.Uri);
// Get a store instance to use for connecting
var store = await _lightningNodeService.GetStore(storeId);
if (store == null)
{
_logger.LogWarning("Store {StoreId} not found", storeId);
return NotFound(new { success = false, error = "Store not found" });
}
// Attempt to connect to the node
bool success = await _lightningNodeService.ConnectToNode(store, request.Uri);
if (success)
{
_logger.LogInformation("Successfully connected to node {Uri} for store {StoreId}", request.Uri, storeId);
return Json(new { success = true });
}
else
{
_logger.LogWarning("Failed to connect to node {Uri} for store {StoreId}", request.Uri, storeId);
return Json(new { success = false, error = $"Failed to connect to {request.Uri}" });
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error connecting to node for store {StoreId}: {Error}", storeId, ex.Message);
return Json(new { success = false, error = ex.Message });
}
}
public class ConnectNodeRequest
{
public string Uri { get; set; } = string.Empty;
public string LspSlug { get; set; } = string.Empty;
}
public class PluginPageViewModel
{
public string StoreId { get; set; } = string.Empty;
public IEnumerable<LspProvider> AvailableLsps { get; set; } = Array.Empty<LspProvider>();
public LspProvider? ConnectedLsp { get; set; }
public string SelectedLspSlug { get; set; } = string.Empty;
public string NodePublicKey { get; set; } = string.Empty;
public bool UserHasLightningNode { get; set; }
public bool UserNodeIsConnectedToLsp { get; set; }
public bool UserNodeFailedToConnectToLsp { get; set; }
}
}
}