Skip to content

Commit

Permalink
Merge pull request #419 from stakwork/feat/add-lightning-peer
Browse files Browse the repository at this point in the history
Feat/add lightning peer
  • Loading branch information
Evanfeenstra authored Nov 28, 2024
2 parents 594eb21 + a0d5dd2 commit 83cf9e9
Show file tree
Hide file tree
Showing 19 changed files with 429 additions and 31 deletions.
9 changes: 7 additions & 2 deletions app/src/api/cln.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
5 changes: 4 additions & 1 deletion app/src/api/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ export type Cmd =
| "ChangeChildSwarmPassword"
| "GetLightningBotsDetails"
| "ChangeLightningBotLabel"
| "CreateInvoiceForLightningBot";
| "CreateInvoiceForLightningBot"
| "GetLightningPeers"
| "AddLightningPeer"
| "UpdateLightningPeer";

interface CmdData {
cmd: Cmd;
Expand Down
9 changes: 7 additions & 2 deletions app/src/api/lnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
34 changes: 32 additions & 2 deletions app/src/api/swarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,36 @@ 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");
}

export async function add_lightning_peer({
pubkey,
alias,
}: {
pubkey: string;
alias: string;
}) {
return await swarmCmd("AddLightningPeer", { pubkey, alias });
}

export async function update_lightning_peer({
pubkey,
alias,
}: {
pubkey: string;
alias: string;
}) {
return await swarmCmd("UpdateLightningPeer", { pubkey, alias });
}
9 changes: 9 additions & 0 deletions app/src/helpers/cln.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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;
}
23 changes: 21 additions & 2 deletions app/src/helpers/swarm.ts
Original file line number Diff line number Diff line change
@@ -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<Stack>,
Expand Down Expand Up @@ -87,3 +91,18 @@ 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)}`;
}

export function formatPubkeyAliasDisplay(pubkey: string, alias: string) {
return `${alias} (${formatPubkey(pubkey)})`;
}
8 changes: 7 additions & 1 deletion app/src/lnd/AddChannel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -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,
}))
: [];
Expand Down
9 changes: 7 additions & 2 deletions app/src/lnd/ChannelRows.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -157,7 +160,9 @@
</div>
{/if}
<div class="td">
<span class="pubkey">{chan.remote_pubkey}</span>
<span class="pubkey"
>{peersObj[chan.remote_pubkey] || chan.remote_pubkey}</span
>
</div>
</div>
{#if selectedChannelParter === chan.remote_pubkey}
Expand Down
15 changes: 12 additions & 3 deletions app/src/lnd/Lnd.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
selectedNode,
hsmd,
hsmdClients,
lightningPeers,
} from "../store";
import { onMount } from "svelte";
import { get_clients } from "../api/hsmd";
import { handleGetLightningPeers } from "../helpers/swarm";
export let tag = "";
export let type = "";
Expand Down Expand Up @@ -43,9 +45,16 @@
}
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
await handleGetLightningPeers();
} catch (error) {
console.log(error);
}
});
</script>
Expand Down
Loading

0 comments on commit 83cf9e9

Please sign in to comment.