fix: handle invalid client tag

This commit is contained in:
2025-04-30 12:22:18 +01:00
parent 992d7f19be
commit 3d778f7ec7
2 changed files with 15 additions and 1 deletions

View File

@ -5,7 +5,7 @@ import { Link } from "react-router-dom";
export function ClientTag({ ev }: { ev: TaggedNostrEvent }) { export function ClientTag({ ev }: { ev: TaggedNostrEvent }) {
const tag = ev.tags.find(a => a[0] === "client"); const tag = ev.tags.find(a => a[0] === "client");
if (!tag) return; if (!tag) return;
const link = tag[2] && tag[2].includes(":") ? NostrLink.fromTag(["a", tag[2]]) : undefined; const link = tag[2] && tag[2].includes(":") ? NostrLink.tryFromTag(["a", tag[2]]) : undefined;
return ( return (
<span className="text-xs text-gray-light"> <span className="text-xs text-gray-light">
{" "} {" "}

View File

@ -235,16 +235,30 @@ export class NostrLink implements ToNostrEventTag {
} }
case "A": { case "A": {
const [kind, author, dTag] = tag[1].split(":"); const [kind, author, dTag] = tag[1].split(":");
if (!isHex(author)) {
throw new Error(`Invalid author in A tag: ${tag[1]}`);
}
return new NostrLink(NostrPrefix.Address, dTag, Number(kind), author, relays, "root"); return new NostrLink(NostrPrefix.Address, dTag, Number(kind), author, relays, "root");
} }
case "a": { case "a": {
const [kind, author, dTag] = tag[1].split(":"); const [kind, author, dTag] = tag[1].split(":");
if (!isHex(author)) {
throw new Error(`Invalid author in a tag: ${tag[1]}`);
}
return new NostrLink(NostrPrefix.Address, dTag, Number(kind), author, relays, tag[3]); return new NostrLink(NostrPrefix.Address, dTag, Number(kind), author, relays, tag[3]);
} }
} }
throw new Error("Unknown tag!"); throw new Error("Unknown tag!");
} }
static tryFromTag(tag: Array<string>, author?: string, kind?: number) {
try {
return NostrLink.fromTag(tag, author, kind);
} catch (e) {
// ignored
}
}
static fromTags(tags: ReadonlyArray<Array<string>>) { static fromTags(tags: ReadonlyArray<Array<string>>) {
return removeUndefined( return removeUndefined(
tags.map(a => { tags.map(a => {