web_lib

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

commit e3598faba0c86742d80ea8a7d3c2891692d37f81
parent 10deb56a849864c330e41331a5c161f993ecab80
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Thu, 17 Apr 2025 22:57:01 +0000

utils-nostr: add nip-90 job request event utils, types

Diffstat:
Mutils-nostr/src/lib/ndk.ts | 30+++++++++++++++++++++++-------
Mutils-nostr/src/lib/tags.ts | 24+++++++++++++++++++++---
Mutils-nostr/src/lib/types.ts | 39+++++++++++++++++++++++++++++++++++++++
3 files changed, 83 insertions(+), 10 deletions(-)

diff --git a/utils-nostr/src/lib/ndk.ts b/utils-nostr/src/lib/ndk.ts @@ -1,4 +1,4 @@ -import { INostrClassified, INostrFollow, NostrEventTags, tags_classified, tags_follow_list, time_now_ms, type INostrMetadata } from '$root'; +import { INostrClassified, INostrFollow, INostrJobRequest, NostrEventTags, tags_classified, tags_follow_list, tags_job_request, time_now_ms, type INostrMetadata } from '$root'; import NDK, { NDKEvent, NDKKind, NDKPrivateKeySigner, NDKUser } from '@nostr-dev-kit/ndk'; export type NDKEventFigure<T extends object> = { @@ -33,6 +33,7 @@ export const ndk_event = async (opts: NDKEventFigure<{ }>): Promise<NDKEvent | undefined> => { try { const { $ndk: ndk, $ndk_user: ndk_user, basis } = opts; + console.log(JSON.stringify(basis, null, 4), `basis`) const time_now = time_now_ms(); const tags: NostrEventTags = [ ['published_at', time_now.toString()], @@ -54,13 +55,13 @@ export const ndk_event = async (opts: NDKEventFigure<{ export const ndk_event_metadata = async (opts: NDKEventFigure<{ metadata: INostrMetadata }>): Promise<NDKEvent | undefined> => { - const { $ndk, $ndk_user, metadata: param } = opts; + const { $ndk, $ndk_user, metadata: data } = opts; return await ndk_event({ $ndk, $ndk_user, basis: { kind: 0, - content: JSON.stringify(param), + content: JSON.stringify(data), }, }); }; @@ -68,14 +69,14 @@ export const ndk_event_metadata = async (opts: NDKEventFigure<{ export const ndk_event_follows = async (opts: NDKEventFigure<{ list: INostrFollow[]; }>): Promise<NDKEvent | undefined> => { - const { $ndk, $ndk_user, list: param } = opts; + const { $ndk, $ndk_user, list: data } = opts; return await ndk_event({ $ndk, $ndk_user, basis: { kind: 3, content: ``, - tags: tags_follow_list(param), + tags: tags_follow_list(data), }, }); }; @@ -83,14 +84,29 @@ export const ndk_event_follows = async (opts: NDKEventFigure<{ export const ndk_event_classified = async (opts: NDKEventFigure<{ classified: INostrClassified; }>): Promise<NDKEvent | undefined> => { - const { $ndk, $ndk_user, classified: param } = opts; + const { $ndk, $ndk_user, classified: data } = opts; return await ndk_event({ $ndk, $ndk_user, basis: { kind: NDKKind.Classified, content: ``, - tags: tags_classified(param), + tags: tags_classified(data), + }, + }); +}; + +export const ndk_event_job_request = async (opts: NDKEventFigure<{ + data: INostrJobRequest; +}>): Promise<NDKEvent | undefined> => { + const { $ndk, $ndk_user, data } = opts; + return await ndk_event({ + $ndk, + $ndk_user, + basis: { + kind: NDKKind.DVMReqDiscoveryNostrContent, + content: ``, + tags: tags_job_request(data) }, }); }; diff --git a/utils-nostr/src/lib/tags.ts b/utils-nostr/src/lib/tags.ts @@ -1,4 +1,4 @@ -import { INostrClassified, NostrEventTagClient, NostrEventTagLocation, NostrEventTagMediaUpload, NostrEventTagPrice, NostrEventTagQuantity, type INostrFollow, type NostrEventTag, type NostrEventTags } from "$root"; +import { INostrClassified, INostrJobRequest, NostrEventTagClient, NostrEventTagLocation, NostrEventTagMediaUpload, NostrEventTagPrice, NostrEventTagQuantity, type INostrFollow, type NostrEventTag, type NostrEventTags } from "$root"; import { ngeotags, type InputData as NostrGeotagsInputData } from "nostr-geotags"; export const tag_client = (opts: NostrEventTagClient, d_tag?: string): NostrEventTag => { @@ -73,4 +73,23 @@ export const tags_classified = (opts: INostrClassified): NostrEventTags => { if (opts.images) for (const image_tags of opts.images) tags.push(tag_classified_image(image_tags)); tags.push(...tags_classified_location_geotags(location)); return tags; -}; -\ No newline at end of file +}; + +export const tags_job_request = (opts: INostrJobRequest): NostrEventTags => { + const tag_i: string[] = [`i`]; + if (`classified` in opts.input && opts.input?.classified) { + const { classified: event_request } = opts.input; + let marker = `*`; + let data = `*`; + if (event_request.marker && `order` in event_request.marker) { + marker = `order`; + data = JSON.stringify({ event: { id: event_request.id }, order: event_request.marker.order }); + } + tag_i.push(...[data, `text`, event_request.relay, marker]); + tag_i.push(...(opts.input.tags || [])) + } + + const tags: NostrEventTags = [tag_i]; + tags.push(...(opts.tags || [])) + return tags; +}; diff --git a/utils-nostr/src/lib/types.ts b/utils-nostr/src/lib/types.ts @@ -29,6 +29,45 @@ export type INostrClassified = { client?: NostrEventTagClient; }; +export type NostrJobRequestMassUnit = 'g' | 'kg' | 'lb'; + +export type INostrJobRequestOrderQuantity = { + amount: number; + unit: string; + count: number; + mass_g: number; + label: string; +}; + +export type INostrJobRequestOrderPrice = { + amount: number; + currency: string; + quantity_amount: number; + quantity_unit: NostrJobRequestMassUnit; +}; + +export type INostrJobRequestOrder = { + price: INostrJobRequestOrderPrice; + quantity: INostrJobRequestOrderQuantity; +} + +export type INostrJobRequestInput = { + tags?: string[]; +} & ({ + classified: { + id: string; + relay: string; + marker?: ({ + order: INostrJobRequestOrder; + }); + } +}) + +export type INostrJobRequest = { + input: INostrJobRequestInput; + tags?: string[][]; +}; + export type INostrEventEventSign = { secret_key: string; event: NostrToolsEventTemplate;