web_lib

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

commit cfa842ea04f7e91d2cd67c47982a58ae7c5433c5
parent 788cab0d83f906eeae81fab23205601b34b148f7
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Fri, 27 Dec 2024 02:15:58 +0000

client: move nostr client to `@radroots/utils`. update packages, .gitignore

Diffstat:
Mclient/.gitignore | 3++-
Mclient/package.json | 2+-
Mclient/src/index.ts | 4----
Dclient/src/nostr/client.ts | 17-----------------
Dclient/src/nostr/events.ts | 10----------
Dclient/src/nostr/lib.ts | 160-------------------------------------------------------------------------------
Dclient/src/nostr/types.ts | 28----------------------------
7 files changed, 3 insertions(+), 221 deletions(-)

diff --git a/client/.gitignore b/client/.gitignore @@ -27,7 +27,8 @@ yarn-error.log* # turbo .turbo -_tmp +.tmp* +.dev* .vscode notes*.txt justfile diff --git a/client/package.json b/client/package.json @@ -24,7 +24,7 @@ "@tauri-apps/plugin-notification": "^2.0.0", "@tauri-apps/plugin-os": "^2.0.0", "@tauri-apps/plugin-store": "^2.1.0", - "nostr-tools": "^2.7.2" + "nostr-tools": "^2.10.4" }, "devDependencies": { "@types/debug": "^4.1.12", diff --git a/client/src/index.ts b/client/src/index.ts @@ -19,10 +19,6 @@ export * from "./keystore/tauri" export * from "./keystore/types" export * from "./logger/tauri" export * from "./logger/types" -export * from "./nostr/client" -export * from "./nostr/events" -export * from "./nostr/lib" -export * from "./nostr/types" export * from "./notification/tauri" export * from "./notification/types" export * from "./os/tauri" diff --git a/client/src/nostr/client.ts b/client/src/nostr/client.ts @@ -1,16 +0,0 @@ -import { ClientNostrEvents } from "./events"; -import { ClientNostrLib } from "./lib"; -import type { IClientNostr } from "./types"; - -export class ClientNostr implements IClientNostr { - private _ev: ClientNostrEvents = new ClientNostrEvents(); - private _lib: ClientNostrLib = new ClientNostrLib(); - - public get ev() { - return this._ev; - } - - public get lib() { - return this._lib; - } -} -\ No newline at end of file diff --git a/client/src/nostr/events.ts b/client/src/nostr/events.ts @@ -1,9 +0,0 @@ -import { type NDKEvent } from "@nostr-dev-kit/ndk"; -import type { IClientNostrEvents } from "./types"; - -export class ClientNostrEvents implements IClientNostrEvents { - public first_tag_value(event: NDKEvent, tag_name: string): string { - const tag = event.getMatchingTags(tag_name)[0]; - return tag ? tag[1] : ""; - } -} -\ No newline at end of file diff --git a/client/src/nostr/lib.ts b/client/src/nostr/lib.ts @@ -1,160 +0,0 @@ -import { bytesToHex, hexToBytes } from '@noble/hashes/utils'; -import { Relay, generateSecretKey, getPublicKey, nip19, } from 'nostr-tools'; -import type { IClientNostrLib, IClientNostrLibRelayConnectResponse } from './types'; - -export class ClientNostrLib implements IClientNostrLib { - private generate_key_bytes(): Uint8Array { - const secret_key = generateSecretKey(); - return secret_key; - }; - - private get_key_hex(bytes: Uint8Array): string { - const hex = bytesToHex(bytes); - return hex; - }; - - private get_key_bytes(hex: string): Uint8Array { - const bytes = hexToBytes(hex); - return bytes; - }; - - public async relay_connect(url: string): Promise<IClientNostrLibRelayConnectResponse | undefined> { - try { - - if (!url) return undefined; - const conn = await Relay.connect(url); - if (conn && typeof conn.connected === `boolean` && typeof conn.url === `string` && conn.url) return { url: conn.url, connected: conn.connected }; - return undefined; - } catch (e) { - return undefined; - }; - } - - /** - * - * @returns nostr secret key hex - */ - public generate_key(): string { - const bytes = this.generate_key_bytes(); - const hex = this.get_key_hex(bytes); - return hex; - }; - - /** - * - * @returns nostr public key hex - */ - public public_key(secret_key_hex: string | undefined): string { - try { - if (!secret_key_hex) return ``; - const bytes = this.get_key_bytes(secret_key_hex); - const hex = getPublicKey(bytes) - return hex; - } catch (e) { - return `` - } - } - - /** - * - * @returns nostr secret key to public key hex - */ - public publickey_decode(secret_key_hex?: string): string | undefined { - try { - if (secret_key_hex) { - return getPublicKey(this.get_key_bytes(secret_key_hex)) - } - return undefined; - } catch (e) { - return undefined; - } - } - - /** - * - * @returns nostr public key npub - */ - public npub(public_key_hex: string | undefined, fallback_to_hex?: boolean): string { - if (!public_key_hex) return ``; - const npub = nip19.npubEncode(public_key_hex); - return npub ? npub : fallback_to_hex ? public_key_hex : ``; - } - - /** - * - * @returns public key hex from npub - */ - public npub_decode(npub: string): string { - const decode = nip19.decode(npub); - console.log(`decode `, decode) - if (decode && decode.type === `npub` && decode.data) return decode.data - return ``; - } - - /** - * - * @returns nostr secret key nsec - */ - public nsec(secret_key_hex: string | undefined): string { - if (!secret_key_hex) return ``; - const bytes = this.get_key_bytes(secret_key_hex); - const nsec = nip19.nsecEncode(bytes); - return nsec; - } - - /** - * - * @returns nostr secret key hex from nsec - */ - public nsec_decode(nsec: string): string | undefined { - try { - if (!nsec) return undefined; - const decode = nip19.decode(nsec); - if (decode && decode.type === `nsec` && decode.data) return bytesToHex(decode.data); - return undefined; - } catch (e) { - return undefined; - } - } - - /** - * - * @returns - */ - public nevent(event_pointer: nip19.EventPointer, relays: string[]): string { - return nip19.neventEncode(event_pointer) - } - - /** - * - * @returns nostr public key nprofile - */ - public nprofile(public_key_hex: string, relays: string[]): string { - if (!public_key_hex || !relays.length) return ``; - return nip19.nprofileEncode({ pubkey: public_key_hex, relays }) - } - - /** - * - * @returns nostr public key nprofile - */ - public nprofile_decode(nprofile: string): [string, string[]] | undefined { - if (!nprofile) return undefined; - const decode = nip19.decode(nprofile); - if (decode && decode.type === `nprofile` && decode.data && decode.data.pubkey && decode.data.relays) return [decode.data.pubkey, decode.data.relays] - return undefined; - } - - /** - * - * @returns - */ - public secretkey_to_publickey(nsec_or_hex: string): string | undefined { - if (nsec_or_hex.startsWith(`nsec1`)) { - return this.nsec_decode(nsec_or_hex); - } else if (nsec_or_hex.length === 64) { - return this.publickey_decode(nsec_or_hex) - } - return undefined; - } -}; diff --git a/client/src/nostr/types.ts b/client/src/nostr/types.ts @@ -1,28 +0,0 @@ -import { NDKEvent } from "@nostr-dev-kit/ndk"; - -export type IClientNostrEvents = { - first_tag_value(event: NDKEvent, tag_name: string): string; -}; - -export type IClientNostrLibRelayConnectResponse = { - url: string; - connected: boolean; -}; - -export type IClientNostrLib = { - relay_connect(url: string): Promise<IClientNostrLibRelayConnectResponse | undefined>; - generate_key(): string; - public_key(secret_key_hex: string | undefined): string; - npub(public_key_hex: string | undefined): string; - npub_decode(npub: string): string; - nsec(secret_key_hex: string | undefined): string; - nsec_decode(nsec: string): string | undefined; - nprofile(public_key_hex: string, relays: string[]): string; - nprofile_decode(nprofile: string): [string, string[]] | undefined; - secretkey_to_publickey(nsec_or_hex: string): string | undefined; -}; - -export type IClientNostr = { - ev: IClientNostrEvents; - lib: IClientNostrLib -};