refactor: update nip51 support

This commit is contained in:
2023-11-10 13:17:19 +00:00
parent e087df2f63
commit b765cb29b7
19 changed files with 192 additions and 205 deletions

View File

@ -1,4 +1,4 @@
import { bech32ToHex, hexToBech32, isHex, unwrap } from "@snort/shared";
import { bech32ToHex, hexToBech32, isHex, removeUndefined, unwrap } from "@snort/shared";
import {
decodeTLV,
encodeTLV,
@ -12,7 +12,19 @@ import {
} from ".";
import { findTag } from "./utils";
export class NostrLink {
export interface ToNostrEventTag {
toEventTag(): Array<string> | undefined;
}
export class NostrHashtagLink implements ToNostrEventTag {
constructor(readonly tag: string) {}
toEventTag(): string[] | undefined {
return ["t", this.tag];
}
}
export class NostrLink implements ToNostrEventTag {
constructor(
readonly type: NostrPrefix,
readonly id: string,
@ -42,8 +54,8 @@ export class NostrLink {
toEventTag() {
const relayEntry = this.relays ? [this.relays[0]] : [];
if (this.type === NostrPrefix.PublicKey) {
return ["p", this.id];
if (this.type === NostrPrefix.PublicKey || this.type === NostrPrefix.Profile) {
return ["p", this.id, ...relayEntry];
} else if (this.type === NostrPrefix.Note || this.type === NostrPrefix.Event) {
return ["e", this.id, ...relayEntry];
} else if (this.type === NostrPrefix.Address) {
@ -174,6 +186,18 @@ export class NostrLink {
throw new Error(`Unknown tag kind ${tag[0]}`);
}
static fromTags(tags: Array<Array<string>>) {
return removeUndefined(
tags.map(a => {
try {
return NostrLink.fromTag(a);
} catch {
// ignored, cant be mapped
}
}),
);
}
static fromEvent(ev: TaggedNostrEvent | NostrEvent) {
const relays = "relays" in ev ? ev.relays : undefined;