Support nostr: links in notes

This commit is contained in:
hzrd149 2023-04-10 19:43:17 -05:00
parent d4321c03fb
commit 52033fc7ff
4 changed files with 42 additions and 3 deletions
.changeset
src

@ -0,0 +1,5 @@
---
"nostrudel": minor
---
Support nostr: links in notes

@ -3,12 +3,13 @@ import { AspectRatio, Box, Button, ButtonGroup, Image, ImageProps, Link, useDisc
import { InlineInvoiceCard } from "../inline-invoice-card";
import { TweetEmbed } from "../tweet-embed";
import { UserLink } from "../user-link";
import { normalizeToHex } from "../../helpers/nip19";
import { DraftNostrEvent, NostrEvent } from "../../types/nostr-event";
import settings from "../../services/settings";
import styled from "@emotion/styled";
import QuoteNote from "./quote-note";
import { useExpand } from "./expanded";
import { nip19 } from "nostr-tools";
import { EventPointer, ProfilePointer } from "nostr-tools/lib/nip19";
const BlurredImage = (props: ImageProps) => {
const { isOpen, onToggle } = useDisclosure();
@ -191,6 +192,38 @@ const embeds: EmbedType[] = [
),
isMedia: false,
},
// nostr: links
// nostr:nevent1qqsthg2qlxp9l7egtwa92t8lusm7pjknmjwa75ctrrpcjyulr9754fqpz3mhxue69uhhyetvv9ujuerpd46hxtnfduq36amnwvaz7tmwdaehgu3dwp6kytnhv4kxcmmjv3jhytnwv46q2qg5q9
// nostr:nevent1qqsq3wc73lqxd70lg43m5rul57d4mhcanttjat56e30yx5zla48qzlspz9mhxue69uhkummnw3e82efwvdhk6qgdwaehxw309ahx7uewd3hkcq5hsum
{
regexp: /(nostr:)?((npub|note|nprofile|nevent)1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58,})/i,
render: (match, event) => {
try {
const decoded = nip19.decode(match[2]);
console.log(decoded);
switch (decoded.type) {
case "npub":
return <UserLink color="blue.500" pubkey={decoded.data as string} showAt />;
case "nprofile": {
const pointer = decoded.data as ProfilePointer;
return <UserLink color="blue.500" pubkey={pointer.pubkey} showAt />;
}
case "note":
return <QuoteNote noteId={decoded.data as string} />;
case "nevent": {
const pointer = decoded.data as EventPointer;
return <QuoteNote noteId={pointer.id} relay={pointer.relays?.[0]} />;
}
default:
return match[0];
}
} catch (e) {
return match[0];
}
},
isMedia: false,
},
// Nostr Mention Links
{
regexp: /#\[(\d+)\]/,

@ -6,7 +6,7 @@ export function encodeText(prefix: string, text: string) {
}
export function decodeText(encoded: string) {
const decoded = bech32.decode(encoded, 256);
const decoded = bech32.decode(encoded, Infinity);
const text = new TextDecoder().decode(new Uint8Array(bech32.fromWords(decoded.words)));
return {
text,

@ -9,12 +9,13 @@ export enum Bech32Prefix {
Pubkey = "npub",
SecKey = "nsec",
Note = "note",
Profile = "nprofile",
}
export function isBech32Key(bech32String: string) {
try {
const { prefix } = bech32.decode(bech32String.toLowerCase());
if (!["npub", "nsec", "note"].includes(prefix)) return false;
if (!prefix) return false;
if (!isHex(bech32ToHex(bech32String))) return false;
} catch (error) {
return false;