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

@ -24,11 +24,17 @@ enum EventKind {
Relays = 10002, // NIP-65
Ephemeral = 20_000,
Auth = 22242, // NIP-42
PubkeyLists = 30000, // NIP-51a
NoteLists = 30001, // NIP-51b
MuteList = 10_000, // NIP-51
PinList = 10_001, // NIP-51
CategorizedPeople = 30000, // NIP-51a
CategorizedBookmarks = 30001, // NIP-51b
TagLists = 30002, // NIP-51c
Badge = 30009, // NIP-58
ProfileBadges = 30008, // NIP-58
LongFormTextNote = 30023, // NIP-23
AppData = 30_078, // NIP-78
LiveEvent = 30311, // NIP-102
@ -37,7 +43,7 @@ enum EventKind {
SimpleChatMetadata = 39_000, // NIP-29
ZapRequest = 9734, // NIP 57
ZapReceipt = 9735, // NIP 57
HttpAuthentication = 27235, // NIP XX - HTTP Authentication
HttpAuthentication = 27235, // NIP 98 - HTTP Authentication
}
export default EventKind;

View File

@ -1,6 +1,6 @@
import * as secp from "@noble/curves/secp256k1";
import * as utils from "@noble/curves/abstract/utils";
import { unwrap, getPublicKey, unixNow } from "@snort/shared";
import { unwrap } from "@snort/shared";
import {
decodeEncryptionPayload,
@ -8,7 +8,6 @@ import {
EventSigner,
FullRelaySettings,
HexKey,
Lists,
MessageEncryptorVersion,
NostrEvent,
NostrLink,
@ -18,6 +17,7 @@ import {
RelaySettings,
SignerSupports,
TaggedNostrEvent,
ToNostrEventTag,
u256,
UserMetadata,
} from ".";
@ -104,11 +104,14 @@ export class EventPublisher {
return await this.#sign(eb);
}
async muted(keys: HexKey[], priv: HexKey[]) {
const eb = this.#eb(EventKind.PubkeyLists);
eb.tag(["d", Lists.Muted]);
keys.forEach(p => {
/**
* Build a mute list event using lists of pubkeys
* @param pub Public mute list
* @param priv Private mute list
*/
async muted(pub: Array<string>, priv: Array<string>) {
const eb = this.#eb(EventKind.MuteList);
pub.forEach(p => {
eb.tag(["p", p]);
});
if (priv.length > 0) {
@ -119,20 +122,26 @@ export class EventPublisher {
return await this.#sign(eb);
}
async noteList(notes: u256[], list: Lists) {
const eb = this.#eb(EventKind.NoteLists);
eb.tag(["d", list]);
/**
* Build a pin list event using lists of event links
*/
async pinned(notes: Array<ToNostrEventTag>) {
const eb = this.#eb(EventKind.PinList);
notes.forEach(n => {
eb.tag(["e", n]);
eb.tag(unwrap(n.toEventTag()));
});
return await this.#sign(eb);
}
async tags(tags: string[]) {
const eb = this.#eb(EventKind.TagLists);
eb.tag(["d", Lists.Followed]);
tags.forEach(t => {
eb.tag(["t", t]);
/**
* Build a categorized bookmarks event with a given label
* @param notes List of bookmarked links
*/
async bookmarks(notes: Array<ToNostrEventTag>, list: "bookmark" | "follow") {
const eb = this.#eb(EventKind.CategorizedBookmarks);
eb.tag(["d", list]);
notes.forEach(n => {
eb.tag(unwrap(n.toEventTag()));
});
return await this.#sign(eb);
}
@ -263,6 +272,7 @@ export class EventPublisher {
eb.tag(["e", id]);
return await this.#sign(eb);
}
/**
* Repost a note (NIP-18)
*/

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;

View File

@ -70,17 +70,6 @@ export type UserMetadata = {
lud16?: string;
};
/**
* NIP-51 list types
*/
export enum Lists {
Muted = "mute",
Pinned = "pin",
Bookmarked = "bookmark",
Followed = "follow",
Badges = "profile_badges",
}
export interface FullRelaySettings {
url: string;
settings: RelaySettings;