Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bolt 12 address phoenixD #1578

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/lib/components/CmsDesign.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@
: ''}{token.height ? `height: ${token.height}px;` : ''}"
/>
<PictureComponent picture={pictureById[token.slug]} class="my-5 lg:hidden block" />
{:else if token.type === 'qrCode'}
{#if token.slug === 'Bolt12'}
<a href="lightning:{$page.data.bolt12Address}">
<img src="{$page.url.origin}/phoenixd/bolt12/qrcode" class="w-96 h-96" alt="QR code" />
</a>
{/if}
{:else if token.type === 'currencyCalculatorWidget'}
<CurrencyCalculator />
{:else if token.type === 'html'}
Expand Down Expand Up @@ -258,6 +264,16 @@
/>
{:else if token.type === 'pictureWidget'}
<PictureComponent picture={pictureById[token.slug]} class="my-5" />
{:else if token.type === 'qrCode'}
{#if token.slug === 'Bolt12'}
<a href="lightning:{$page.data.bolt12Address}">
<img
src="{$page.url.origin}/phoenixd/bolt12/qrcode"
class="w-96 h-96"
alt="QR code"
/>
</a>
{/if}
{:else if token.type === 'currencyCalculatorWidget'}
<CurrencyCalculator />
{:else if token.type === 'html'}
Expand Down
12 changes: 12 additions & 0 deletions src/lib/server/cms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type TokenObject =
display: string | undefined;
raw: string;
}
| { type: 'qrCode'; slug: string; raw: string }
| { type: 'currencyCalculatorWidget'; slug: string; raw: string };

export async function cmsFromContent(
Expand All @@ -100,6 +101,7 @@ export async function cmsFromContent(
/\[TagProducts=(?<slug>[\p{L}\d_-]+)(?:[?\s]display=(?<display>[a-z0-9-]+))?\]/giu;
const GALLERY_WIDGET_REGEX =
/\[Gallery=(?<slug>[\p{L}\d_-]+)(?:[?\s]display=(?<display>[a-z0-9-]+))?\]/giu;
const QRCODE_REGEX = /\[QRCode=(?<slug>[\p{L}\d_-]+)\]/giu;
const CURRENCY_CALCULATOR_WIDGET_REGEX = /\[CurrencyCalculator=(?<slug>[a-z0-9-]+)\]/giu;

const productSlugs = new Set<string>();
Expand All @@ -112,6 +114,7 @@ export async function cmsFromContent(
const countdownFormSlugs = new Set<string>();
const tagProductsSlugs = new Set<string>();
const gallerySlugs = new Set<string>();
const qrCodeSlugs = new Set<string>();
const currencyCalculatorSlugs = new Set<string>();

const tokens: {
Expand Down Expand Up @@ -143,6 +146,7 @@ export async function cmsFromContent(
...matchAndSort(content, COUNTDOWN_WIDGET_REGEX, 'countdownWidget'),
...matchAndSort(content, TAG_PRODUCTS_REGEX, 'tagProducts'),
...matchAndSort(content, GALLERY_WIDGET_REGEX, 'galleryWidget'),
...matchAndSort(content, QRCODE_REGEX, 'qrCode'),
...matchAndSort(content, CURRENCY_CALCULATOR_WIDGET_REGEX, 'currencyCalculatorWidget')
].sort((a, b) => (a.index ?? 0) - (b.index ?? 0));
for (const match of matches) {
Expand Down Expand Up @@ -250,6 +254,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;
case 'currencyCalculatorWidget':
currencyCalculatorSlugs.add(match.groups.slug);
token.push({
Expand Down
15 changes: 15 additions & 0 deletions src/lib/server/phoenixd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ export async function phoenixdBalance(): Promise<{ balanceSat: number; feeCredit
return await res.json();
}

export async function phoenixdGetBolt12(): Promise<string> {
const res = await fetch(`${runtimeConfig.phoenixd.url}/getoffer`, {
headers: {
Authorization: `Basic ${Buffer.from(`:${runtimeConfig.phoenixd.password}`).toString(
'base64'
)}`
}
});

if (!res.ok) {
throw error(500, `Error fetching Bolt12 offer: ${res.status} ${await res.text()}`);
}

return await res.text();
ithiame marked this conversation as resolved.
Show resolved Hide resolved
}
export async function phoenixdDetected(url?: string): Promise<boolean> {
return await Promise.race<boolean>([
fetch(`${url || runtimeConfig.phoenixd.url}/getinfo`).then(
Expand Down
3 changes: 2 additions & 1 deletion src/lib/server/runtime-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ const baseConfig = {
phoenixd: {
url: 'http://localhost:9740',
enabled: false,
password: ''
password: '',
bolt12Address: ''
},
productActionSettings: {
eShop: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isPhoenixdConfigured,
phoenixdBalance,
phoenixdDetected,
phoenixdGetBolt12,
phoenixdInfo,
phoenixdPayInvoice,
phoenixdSendOnChain
Expand All @@ -29,17 +30,19 @@ 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 {
phoenixd: runtimeConfig.phoenixd,
nodeInfo: null,
balance: null
balance: null,
bolt12Address: null
};
}
};
Expand Down Expand Up @@ -76,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' },
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
let withdrawMode = 'bolt11' as 'bolt11' | 'bitcoin';

let defaultUrl = data.phoenixd.url || 'http://localhost:9740';
let showBolt12 = false;
</script>

<h1 class="text-3xl">PhoenixD</h1>
Expand Down Expand Up @@ -92,13 +93,20 @@
<div class="flex gap-2">
<button class="btn btn-black" type="submit">Save</button>
<button class="btn btn-red" type="submit" form="disableForm">Reset</button>
<button class="btn btn-gray" type="button" on:click={() => (showBolt12 = !showBolt12)}
>Get bolt12 address</button
>

{#if data.nodeInfo}
<button class="btn btn-blue ml-auto" type="button" on:click={() => showDialog()}
>Withdraw</button
>
{/if}
</div>
{#if showBolt12}
<p class="break-words">Bolt12 address: {data.bolt12Address}</p>
<p>To use it on page CMS, use this code : <code>[QRCode=Bolt12]</code></p>
{/if}
</form>
<form method="POST" action="?/disable" id="disableForm"></form>
<dialog bind:this={withdrawDialog} class="max-w-full w-[500px] rounded">
Expand Down
22 changes: 22 additions & 0 deletions src/routes/(app)/phoenixd/bolt12/qrcode/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { runtimeConfig } from '$lib/server/runtime-config';
import qrcode from 'qrcode';

export async function GET({}) {
try {
const bolt12Address = runtimeConfig.phoenixd.bolt12Address;

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
});
}
}
3 changes: 2 additions & 1 deletion src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function load(event) {
viewportWidth,
contactModes: runtimeConfig.contactModes,
hideFromSearchEngines: runtimeConfig.hideFromSearchEngines,
ageRestriction: runtimeConfig.ageRestriction
ageRestriction: runtimeConfig.ageRestriction,
bolt12Address: runtimeConfig.phoenixd.bolt12Address
};
}
Loading