web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

commit 8ad05324931f656e17c487f9da64439ac93eaa99
parent 69294840a2967aa2ea1637e7410999a5ad3ccc64
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Fri,  4 Apr 2025 04:47:02 +0000

utils-nostr: add nostr event service methods

Diffstat:
Mutils-nostr/package.json | 2+-
Mutils-nostr/src/services/events/lib.ts | 40++++++++++++++++++++++++++++++++++------
Mutils-nostr/src/services/events/types.ts | 6++++--
3 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/utils-nostr/package.json b/utils-nostr/package.json @@ -29,7 +29,7 @@ "@noble/curves": "^1.6.0", "@noble/hashes": "^1.4.0", "@nostr-dev-kit/ndk": "^2.11.0", - "nostr-geotags": "^0.7.1", + "nostr-geotags": "workspace:*", "nostr-tools": "^2.10.4", "uuid": "^10.0.0" } diff --git a/utils-nostr/src/services/events/lib.ts b/utils-nostr/src/services/events/lib.ts @@ -1,6 +1,6 @@ -import { INostrEventEventSign, INostrEventService, INostrEventServiceFormatTagsBasisNip99, INostrEventServiceNeventEncode, lib_nostr_event_sign, lib_nostr_event_sign_attest, lib_nostr_event_verify, lib_nostr_event_verify_serialized, lib_nostr_nevent_encode, NostrEventTagClient, NostrEventTagLocation, NostrEventTagMediaUpload, NostrEventTagPrice, NostrEventTagQuantity } from "$root"; -import { type NDKEvent } from "@nostr-dev-kit/ndk"; -import ngeotags, { type GeoTags as NostrGeotagsGeotags, type InputData as NostrGeotagsInputData } from "nostr-geotags"; +import { INostrEventEventSign, INostrEventService, INostrEventServiceFormatTagsBasisNip99, INostrEventServiceNeventEncode, lib_nostr_event_sign, lib_nostr_event_sign_attest, lib_nostr_event_verify, lib_nostr_event_verify_serialized, lib_nostr_nevent_encode, ndk_event, NostrEventTagClient, NostrEventTagLocation, NostrEventTagMediaUpload, NostrEventTagPrice, NostrEventTagQuantity, NostrMetadata } from "$root"; +import NDK, { NDKKind, type NDKEvent } from "@nostr-dev-kit/ndk"; +import { ngeotags, type GeoTags as NostrGeotagsGeotags, type InputData as NostrGeotagsInputData } from "nostr-geotags"; import { type NostrEvent as NostrToolsEvent } from "nostr-tools"; export class NostrEventService implements INostrEventService { @@ -14,7 +14,6 @@ export class NostrEventService implements INostrEventService { return tag; }; - private fmt_tag_quantity = (opts: NostrEventTagQuantity): string[] => { const tag = [`quantity`, opts.amt, opts.unit]; if (opts.label) tag.push(opts.label); @@ -93,4 +92,34 @@ export class NostrEventService implements INostrEventService { public nevent_encode = (opts: INostrEventServiceNeventEncode): string => { return lib_nostr_nevent_encode(opts); }; -} -\ No newline at end of file + + public metadata = async ($ndk: NDK, opts: NostrMetadata): Promise<NDKEvent | undefined> => { + const $ndk_user = await $ndk.signer?.user(); + if (!$ndk_user) return undefined; + const ev = await ndk_event({ + $ndk, + $ndk_user, + basis: { + kind: NDKKind.Metadata, + content: JSON.stringify(opts), + }, + }); + return ev; + } + + public classified = async ($ndk: NDK, opts: INostrEventServiceFormatTagsBasisNip99): Promise<NDKEvent | undefined> => { + const $ndk_user = await $ndk.signer?.user(); + if (!$ndk_user) return undefined; + const ev = await ndk_event({ + $ndk, + $ndk_user, + basis: { + kind: NDKKind.Classified, + content: ``, + tags: this.fmt_tags_basis_nip99(opts), + }, + }); + return ev; + } +} + diff --git a/utils-nostr/src/services/events/types.ts b/utils-nostr/src/services/events/types.ts @@ -1,5 +1,5 @@ -import type { INostrEventEventSign } from "$root"; -import { NDKEvent } from "@nostr-dev-kit/ndk"; +import type { INostrEventEventSign, NostrMetadata } from "$root"; +import NDK, { NDKEvent } from "@nostr-dev-kit/ndk"; import { type NostrEvent as NostrToolsEvent } from "nostr-tools"; export type INostrEventServiceFormatTagsBasisNip99 = { @@ -27,6 +27,8 @@ export type INostrEventService = { nostr_event_verify_serialized: (event_serialized: string) => boolean; nostr_event_verify: (event: NostrToolsEvent) => boolean; nevent_encode: (opts: INostrEventServiceNeventEncode) => string; + metadata: (ndk: NDK, opts: NostrMetadata) => Promise<NDKEvent | undefined>; + classified: (ndk: NDK, opts: INostrEventServiceFormatTagsBasisNip99) => Promise<NDKEvent | undefined>; }; export type NostrEventTagListing = {