From c77e0ab8416cabef153f56bea3feb894ba0fc9a7 Mon Sep 17 00:00:00 2001
From: ithiame <ithiame@agiltoo.fr>
Date: Tue, 5 Nov 2024 12:52:31 +0000
Subject: [PATCH] =?UTF-8?q?=20=E2=9A=A1=20Manage=20nostr-bot=20certify=20w?=
 =?UTF-8?q?ith=20lightning=20address=20via=20phoenixd=20getlnaddress=20#15?=
 =?UTF-8?q?80?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/lib/server/phoenixd.ts                           | 12 ++++++++++++
 src/lib/server/runtime-config.ts                     |  3 ++-
 .../admin[[hash=admin_hash]]/nostr/+page.server.ts   |  2 ++
 .../phoenixd/+page.server.ts                         |  2 ++
 4 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/src/lib/server/phoenixd.ts b/src/lib/server/phoenixd.ts
index 47cd7e777..2c43a0620 100644
--- a/src/lib/server/phoenixd.ts
+++ b/src/lib/server/phoenixd.ts
@@ -34,6 +34,18 @@ export async function phoenixdBalance(): Promise<{ balanceSat: number; feeCredit
 	return await res.json();
 }
 
+export async function phoenixdLndAddress(): Promise<string> {
+	const res = await fetch(`${runtimeConfig.phoenixd.url}/getlnaddress`, {
+		headers: {
+			Authorization: `Basic ${Buffer.from(`:${runtimeConfig.phoenixd.password}`).toString(
+				'base64'
+			)}`
+		}
+	});
+
+	return await res.text();
+}
+
 export async function phoenixdDetected(url?: string): Promise<boolean> {
 	return await Promise.race<boolean>([
 		fetch(`${url || runtimeConfig.phoenixd.url}/getinfo`).then(
diff --git a/src/lib/server/runtime-config.ts b/src/lib/server/runtime-config.ts
index d6eed6249..6dfc5da16 100644
--- a/src/lib/server/runtime-config.ts
+++ b/src/lib/server/runtime-config.ts
@@ -123,7 +123,8 @@ const baseConfig = {
 	phoenixd: {
 		url: 'http://localhost:9740',
 		enabled: false,
-		password: ''
+		password: '',
+		lndAddress: ''
 	},
 	productActionSettings: {
 		eShop: {
diff --git a/src/routes/(app)/admin[[hash=admin_hash]]/nostr/+page.server.ts b/src/routes/(app)/admin[[hash=admin_hash]]/nostr/+page.server.ts
index 4b6c2a9ba..43bb494d2 100644
--- a/src/routes/(app)/admin[[hash=admin_hash]]/nostr/+page.server.ts
+++ b/src/routes/(app)/admin[[hash=admin_hash]]/nostr/+page.server.ts
@@ -56,6 +56,8 @@ export const actions = {
 				display_name: runtimeConfig.brandName,
 				website: ORIGIN,
 				...(lnAddress && { lud16: `ln@${domainName}` }),
+				...(!lnAddress &&
+					runtimeConfig.phoenixd.lndAddress && { lud16: runtimeConfig.phoenixd.lndAddress }),
 				// about: '',
 				...(runtimeConfig.logo && { picture: pictureUrl }),
 				nip05: `_@${domainName}`,
diff --git a/src/routes/(app)/admin[[hash=admin_hash]]/phoenixd/+page.server.ts b/src/routes/(app)/admin[[hash=admin_hash]]/phoenixd/+page.server.ts
index 26b7760f7..7f4abbb71 100644
--- a/src/routes/(app)/admin[[hash=admin_hash]]/phoenixd/+page.server.ts
+++ b/src/routes/(app)/admin[[hash=admin_hash]]/phoenixd/+page.server.ts
@@ -6,6 +6,7 @@ import {
 	phoenixdBalance,
 	phoenixdDetected,
 	phoenixdInfo,
+	phoenixdLndAddress,
 	phoenixdPayInvoice,
 	phoenixdSendOnChain
 } from '$lib/server/phoenixd.js';
@@ -76,6 +77,7 @@ export const actions = {
 			.parse(Object.fromEntries(await event.request.formData()));
 
 		runtimeConfig.phoenixd.password = parsed.password;
+		runtimeConfig.phoenixd.lndAddress = await phoenixdLndAddress();
 
 		await collections.runtimeConfig.updateOne(
 			{ _id: 'phoenixd' },