From 042726525c1f09b8816959aaec65cc04b3b1f6a6 Mon Sep 17 00:00:00 2001 From: Kieran Date: Tue, 14 Feb 2023 10:34:43 +0000 Subject: [PATCH] bug: fix build --- README.md | 1 + src/Nostr/Links.ts | 77 ++++++++++++++++++---------------- src/Pages/NostrLinkHandler.tsx | 37 ++++++++-------- src/Util.ts | 2 +- 4 files changed, 59 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 7d28d53..9db9827 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Snort supports the following NIP's: - [x] NIP-15: End of Stored Events Notice - [x] NIP-19: bech32-encoded entities - [x] NIP-20: Command Results +- [x] NIP-21: `nostr:` Protocol handler (`web+nostr`) - [x] NIP-25: Reactions - [x] NIP-26: Delegated Event Signing (Display delegated signings only) - [ ] NIP-28: Public Chat diff --git a/src/Nostr/Links.ts b/src/Nostr/Links.ts index 086ddbe..01442d5 100644 --- a/src/Nostr/Links.ts +++ b/src/Nostr/Links.ts @@ -2,55 +2,58 @@ import * as secp from "@noble/secp256k1"; import { bech32 } from "bech32"; export enum NostrPrefix { - PublicKey = "npub", - PrivateKey = "nsec", - Note = "note", + PublicKey = "npub", + PrivateKey = "nsec", + Note = "note", - // TLV prefixes - Profile = "nprofile", - Event = "nevent", - Relay = "nrelay" + // TLV prefixes + Profile = "nprofile", + Event = "nevent", + Relay = "nrelay", } export interface TLVEntry { - type: number, - length: number, - value: string // hex encoded data + type: number; + length: number; + value: string; // hex encoded data } export function encodeTLV(hex: string, prefix: NostrPrefix, relays?: string[]) { - if (typeof hex !== "string" || hex.length === 0 || hex.length % 2 !== 0) { - return ""; - } + if (typeof hex !== "string" || hex.length === 0 || hex.length % 2 !== 0) { + return ""; + } - let enc = new TextEncoder(); - let buf = secp.utils.hexToBytes(hex); + const enc = new TextEncoder(); + const buf = secp.utils.hexToBytes(hex); - let tl0 = [0, buf.length, ...buf]; - let tl1 = relays?.map(a => { - let data = enc.encode(a); + const tl0 = [0, buf.length, ...buf]; + const tl1 = + relays + ?.map(a => { + const data = enc.encode(a); return [1, data.length, ...data]; - }).flat() ?? []; + }) + .flat() ?? []; - return bech32.encode(prefix, bech32.toWords([...tl0, ...tl1])); + return bech32.encode(prefix, bech32.toWords([...tl0, ...tl1])); } export function decodeTLV(str: string) { - let decoded = bech32.decode(str); - let data = bech32.fromWords(decoded.words); + const decoded = bech32.decode(str); + const data = bech32.fromWords(decoded.words); - let entries: TLVEntry[] = []; - let x = 0; - while (x < data.length) { - let t = data[x]; - let l = data[x + 1]; - let v = data.slice(x + 2, x + 2 + l); - entries.push({ - type: t, - length: l, - value: secp.utils.bytesToHex(new Uint8Array(v)) - }); - x += 2 + l; - } - return entries; -} \ No newline at end of file + const entries: TLVEntry[] = []; + let x = 0; + while (x < data.length) { + const t = data[x]; + const l = data[x + 1]; + const v = data.slice(x + 2, x + 2 + l); + entries.push({ + type: t, + length: l, + value: secp.utils.bytesToHex(new Uint8Array(v)), + }); + x += 2 + l; + } + return entries; +} diff --git a/src/Pages/NostrLinkHandler.tsx b/src/Pages/NostrLinkHandler.tsx index e53f0fd..bfd623b 100644 --- a/src/Pages/NostrLinkHandler.tsx +++ b/src/Pages/NostrLinkHandler.tsx @@ -1,26 +1,23 @@ import { NostrPrefix } from "Nostr/Links"; import { useEffect } from "react"; -import { redirect, useNavigate, useParams } from "react-router-dom"; +import { useNavigate, useParams } from "react-router-dom"; export default function NostrLinkHandler() { - const params = useParams(); - const navigate = useNavigate(); - const link = decodeURIComponent(params["*"] ?? ""); + const params = useParams(); + const navigate = useNavigate(); + const link = decodeURIComponent(params["*"] ?? ""); - useEffect(() => { - if (link.length > 0) { - let ls = link.split(":"); - let entity = ls[1]; - if (entity.startsWith(NostrPrefix.PublicKey) || entity.startsWith(NostrPrefix.Profile)) { - navigate(`/p/${entity}`); - } - else if (entity.startsWith(NostrPrefix.Event) || entity.startsWith(NostrPrefix.Note)) { - navigate(`/e/${entity}`) - } - } - }, [link]); + useEffect(() => { + if (link.length > 0) { + const ls = link.split(":"); + const entity = ls[1]; + if (entity.startsWith(NostrPrefix.PublicKey) || entity.startsWith(NostrPrefix.Profile)) { + navigate(`/p/${entity}`); + } else if (entity.startsWith(NostrPrefix.Event) || entity.startsWith(NostrPrefix.Note)) { + navigate(`/e/${entity}`); + } + } + }, [link]); - return ( - <>Could not handle {link} - ) -} \ No newline at end of file + return <>Could not handle {link}; +} diff --git a/src/Util.ts b/src/Util.ts index bec605f..939b192 100644 --- a/src/Util.ts +++ b/src/Util.ts @@ -79,7 +79,7 @@ export function hexToBech32(hrp: string, hex?: string) { try { if (hrp === NostrPrefix.Note || hrp === NostrPrefix.PrivateKey || hrp === NostrPrefix.PublicKey) { - let buf = secp.utils.hexToBytes(hex); + const buf = secp.utils.hexToBytes(hex); return bech32.encode(hrp, bech32.toWords(buf)); } else { return encodeTLV(hex, hrp as NostrPrefix);