From 129d66f2c2ff9da838366e3eb22669f5beb929fd Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Wed, 27 Nov 2024 09:28:27 +0100 Subject: [PATCH 1/8] chore: create lightning peers config --- src/config.rs | 8 ++++++++ src/defaults.rs | 2 ++ src/secondbrain.rs | 2 ++ src/sphinxv2.rs | 3 +++ 4 files changed, 15 insertions(+) diff --git a/src/config.rs b/src/config.rs index 8f0458ef..87194f2f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -70,6 +70,7 @@ pub struct Stack { #[serde(skip_serializing_if = "Option::is_none")] pub global_mem_limit: Option, pub backup_services: Option>, + pub lightning_peers: Option, } #[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] @@ -88,6 +89,12 @@ pub struct User { pub role: Role, } +#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] +pub struct LightningPeer { + pub alias: String, + pub pubkey: String, +} + #[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] pub struct SendSwarmDetailsBody { pub username: String, @@ -308,6 +315,7 @@ impl Stack { custom_2b_domain: self.custom_2b_domain.clone(), global_mem_limit: self.global_mem_limit, backup_services: self.backup_services.clone(), + lightning_peers: self.lightning_peers.clone(), } } } diff --git a/src/defaults.rs b/src/defaults.rs index 3bb6f683..6aca868c 100644 --- a/src/defaults.rs +++ b/src/defaults.rs @@ -180,6 +180,7 @@ impl Default for Stack { custom_2b_domain: env_no_empty("NAV_BOLTWALL_SHARED_HOST"), global_mem_limit: None, backup_services: None, + lightning_peers: None, } } } @@ -336,6 +337,7 @@ pub fn llama_only(network: &str, host: Option) -> Stack { custom_2b_domain: None, global_mem_limit: None, backup_services: None, + lightning_peers: None, } } diff --git a/src/secondbrain.rs b/src/secondbrain.rs index c7eafc0b..5582e8f2 100644 --- a/src/secondbrain.rs +++ b/src/secondbrain.rs @@ -29,6 +29,7 @@ pub fn only_second_brain(network: &str, host: Option, lightning_provider custom_2b_domain: env_no_empty("NAV_BOLTWALL_SHARED_HOST"), global_mem_limit: None, backup_services: Some(vec!["boltwall".to_string(), "neo4j".to_string()]), + lightning_peers: None, } } @@ -128,5 +129,6 @@ pub fn default_local_stack(host: Option, network: &str, nodes: Vec custom_2b_domain: None, global_mem_limit: None, backup_services: None, + lightning_peers: None, } } diff --git a/src/sphinxv2.rs b/src/sphinxv2.rs index 27c8963b..3505f86d 100644 --- a/src/sphinxv2.rs +++ b/src/sphinxv2.rs @@ -77,6 +77,7 @@ pub fn sphinxv2_only(network: &str, host: Option) -> Stack { custom_2b_domain: None, global_mem_limit: None, backup_services: None, + lightning_peers: None, } } @@ -113,6 +114,7 @@ pub fn sphinxv1_only(network: &str, host: Option) -> Stack { custom_2b_domain: None, global_mem_limit: None, backup_services: Some(vec!["mixer".to_string(), "tribes".to_string()]), + lightning_peers: None, } } @@ -131,5 +133,6 @@ pub fn config_only(host: Option) -> Stack { custom_2b_domain: None, global_mem_limit: None, backup_services: None, + lightning_peers: None, } } From 2ac04298a8f761e4ce0a7e2838b065a216f6db7a Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Wed, 27 Nov 2024 12:09:19 +0100 Subject: [PATCH 2/8] chore: get all lightning peers --- app/src/api/cmd.ts | 3 ++- app/src/api/swarm.ts | 14 ++++++++++++-- app/src/lnd/Lnd.svelte | 4 ++++ app/src/store.ts | 2 ++ src/cmd.rs | 1 + src/handler.rs | 5 +++++ 6 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/src/api/cmd.ts b/app/src/api/cmd.ts index 321f164c..bda03319 100644 --- a/app/src/api/cmd.ts +++ b/app/src/api/cmd.ts @@ -98,7 +98,8 @@ export type Cmd = | "ChangeChildSwarmPassword" | "GetLightningBotsDetails" | "ChangeLightningBotLabel" - | "CreateInvoiceForLightningBot"; + | "CreateInvoiceForLightningBot" + | "GetLightningPeers"; interface CmdData { cmd: Cmd; diff --git a/app/src/api/swarm.ts b/app/src/api/swarm.ts index 27a273b3..092d6b41 100644 --- a/app/src/api/swarm.ts +++ b/app/src/api/swarm.ts @@ -393,6 +393,16 @@ export async function change_lightning_bot_label({ return await swarmCmd("ChangeLightningBotLabel", { id, new_label }); } -export async function create_invoice_for_lightning_bot({id, amt_msat}: {id: string, amt_msat: number}) { - return await swarmCmd("CreateInvoiceForLightningBot", {id, amt_msat}) +export async function create_invoice_for_lightning_bot({ + id, + amt_msat, +}: { + id: string; + amt_msat: number; +}) { + return await swarmCmd("CreateInvoiceForLightningBot", { id, amt_msat }); +} + +export async function get_lightning_peers() { + return await swarmCmd("GetLightningPeers"); } diff --git a/app/src/lnd/Lnd.svelte b/app/src/lnd/Lnd.svelte index b70c6756..1a7382c1 100644 --- a/app/src/lnd/Lnd.svelte +++ b/app/src/lnd/Lnd.svelte @@ -13,6 +13,7 @@ } from "../store"; import { onMount } from "svelte"; import { get_clients } from "../api/hsmd"; + import { get_lightning_peers } from "../api/swarm"; export let tag = ""; export let type = ""; @@ -47,6 +48,9 @@ const clients = await get_clients(tag); if (clients) hsmdClients.set(clients); } + + //get all lightning peers + const res = await get_lightning_peers(); }); diff --git a/app/src/store.ts b/app/src/store.ts index 5845aa7b..fba01cab 100644 --- a/app/src/store.ts +++ b/app/src/store.ts @@ -38,6 +38,8 @@ export const people = writable([]); export const channels = writable<{ [tag: string]: LndChannel[] }>({}); +export const lightningPeers = writable<{ [pubkey: string]: string }>({}); + export const proxy = writable({ total: 0, user_count: 0, diff --git a/src/cmd.rs b/src/cmd.rs index cc19bd85..eb27ca0e 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -161,6 +161,7 @@ pub enum SwarmCmd { GetSignedInUserDetails, GetAllImageActualVersion, ChangeUserPasswordBySuperAdmin(ChangeUserPasswordBySuperAdminInfo), + GetLightningPeers, } #[derive(Serialize, Deserialize, Debug, Clone)] diff --git a/src/handler.rs b/src/handler.rs index 1f006e35..2d3170a7 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -449,6 +449,11 @@ pub async fn handle( .await; Some(serde_json::to_string(&res)?) } + SwarmCmd::GetLightningPeers => { + log::info!("Get all lightning peers"); + let res = &state.stack.lightning_peers; + Some(serde_json::to_string(&res)?) + } }, Cmd::Relay(c) => { let client = state.clients.relay.get(tag).context("no relay client")?; From 005f5abb99cac9388d1a45424a17bddbaf989466 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Wed, 27 Nov 2024 17:06:43 +0100 Subject: [PATCH 3/8] chore: add lighting peer feature --- app/src/api/cmd.ts | 3 +- app/src/api/swarm.ts | 10 ++++ app/src/lnd/Peers.svelte | 100 +++++++++++++++++++++++++++++++++++++-- src/cmd.rs | 3 +- src/config.rs | 2 +- src/conn/swarm/mod.rs | 50 +++++++++++++++++++- src/handler.rs | 6 +++ 7 files changed, 166 insertions(+), 8 deletions(-) diff --git a/app/src/api/cmd.ts b/app/src/api/cmd.ts index bda03319..5c8ce60f 100644 --- a/app/src/api/cmd.ts +++ b/app/src/api/cmd.ts @@ -99,7 +99,8 @@ export type Cmd = | "GetLightningBotsDetails" | "ChangeLightningBotLabel" | "CreateInvoiceForLightningBot" - | "GetLightningPeers"; + | "GetLightningPeers" + | "AddLightningPeer"; interface CmdData { cmd: Cmd; diff --git a/app/src/api/swarm.ts b/app/src/api/swarm.ts index 092d6b41..263475db 100644 --- a/app/src/api/swarm.ts +++ b/app/src/api/swarm.ts @@ -406,3 +406,13 @@ export async function create_invoice_for_lightning_bot({ export async function get_lightning_peers() { return await swarmCmd("GetLightningPeers"); } + +export async function add_lightning_peer({ + pubkey, + alias, +}: { + pubkey: string; + alias: string; +}) { + return await swarmCmd("AddLightningPeer", { pubkey, alias }); +} diff --git a/app/src/lnd/Peers.svelte b/app/src/lnd/Peers.svelte index a5e95fe5..0ba2e5f7 100644 --- a/app/src/lnd/Peers.svelte +++ b/app/src/lnd/Peers.svelte @@ -3,6 +3,7 @@ Button, TextInput, InlineNotification, + Modal, } from "carbon-components-svelte"; import Add from "carbon-icons-svelte/lib/Add.svelte"; import ArrowLeft from "carbon-icons-svelte/lib/ArrowLeft.svelte"; @@ -15,6 +16,7 @@ isOnboarding, } from "../store"; import { parseClnListPeerRes } from "../helpers/cln"; + import { add_lightning_peer } from "../api/swarm"; $: pubkey = ""; $: host = ""; @@ -29,19 +31,28 @@ let show_notification = false; let message = ""; + let open_add_peer_detail = false; + let isSubmitting = false; + let alias = ""; + let peerPubkey = ""; + let error_notification = false; + let error_message = false; async function addPeer() { + message = ""; if (type === "Cln") { const peer = await CLN.add_peer(tag, pubkey, host); show_notification = true; if (typeof peer === "string") { message = peer; + error_message = true; return; } if (typeof peer !== "object") { message = "unexpected error"; + error_message = true; console.log(peer); return; } @@ -84,14 +95,48 @@ } } + function handleOpenAddPeer() { + open_add_peer_detail = true; + alias = ""; + peerPubkey = ""; + } + + function handleOnCloseAddPeer() { + open_add_peer_detail = false; + alias = ""; + peerPubkey = ""; + } + + async function handleAddPeer() { + message = ""; + isSubmitting = true; + try { + const res = await add_lightning_peer({ pubkey: peerPubkey, alias }); + message = res.message; + if (res.success) { + show_notification = true; + handleOnCloseAddPeer(); + return; + } + error_notification = true; + } catch (error) { + error_notification = true; + } finally { + isSubmitting = false; + } + } + $: peersLength = peers && peers.length ? peers.length : "No"; $: peersLabel = peers && peers.length <= 1 ? "peer" : "peers"; $: addDisabled = !pubkey || !host;
-
{}}> - +
+
{}}> + +
+
{#if peers && peers.length} @@ -113,8 +158,8 @@ {#if show_notification} { @@ -147,9 +192,52 @@ >
+ (open_add_peer_detail = false)} + on:open + on:close={handleOnCloseAddPeer} + on:submit={handleAddPeer} + > + {#if error_notification} + { + e.preventDefault(); + error_notification = false; + }} + /> + {/if} +
+ +
+
+ +
+
diff --git a/src/cmd.rs b/src/cmd.rs index eb27ca0e..2ae06dbe 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -2,7 +2,7 @@ use anyhow::Error; use reqwest::Response; use std::collections::HashMap; -use crate::{images::Image, utils::make_reqwest_client}; +use crate::{config::LightningPeer, images::Image, utils::make_reqwest_client}; use anyhow::Context; use serde::{Deserialize, Serialize}; use sphinx_auther::secp256k1::PublicKey; @@ -162,6 +162,7 @@ pub enum SwarmCmd { GetAllImageActualVersion, ChangeUserPasswordBySuperAdmin(ChangeUserPasswordBySuperAdminInfo), GetLightningPeers, + AddLightningPeer(LightningPeer), } #[derive(Serialize, Deserialize, Debug, Clone)] diff --git a/src/config.rs b/src/config.rs index 87194f2f..d685718a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -70,7 +70,7 @@ pub struct Stack { #[serde(skip_serializing_if = "Option::is_none")] pub global_mem_limit: Option, pub backup_services: Option>, - pub lightning_peers: Option, + pub lightning_peers: Option>, } #[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] diff --git a/src/conn/swarm/mod.rs b/src/conn/swarm/mod.rs index 67cea928..d76ae816 100644 --- a/src/conn/swarm/mod.rs +++ b/src/conn/swarm/mod.rs @@ -1,10 +1,11 @@ use anyhow::Result; use serde::{Deserialize, Serialize}; +use serde_json::Value; use std::time::Duration; use crate::{ cmd::{ChangeUserPasswordBySuperAdminInfo, GetDockerImageTagsDetails}, - config::State, + config::{LightningPeer, State}, }; #[derive(Serialize, Deserialize, Debug, Clone)] @@ -18,6 +19,13 @@ pub struct ChangePasswordBySuperAdminResponse { pub message: String, } +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct SwarmResponse { + pub success: bool, + pub message: String, + pub data: Option, +} + pub async fn update_swarm() -> Result { let password = std::env::var("SWARM_UPDATER_PASSWORD").unwrap_or(String::new()); @@ -148,3 +156,43 @@ pub async fn change_swarm_user_password_by_user_admin( } } } + +pub fn add_new_lightning_peer( + state: &mut State, + info: LightningPeer, + must_save_stack: &mut bool, +) -> SwarmResponse { + if info.pubkey.is_empty() || info.alias.is_empty() { + return SwarmResponse { + success: false, + message: "pubkey and alias cannot be empty".to_string(), + data: None, + }; + } + + let lightning_peers_clone = state.stack.lightning_peers.clone(); + + if let Some(mut lightning_peers) = lightning_peers_clone { + let peer_exist = lightning_peers + .iter() + .position(|peer| peer.pubkey == info.pubkey); + if peer_exist.is_some() { + return SwarmResponse { + success: false, + message: "public key already exist, please update".to_string(), + data: None, + }; + } + lightning_peers.push(info); + state.stack.lightning_peers = Some(lightning_peers); + } else { + state.stack.lightning_peers = Some(vec![info]); + } + + *must_save_stack = true; + SwarmResponse { + success: true, + message: "peer added successfully".to_string(), + data: None, + } +} diff --git a/src/handler.rs b/src/handler.rs index 2d3170a7..117f54bf 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -10,6 +10,7 @@ use crate::config::User; use crate::config::{Clients, Node, Stack, State, STATE}; use crate::conn::boltwall::get_api_token; use crate::conn::boltwall::update_user; +use crate::conn::swarm::add_new_lightning_peer; use crate::conn::swarm::{change_swarm_user_password_by_user_admin, get_image_tags}; use crate::dock::*; use crate::images::DockerHubImage; @@ -454,6 +455,11 @@ pub async fn handle( let res = &state.stack.lightning_peers; Some(serde_json::to_string(&res)?) } + SwarmCmd::AddLightningPeer(info) => { + log::info!("Add new lightning peers"); + let res = add_new_lightning_peer(&mut state, info, &mut must_save_stack); + Some(serde_json::to_string(&res)?) + } }, Cmd::Relay(c) => { let client = state.clients.relay.get(tag).context("no relay client")?; From b871826be220ce92799df68d1a53c57022875539 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Wed, 27 Nov 2024 22:02:41 +0100 Subject: [PATCH 4/8] chore: display alias and pubkey for list of known peers --- app/src/helpers/cln.ts | 9 +++++++++ app/src/helpers/swarm.ts | 19 +++++++++++++++++-- app/src/lnd/Lnd.svelte | 19 ++++++++++++------- app/src/lnd/Peers.svelte | 17 +++++++++++++++-- app/src/nodes.ts | 5 +++++ app/src/store.ts | 4 ++-- 6 files changed, 60 insertions(+), 13 deletions(-) diff --git a/app/src/helpers/cln.ts b/app/src/helpers/cln.ts index da55e6ee..b149c050 100644 --- a/app/src/helpers/cln.ts +++ b/app/src/helpers/cln.ts @@ -6,6 +6,7 @@ import { } from "./"; import long from "long"; import type { LndChannel, LndPeer } from "../api/lnd"; +import type { LightningPeer } from "../nodes"; enum ClnChannelState { CHANNELD_AWAITING_LOCKIN = "CHANNELD_AWAITING_LOCKIN", @@ -238,3 +239,11 @@ export function parseClnInvoices(transactions) { return []; } } + +export function convertLightningPeersToObject(lightningPeers: LightningPeer[]) { + const peersObj = {}; + for (let i = 0; i < lightningPeers.length; i++) { + peersObj[lightningPeers[i].pubkey] = lightningPeers[i].alias; + } + return peersObj; +} diff --git a/app/src/helpers/swarm.ts b/app/src/helpers/swarm.ts index fb8a6db4..e7ef65e4 100644 --- a/app/src/helpers/swarm.ts +++ b/app/src/helpers/swarm.ts @@ -1,7 +1,11 @@ import type { Writable } from "svelte/store"; -import { get_all_image_actual_version, get_image_tags } from "../api/swarm"; +import { + get_all_image_actual_version, + get_image_tags, + get_lightning_peers, +} from "../api/swarm"; import type { Stack, Node } from "../nodes"; -import { swarmVersion } from "../store"; +import { lightningPeers, swarmVersion } from "../store"; export async function getImageVersion( stack: Writable, @@ -87,3 +91,14 @@ export function splitPubkey(pubkey: string) { } return pubkey; } + +export async function handleGetLightningPeers() { + const res = await get_lightning_peers(); + if (Array.isArray(res)) { + lightningPeers.set(res); + } +} + +export function formatPubkey(pk: string) { + return `${pk.substring(0, 6)}...${pk.substring(pk.length - 6)}`; +} diff --git a/app/src/lnd/Lnd.svelte b/app/src/lnd/Lnd.svelte index 1a7382c1..59ae385c 100644 --- a/app/src/lnd/Lnd.svelte +++ b/app/src/lnd/Lnd.svelte @@ -10,10 +10,11 @@ selectedNode, hsmd, hsmdClients, + lightningPeers, } from "../store"; import { onMount } from "svelte"; import { get_clients } from "../api/hsmd"; - import { get_lightning_peers } from "../api/swarm"; + import { handleGetLightningPeers } from "../helpers/swarm"; export let tag = ""; export let type = ""; @@ -44,13 +45,17 @@ } onMount(async () => { - if (type === "Cln") { - const clients = await get_clients(tag); - if (clients) hsmdClients.set(clients); - } + try { + if (type === "Cln") { + const clients = await get_clients(tag); + if (clients) hsmdClients.set(clients); + } - //get all lightning peers - const res = await get_lightning_peers(); + //get all lightning peers + await handleGetLightningPeers(); + } catch (error) { + console.log(error); + } }); diff --git a/app/src/lnd/Peers.svelte b/app/src/lnd/Peers.svelte index 0ba2e5f7..4839ae71 100644 --- a/app/src/lnd/Peers.svelte +++ b/app/src/lnd/Peers.svelte @@ -14,9 +14,14 @@ finishedOnboarding, createdPeerForOnboarding, isOnboarding, + lightningPeers, } from "../store"; - import { parseClnListPeerRes } from "../helpers/cln"; + import { + convertLightningPeersToObject, + parseClnListPeerRes, + } from "../helpers/cln"; import { add_lightning_peer } from "../api/swarm"; + import { formatPubkey, handleGetLightningPeers } from "../helpers/swarm"; $: pubkey = ""; $: host = ""; @@ -115,6 +120,7 @@ message = res.message; if (res.success) { show_notification = true; + await handleGetLightningPeers(); handleOnCloseAddPeer(); return; } @@ -129,6 +135,11 @@ $: peersLength = peers && peers.length ? peers.length : "No"; $: peersLabel = peers && peers.length <= 1 ? "peer" : "peers"; $: addDisabled = !pubkey || !host; + $: peerObj = convertLightningPeersToObject($lightningPeers); + + function formatPubkeyAliasDisplay(pubkey: string, alias: string) { + return `${alias} (${formatPubkey(pubkey)})`; + }
@@ -144,7 +155,9 @@
{#each peers as peer}
-
{peer.pub_key}
+
+ {`${peerObj[peer.pub_key] ? formatPubkeyAliasDisplay(peer.pub_key, peerObj[peer.pub_key]) : peer.pub_key}`} +
{peer.address}
([]); export const channels = writable<{ [tag: string]: LndChannel[] }>({}); -export const lightningPeers = writable<{ [pubkey: string]: string }>({}); +export const lightningPeers = writable([]); export const proxy = writable({ total: 0, From 16baecc2536003e7b4606d97e275aab918b1fec7 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Wed, 27 Nov 2024 22:41:35 +0100 Subject: [PATCH 5/8] chore[frontend]: show alias and pub when for pubkey that exist --- app/src/helpers/swarm.ts | 4 ++++ app/src/lnd/AddChannel.svelte | 8 +++++++- app/src/lnd/ChannelRows.svelte | 9 +++++++-- app/src/lnd/Peers.svelte | 10 +++++----- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/app/src/helpers/swarm.ts b/app/src/helpers/swarm.ts index e7ef65e4..1cfbe6b1 100644 --- a/app/src/helpers/swarm.ts +++ b/app/src/helpers/swarm.ts @@ -102,3 +102,7 @@ export async function handleGetLightningPeers() { export function formatPubkey(pk: string) { return `${pk.substring(0, 6)}...${pk.substring(pk.length - 6)}`; } + +export function formatPubkeyAliasDisplay(pubkey: string, alias: string) { + return `${alias} (${formatPubkey(pubkey)})`; +} diff --git a/app/src/lnd/AddChannel.svelte b/app/src/lnd/AddChannel.svelte index 3dbf9d11..c657d96b 100644 --- a/app/src/lnd/AddChannel.svelte +++ b/app/src/lnd/AddChannel.svelte @@ -15,20 +15,24 @@ peers as peersStore, channels, channelCreatedForOnboarding, + lightningPeers, } from "../store"; import { formatSatsNumbers, convertSatsToMilliSats } from "../helpers"; import { + convertLightningPeersToObject, parseClnListFunds, parseClnListPeerChannelsRes, parseClnListPeerRes, } from "../helpers/cln"; import { getLndPendingAndActiveChannels } from "../helpers/lnd"; + import { formatPubkeyAliasDisplay } from "../helpers/swarm"; export let activeKey: string = null; $: pubkey = activeKey ? activeKey : ""; $: amount = 0; $: sats = 0; + $: peersObj = convertLightningPeersToObject($lightningPeers); export let tag = ""; export let type = ""; @@ -47,7 +51,9 @@ $: peerData = peers?.length ? peers.map((p) => ({ id: p.pub_key, - text: p.pub_key, + text: peersObj[p.pub_key] + ? formatPubkeyAliasDisplay(p.pub_key, peersObj[p.pub_key]) + : p.pub_key, })) : []; diff --git a/app/src/lnd/ChannelRows.svelte b/app/src/lnd/ChannelRows.svelte index c1d5ce4b..95c265c0 100644 --- a/app/src/lnd/ChannelRows.svelte +++ b/app/src/lnd/ChannelRows.svelte @@ -4,17 +4,20 @@ import ReceiveLine from "../components/ReceiveLine.svelte"; import DotWrap from "../components/DotWrap.svelte"; import Dot from "../components/Dot.svelte"; - import { channels } from "../store"; + import { channels, lightningPeers } from "../store"; import { formatSatsNumbers } from "../helpers"; import { getTransactionStatus, getBlockTip } from "../helpers/bitcoin"; import Exit from "carbon-icons-svelte/lib/Exit.svelte"; import { onDestroy, onMount } from "svelte"; + import { convertLightningPeersToObject } from "../helpers/cln"; export let tag = ""; export let onclose = (id: string, dest: string) => {}; let channel_arr = $channels[tag]; + $: peersObj = convertLightningPeersToObject($lightningPeers); + function copyText(txt: string) { navigator.clipboard.writeText(txt); } @@ -157,7 +160,9 @@
{/if}
- {chan.remote_pubkey} + {peersObj[chan.remote_pubkey] || chan.remote_pubkey}
{#if selectedChannelParter === chan.remote_pubkey} diff --git a/app/src/lnd/Peers.svelte b/app/src/lnd/Peers.svelte index 4839ae71..d7ca79b4 100644 --- a/app/src/lnd/Peers.svelte +++ b/app/src/lnd/Peers.svelte @@ -21,7 +21,11 @@ parseClnListPeerRes, } from "../helpers/cln"; import { add_lightning_peer } from "../api/swarm"; - import { formatPubkey, handleGetLightningPeers } from "../helpers/swarm"; + import { + formatPubkey, + formatPubkeyAliasDisplay, + handleGetLightningPeers, + } from "../helpers/swarm"; $: pubkey = ""; $: host = ""; @@ -136,10 +140,6 @@ $: peersLabel = peers && peers.length <= 1 ? "peer" : "peers"; $: addDisabled = !pubkey || !host; $: peerObj = convertLightningPeersToObject($lightningPeers); - - function formatPubkeyAliasDisplay(pubkey: string, alias: string) { - return `${alias} (${formatPubkey(pubkey)})`; - }
From daff370bacda9a8ff2ba9c23d85eb9bdc930ac05 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Wed, 27 Nov 2024 23:38:23 +0100 Subject: [PATCH 6/8] chore[frontend]: edit alias --- app/src/api/cmd.ts | 3 ++- app/src/api/swarm.ts | 10 ++++++++++ app/src/lnd/Peers.svelte | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/app/src/api/cmd.ts b/app/src/api/cmd.ts index 5c8ce60f..755e6cdf 100644 --- a/app/src/api/cmd.ts +++ b/app/src/api/cmd.ts @@ -100,7 +100,8 @@ export type Cmd = | "ChangeLightningBotLabel" | "CreateInvoiceForLightningBot" | "GetLightningPeers" - | "AddLightningPeer"; + | "AddLightningPeer" + | "UpdateLightningPeer"; interface CmdData { cmd: Cmd; diff --git a/app/src/api/swarm.ts b/app/src/api/swarm.ts index 263475db..2afff8d8 100644 --- a/app/src/api/swarm.ts +++ b/app/src/api/swarm.ts @@ -416,3 +416,13 @@ export async function add_lightning_peer({ }) { return await swarmCmd("AddLightningPeer", { pubkey, alias }); } + +export async function update_lightning_peer({ + pubkey, + alias, +}: { + pubkey: string; + alias: string; +}) { + return await swarmCmd("UpdateLightningPeer", { pubkey, alias }); +} diff --git a/app/src/lnd/Peers.svelte b/app/src/lnd/Peers.svelte index d7ca79b4..d074a689 100644 --- a/app/src/lnd/Peers.svelte +++ b/app/src/lnd/Peers.svelte @@ -20,7 +20,7 @@ convertLightningPeersToObject, parseClnListPeerRes, } from "../helpers/cln"; - import { add_lightning_peer } from "../api/swarm"; + import { add_lightning_peer, update_lightning_peer } from "../api/swarm"; import { formatPubkey, formatPubkeyAliasDisplay, @@ -46,6 +46,7 @@ let peerPubkey = ""; let error_notification = false; let error_message = false; + let isUpdate = false; async function addPeer() { message = ""; @@ -114,13 +115,19 @@ open_add_peer_detail = false; alias = ""; peerPubkey = ""; + isUpdate = false; } async function handleAddPeer() { message = ""; isSubmitting = true; try { - const res = await add_lightning_peer({ pubkey: peerPubkey, alias }); + let res; + if (isUpdate) { + res = await update_lightning_peer({ pubkey: peerPubkey, alias }); + } else { + res = await add_lightning_peer({ pubkey: peerPubkey, alias }); + } message = res.message; if (res.success) { show_notification = true; @@ -136,6 +143,13 @@ } } + function openEditAlias(pubkey: string) { + isUpdate = true; + peerPubkey = pubkey; + alias = peerObj[pubkey]; + open_add_peer_detail = true; + } + $: peersLength = peers && peers.length ? peers.length : "No"; $: peersLabel = peers && peers.length <= 1 ? "peer" : "peers"; $: addDisabled = !pubkey || !host; @@ -159,6 +173,14 @@ {`${peerObj[peer.pub_key] ? formatPubkeyAliasDisplay(peer.pub_key, peerObj[peer.pub_key]) : peer.pub_key}`}
{peer.address}
+ {#if peerObj[peer.pub_key]} + + {/if} @@ -207,9 +229,13 @@
(open_add_peer_detail = false)} on:open @@ -240,6 +266,7 @@ labelText="Pubkey" placeholder="Enter Peer Pubkey..." bind:value={peerPubkey} + readonly={isUpdate || false} /> From ae943284a2b363554c96987bb3a434e61a09fcb6 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Thu, 28 Nov 2024 00:04:55 +0100 Subject: [PATCH 7/8] chore: update peer backend --- src/cmd.rs | 1 + src/conn/swarm/mod.rs | 45 +++++++++++++++++++++++++++++++++++++++++++ src/handler.rs | 8 +++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/cmd.rs b/src/cmd.rs index 2ae06dbe..d1eaad21 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -163,6 +163,7 @@ pub enum SwarmCmd { ChangeUserPasswordBySuperAdmin(ChangeUserPasswordBySuperAdminInfo), GetLightningPeers, AddLightningPeer(LightningPeer), + UpdateLightningPeer(LightningPeer), } #[derive(Serialize, Deserialize, Debug, Clone)] diff --git a/src/conn/swarm/mod.rs b/src/conn/swarm/mod.rs index d76ae816..e24133de 100644 --- a/src/conn/swarm/mod.rs +++ b/src/conn/swarm/mod.rs @@ -196,3 +196,48 @@ pub fn add_new_lightning_peer( data: None, } } + +pub fn update_lightning_peer( + state: &mut State, + info: LightningPeer, + must_save_stack: &mut bool, +) -> SwarmResponse { + if info.alias.is_empty() { + return SwarmResponse { + success: false, + message: "alias cannot be empty".to_string(), + data: None, + }; + } + + if state.stack.lightning_peers.is_none() { + return SwarmResponse { + success: false, + message: "pubkey does not exist".to_string(), + data: None, + }; + }; + + if let Some(mut clone_lightning_peers) = state.stack.lightning_peers.clone() { + let pos = clone_lightning_peers + .iter() + .position(|peer| peer.pubkey == info.pubkey); + + if pos.is_none() { + return SwarmResponse { + success: false, + message: "invalid pubkey".to_string(), + data: None, + }; + } + + clone_lightning_peers[pos.unwrap()] = info; + state.stack.lightning_peers = Some(clone_lightning_peers); + } + *must_save_stack = true; + SwarmResponse { + success: true, + message: "alias updated successfully".to_string(), + data: None, + } +} diff --git a/src/handler.rs b/src/handler.rs index 117f54bf..5993997a 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -11,6 +11,7 @@ use crate::config::{Clients, Node, Stack, State, STATE}; use crate::conn::boltwall::get_api_token; use crate::conn::boltwall::update_user; use crate::conn::swarm::add_new_lightning_peer; +use crate::conn::swarm::update_lightning_peer; use crate::conn::swarm::{change_swarm_user_password_by_user_admin, get_image_tags}; use crate::dock::*; use crate::images::DockerHubImage; @@ -456,10 +457,15 @@ pub async fn handle( Some(serde_json::to_string(&res)?) } SwarmCmd::AddLightningPeer(info) => { - log::info!("Add new lightning peers"); + log::info!("Add new lightning peer"); let res = add_new_lightning_peer(&mut state, info, &mut must_save_stack); Some(serde_json::to_string(&res)?) } + SwarmCmd::UpdateLightningPeer(info) => { + log::info!("Update Lightning peer"); + let res = update_lightning_peer(&mut state, info, &mut must_save_stack); + Some(serde_json::to_string(&res)?) + } }, Cmd::Relay(c) => { let client = state.clients.relay.get(tag).context("no relay client")?; From a0d5dd23e654ae44d6f2e63329b3cfe946d000e0 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Thu, 28 Nov 2024 14:18:53 +0100 Subject: [PATCH 8/8] chore: add alias when creating peer node connection --- app/src/api/cln.ts | 9 +++++++-- app/src/api/lnd.ts | 9 +++++++-- app/src/lnd/Peers.svelte | 16 +++++++++++++--- src/cmd.rs | 1 + src/handler.rs | 37 +++++++++++++++++++++++++++++++++---- 5 files changed, 61 insertions(+), 11 deletions(-) diff --git a/app/src/api/cln.ts b/app/src/api/cln.ts index 526d55a1..118d752c 100644 --- a/app/src/api/cln.ts +++ b/app/src/api/cln.ts @@ -86,6 +86,11 @@ export async function create_channel( return await clnCmd("AddChannel", tag, { pubkey, amount, satsperbyte }); } -export async function add_peer(tag: string, pubkey: string, host: string) { - return await clnCmd("AddPeer", tag, { pubkey, host }); +export async function add_peer( + tag: string, + pubkey: string, + host: string, + alias?: string +) { + return await clnCmd("AddPeer", tag, { pubkey, host, alias }); } diff --git a/app/src/api/lnd.ts b/app/src/api/lnd.ts index 224cc824..5037eb2c 100644 --- a/app/src/api/lnd.ts +++ b/app/src/api/lnd.ts @@ -59,8 +59,13 @@ export async function list_channels(tag: string) { return await lndCmd("ListChannels", tag); } -export async function add_peer(tag: string, pubkey: string, host: string) { - return await lndCmd("AddPeer", tag, { pubkey, host }); +export async function add_peer( + tag: string, + pubkey: string, + host: string, + alias?: string +) { + return await lndCmd("AddPeer", tag, { pubkey, host, alias }); } export async function list_peers(tag: string) { diff --git a/app/src/lnd/Peers.svelte b/app/src/lnd/Peers.svelte index d074a689..6860165f 100644 --- a/app/src/lnd/Peers.svelte +++ b/app/src/lnd/Peers.svelte @@ -47,11 +47,12 @@ let error_notification = false; let error_message = false; let isUpdate = false; + let peerAlias = ""; async function addPeer() { message = ""; if (type === "Cln") { - const peer = await CLN.add_peer(tag, pubkey, host); + const peer = await CLN.add_peer(tag, pubkey, host, peerAlias); show_notification = true; if (typeof peer === "string") { @@ -69,6 +70,8 @@ if (peer) { pubkey = ""; host = ""; + peerAlias = ""; + await handleGetLightningPeers(); const peersData = await CLN.list_peers(tag); const thepeers = await parseClnListPeerRes(peersData); peersStore.update((peer) => { @@ -77,11 +80,12 @@ createdPeerForOnboarding.update(() => true); } } else { - if (await add_peer(tag, pubkey, host)) { + if (await add_peer(tag, pubkey, host, peerAlias)) { show_notification = true; pubkey = ""; host = ""; - + peerAlias = ""; + await handleGetLightningPeers(); setTimeout(async () => { const peersData = await list_peers(tag); peersStore.update((ps) => { @@ -217,6 +221,12 @@ bind:value={host} />
+ +