From ee4d6d88c76fa279e643faabf4216c88145e0b2c Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Thu, 11 Jul 2024 21:48:50 +0200 Subject: [PATCH] Add more Nostr settings --- src/lib/index.ts | 39 +++++++++++++++++++++++++++++++++++++- src/lib/locales/de.json | 3 ++- src/lib/locales/en.json | 3 ++- src/lib/locales/es.json | 3 ++- src/lib/locales/nl.json | 3 ++- src/routes/Settings.svelte | 22 ++++++++++++++++++--- src/routes/Status.svelte | 4 +++- 7 files changed, 68 insertions(+), 9 deletions(-) diff --git a/src/lib/index.ts b/src/lib/index.ts index 692bf62..e16e986 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -41,4 +41,41 @@ const isValidNostrRelay = async (url: string): Promise => { } }; -export { isValidNpub, isValidNostrRelay }; +/** + * Validates if the given parameter is a valid hex public key. + * @param pubkey - The public key to validate. + * @returns A boolean indicating if the public key is valid. + */ +const isValidHexPubKey = (pubkey: string): boolean => { + return /^[0-9a-f]{64}$/i.test(pubkey); +}; + +/** + * Checks if a parameter is a valid pubkey or npub and converts npub to pubkey. + * @param input - The input string to check and convert. + * @returns The pubkey if valid, otherwise null. + */ + +const getPubKey = (input: string): string | null => { + try { + // If input is a valid hex public key + if (isValidHexPubKey(input)) { + return input; + } + + // Try to decode the input as npub + const { type, data } = nip19.decode(input); + + // Check if the decoded type is 'npub' and the data length is 64 characters (32 bytes in hex) + if (type === 'npub' && data.length === 64) { + return data; + } + + return null; + } catch (e) { + // If any error is thrown, the input is not valid + return null; + } +}; + +export { isValidNpub, isValidNostrRelay, isValidHexPubKey, getPubKey }; diff --git a/src/lib/locales/de.json b/src/lib/locales/de.json index 591c552..b1c17e8 100644 --- a/src/lib/locales/de.json +++ b/src/lib/locales/de.json @@ -36,7 +36,8 @@ "flFlashOnUpd": "Displaybeleuchting bei neuem Block", "mempoolInstanceHelpText": "Nur wirksam, wenn die BTClock-Datenquelle deaktiviert ist. \nZur Anwendung ist ein Neustart erforderlich.", "luxLightToggle": "Automatisches Umschalten des Frontlichts bei Lux", - "wpTimeout": "WiFi-Konfigurationsportal timeout" + "wpTimeout": "WiFi-Konfigurationsportal timeout", + "useNostr": "Nostr-Datenquelle verwenden" }, "control": { "systemInfo": "Systeminfo", diff --git a/src/lib/locales/en.json b/src/lib/locales/en.json index 76a87a5..f2e4e64 100644 --- a/src/lib/locales/en.json +++ b/src/lib/locales/en.json @@ -38,7 +38,8 @@ "luxLightToggle": "Auto toggle frontlight at lux", "wpTimeout": "WiFi-config portal timeout", "nostrPubKey": "Nostr source pubkey", - "nostrRelay": "Nostr Relay" + "nostrRelay": "Nostr Relay", + "useNostr": "Use Nostr datasource" }, "control": { "systemInfo": "System info", diff --git a/src/lib/locales/es.json b/src/lib/locales/es.json index 6976f77..68c2118 100644 --- a/src/lib/locales/es.json +++ b/src/lib/locales/es.json @@ -35,7 +35,8 @@ "flFlashOnUpd": "Luz de la pantalla parpadea con un nuevo bloque", "mempoolInstanceHelpText": "Solo es efectivo cuando la fuente de datos BTClock está deshabilitada. \nEs necesario reiniciar para aplicar.", "luxLightToggle": "Cambio automático de luz frontal en lux", - "wpTimeout": "Portal de configuración WiFi timeout" + "wpTimeout": "Portal de configuración WiFi timeout", + "useNostr": "Utilice la fuente de datos Nostr" }, "control": { "turnOff": "Apagar", diff --git a/src/lib/locales/nl.json b/src/lib/locales/nl.json index 6fbd945..847faec 100644 --- a/src/lib/locales/nl.json +++ b/src/lib/locales/nl.json @@ -36,7 +36,8 @@ "flFlashOnUpd": "Knipper displaylicht bij nieuw blok", "mempoolInstanceHelpText": "Alleen effectief als de BTClock-gegevensbron is uitgeschakeld. \nOm toe te passen is een herstart nodig.", "luxLightToggle": "Schakelen displaylicht op lux", - "wpTimeout": "WiFi-config-portal timeout" + "wpTimeout": "WiFi-config-portal timeout", + "useNostr": "Gebruik Nostr-gegevensbron" }, "control": { "systemInfo": "Systeeminformatie", diff --git a/src/routes/Settings.svelte b/src/routes/Settings.svelte index aebb309..32bfd2c 100644 --- a/src/routes/Settings.svelte +++ b/src/routes/Settings.svelte @@ -1,5 +1,5 @@