@bzzsocial/ndk 0.0.0-stage → 3.0.3-bzz.7

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 (245) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +104 -2
  3. package/dist/index.d.mts +7392 -0
  4. package/dist/index.d.ts +7392 -0
  5. package/dist/index.js +15372 -0
  6. package/dist/index.mjs +15195 -0
  7. package/dist/test/index.d.mts +4584 -0
  8. package/dist/test/index.d.ts +4584 -0
  9. package/dist/test/index.js +13744 -0
  10. package/dist/test/index.mjs +13712 -0
  11. package/dist/workers/sig-verification.d.mts +2 -0
  12. package/dist/workers/sig-verification.d.ts +2 -0
  13. package/dist/workers/sig-verification.js +38 -0
  14. package/dist/workers/sig-verification.mjs +36 -0
  15. package/package.json +72 -4
  16. package/src/ai-guardrails/event/publishing.test.ts +176 -0
  17. package/src/ai-guardrails/event/signing.test.ts +124 -0
  18. package/src/ai-guardrails/event/signing.ts +225 -0
  19. package/src/ai-guardrails/index.ts +364 -0
  20. package/src/ai-guardrails/ndk/fetch-events.test.ts +257 -0
  21. package/src/ai-guardrails/ndk/fetch-events.ts +166 -0
  22. package/src/ai-guardrails/ndk.test.ts +64 -0
  23. package/src/ai-guardrails/ndk.ts +34 -0
  24. package/src/ai-guardrails/subscription/deprecated-handlers.test.ts +105 -0
  25. package/src/ai-guardrails/subscription/deprecated-handlers.ts +75 -0
  26. package/src/ai-guardrails/types.ts +69 -0
  27. package/src/app-settings/index.ts +71 -0
  28. package/src/cache/filter.test.ts +139 -0
  29. package/src/cache/filter.ts +78 -0
  30. package/src/cache/index.ts +474 -0
  31. package/src/count/index.test.ts +247 -0
  32. package/src/count/index.ts +213 -0
  33. package/src/dvm/schedule.ts +132 -0
  34. package/src/events/content-tagger.test.ts +284 -0
  35. package/src/events/content-tagger.ts +249 -0
  36. package/src/events/dedup.ts +14 -0
  37. package/src/events/encode.test.ts +35 -0
  38. package/src/events/encryption.test.ts +484 -0
  39. package/src/events/encryption.ts +106 -0
  40. package/src/events/fetch-tagged-event.ts +44 -0
  41. package/src/events/gift-wrapping.test.ts +139 -0
  42. package/src/events/gift-wrapping.ts +155 -0
  43. package/src/events/index.test.ts +863 -0
  44. package/src/events/index.ts +1130 -0
  45. package/src/events/kind.ts +20 -0
  46. package/src/events/kinds/article.ts +152 -0
  47. package/src/events/kinds/blossom-list.ts +91 -0
  48. package/src/events/kinds/cashu/fedimint.ts +95 -0
  49. package/src/events/kinds/cashu/mint-recommendation.ts +95 -0
  50. package/src/events/kinds/cashu/mint.ts +93 -0
  51. package/src/events/kinds/cashu/token.ts +155 -0
  52. package/src/events/kinds/cashu/tx.test.ts +44 -0
  53. package/src/events/kinds/cashu/tx.ts +190 -0
  54. package/src/events/kinds/classified.ts +182 -0
  55. package/src/events/kinds/collaborative.test.ts +453 -0
  56. package/src/events/kinds/collaborative.ts +346 -0
  57. package/src/events/kinds/drafts.ts +152 -0
  58. package/src/events/kinds/dvm/NDKTranscriptionDVM.ts +69 -0
  59. package/src/events/kinds/dvm/feedback.ts +51 -0
  60. package/src/events/kinds/dvm/index.ts +6 -0
  61. package/src/events/kinds/dvm/request.ts +131 -0
  62. package/src/events/kinds/dvm/result.ts +93 -0
  63. package/src/events/kinds/follow-pack.test.ts +169 -0
  64. package/src/events/kinds/follow-pack.ts +123 -0
  65. package/src/events/kinds/highlight.ts +116 -0
  66. package/src/events/kinds/image.ts +51 -0
  67. package/src/events/kinds/index.ts +224 -0
  68. package/src/events/kinds/interest-list.test.ts +171 -0
  69. package/src/events/kinds/interest-list.ts +82 -0
  70. package/src/events/kinds/lists/index.test.ts +51 -0
  71. package/src/events/kinds/lists/index.ts +415 -0
  72. package/src/events/kinds/nip89/app-handler.test.ts +106 -0
  73. package/src/events/kinds/nip89/app-handler.ts +83 -0
  74. package/src/events/kinds/nutzap/index.test.ts +192 -0
  75. package/src/events/kinds/nutzap/index.ts +384 -0
  76. package/src/events/kinds/nutzap/mint-list.ts +76 -0
  77. package/src/events/kinds/nutzap/proof.ts +18 -0
  78. package/src/events/kinds/nutzap/validation.test.ts +340 -0
  79. package/src/events/kinds/nutzap/validation.ts +79 -0
  80. package/src/events/kinds/p2p-order.test.ts +359 -0
  81. package/src/events/kinds/p2p-order.ts +469 -0
  82. package/src/events/kinds/project-template.ts +132 -0
  83. package/src/events/kinds/project.ts +114 -0
  84. package/src/events/kinds/relay-feed-list.example.ts +86 -0
  85. package/src/events/kinds/relay-feed-list.test.ts +97 -0
  86. package/src/events/kinds/relay-feed-list.ts +108 -0
  87. package/src/events/kinds/relay-list.ts +118 -0
  88. package/src/events/kinds/repost.ts +67 -0
  89. package/src/events/kinds/simple-group/index.ts +234 -0
  90. package/src/events/kinds/simple-group/member-list.ts +43 -0
  91. package/src/events/kinds/simple-group/metadata.ts +64 -0
  92. package/src/events/kinds/story.test.ts +141 -0
  93. package/src/events/kinds/story.ts +458 -0
  94. package/src/events/kinds/subscriptions/amount.ts +61 -0
  95. package/src/events/kinds/subscriptions/receipt.ts +148 -0
  96. package/src/events/kinds/subscriptions/subscription-start.ts +149 -0
  97. package/src/events/kinds/subscriptions/tier.test.ts +76 -0
  98. package/src/events/kinds/subscriptions/tier.ts +121 -0
  99. package/src/events/kinds/task.ts +42 -0
  100. package/src/events/kinds/thread.ts +45 -0
  101. package/src/events/kinds/video.ts +140 -0
  102. package/src/events/kinds/voice-message.ts +257 -0
  103. package/src/events/kinds/wiki.ts +88 -0
  104. package/src/events/kinds/zap.ts +115 -0
  105. package/src/events/nip10.test.ts +108 -0
  106. package/src/events/nip10.ts +75 -0
  107. package/src/events/nip19.test.ts +49 -0
  108. package/src/events/nip19.ts +36 -0
  109. package/src/events/nip73.ts +13 -0
  110. package/src/events/repost.test.ts +40 -0
  111. package/src/events/repost.ts +44 -0
  112. package/src/events/serializer.test.ts +174 -0
  113. package/src/events/serializer.ts +120 -0
  114. package/src/events/signature.ts +97 -0
  115. package/src/events/signed-event-types.test.ts +149 -0
  116. package/src/events/validation.ts +113 -0
  117. package/src/events/wrap.test.ts +164 -0
  118. package/src/events/wrap.ts +128 -0
  119. package/src/index.ts +103 -0
  120. package/src/light-bolt11-decoder.d.ts +4 -0
  121. package/src/ndk/active-user.test.ts +89 -0
  122. package/src/ndk/active-user.ts +41 -0
  123. package/src/ndk/entity.ts +31 -0
  124. package/src/ndk/fetch-event-from-tag.ts +156 -0
  125. package/src/ndk/fetchEvent-guardrails.test.ts +208 -0
  126. package/src/ndk/fetchEvent-invalid-filter.test.ts +80 -0
  127. package/src/ndk/fetchUser.test.ts +149 -0
  128. package/src/ndk/guardrailOff.test.ts +75 -0
  129. package/src/ndk/index.ts +1249 -0
  130. package/src/ndk/nip19.test.ts +122 -0
  131. package/src/ndk/queue/index.ts +89 -0
  132. package/src/nip19/index.ts +5 -0
  133. package/src/nip49/index.test.ts +100 -0
  134. package/src/nip49/index.ts +5 -0
  135. package/src/outbox/index.ts +128 -0
  136. package/src/outbox/read/with-authors.ts +24 -0
  137. package/src/outbox/relay-ranking.ts +27 -0
  138. package/src/outbox/tracker.test.ts +68 -0
  139. package/src/outbox/tracker.ts +185 -0
  140. package/src/outbox/write.ts +37 -0
  141. package/src/relay/auth-on-demand.test.ts +197 -0
  142. package/src/relay/auth-policies.test.ts +19 -0
  143. package/src/relay/auth-policies.ts +109 -0
  144. package/src/relay/auth-retry.test.ts +241 -0
  145. package/src/relay/connections.test.ts +231 -0
  146. package/src/relay/connectivity.test.ts +370 -0
  147. package/src/relay/connectivity.ts +1106 -0
  148. package/src/relay/index.test.ts +167 -0
  149. package/src/relay/index.ts +446 -0
  150. package/src/relay/keepalive-bun.test.ts +86 -0
  151. package/src/relay/keepalive.test.ts +196 -0
  152. package/src/relay/keepalive.ts +118 -0
  153. package/src/relay/nip11.test.ts +196 -0
  154. package/src/relay/nip11.ts +85 -0
  155. package/src/relay/nip42.test.ts +269 -0
  156. package/src/relay/pool/index.test.ts +183 -0
  157. package/src/relay/pool/index.ts +537 -0
  158. package/src/relay/pool/system-disconnection.test.ts +280 -0
  159. package/src/relay/publisher.ts +114 -0
  160. package/src/relay/reconnection-backoff.test.ts +92 -0
  161. package/src/relay/reconnection-integration.bun.test.ts +112 -0
  162. package/src/relay/score.ts +2 -0
  163. package/src/relay/sets/calculate.test.ts +220 -0
  164. package/src/relay/sets/calculate.ts +203 -0
  165. package/src/relay/sets/index.ts +416 -0
  166. package/src/relay/sets/publish.test.ts +231 -0
  167. package/src/relay/sets/utils.ts +29 -0
  168. package/src/relay/signature-verification-stats.ts +150 -0
  169. package/src/relay/signature-verification.test.ts +192 -0
  170. package/src/relay/simple-verification.test.ts +13 -0
  171. package/src/relay/sub-manager.ts +81 -0
  172. package/src/relay/subscription.test.ts +166 -0
  173. package/src/relay/subscription.ts +542 -0
  174. package/src/relay/validation-ratio.test.ts +37 -0
  175. package/src/signers/deserialization.ts +39 -0
  176. package/src/signers/index.ts +97 -0
  177. package/src/signers/nip07/index.test.ts +32 -0
  178. package/src/signers/nip07/index.ts +302 -0
  179. package/src/signers/nip46/backend/connect.ts +41 -0
  180. package/src/signers/nip46/backend/get-public-key.ts +12 -0
  181. package/src/signers/nip46/backend/index.test.ts +340 -0
  182. package/src/signers/nip46/backend/index.ts +232 -0
  183. package/src/signers/nip46/backend/nip04-decrypt.ts +39 -0
  184. package/src/signers/nip46/backend/nip04-encrypt.ts +39 -0
  185. package/src/signers/nip46/backend/nip44-decrypt.ts +39 -0
  186. package/src/signers/nip46/backend/nip44-encrypt.ts +39 -0
  187. package/src/signers/nip46/backend/ping.ts +25 -0
  188. package/src/signers/nip46/backend/sign-event.ts +48 -0
  189. package/src/signers/nip46/backend/switch-relays.ts +28 -0
  190. package/src/signers/nip46/index.test.ts +560 -0
  191. package/src/signers/nip46/index.ts +640 -0
  192. package/src/signers/nip46/nostrconnect.ts +50 -0
  193. package/src/signers/nip46/restore.test.ts +44 -0
  194. package/src/signers/nip46/rpc.ts +221 -0
  195. package/src/signers/private-key/index.test.ts +151 -0
  196. package/src/signers/private-key/index.ts +241 -0
  197. package/src/signers/registry.ts +14 -0
  198. package/src/signers/serialization.test.ts +212 -0
  199. package/src/signers/types.ts +7 -0
  200. package/src/subscription/eose.test.ts +92 -0
  201. package/src/subscription/event-dedup.test.ts +445 -0
  202. package/src/subscription/exclusive-relay.test.ts +444 -0
  203. package/src/subscription/future-timestamp-grace.test.ts +276 -0
  204. package/src/subscription/grouping.test.ts +71 -0
  205. package/src/subscription/grouping.ts +76 -0
  206. package/src/subscription/index.test.ts +113 -0
  207. package/src/subscription/index.ts +1136 -0
  208. package/src/subscription/manager.test.ts +64 -0
  209. package/src/subscription/manager.ts +121 -0
  210. package/src/subscription/outbox-late-arrival.test.ts +172 -0
  211. package/src/subscription/utils/format-filters.ts +57 -0
  212. package/src/subscription/utils.test.ts +91 -0
  213. package/src/subscription/utils.ts +276 -0
  214. package/src/subscription/verify-before-emit.test.ts +108 -0
  215. package/src/subscription.test.ts +175 -0
  216. package/src/test-relay-autoconnect.ts +133 -0
  217. package/src/thread/index.test.ts +267 -0
  218. package/src/thread/index.ts +242 -0
  219. package/src/types.ts +45 -0
  220. package/src/user/follows.test.ts +58 -0
  221. package/src/user/follows.ts +45 -0
  222. package/src/user/index.test.ts +365 -0
  223. package/src/user/index.ts +465 -0
  224. package/src/user/nip05.test.ts +39 -0
  225. package/src/user/nip05.ts +107 -0
  226. package/src/user/pin.ts +45 -0
  227. package/src/user/profile.ts +112 -0
  228. package/src/utils/filter-validation.test.ts +925 -0
  229. package/src/utils/filter-validation.ts +436 -0
  230. package/src/utils/filter.ts +41 -0
  231. package/src/utils/get-users-relay-list.ts +171 -0
  232. package/src/utils/imeta.test.ts +100 -0
  233. package/src/utils/imeta.ts +74 -0
  234. package/src/utils/index.ts +5 -0
  235. package/src/utils/normalize-url.ts +352 -0
  236. package/src/utils/timeout.ts +15 -0
  237. package/src/utils/validation.ts +86 -0
  238. package/src/workers/sig-verification.ts +54 -0
  239. package/src/zap/invoice.ts +111 -0
  240. package/src/zapper/index.test.ts +151 -0
  241. package/src/zapper/index.ts +701 -0
  242. package/src/zapper/ln.ts +84 -0
  243. package/src/zapper/nip57.test.ts +329 -0
  244. package/src/zapper/nip57.ts +85 -0
  245. package/src/zapper/nip61.ts +57 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Pablo Fernandez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,105 @@
1
- # Temporary Holding Version
1
+ # @nostr-dev-kit/ndk
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm version](https://img.shields.io/npm/v/@nostr-dev-kit/ndk.svg)](https://www.npmjs.com/package/@nostr-dev-kit/ndk)
4
+ [![Build Status](https://github.com/nostr-dev-kit/ndk/actions/workflows/deploy.yml/badge.svg)](https://github.com/nostr-dev-kit/ndk/actions)
5
+ [![AI Guardrails Included](https://img.shields.io/badge/AI%20Guardrails-Included-brightgreen)](https://github.com/nostr-dev-kit/ndk/tree/master/ndk/src/ai-guardrails)
6
+
7
+ > NDK (Nostr Development Kit) is a TypeScript/JavaScript library that simplifies building Nostr clients, relays, and related applications.
8
+
9
+ ## ⚠️ Important: Enable AI Guardrails
10
+
11
+ **If you're new to NDK or using an AI assistant, enable AI Guardrails:**
12
+
13
+ ```typescript
14
+ const ndk = new NDK({ aiGuardrails: true });
15
+ ```
16
+
17
+ This prevents 90% of common mistakes (bech32 in filters, missing fields, invalid formats) with clear error messages. Disable in production.
18
+
19
+ ## 🤖 For AI Assistants & New Developers
20
+
21
+ NDK includes **AI Guardrails** - runtime validation that catches common mistakes:
22
+
23
+ - Using npub/note1 in filters (must be hex)
24
+ - Missing required fields on events
25
+ - Invalid tag formats
26
+ - Performance anti-patterns
27
+
28
+ **Enable with `aiGuardrails: true`** - see Quick Start below.
29
+
30
+ ## Features
31
+
32
+ - Outbox model support
33
+ - Relay connection pool with automatic reconnection and failover
34
+ - Flexible subscription API with caching, batching, and auto-closing
35
+ - Event creation, validation, and wrappers for major NIPs (e.g., NIP-01, NIP-04, NIP-07, NIP-18, NIP-49, NIP-57, NIP-60, NIP-61)
36
+ - Signer adapters: private key, encrypted keys (NIP-49), browser extension (NIP-07), remote signing (NIP-46)
37
+ - Pluggable cache adapters (Redis, Dexie, SQLite, etc.)
38
+ - Data Vending Machine support (NIP-90)
39
+ - Zap utilities (NIP-57, NIP-61)
40
+ - Threading, event kinds, and utility functions (URL normalization, metadata tags, filters)
41
+ - Modular design with many pluggable packages for different frameworks (Mobile, Svelte 4 and 5, React)
42
+
43
+ ## Installation
44
+
45
+ ```bash
46
+ npm install @nostr-dev-kit/ndk
47
+ # or
48
+ yarn add @nostr-dev-kit/ndk
49
+ # or
50
+ bun add @nostr-dev-kit/ndk
51
+ ```
52
+
53
+ ## Quick Start
54
+
55
+ ```typescript
56
+ import NDK, { NDKEvent, NDKPrivateKeySigner } from '@nostr-dev-kit/ndk';
57
+
58
+ async function main() {
59
+ const signer = NDKPrivateKeySigner.generate();
60
+ const ndk = new NDK({
61
+ explicitRelayUrls: ['wss://relay.primal.net'],
62
+ signer,
63
+
64
+ // ⚠️ STRONGLY RECOMMENDED: Enable during development
65
+ // Catches common mistakes before they cause silent failures
66
+ aiGuardrails: true
67
+ });
68
+
69
+ // Connect to relays
70
+ await ndk.connect();
71
+
72
+ // Publish a simple text note
73
+ const event = new NDKEvent(ndk, {
74
+ kind: 1,
75
+ content: 'Hello Nostr via NDK!',
76
+ })
77
+ await event.sign();
78
+ event.publish();
79
+
80
+ // subscribe to all event interactions
81
+ ndk.subscribe(event.filter(), { closeOnEose: false }, {
82
+ onEvent: (replyEvent: NDKEvent) => console.log(replyEvent.author.npub, "interacted with our hello world with a kind", replyEvent.kind);
83
+ })
84
+
85
+ // Subscribe to incoming text notes
86
+ const subscription = ndk.subscribe(
87
+ { kinds: [1] },
88
+ { closeOnEose: true },
89
+ {
90
+ onEvent: (evt) => console.log('Received event:', evt),
91
+ onEose: () => console.log('End of stream'),
92
+ }
93
+ );
94
+ }
95
+
96
+ main().catch(console.error);
97
+ ```
98
+
99
+ ## Documentation
100
+
101
+ Full API reference and guides are available at [https://nostr-dev-kit.github.io/ndk](https://nostr-dev-kit.github.io/ndk).
102
+
103
+ ## License
104
+
105
+ MIT