From 2ed559aa845e3ab5d97e08bd00bf21a67494c21d Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Thu, 11 Jul 2024 17:34:12 +0200 Subject: [PATCH] Add nostr settings --- package.json | 2 + src/lib/index.ts | 45 +++++++++++++++++++- src/lib/locales/en.json | 4 +- src/routes/Settings.svelte | 43 +++++++++++++++++++ yarn.lock | 85 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 177 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 5d332e6..f3af94e 100644 --- a/package.json +++ b/package.json @@ -48,8 +48,10 @@ "@fontsource/antonio": "^5.0.17", "@fontsource/oswald": "^5.0.17", "@fontsource/ubuntu": "^5.0.8", + "@noble/secp256k1": "^2.1.0", "@playwright/test": "^1.40.0", "bootstrap": "^5.3.2", + "nostr-tools": "^2.7.1", "patch-package": "^8.0.0", "svelte-i18n": "^4.0.0", "sveltestrap": "^5.11.2", diff --git a/src/lib/index.ts b/src/lib/index.ts index 856f2b6..692bf62 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -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 indicating if the URL is a valid Nostr relay. + */ +const isValidNostrRelay = async (url: string): Promise => { + 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 }; diff --git a/src/lib/locales/en.json b/src/lib/locales/en.json index d04c856..76a87a5 100644 --- a/src/lib/locales/en.json +++ b/src/lib/locales/en.json @@ -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", diff --git a/src/routes/Settings.svelte b/src/routes/Settings.svelte index 9ec5da1..aebb309 100644 --- a/src/routes/Settings.svelte +++ b/src/routes/Settings.svelte @@ -1,4 +1,5 @@