diff --git a/package.json b/package.json index e2dca72c..09670084 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "husky": "^8.0.3", "localforage": "^1.10.0", "localforage-memoryStorageDriver": "^0.9.2", - "nostr-tools": "^1.4.1", + "nostr-tools": "^1.7.4", "npm-run-all": "^4.1.5", "qr-scanner": "^1.4.2", "qrcode": "^1.5.1", diff --git a/src/views/search/Scan.svelte b/src/views/search/Scan.svelte index 663d2ea5..8857aea0 100644 --- a/src/views/search/Scan.svelte +++ b/src/views/search/Scan.svelte @@ -3,18 +3,40 @@ import {onDestroy} from 'svelte' import {navigate} from 'svelte-routing' import {waitFor} from 'hurdak/lib/hurdak' + import {any, flip, startsWith} from 'ramda' + import {nip05, nip19} from 'nostr-tools' import Input from 'src/partials/Input.svelte' import Anchor from 'src/partials/Anchor.svelte' import Spinner from 'src/partials/Spinner.svelte' + import {toast} from "src/app/ui" let mode = 'input', video, ready, value, scanner const onDecode = result => { - goToEntity(result.data) + handleInput(result.data) } - const goToEntity = entity => { - navigate("/" + entity.replace('nostr:', '')) + const handleInput = async input => { + input = input.replace('nostr:', '') + + if (any(flip(startsWith)(input), ["note1", "npub1", "nevent1", "nprofile1"])) { + navigate("/" + input) + return + } + + if (input.match(/^[a-f0-9]{64}$/)) { + navigate("/" + nip19.npubEncode(input)) + return + } + + let profile = await nip05.queryProfile(input) + if (profile) { + navigate("/" + nip19.nprofileEncode(profile)) + return + } + + toast.show("warning", "That isn't a valid nostr identifier") + return } const setMode = async newMode => { @@ -46,17 +68,17 @@ {#if mode === 'input'} -
+
handleInput(value)}> - goToEntity(value)}> + handleInput(value)}> setMode('scan')}> -
+
- Enter any nostr identifier (npub, nevent, nprofile or note), or click on the + Enter any nostr identifier (npub, nevent, nprofile, note or user@domain.tld), or click on the camera icon to scan with your device's camera instead.
{:else}