NostrLink.encode(prefix: NostrPrefix)

This commit is contained in:
Martti Malmi
2023-10-18 10:01:25 +03:00
parent 700db8f62c
commit 2e663dcb4c
17 changed files with 31 additions and 49 deletions

View File

@ -1,14 +1,14 @@
import { bech32ToHex, hexToBech32, unwrap } from "@snort/shared";
import {
NostrPrefix,
decodeTLV,
TLVEntryType,
encodeTLV,
NostrEvent,
TaggedNostrEvent,
EventExt,
Tag,
EventKind,
NostrEvent,
NostrPrefix,
Tag,
TaggedNostrEvent,
TLVEntryType,
} from ".";
import { findTag } from "./utils";
@ -21,11 +21,11 @@ export class NostrLink {
readonly relays?: Array<string>,
) {}
encode(): string {
if (this.type === NostrPrefix.Note || this.type === NostrPrefix.PrivateKey || this.type === NostrPrefix.PublicKey) {
return hexToBech32(this.type, this.id);
encode(type: NostrPrefix = this.type): string {
if (type === NostrPrefix.Note || type === NostrPrefix.PrivateKey || type === NostrPrefix.PublicKey) {
return hexToBech32(type, this.id);
} else {
return encodeTLV(this.type, this.id, this.relays, this.kind, this.author);
return encodeTLV(type, this.id, this.relays, this.kind, this.author);
}
}
@ -163,14 +163,14 @@ export class NostrLink {
throw new Error(`Unknown tag kind ${tag[0]}`);
}
static fromEvent(ev: TaggedNostrEvent | NostrEvent, prefixHint = NostrPrefix.Event) {
static fromEvent(ev: TaggedNostrEvent | NostrEvent) {
const relays = "relays" in ev ? ev.relays : undefined;
if (ev.kind >= 30_000 && ev.kind < 40_000) {
const dTag = unwrap(findTag(ev, "d"));
return new NostrLink(NostrPrefix.Address, dTag, ev.kind, ev.pubkey, relays);
}
return new NostrLink(prefixHint, ev.id, ev.kind, ev.pubkey, relays);
return new NostrLink(NostrPrefix.Event, ev.id, ev.kind, ev.pubkey, relays);
}
}