Add nostr settings

This commit is contained in:
Djuri 2024-07-11 17:34:12 +02:00
parent 2363d98965
commit 2ed559aa84
5 changed files with 177 additions and 2 deletions

View file

@ -1 +1,44 @@
// place files you want to import through the `$lib` alias in this folder.
import * as nip19 from 'nostr-tools/nip19';
import { Relay } from 'nostr-tools';
/**
* Validates if the given npub is a valid Nostr Public Key.
* @param npub - The npub (Nostr Public Key) to validate.
* @returns A boolean indicating if the npub is valid.
*/
const isValidNpub = (npub: string): boolean => {
try {
// Decode the npub using NIP-19
const { type, data } = nip19.decode(npub);
// Check if the type is 'npub' and the data length is 32 bytes
return type === 'npub' && data.length === 64;
} catch (e) {
// If any error is thrown, the npub is not valid
return false;
}
};
/**
* Validates if the given URL is a valid Nostr relay.
* @param url - The URL of the Nostr relay to validate.
* @returns A Promise<boolean> indicating if the URL is a valid Nostr relay.
*/
const isValidNostrRelay = async (url: string): Promise<boolean> => {
try {
const relay: Relay = await Relay.connect(url);
// If the relay is successfully connected, it's a valid Nostr relay
if (relay.connected) {
// Close the connection to clean up
relay.close();
return true;
}
return false;
} catch (e) {
// If any error is thrown, the URL is not a valid Nostr relay
return false;
}
};
export { isValidNpub, isValidNostrRelay };

View file

@ -36,7 +36,9 @@
"flFlashOnUpd": "Frontlight flash on new block",
"mempoolInstanceHelpText": "Only effective when BTClock data-source is disabled. A restart is required to apply.",
"luxLightToggle": "Auto toggle frontlight at lux",
"wpTimeout": "WiFi-config portal timeout"
"wpTimeout": "WiFi-config portal timeout",
"nostrPubKey": "Nostr source pubkey",
"nostrRelay": "Nostr Relay"
},
"control": {
"systemInfo": "System info",

View file

@ -1,4 +1,5 @@
<script lang="ts">
import { isValidNpub, isValidNostrRelay } from '$lib';
import { PUBLIC_BASE_URL } from '$lib/config';
import { uiSettings } from '$lib/uiSettings';
import { createEventDispatcher } from 'svelte';
@ -78,6 +79,11 @@
});
};
let validNostrRelay = false;
const testNostrRelay = async () => {
validNostrRelay = await isValidNostrRelay($settings.nostrRelay);
};
const onFlBrightnessChange = async () => {
await fetch(`${PUBLIC_BASE_URL}/api/frontlight/brightness/${$settings.flMaxBrightness}`, {
method: 'GET',
@ -273,6 +279,43 @@
</Col>
</Row>
{/if}
{#if $settings.nostrPubKey}
<Row>
<Label md={6} for="nostrPubKey" size={$uiSettings.inputSize}
>{$_('section.settings.nostrPubKey')}</Label
>
<Col md="6">
<Input
type="text"
bind:value={$settings.nostrPubKey}
name="nostrPubKey"
id="nostrPubKey"
invalid={!isValidNpub($settings.nostrPubKey)}
bsSize={$uiSettings.inputSize}
></Input>
</Col>
</Row>
<Row>
<Label md={6} for="nostrRelay" size={$uiSettings.inputSize}
>{$_('section.settings.nostrRelay')}</Label
>
<Col md="6">
<InputGroup size={$uiSettings.inputSize}>
<Input
type="text"
bind:value={$settings.nostrRelay}
name="nostrRelay"
id="nostrRelay"
valid={validNostrRelay}
bsSize={$uiSettings.inputSize}
></Input>
<Button type="button" color="success" on:click={testNostrRelay}
>{$_('test', { default: 'Test' })}</Button
>
</InputGroup>
</Col>
</Row>
{/if}
<Row>
<Label md={6} for="mempoolInstance" size="sm"
>{$_('section.settings.mempoolnstance')}</Label