@nostrify/nostrify 0.46.9 → 0.46.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/.turbo/turbo-build.log +17 -122
  2. package/CHANGELOG.md +8 -0
  3. package/dist/BunkerURI.js +48 -0
  4. package/dist/BunkerURI.ts +58 -0
  5. package/dist/NBrowserSigner.js +82 -0
  6. package/dist/NBrowserSigner.ts +100 -0
  7. package/dist/NCache.js +39 -0
  8. package/dist/NCache.ts +73 -0
  9. package/dist/NConnectSigner.js +132 -0
  10. package/dist/NConnectSigner.ts +188 -0
  11. package/dist/NIP05.d.ts +1 -1
  12. package/dist/NIP05.js +35 -0
  13. package/dist/NIP05.ts +51 -0
  14. package/dist/NIP50.js +21 -0
  15. package/dist/NIP50.ts +24 -0
  16. package/dist/NIP98.js +74 -0
  17. package/dist/NIP98.ts +97 -0
  18. package/dist/NKinds.js +25 -0
  19. package/dist/NKinds.ts +26 -0
  20. package/dist/NPool.js +154 -0
  21. package/dist/NPool.ts +243 -0
  22. package/dist/NRelay1.js +318 -0
  23. package/dist/NRelay1.ts +447 -0
  24. package/dist/NSchema.d.ts +2 -2
  25. package/dist/NSchema.d.ts.map +1 -1
  26. package/dist/NSchema.js +216 -0
  27. package/dist/NSchema.ts +291 -0
  28. package/dist/NSecSigner.js +38 -0
  29. package/dist/NSecSigner.ts +62 -0
  30. package/dist/NSet.js +143 -0
  31. package/dist/NSet.ts +210 -0
  32. package/dist/RelayError.js +20 -0
  33. package/dist/RelayError.ts +22 -0
  34. package/dist/ln/LNURL.js +101 -0
  35. package/dist/ln/LNURL.ts +146 -0
  36. package/dist/ln/mod.js +4 -0
  37. package/dist/ln/mod.ts +4 -0
  38. package/dist/ln/types/LNURLCallback.js +0 -0
  39. package/dist/ln/types/LNURLCallback.ts +7 -0
  40. package/dist/ln/types/LNURLDetails.js +0 -0
  41. package/dist/ln/types/LNURLDetails.ts +19 -0
  42. package/dist/mod.js +30 -0
  43. package/dist/mod.ts +16 -0
  44. package/dist/test/ErrorRelay.js +23 -0
  45. package/dist/test/ErrorRelay.ts +52 -0
  46. package/dist/test/MockRelay.js +61 -0
  47. package/dist/test/MockRelay.ts +92 -0
  48. package/dist/test/TestRelayServer.js +149 -0
  49. package/dist/test/TestRelayServer.ts +185 -0
  50. package/dist/test/mod.js +24 -0
  51. package/dist/test/mod.ts +28 -0
  52. package/dist/tsconfig.tsbuildinfo +1 -1
  53. package/dist/uploaders/BlossomUploader.js +70 -0
  54. package/dist/uploaders/BlossomUploader.ts +98 -0
  55. package/dist/uploaders/NostrBuildUploader.js +65 -0
  56. package/dist/uploaders/NostrBuildUploader.ts +89 -0
  57. package/dist/uploaders/mod.js +6 -0
  58. package/dist/uploaders/mod.ts +2 -0
  59. package/dist/utils/CircularSet.js +29 -0
  60. package/dist/utils/CircularSet.ts +36 -0
  61. package/dist/utils/Machina.js +38 -0
  62. package/dist/utils/Machina.ts +66 -0
  63. package/dist/utils/N64.js +17 -0
  64. package/dist/utils/N64.ts +23 -0
  65. package/dist/utils/mod.js +6 -0
  66. package/dist/utils/mod.ts +2 -0
  67. package/package.json +2 -2
@@ -1,124 +1,19 @@
1
1
 
2
2
 
3
- > @nostrify/nostrify@0.46.8 build /home/sid/repos/nostrify/packages/nostrify
4
- > tsc -p tsconfig.json && node ../../esbuild.config.js --package ./
5
-
6
- NIP05.ts:18:5 - error TS2322: Type 'ZodObject<{ pubkey: ZodString; relays: ZodOptional<ZodArray<ZodType<`ws://${string}` | `wss://${string}`, ZodTypeDef, `ws://${string}` | `wss://${string}`>, "many">>; }, "strip", ZodTypeAny, { ...; }, { ...; }>' is not assignable to type 'ZodType<NProfilePointer, ZodTypeDef, NProfilePointer>'.
7
- Types of property '_type' are incompatible.
8
- Type '{ pubkey?: string; relays?: (`ws://${string}` | `wss://${string}`)[]; }' is not assignable to type 'NProfilePointer'.
9
- Property 'pubkey' is optional in type '{ pubkey?: string; relays?: (`ws://${string}` | `wss://${string}`)[]; }' but required in type 'NProfilePointer'.
10
-
11
- 18 return z.object({
12
-    ~~~~~~
13
-
14
- NSchema.ts:45:5 - error TS2322: Type 'ZodObject<{ id: ZodString; kind: ZodNumber; pubkey: ZodString; tags: ZodArray<ZodArray<ZodString, "many">, "many">; content: ZodString; created_at: ZodNumber; sig: ZodString; }, "strip", ZodTypeAny, { ...; }, { ...; }>' is not assignable to type 'ZodType<NostrEvent, ZodTypeDef, NostrEvent>'.
15
- Types of property '_type' are incompatible.
16
- Type '{ pubkey?: string; tags?: string[][]; id?: string; sig?: string; created_at?: number; kind?: number; content?: string; }' is not assignable to type 'NostrEvent'.
17
- Property 'id' is optional in type '{ pubkey?: string; tags?: string[][]; id?: string; sig?: string; created_at?: number; kind?: number; content?: string; }' but required in type 'NostrEvent'.
18
-
19
- 45 return z.object({
20
-    ~~~~~~
21
-
22
- NSchema.ts:108:5 - error TS2322: Type 'ZodTuple<[ZodLiteral<"EVENT">, ZodType<NostrEvent, ZodTypeDef, NostrEvent>], null>' is not assignable to type 'ZodType<NostrClientEVENT, ZodTypeDef, NostrClientEVENT>'.
23
- Types of property '_type' are incompatible.
24
- Type '["EVENT", NostrEvent, ...unknown[]]' is not assignable to type 'NostrClientEVENT'.
25
- Target allows only 2 element(s) but source may have more.
26
-
27
- 108 return z.tuple([z.literal('EVENT'), NSchema.event()]);
28
-    ~~~~~~
29
-
30
- NSchema.ts:123:5 - error TS2322: Type 'ZodTuple<[ZodLiteral<"CLOSE">, ZodString], null>' is not assignable to type 'ZodType<NostrClientCLOSE, ZodTypeDef, NostrClientCLOSE>'.
31
- Types of property '_type' are incompatible.
32
- Type '["CLOSE", string, ...unknown[]]' is not assignable to type 'NostrClientCLOSE'.
33
- Target allows only 2 element(s) but source may have more.
34
-
35
- 123 return z.tuple([z.literal('CLOSE'), z.string()]);
36
-    ~~~~~~
37
-
38
- NSchema.ts:128:5 - error TS2322: Type 'ZodTuple<[ZodLiteral<"AUTH">, ZodType<NostrEvent, ZodTypeDef, NostrEvent>], null>' is not assignable to type 'ZodType<NostrClientAUTH, ZodTypeDef, NostrClientAUTH>'.
39
- Types of property '_type' are incompatible.
40
- Type '["AUTH", NostrEvent, ...unknown[]]' is not assignable to type 'NostrClientAUTH'.
41
- Target allows only 2 element(s) but source may have more.
42
-
43
- 128 return z.tuple([z.literal('AUTH'), NSchema.event()]);
44
-    ~~~~~~
45
-
46
- NSchema.ts:144:5 - error TS2322: Type 'ZodTuple<[ZodLiteral<"EVENT">, ZodString, ZodType<NostrEvent, ZodTypeDef, NostrEvent>], null>' is not assignable to type 'ZodType<NostrRelayEVENT, ZodTypeDef, NostrRelayEVENT>'.
47
- Types of property '_type' are incompatible.
48
- Type '["EVENT", string, NostrEvent, ...unknown[]]' is not assignable to type 'NostrRelayEVENT'.
49
- Target allows only 3 element(s) but source may have more.
50
-
51
- 144 return z.tuple([z.literal('EVENT'), z.string(), NSchema.event()]);
52
-    ~~~~~~
53
-
54
- NSchema.ts:149:5 - error TS2322: Type 'ZodTuple<[ZodLiteral<"OK">, ZodString, ZodBoolean, ZodString], null>' is not assignable to type 'ZodType<NostrRelayOK, ZodTypeDef, NostrRelayOK>'.
55
- Types of property '_type' are incompatible.
56
- Type '["OK", string, boolean, string, ...unknown[]]' is not assignable to type 'NostrRelayOK'.
57
- Target allows only 4 element(s) but source may have more.
58
-
59
- 149 return z.tuple([z.literal('OK'), NSchema.id(), z.boolean(), z.string()]);
60
-    ~~~~~~
61
-
62
- NSchema.ts:154:5 - error TS2322: Type 'ZodTuple<[ZodLiteral<"EOSE">, ZodString], null>' is not assignable to type 'ZodType<NostrRelayEOSE, ZodTypeDef, NostrRelayEOSE>'.
63
- Types of property '_type' are incompatible.
64
- Type '["EOSE", string, ...unknown[]]' is not assignable to type 'NostrRelayEOSE'.
65
- Target allows only 2 element(s) but source may have more.
66
-
67
- 154 return z.tuple([z.literal('EOSE'), z.string()]);
68
-    ~~~~~~
69
-
70
- NSchema.ts:159:5 - error TS2322: Type 'ZodTuple<[ZodLiteral<"NOTICE">, ZodString], null>' is not assignable to type 'ZodType<NostrRelayNOTICE, ZodTypeDef, NostrRelayNOTICE>'.
71
- Types of property '_type' are incompatible.
72
- Type '["NOTICE", string, ...unknown[]]' is not assignable to type 'NostrRelayNOTICE'.
73
- Target allows only 2 element(s) but source may have more.
74
-
75
- 159 return z.tuple([z.literal('NOTICE'), z.string()]);
76
-    ~~~~~~
77
-
78
- NSchema.ts:164:5 - error TS2322: Type 'ZodTuple<[ZodLiteral<"CLOSED">, ZodString, ZodString], null>' is not assignable to type 'ZodType<NostrRelayCLOSED, ZodTypeDef, NostrRelayCLOSED>'.
79
- Types of property '_type' are incompatible.
80
- Type '["CLOSED", string, string, ...unknown[]]' is not assignable to type 'NostrRelayCLOSED'.
81
- Target allows only 3 element(s) but source may have more.
82
-
83
- 164 return z.tuple([z.literal('CLOSED'), z.string(), z.string()]);
84
-    ~~~~~~
85
-
86
- NSchema.ts:169:5 - error TS2322: Type 'ZodTuple<[ZodLiteral<"AUTH">, ZodString], null>' is not assignable to type 'ZodType<NostrRelayAUTH, ZodTypeDef, NostrRelayAUTH>'.
87
- Types of property '_type' are incompatible.
88
- Type '["AUTH", string, ...unknown[]]' is not assignable to type 'NostrRelayAUTH'.
89
- Target allows only 2 element(s) but source may have more.
90
-
91
- 169 return z.tuple([z.literal('AUTH'), z.string()]);
92
-    ~~~~~~
93
-
94
- NSchema.ts:174:5 - error TS2322: Type 'ZodTuple<[ZodLiteral<"COUNT">, ZodString, ZodObject<{ count: ZodNumber; approximate: ZodOptional<ZodBoolean>; }, "strip", ZodTypeAny, { ...; }, { ...; }>], null>' is not assignable to type 'ZodType<NostrRelayCOUNT, ZodTypeDef, NostrRelayCOUNT>'.
95
- Types of property '_type' are incompatible.
96
- Type '["COUNT", string, { count?: number; approximate?: boolean; }, ...unknown[]]' is not assignable to type 'NostrRelayCOUNT'.
97
- Target allows only 3 element(s) but source may have more.
98
-
99
- 174 return z.tuple([
100
-    ~~~~~~
101
-
102
- NSchema.ts:215:5 - error TS2322: Type 'ZodObject<{ id: ZodString; method: ZodString; params: ZodArray<ZodString, "many">; }, "strip", ZodTypeAny, { id?: string; params?: string[]; method?: string; }, { ...; }>' is not assignable to type 'ZodType<NostrConnectRequest, ZodTypeDef, NostrConnectRequest>'.
103
- Types of property '_type' are incompatible.
104
- Type '{ id?: string; params?: string[]; method?: string; }' is not assignable to type 'NostrConnectRequest'.
105
- Property 'id' is optional in type '{ id?: string; params?: string[]; method?: string; }' but required in type 'NostrConnectRequest'.
106
-
107
- 215 return z.object({
108
-    ~~~~~~
109
-
110
- NSchema.ts:224:5 - error TS2322: Type 'ZodObject<{ id: ZodString; result: ZodString; error: ZodOptional<ZodString>; }, "strip", ZodTypeAny, { id?: string; result?: string; error?: string; }, { ...; }>' is not assignable to type 'ZodType<NostrConnectResponse, ZodTypeDef, NostrConnectResponse>'.
111
- Types of property '_type' are incompatible.
112
- Type '{ id?: string; result?: string; error?: string; }' is not assignable to type 'NostrConnectResponse'.
113
- Property 'id' is optional in type '{ id?: string; result?: string; error?: string; }' but required in type 'NostrConnectResponse'.
114
-
115
- 224 return z.object({
116
-    ~~~~~~
117
-
118
-
119
- Found 14 errors in 2 files.
120
-
121
- Errors Files
122
- 1 NIP05.ts:18
123
- 13 NSchema.ts:45
124
-  ELIFECYCLE  Command failed with exit code 2.
3
+ > @nostrify/nostrify@0.46.9 build /home/sid/repos/nostrify/packages/nostrify
4
+ > npx tsc -p tsconfig.json && node ../../esbuild.config.js --package ./
5
+
6
+ npm warn Unknown env config "verify-deps-before-run". This will stop working in the next major version of npm.
7
+ npm warn Unknown env config "_jsr-registry". This will stop working in the next major version of npm.
8
+ ⠙Building with esbuild...
9
+
10
+ dist/NRelay1.js 8.9kb
11
+ dist/NSchema.js 5.8kb
12
+ dist/NPool.js 5.0kb
13
+ dist/test/TestRelayServer.js 4.4kb
14
+ dist/NSet.js 4.1kb
15
+ ...and 25 more output files...
16
+
17
+ Done in 24ms
18
+ Copying source files...
19
+ Done!
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.46.10
4
+
5
+ ### Patch Changes
6
+
7
+ - distribute ts files
8
+ - Updated dependencies
9
+ - @nostrify/types@0.36.6
10
+
3
11
  ## 0.46.9
4
12
 
5
13
  ### Patch Changes
@@ -0,0 +1,48 @@
1
+ class BunkerURI {
2
+ /** Remote signer pubkey. */
3
+ pubkey;
4
+ /** Relay URLs on which the client is listening for responses from the remote-signer. */
5
+ relays;
6
+ /** A short random string that the remote-signer should return as the `result` field of its response. */
7
+ secret;
8
+ constructor(uri) {
9
+ const url = new URL(uri);
10
+ const params = new URLSearchParams(url.search);
11
+ const pubkey = url.hostname || url.pathname.slice(2);
12
+ const relays = params.getAll("relay");
13
+ const secret = params.get("secret") ?? void 0;
14
+ if (!pubkey) {
15
+ throw new Error("Invalid bunker URI");
16
+ }
17
+ this.pubkey = pubkey;
18
+ this.relays = relays;
19
+ this.secret = secret;
20
+ }
21
+ /** Convert into a `bunker://` URI string. */
22
+ get href() {
23
+ return this.toString();
24
+ }
25
+ /** Convert into a `bunker://` URI string. */
26
+ toString() {
27
+ return BunkerURI.toString(this);
28
+ }
29
+ /** Convert a bunker data object into a `BunkerURI` instance. */
30
+ static fromJSON(data) {
31
+ const uri = BunkerURI.toString(data);
32
+ return new BunkerURI(uri);
33
+ }
34
+ /** Convert a bunker data object into a bunker URI string. */
35
+ static toString(data) {
36
+ const search = new URLSearchParams();
37
+ for (const relay of data.relays) {
38
+ search.append("relay", relay);
39
+ }
40
+ if (data.secret) {
41
+ search.set("secret", data.secret);
42
+ }
43
+ return `bunker://${data.pubkey}?${search.toString()}`;
44
+ }
45
+ }
46
+ export {
47
+ BunkerURI
48
+ };
@@ -0,0 +1,58 @@
1
+ /** Construct a [NIP-46](https://github.com/nostr-protocol/nips/blob/master/46.md) bunker URI. */
2
+ export class BunkerURI {
3
+ /** Remote signer pubkey. */
4
+ pubkey: string;
5
+ /** Relay URLs on which the client is listening for responses from the remote-signer. */
6
+ relays: string[];
7
+ /** A short random string that the remote-signer should return as the `result` field of its response. */
8
+ secret?: string;
9
+
10
+ constructor(uri: string) {
11
+ const url = new URL(uri);
12
+ const params = new URLSearchParams(url.search);
13
+
14
+ // https://github.com/denoland/deno/issues/26440
15
+ const pubkey = url.hostname || url.pathname.slice(2);
16
+ const relays = params.getAll('relay');
17
+ const secret = params.get('secret') ?? undefined;
18
+
19
+ if (!pubkey) {
20
+ throw new Error('Invalid bunker URI');
21
+ }
22
+
23
+ this.pubkey = pubkey;
24
+ this.relays = relays;
25
+ this.secret = secret;
26
+ }
27
+
28
+ /** Convert into a `bunker://` URI string. */
29
+ get href(): string {
30
+ return this.toString();
31
+ }
32
+
33
+ /** Convert into a `bunker://` URI string. */
34
+ toString(): string {
35
+ return BunkerURI.toString(this);
36
+ }
37
+
38
+ /** Convert a bunker data object into a `BunkerURI` instance. */
39
+ static fromJSON(data: { pubkey: string; relays: string[]; secret?: string }): BunkerURI {
40
+ const uri = BunkerURI.toString(data);
41
+ return new BunkerURI(uri);
42
+ }
43
+
44
+ /** Convert a bunker data object into a bunker URI string. */
45
+ private static toString(data: { pubkey: string; relays: string[]; secret?: string }): string {
46
+ const search = new URLSearchParams();
47
+
48
+ for (const relay of data.relays) {
49
+ search.append('relay', relay);
50
+ }
51
+
52
+ if (data.secret) {
53
+ search.set('secret', data.secret);
54
+ }
55
+
56
+ return `bunker://${data.pubkey}?${search.toString()}`;
57
+ }
58
+ }
@@ -0,0 +1,82 @@
1
+ class NBrowserSigner {
2
+ get nostr() {
3
+ const nostr = globalThis.nostr;
4
+ if (!nostr) {
5
+ throw new Error("Browser extension not available");
6
+ }
7
+ return nostr;
8
+ }
9
+ async getPublicKey() {
10
+ const pubkey = await this.nostr.getPublicKey();
11
+ if (typeof pubkey !== "string") {
12
+ throw new Error(`Nostr public key retrieval failed: expected string, got ${JSON.stringify(pubkey)}`);
13
+ }
14
+ return pubkey;
15
+ }
16
+ async signEvent(event) {
17
+ const signed = await this.nostr.signEvent(event);
18
+ if (typeof signed !== "object" || !signed.id || !signed.pubkey || !signed.sig) {
19
+ throw new Error(
20
+ `Nostr event signing failed: expected object with id, pubkey, and sig, got ${JSON.stringify(signed)}`
21
+ );
22
+ }
23
+ return signed;
24
+ }
25
+ async getRelays() {
26
+ if (!this.nostr.getRelays) {
27
+ return {};
28
+ }
29
+ const relays = await this.nostr.getRelays();
30
+ if (typeof relays !== "object" || relays === null) {
31
+ throw new Error(`Nostr getRelays failed: expected object, got ${JSON.stringify(relays)}`);
32
+ }
33
+ return this.nostr.getRelays();
34
+ }
35
+ get nip04() {
36
+ const nostr = this.nostr;
37
+ if (!nostr.nip04) {
38
+ return void 0;
39
+ }
40
+ return {
41
+ encrypt: async (pubkey, plaintext) => {
42
+ const encrypted = await nostr.nip04.encrypt(pubkey, plaintext);
43
+ if (typeof encrypted !== "string") {
44
+ throw new Error(`NIP-04 encryption failed: expected string result, got ${JSON.stringify(encrypted)}`);
45
+ }
46
+ return encrypted;
47
+ },
48
+ decrypt: async (pubkey, ciphertext) => {
49
+ const decrypted = await nostr.nip04.decrypt(pubkey, ciphertext);
50
+ if (typeof decrypted !== "string") {
51
+ throw new Error(`NIP-04 decryption failed: expected string result, got ${JSON.stringify(decrypted)}`);
52
+ }
53
+ return decrypted;
54
+ }
55
+ };
56
+ }
57
+ get nip44() {
58
+ const nostr = this.nostr;
59
+ if (!nostr.nip44) {
60
+ return void 0;
61
+ }
62
+ return {
63
+ encrypt: async (pubkey, plaintext) => {
64
+ const encrypted = await nostr.nip44.encrypt(pubkey, plaintext);
65
+ if (typeof encrypted !== "string") {
66
+ throw new Error(`NIP-44 encryption failed: expected string result, got ${JSON.stringify(encrypted)}`);
67
+ }
68
+ return encrypted;
69
+ },
70
+ decrypt: async (pubkey, ciphertext) => {
71
+ const decrypted = await nostr.nip44.decrypt(pubkey, ciphertext);
72
+ if (typeof decrypted !== "string") {
73
+ throw new Error(`NIP-44 decryption failed: expected string result, got ${JSON.stringify(decrypted)}`);
74
+ }
75
+ return decrypted;
76
+ }
77
+ };
78
+ }
79
+ }
80
+ export {
81
+ NBrowserSigner
82
+ };
@@ -0,0 +1,100 @@
1
+ import type { NostrEvent, NostrSigner } from '@nostrify/types';
2
+
3
+ /**
4
+ * NIP-07-compatible signer that proxies to browser extension, normalizing behavior across different implementations.
5
+ *
6
+ * This signer delegates all operations to the browser's `window.nostr` object,
7
+ * which is typically provided by browser extensions like Alby, nos2x, etc.
8
+ *
9
+ * Usage:
10
+ *
11
+ * ```ts
12
+ * const signer = new NBrowserSigner();
13
+ * const pubkey = await signer.getPublicKey();
14
+ * const event = await signer.signEvent({ kind: 1, content: 'Hello, world!', tags: [], created_at: 0 });
15
+ * ```
16
+ */
17
+ export class NBrowserSigner implements NostrSigner {
18
+ private get nostr(): NostrSigner {
19
+ const nostr = (globalThis as { nostr?: NostrSigner }).nostr;
20
+ if (!nostr) {
21
+ throw new Error('Browser extension not available');
22
+ }
23
+ return nostr;
24
+ }
25
+
26
+ async getPublicKey(): Promise<string> {
27
+ const pubkey = await this.nostr.getPublicKey();
28
+ if (typeof pubkey !== 'string') {
29
+ throw new Error(`Nostr public key retrieval failed: expected string, got ${JSON.stringify(pubkey)}`);
30
+ }
31
+ return pubkey;
32
+ }
33
+
34
+ async signEvent(event: Omit<NostrEvent, 'id' | 'pubkey' | 'sig'>): Promise<NostrEvent> {
35
+ const signed = await this.nostr.signEvent(event);
36
+ if (typeof signed !== 'object' || !signed.id || !signed.pubkey || !signed.sig) {
37
+ throw new Error(
38
+ `Nostr event signing failed: expected object with id, pubkey, and sig, got ${JSON.stringify(signed)}`,
39
+ );
40
+ }
41
+ return signed;
42
+ }
43
+
44
+ async getRelays(): Promise<Record<string, { read: boolean; write: boolean }>> {
45
+ if (!this.nostr.getRelays) {
46
+ return {};
47
+ }
48
+ const relays = await this.nostr.getRelays();
49
+ if (typeof relays !== 'object' || relays === null) {
50
+ throw new Error(`Nostr getRelays failed: expected object, got ${JSON.stringify(relays)}`);
51
+ }
52
+ return this.nostr.getRelays();
53
+ }
54
+
55
+ get nip04(): NostrSigner['nip04'] {
56
+ const nostr = this.nostr;
57
+ if (!nostr.nip04) {
58
+ return undefined;
59
+ }
60
+ return {
61
+ encrypt: async (pubkey: string, plaintext: string): Promise<string> => {
62
+ const encrypted = await nostr.nip04!.encrypt(pubkey, plaintext);
63
+ if (typeof encrypted !== 'string') {
64
+ throw new Error(`NIP-04 encryption failed: expected string result, got ${JSON.stringify(encrypted)}`);
65
+ }
66
+ return encrypted;
67
+ },
68
+ decrypt: async (pubkey: string, ciphertext: string): Promise<string> => {
69
+ const decrypted = await nostr.nip04!.decrypt(pubkey, ciphertext);
70
+ if (typeof decrypted !== 'string') {
71
+ throw new Error(`NIP-04 decryption failed: expected string result, got ${JSON.stringify(decrypted)}`);
72
+ }
73
+ return decrypted;
74
+ },
75
+ };
76
+ }
77
+
78
+ get nip44(): NostrSigner['nip44'] {
79
+ const nostr = this.nostr;
80
+ if (!nostr.nip44) {
81
+ return undefined;
82
+ }
83
+ return {
84
+ encrypt: async (pubkey: string, plaintext: string): Promise<string> => {
85
+ const encrypted = await nostr.nip44!.encrypt(pubkey, plaintext);
86
+ if (typeof encrypted !== 'string') {
87
+ throw new Error(`NIP-44 encryption failed: expected string result, got ${JSON.stringify(encrypted)}`);
88
+ }
89
+ return encrypted;
90
+ },
91
+ decrypt: async (pubkey: string, ciphertext: string): Promise<string> => {
92
+ const decrypted = await nostr.nip44!.decrypt(pubkey, ciphertext);
93
+ if (typeof decrypted !== 'string') {
94
+ throw new Error(`NIP-44 decryption failed: expected string result, got ${JSON.stringify(decrypted)}`);
95
+ }
96
+ return decrypted;
97
+ },
98
+ };
99
+ }
100
+ }
package/dist/NCache.js ADDED
@@ -0,0 +1,39 @@
1
+ import { LRUCache } from "lru-cache";
2
+ import { matchFilters } from "nostr-tools";
3
+ import { NSet } from "./NSet.js";
4
+ class NCache extends NSet {
5
+ constructor(...args) {
6
+ super(new LRUCache(...args));
7
+ }
8
+ async event(event) {
9
+ this.add(event);
10
+ }
11
+ async query(filters) {
12
+ const events = [];
13
+ for (const event of this) {
14
+ if (matchFilters(filters, event)) {
15
+ this.cache.get(event.id);
16
+ events.push(event);
17
+ }
18
+ }
19
+ return events;
20
+ }
21
+ async remove(filters) {
22
+ for (const event of this) {
23
+ if (matchFilters(filters, event)) {
24
+ this.delete(event);
25
+ }
26
+ }
27
+ }
28
+ async count(filters) {
29
+ const events = await this.query(filters);
30
+ return {
31
+ count: events.length,
32
+ approximate: false
33
+ };
34
+ }
35
+ [Symbol.toStringTag] = "NCache";
36
+ }
37
+ export {
38
+ NCache
39
+ };
package/dist/NCache.ts ADDED
@@ -0,0 +1,73 @@
1
+ // deno-lint-ignore-file require-await
2
+
3
+ import type { NostrEvent, NostrFilter, NostrRelayCOUNT, NStore } from '@nostrify/types';
4
+ import { LRUCache } from 'lru-cache';
5
+ import { matchFilters } from 'nostr-tools';
6
+
7
+ import { NSet } from './NSet.ts';
8
+
9
+ /**
10
+ * Nostr LRU cache based on [`npm:lru-cache`](https://www.npmjs.com/package/lru-cache).
11
+ * It implements both `NStore` and `NSet` interfaces.
12
+ *
13
+ * ```ts
14
+ * // Accepts the options of `npm:lru-cache`:
15
+ * const cache = new NCache({ max: 1000 });
16
+ *
17
+ * // Events can be added like a regular `Set`:
18
+ * cache.add(event1);
19
+ * cache.add(event2);
20
+ *
21
+ * // Can be queried like `NStore`:
22
+ * const events = await cache.query([{ kinds: [1] }]);
23
+ *
24
+ * // Can be iterated like `NSet`:
25
+ * for (const event of cache) {
26
+ * console.log(event);
27
+ * }
28
+ * ```
29
+ */
30
+ class NCache extends NSet implements NStore {
31
+ constructor(
32
+ ...args: ConstructorParameters<typeof LRUCache<string, NostrEvent>>
33
+ ) {
34
+ super(new LRUCache<string, NostrEvent>(...args) as Map<string, NostrEvent>);
35
+ }
36
+
37
+ async event(event: NostrEvent): Promise<void> {
38
+ this.add(event);
39
+ }
40
+
41
+ async query(filters: NostrFilter[]): Promise<NostrEvent[]> {
42
+ const events: NostrEvent[] = [];
43
+
44
+ for (const event of this) {
45
+ if (matchFilters(filters, event)) {
46
+ this.cache.get(event.id);
47
+ events.push(event);
48
+ }
49
+ }
50
+
51
+ return events;
52
+ }
53
+
54
+ async remove(filters: NostrFilter[]): Promise<void> {
55
+ for (const event of this) {
56
+ if (matchFilters(filters, event)) {
57
+ this.delete(event);
58
+ }
59
+ }
60
+ }
61
+
62
+ async count(filters: NostrFilter[]): Promise<NostrRelayCOUNT[2]> {
63
+ const events = await this.query(filters);
64
+ return {
65
+ count: events.length,
66
+ approximate: false,
67
+ };
68
+ }
69
+
70
+ override [Symbol.toStringTag] = 'NCache';
71
+ }
72
+
73
+ export { NCache };