commit e16e1f187b22a338d1ac52034b52a2de300c6ab7
parent 1a5b1adf285cb5456b0d8512b726f5aea3a2bb3f
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Fri, 30 Aug 2024 21:19:10 +0000
utils: migrate nostr tools class to `@radroots/client`
Diffstat:
3 files changed, 0 insertions(+), 119 deletions(-)
diff --git a/utils/package.json b/utils/package.json
@@ -18,10 +18,8 @@
"access": "public"
},
"dependencies": {
- "@noble/hashes": "^1.4.0",
"jshashes": "^1.0.8",
"ngeohash": "0.6.3",
- "nostr-tools": "^2.7.2",
"uuid": "^10.0.0"
}
}
\ No newline at end of file
diff --git a/utils/src/index.ts b/utils/src/index.ts
@@ -2,6 +2,5 @@
import "./global.d.ts";
export * from "./geolocation"
export * from "./jshashes"
-export * from "./nostr-tools"
export * from "./types"
export * from "./utils"
diff --git a/utils/src/nostr-tools.ts b/utils/src/nostr-tools.ts
@@ -1,116 +0,0 @@
-import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
-import { Relay, generateSecretKey, getPublicKey, nip19, } from 'nostr-tools';
-
-export class NostrTools {
- 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<{ url: string, connected: boolean } | 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 {
- if (!secret_key_hex) throw new Error('Error: Secret key hex');
- const bytes = this.get_key_bytes(secret_key_hex);
- const hex = getPublicKey(bytes)
- return hex;
- }
-
- /**
- *
- * @returns nostr public key npub
- */
- public npub(public_key_hex: string | undefined): string | undefined {
- if (!public_key_hex) return undefined;
- const npub = nip19.npubEncode(public_key_hex)
- return npub;
- }
-
- /**
- *
- * @returns public key hex from npub
- */
- public npub_decode(npub: string): string {
- // if (!npub) return undefined;
- const hex = nip19.decode(npub);
- if (hex && hex.type === `npub` && hex.data) return hex.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 {
- if (!nsec) return undefined;
- const decode = nip19.decode(nsec);
- if (decode && decode.type === `nsec` && decode.data && typeof decode.data === `string`) return decode.data
- return undefined;
- }
-
- /**
- *
- * @returns nostr public key nprofile
- */
- public nprofile(public_key_hex: string, relays: string[]): string {
- if (!public_key_hex || !relays.length) return ``;
- const nprofile = nip19.nprofileEncode({ pubkey: public_key_hex, relays })
- return nprofile;
- }
-
- /**
- *
- * @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;
- }
-};