From 8ddb81e0695e9c7d5c2812d881c9f02b8f445858 Mon Sep 17 00:00:00 2001 From: ithiame Date: Mon, 4 Nov 2024 15:52:38 +0000 Subject: [PATCH 1/4] Bolt 12 address phoenixD --- src/lib/components/CmsDesign.svelte | 6 ++++++ src/lib/server/cms.ts | 17 +++++++++++++++-- src/lib/server/phoenixd.ts | 11 +++++++++++ .../phoenixd/+page.server.ts | 6 ++++-- .../phoenixd/+page.svelte | 8 ++++++++ .../(app)/phoenixd/bolt12/qrcode/+server.ts | 10 ++++++++++ src/routes/+layout.server.ts | 4 +++- 7 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/routes/(app)/phoenixd/bolt12/qrcode/+server.ts diff --git a/src/lib/components/CmsDesign.svelte b/src/lib/components/CmsDesign.svelte index 9905a0590..5230df9f9 100644 --- a/src/lib/components/CmsDesign.svelte +++ b/src/lib/components/CmsDesign.svelte @@ -181,6 +181,12 @@ : ''}{token.height ? `height: ${token.height}px;` : ''}" /> + {:else if token.type === 'qrCode'} + {#if token.slug === 'Bolt12'} + + QR code + + {/if} {:else if token.type === 'html'}
diff --git a/src/lib/server/cms.ts b/src/lib/server/cms.ts index 03e5e1be4..5c2b6df26 100644 --- a/src/lib/server/cms.ts +++ b/src/lib/server/cms.ts @@ -76,7 +76,9 @@ type TokenObject = slug: string; display: string | undefined; raw: string; - }; + } + | { type: 'qrCode'; slug: string; raw: string }; + export async function cmsFromContent( { content, mobileContent }: { content: string; mobileContent?: string }, locals: Partial> @@ -98,6 +100,7 @@ export async function cmsFromContent( /\[TagProducts=(?[\p{L}\d_-]+)(?:[?\s]display=(?[a-z0-9-]+))?\]/giu; const GALLERY_WIDGET_REGEX = /\[Gallery=(?[\p{L}\d_-]+)(?:[?\s]display=(?[a-z0-9-]+))?\]/giu; + const QRCODE_REGEX = /\[QRCode=(?[\p{L}\d_-]+)\]/giu; const productSlugs = new Set(); const challengeSlugs = new Set(); @@ -109,6 +112,7 @@ export async function cmsFromContent( const countdownFormSlugs = new Set(); const tagProductsSlugs = new Set(); const gallerySlugs = new Set(); + const qrCodeSlugs = new Set(); const tokens: { desktop: Array; @@ -138,7 +142,8 @@ export async function cmsFromContent( ...matchAndSort(content, PICTURE_WIDGET_REGEX, 'pictureWidget'), ...matchAndSort(content, COUNTDOWN_WIDGET_REGEX, 'countdownWidget'), ...matchAndSort(content, TAG_PRODUCTS_REGEX, 'tagProducts'), - ...matchAndSort(content, GALLERY_WIDGET_REGEX, 'galleryWidget') + ...matchAndSort(content, GALLERY_WIDGET_REGEX, 'galleryWidget'), + ...matchAndSort(content, QRCODE_REGEX, 'qrCode') ].sort((a, b) => (a.index ?? 0) - (b.index ?? 0)); for (const match of matches) { const html = trimPrefix(trimSuffix(content.slice(index, match.index), '

'), '

'); @@ -245,6 +250,14 @@ export async function cmsFromContent( raw: match[0] }); break; + case 'qrCode': + qrCodeSlugs.add(match.groups.slug); + token.push({ + type: 'qrCode', + slug: match.groups.slug, + raw: match[0] + }); + break; } } index = match.index + match[0].length; diff --git a/src/lib/server/phoenixd.ts b/src/lib/server/phoenixd.ts index 47cd7e777..d3ee5efe2 100644 --- a/src/lib/server/phoenixd.ts +++ b/src/lib/server/phoenixd.ts @@ -34,6 +34,17 @@ export async function phoenixdBalance(): Promise<{ balanceSat: number; feeCredit return await res.json(); } +export async function phoenixdGetBolt12(): Promise { + const res = await fetch(`${runtimeConfig.phoenixd.url}/getoffer`, { + headers: { + Authorization: `Basic ${Buffer.from(`:${runtimeConfig.phoenixd.password}`).toString( + 'base64' + )}` + } + }); + + return await res.text(); +} export async function phoenixdDetected(url?: string): Promise { return await Promise.race([ fetch(`${url || runtimeConfig.phoenixd.url}/getinfo`).then( 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..9f4665b78 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 @@ -5,6 +5,7 @@ import { isPhoenixdConfigured, phoenixdBalance, phoenixdDetected, + phoenixdGetBolt12, phoenixdInfo, phoenixdPayInvoice, phoenixdSendOnChain @@ -29,11 +30,12 @@ export const load = async () => { try { const nodeInfo = await phoenixdInfo(); const balance = await phoenixdBalance(); - + const bolt12Address = await phoenixdGetBolt12(); return { phoenixd: runtimeConfig.phoenixd, nodeInfo, - balance + balance, + bolt12Address }; } catch (err) { return { diff --git a/src/routes/(app)/admin[[hash=admin_hash]]/phoenixd/+page.svelte b/src/routes/(app)/admin[[hash=admin_hash]]/phoenixd/+page.svelte index 2691a1073..33f677ed0 100644 --- a/src/routes/(app)/admin[[hash=admin_hash]]/phoenixd/+page.svelte +++ b/src/routes/(app)/admin[[hash=admin_hash]]/phoenixd/+page.svelte @@ -22,6 +22,7 @@ let withdrawMode = 'bolt11' as 'bolt11' | 'bitcoin'; let defaultUrl = data.phoenixd.url || 'http://localhost:9740'; + let showBolt12 = false;

PhoenixD

@@ -92,6 +93,9 @@
+ {#if data.nodeInfo}
+ {#if showBolt12} +

Bolt12 address: {data.bolt12Address}

+

To use it on page CMS, use this code : [QRCode=Bolt12]

+ {/if}
diff --git a/src/routes/(app)/phoenixd/bolt12/qrcode/+server.ts b/src/routes/(app)/phoenixd/bolt12/qrcode/+server.ts new file mode 100644 index 000000000..cf19d108d --- /dev/null +++ b/src/routes/(app)/phoenixd/bolt12/qrcode/+server.ts @@ -0,0 +1,10 @@ +import { phoenixdGetBolt12 } from '$lib/server/phoenixd'; +import qrcode from 'qrcode'; + +export async function GET({}) { + const bolt12Address = await phoenixdGetBolt12(); + return new Response(await qrcode.toString('lightning:' + bolt12Address, { type: 'svg' }), { + headers: { 'content-type': 'image/svg+xml' }, + status: 200 + }); +} diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts index 2d269b889..6e9e4551b 100644 --- a/src/routes/+layout.server.ts +++ b/src/routes/+layout.server.ts @@ -1,3 +1,4 @@ +import { phoenixdGetBolt12 } from '$lib/server/phoenixd'; import { runtimeConfig, runtimeConfigUpdatedAt } from '$lib/server/runtime-config'; import { CUSTOMER_ROLE_ID } from '$lib/types/User'; @@ -38,6 +39,7 @@ export async function load(event) { viewportWidth, contactModes: runtimeConfig.contactModes, hideFromSearchEngines: runtimeConfig.hideFromSearchEngines, - ageRestriction: runtimeConfig.ageRestriction + ageRestriction: runtimeConfig.ageRestriction, + bolt12Address: phoenixdGetBolt12() }; } From 2b57198b3bab095e7629d2e22d3899baebdb5637 Mon Sep 17 00:00:00 2001 From: ithiame Date: Mon, 4 Nov 2024 16:20:57 +0000 Subject: [PATCH 2/4] Bolt 12 address phoenixD --- .../phoenixd/+page.server.ts | 3 ++- .../(app)/phoenixd/bolt12/qrcode/+server.ts | 22 ++++++++++++++----- src/routes/+layout.server.ts | 9 +++++++- 3 files changed, 27 insertions(+), 7 deletions(-) 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 9f4665b78..83aa91cb4 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 @@ -41,7 +41,8 @@ export const load = async () => { return { phoenixd: runtimeConfig.phoenixd, nodeInfo: null, - balance: null + balance: null, + bolt12Address: null }; } }; diff --git a/src/routes/(app)/phoenixd/bolt12/qrcode/+server.ts b/src/routes/(app)/phoenixd/bolt12/qrcode/+server.ts index cf19d108d..2702390a9 100644 --- a/src/routes/(app)/phoenixd/bolt12/qrcode/+server.ts +++ b/src/routes/(app)/phoenixd/bolt12/qrcode/+server.ts @@ -2,9 +2,21 @@ import { phoenixdGetBolt12 } from '$lib/server/phoenixd'; import qrcode from 'qrcode'; export async function GET({}) { - const bolt12Address = await phoenixdGetBolt12(); - return new Response(await qrcode.toString('lightning:' + bolt12Address, { type: 'svg' }), { - headers: { 'content-type': 'image/svg+xml' }, - status: 200 - }); + try { + const bolt12Address = await phoenixdGetBolt12(); + + const svgQRCode = await qrcode.toString('lightning:' + bolt12Address, { type: 'svg' }); + + return new Response(svgQRCode, { + headers: { 'content-type': 'image/svg+xml' }, + status: 200 + }); + } catch (error) { + console.error('Error on phoenixdGetBolt12:', error); + + return new Response("Erreur lors de la génération de l'adresse Bolt12", { + headers: { 'content-type': 'text/plain' }, + status: 500 + }); + } } diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts index 6e9e4551b..dc70385ce 100644 --- a/src/routes/+layout.server.ts +++ b/src/routes/+layout.server.ts @@ -21,6 +21,13 @@ export async function load(event) { return `width=${runtimeConfig.viewportContentWidth}`; } })(); + let bolt12Address = ''; + + try { + bolt12Address = await phoenixdGetBolt12(); + } catch (error) { + bolt12Address = 'Bolt12'; + } return { plausibleScriptUrl: runtimeConfig.plausibleScriptUrl, @@ -40,6 +47,6 @@ export async function load(event) { contactModes: runtimeConfig.contactModes, hideFromSearchEngines: runtimeConfig.hideFromSearchEngines, ageRestriction: runtimeConfig.ageRestriction, - bolt12Address: phoenixdGetBolt12() + bolt12Address }; } From ddee47e5f1e0b23cd96a2532716bca3c4c7dceff Mon Sep 17 00:00:00 2001 From: ithiame Date: Mon, 18 Nov 2024 10:07:59 +0000 Subject: [PATCH 3/4] fix phoenixd bolt12 : save value in phoenixd runtimeConfig --- src/lib/server/runtime-config.ts | 3 ++- .../admin[[hash=admin_hash]]/phoenixd/+page.server.ts | 2 +- src/routes/+layout.server.ts | 10 +--------- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/lib/server/runtime-config.ts b/src/lib/server/runtime-config.ts index d6eed6249..26cd4a637 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: '', + bolt12Address: '' }, productActionSettings: { eShop: { 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 83aa91cb4..7651fd2fd 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 @@ -79,7 +79,7 @@ export const actions = { .parse(Object.fromEntries(await event.request.formData())); runtimeConfig.phoenixd.password = parsed.password; - + runtimeConfig.phoenixd.bolt12Address = await phoenixdGetBolt12(); await collections.runtimeConfig.updateOne( { _id: 'phoenixd' }, { diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts index dc70385ce..fa823f43b 100644 --- a/src/routes/+layout.server.ts +++ b/src/routes/+layout.server.ts @@ -1,4 +1,3 @@ -import { phoenixdGetBolt12 } from '$lib/server/phoenixd'; import { runtimeConfig, runtimeConfigUpdatedAt } from '$lib/server/runtime-config'; import { CUSTOMER_ROLE_ID } from '$lib/types/User'; @@ -21,13 +20,6 @@ export async function load(event) { return `width=${runtimeConfig.viewportContentWidth}`; } })(); - let bolt12Address = ''; - - try { - bolt12Address = await phoenixdGetBolt12(); - } catch (error) { - bolt12Address = 'Bolt12'; - } return { plausibleScriptUrl: runtimeConfig.plausibleScriptUrl, @@ -47,6 +39,6 @@ export async function load(event) { contactModes: runtimeConfig.contactModes, hideFromSearchEngines: runtimeConfig.hideFromSearchEngines, ageRestriction: runtimeConfig.ageRestriction, - bolt12Address + bolt12Address: runtimeConfig.phoenixd.bolt12Address }; } From e5a0e6b1f32f1002b40195c3fe84b1f9cbfb0111 Mon Sep 17 00:00:00 2001 From: ithiame Date: Mon, 18 Nov 2024 14:39:23 +0000 Subject: [PATCH 4/4] fix suggested changes --- src/lib/components/CmsDesign.svelte | 10 ++++++++++ src/lib/server/phoenixd.ts | 4 ++++ src/routes/(app)/phoenixd/bolt12/qrcode/+server.ts | 4 ++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/lib/components/CmsDesign.svelte b/src/lib/components/CmsDesign.svelte index 5230df9f9..743e97108 100644 --- a/src/lib/components/CmsDesign.svelte +++ b/src/lib/components/CmsDesign.svelte @@ -261,6 +261,16 @@ /> {:else if token.type === 'pictureWidget'} + {:else if token.type === 'qrCode'} + {#if token.slug === 'Bolt12'} + + QR code + + {/if} {:else if token.type === 'html'}
diff --git a/src/lib/server/phoenixd.ts b/src/lib/server/phoenixd.ts index d3ee5efe2..aa67103d3 100644 --- a/src/lib/server/phoenixd.ts +++ b/src/lib/server/phoenixd.ts @@ -43,6 +43,10 @@ export async function phoenixdGetBolt12(): Promise { } }); + if (!res.ok) { + throw error(500, `Error fetching Bolt12 offer: ${res.status} ${res.statusText}`); + } + return await res.text(); } export async function phoenixdDetected(url?: string): Promise { diff --git a/src/routes/(app)/phoenixd/bolt12/qrcode/+server.ts b/src/routes/(app)/phoenixd/bolt12/qrcode/+server.ts index 2702390a9..ce4e47030 100644 --- a/src/routes/(app)/phoenixd/bolt12/qrcode/+server.ts +++ b/src/routes/(app)/phoenixd/bolt12/qrcode/+server.ts @@ -1,9 +1,9 @@ -import { phoenixdGetBolt12 } from '$lib/server/phoenixd'; +import { runtimeConfig } from '$lib/server/runtime-config'; import qrcode from 'qrcode'; export async function GET({}) { try { - const bolt12Address = await phoenixdGetBolt12(); + const bolt12Address = runtimeConfig.phoenixd.bolt12Address; const svgQRCode = await qrcode.toString('lightning:' + bolt12Address, { type: 'svg' });