Naddr parsing (#178)

Co-authored-by: BlowaterNostr <127284497+BlowaterNostr@users.noreply.github.com>
This commit is contained in:
Sherry 2023-09-17 06:12:41 +08:00 committed by GitHub
parent 01609b9916
commit fd8f6800a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 2 deletions

View File

@ -1,6 +1,8 @@
import { assertEquals } from "https://deno.land/std@0.176.0/testing/asserts.ts";
import { ChatMessage, groupContinuousMessages, parseContent } from "./message.ts";
import { PrivateKey, PublicKey } from "../lib/nostr-ts/key.ts";
import { NostrAddress } from "../lib/nostr-ts/nip19.ts";
import { NostrKind } from "../lib/nostr-ts/nostr.ts";
Deno.test("inline parse", async (t) => {
const data = [
@ -169,6 +171,23 @@ Deno.test("inline parse", async (t) => {
],
}],
},
{
input:
`naddr1qqxnzd3exsmnjvphxqunqv33qgsp7hwmlh5zccs55shzpfued50pznvypj0wwzn00dtyjzlqkr04w4grqsqqqa28vct2px`,
output: [{
type: "naddr",
start: 0,
end: 99,
addr: new NostrAddress({
pubkey: PublicKey.FromHex(
"1f5ddbfde82c6214a42e20a7996d1e114d840c9ee70a6f7b56490be0b0df5755",
) as PublicKey,
identifier: "1694790709021",
kind: NostrKind.Long_Form,
relays: [],
}),
}],
},
// {
// input: `nostr:nevent1qqsz25j8nrppstgmyry8hgsg4fggtfa6xnym2n4c2xth7usxtydtgpcpp4mhxue69uhhjctzw5hx6egzyze7g05vclndlu36x0vjzw37jykcjkcu8ep9qfqwpjvahmlrq6947qcyqqqqqqgj5mjek`,
// output: [{

View File

@ -1,7 +1,9 @@
import { PublicKey } from "../lib/nostr-ts/key.ts";
import { MessageThread } from "./dm.tsx";
import { DirectedMessage_Event, Text_Note_Event } from "../nostr.ts";
import { NostrProfile, NoteID } from "../lib/nostr-ts/nip19.ts";
import { NostrAddress, NostrProfile, NoteID } from "../lib/nostr-ts/nip19.ts";
import { Int } from "https://deno.land/x/automerge@2.1.0-alpha.12/types.ts";
import { NostrKind } from "../lib/nostr-ts/nostr.ts";
export function* parseContent(content: string) {
// URLs
@ -13,6 +15,9 @@ export function* parseContent(content: string) {
//nprofile
yield* match(/(nostr:)?nprofile[0-9a-z]+/g, content, "nprofile");
//naddr
yield* match(/(nostr:)?naddr[0-9a-z]+/g, content, "naddr");
// notes
yield* match(/note[0-9a-z]{59}/g, content, "note");
@ -82,6 +87,24 @@ function* match(regex: RegExp, content: string, type: ItemType): Generator<Conte
relays: decoded_nProfile.relays,
};
}
} else if (type == "naddr") {
let bech32: string;
if (match[0].startsWith("nostr:")) {
bech32 = content.slice(urlStartPosition + 6, urlEndPosition + 1);
} else {
bech32 = content.slice(urlStartPosition, urlEndPosition + 1);
}
const decoded_nAddr = NostrAddress.decode(bech32);
if (decoded_nAddr instanceof Error) {
// ignore
} else {
yield {
type: "naddr",
start: urlStartPosition,
end: urlEndPosition,
addr: decoded_nAddr,
};
}
} else {
yield {
type: type,
@ -93,7 +116,7 @@ function* match(regex: RegExp, content: string, type: ItemType): Generator<Conte
}
type otherItemType = "url" | "tag";
type ItemType = otherItemType | "note" | "npub" | "nprofile";
type ItemType = otherItemType | "note" | "npub" | "nprofile" | "naddr";
export type ContentItem = {
type: otherItemType;
start: number;
@ -109,6 +132,11 @@ export type ContentItem = {
noteID: NoteID;
start: number;
end: number;
} | {
type: "naddr";
start: number;
end: number;
addr: NostrAddress;
};
// Think of ChatMessage as an materialized view of NostrEvent