commit 99a3376ca680716cfb402a28ac9d71366b7c7d80
parent cbf60aa16ca36795e1517d49ded7a54c2d0244b4
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Fri, 4 Oct 2024 09:36:23 +0000
utils: edit nostr utils, add ndk utils from `@radroots/svelte-lib`
Diffstat:
7 files changed, 143 insertions(+), 48 deletions(-)
diff --git a/utils/package.json b/utils/package.json
@@ -18,6 +18,7 @@
"access": "public"
},
"dependencies": {
+ "@nostr-dev-kit/ndk": "^2.10.0",
"jshashes": "^1.0.8",
"ngeohash": "0.6.3",
"uuid": "^10.0.0"
diff --git a/utils/src/geolocation.ts b/utils/src/geolocation.ts
@@ -1,6 +1,11 @@
import ngeohash from "ngeohash";
import type { LocationPoint } from "./types";
+export type GeolocationCoordinates = {
+ lat: number;
+ lng: number;
+}
+
export const geohash_encode = (opts: {
lat: string | number;
lng: string | number;
diff --git a/utils/src/index.ts b/utils/src/index.ts
@@ -4,7 +4,9 @@ export * from "./currency"
export * from "./error"
export * from "./geolocation"
export * from "./jshashes"
-export * from "./nostr"
+export * from "./nostr/lib"
+export * from "./nostr/ndk"
+export * from "./nostr/types"
export * from "./regex"
export * from "./time"
export * from "./trade"
diff --git a/utils/src/nostr.ts b/utils/src/nostr.ts
@@ -1,47 +0,0 @@
-export type NostrRelayInformationDocument = {
- id?: string;
- name?: string;
- description?: string;
- contact?: string;
- supported_nips?: number[];
- software?: string;
- version?: string;
- limitation_payment_required?: string;
- limitation_restricted_writes?: boolean;
-}
-
-export type NostrRelayInformationDocumentFormFields = { [K in keyof NostrRelayInformationDocument]: string; };
-
-export const parse_nostr_relay_information_document = (data: any): NostrRelayInformationDocument | undefined => {
- const obj = JSON.parse(data);
- return {
- id: typeof obj.id === 'string' ? obj.id : undefined,
- name: typeof obj.name === 'string' ? obj.name : undefined,
- description: typeof obj.description === 'string' ? obj.description : undefined,
- contact: typeof obj.contact === 'string' ? obj.contact : undefined,
- supported_nips: Array.isArray(obj.supported_nips) && obj.supported_nips.every((nip: any) => typeof nip === 'number')
- ? obj.supported_nips
- : undefined,
- software: typeof obj.software === 'string' ? obj.software : undefined,
- version: typeof obj.version === 'string' ? obj.version : undefined,
- limitation_payment_required: obj.limitation && typeof obj.limitation === 'object' && typeof obj.limitation.payment_required === 'string' ? obj.limitation.payment_required : undefined,
- limitation_restricted_writes: obj.limitation && typeof obj.limitation === 'object' && typeof obj.limitation.restricted_writes === 'boolean' ? obj.limitation.restricted_writes : undefined,
- };
-};
-
-export const parse_nostr_relay_information_document_fields = (data: any): NostrRelayInformationDocumentFormFields | undefined => {
- const info_doc = parse_nostr_relay_information_document(data);
- const result: Partial<NostrRelayInformationDocumentFormFields> = {};
- Object.entries(info_doc).forEach(([key, value]: [keyof NostrRelayInformationDocument, string]) => {
- if (typeof value === 'boolean') {
- result[key] = value ? '1' : '0';
- } else if (Array.isArray(value)) {
- result[key] = value.join(', ');
- } else if (value === null || value === undefined) {
- result[key] = '';
- } else {
- result[key] = String(value);
- }
- });
- return result;
-};
diff --git a/utils/src/nostr/lib.ts b/utils/src/nostr/lib.ts
@@ -0,0 +1,66 @@
+import type { NostrRelayInformationDocument, NostrRelayInformationDocumentFormFields } from "./types";
+
+export const parse_nostr_relay_information_document = (data: any): NostrRelayInformationDocument | undefined => {
+ const obj = JSON.parse(data);
+ return {
+ id: typeof obj.id === 'string' ? obj.id : undefined,
+ name: typeof obj.name === 'string' ? obj.name : undefined,
+ description: typeof obj.description === 'string' ? obj.description : undefined,
+ pubkey: typeof obj.pubkey === 'string' ? obj.pubkey : undefined,
+ contact: typeof obj.contact === 'string' ? obj.contact : undefined,
+ supported_nips: Array.isArray(obj.supported_nips) && obj.supported_nips.every((nip: any) => typeof nip === 'number')
+ ? obj.supported_nips
+ : undefined,
+ software: typeof obj.software === 'string' ? obj.software : undefined,
+ version: typeof obj.version === 'string' ? obj.version : undefined,
+ limitation_payment_required: obj.limitation && typeof obj.limitation === 'object' && typeof obj.limitation.payment_required === 'string' ? obj.limitation.payment_required : undefined,
+ limitation_restricted_writes: obj.limitation && typeof obj.limitation === 'object' && typeof obj.limitation.restricted_writes === 'boolean' ? obj.limitation.restricted_writes : undefined,
+ };
+};
+
+export const parse_nostr_relay_information_document_fields = (data: any): NostrRelayInformationDocumentFormFields | undefined => {
+ const info_doc = parse_nostr_relay_information_document(data);
+ if (!info_doc) return;
+ const result: Partial<NostrRelayInformationDocumentFormFields> = {};
+ Object.entries(info_doc).forEach(([key, value]) => {
+ if (typeof value === 'boolean') {
+ result[key as keyof NostrRelayInformationDocument] = value ? '1' : '0';
+ } else if (Array.isArray(value)) {
+ result[key as keyof NostrRelayInformationDocument] = value.join(', ');
+ } else if (value === null || value === undefined) {
+ result[key as keyof NostrRelayInformationDocument] = '';
+ } else {
+ result[key as keyof NostrRelayInformationDocument] = String(value);
+ }
+ });
+ return result;
+};
+
+export const fmt_tags_basis_nip99 = async (opts: {
+ d_tag: string;
+ title: string;
+ image?: {
+ url: string;
+ size: string;
+ };
+ summary?: string;
+ location?: string;
+ price?: {
+ amt: string;
+ currency: string;
+ };
+}): Promise<string[][] | undefined> => {
+ try {
+ const tags: string[][] = [
+ ["d", opts.d_tag],
+ ["title", opts.title],
+ ];
+ if (opts.image) tags.push(["image", opts.image.url, opts.image.size],)
+ if (opts.summary) tags.push(["summary", opts.summary])
+ if (opts.location) tags.push(["location", opts.location])
+ if (opts.price) tags.push(["price", opts.price.amt, opts.price.currency])
+ return tags;
+ } catch (e) {
+ console.log(`(error) fmt_tags_nip99 `, e);
+ }
+};
+\ No newline at end of file
diff --git a/utils/src/nostr/ndk.ts b/utils/src/nostr/ndk.ts
@@ -0,0 +1,53 @@
+import NDK, { NDKEvent, NDKPrivateKeySigner, NDKUser } from '@nostr-dev-kit/ndk';
+import { time_now_ms } from '../time';
+
+export const ndk_init = async (opts: {
+ $ndk: NDK;
+ secret_key: string;
+}): Promise<NDKUser | undefined> => {
+ try {
+ const { $ndk: ndk, secret_key } = opts;
+ const signer = new NDKPrivateKeySigner(secret_key);
+ ndk.signer = signer;
+
+ const user = await signer.user();
+ if (user) {
+ user.ndk = ndk;
+ return user;
+ }
+ } catch (e) {
+ console.log(`(error) ndk_init `, e);
+ };
+};
+
+export const ndk_event = async (opts: {
+ $ndk: NDK;
+ $ndk_user: NDKUser;
+ basis: {
+ kind: number;
+ content: string;
+ tags?: string[][];
+ }
+}): Promise<NDKEvent | undefined> => {
+ try {
+ const { $ndk: ndk, $ndk_user: ndk_user, basis } = opts;
+ const time_now = time_now_ms();
+
+ const tags: string[][] = [
+ ['published_at', time_now.toString()],
+ ];
+
+ if (basis.tags && basis.tags?.length > 0) for (const tag of basis.tags) tags.push(tag);
+
+ const event: NDKEvent = new NDKEvent(ndk, {
+ kind: basis.kind,
+ pubkey: ndk_user.pubkey,
+ content: basis.content,
+ created_at: time_now,
+ tags
+ });
+ return event;
+ } catch (e) {
+ console.log(`(error) ndk_event `, e);
+ };
+};
diff --git a/utils/src/nostr/types.ts b/utils/src/nostr/types.ts
@@ -0,0 +1,14 @@
+export type NostrRelayInformationDocument = {
+ id?: string;
+ name?: string;
+ description?: string;
+ pubkey?: string;
+ contact?: string;
+ supported_nips?: number[];
+ software?: string;
+ version?: string;
+ limitation_payment_required?: string;
+ limitation_restricted_writes?: boolean;
+}
+
+export type NostrRelayInformationDocumentFormFields = { [K in keyof NostrRelayInformationDocument]: string; };