@candypoets/nipworker 0.0.1

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 (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +343 -0
  3. package/dist/assets/worker-Yodgjxhx.js +3 -0
  4. package/dist/assets/worker-Yodgjxhx.js.map +1 -0
  5. package/dist/hooks.d.ts +5 -0
  6. package/dist/hooks.d.ts.map +1 -0
  7. package/dist/hooks.js +1 -0
  8. package/dist/hooks.js.map +1 -0
  9. package/dist/index.d.ts +87 -0
  10. package/dist/index.d.ts.map +1 -0
  11. package/dist/index.js +1 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/index2.js +1 -0
  14. package/dist/index2.js.map +1 -0
  15. package/dist/index3.js +1 -0
  16. package/dist/index3.js.map +1 -0
  17. package/dist/lib/sharedBuffer.d.ts +50 -0
  18. package/dist/lib/sharedBuffer.d.ts.map +1 -0
  19. package/dist/types/index.d.ts +53 -0
  20. package/dist/types/index.d.ts.map +1 -0
  21. package/dist/types/kind0.d.ts +24 -0
  22. package/dist/types/kind0.d.ts.map +1 -0
  23. package/dist/types/kind1.d.ts +17 -0
  24. package/dist/types/kind1.d.ts.map +1 -0
  25. package/dist/types/kind10002.d.ts +7 -0
  26. package/dist/types/kind10002.d.ts.map +1 -0
  27. package/dist/types/kind10019.d.ts +9 -0
  28. package/dist/types/kind10019.d.ts.map +1 -0
  29. package/dist/types/kind17.d.ts +4 -0
  30. package/dist/types/kind17.d.ts.map +1 -0
  31. package/dist/types/kind17375.d.ts +7 -0
  32. package/dist/types/kind17375.d.ts.map +1 -0
  33. package/dist/types/kind3.d.ts +7 -0
  34. package/dist/types/kind3.d.ts.map +1 -0
  35. package/dist/types/kind39089.d.ts +11 -0
  36. package/dist/types/kind39089.d.ts.map +1 -0
  37. package/dist/types/kind4.d.ts +9 -0
  38. package/dist/types/kind4.d.ts.map +1 -0
  39. package/dist/types/kind6.d.ts +6 -0
  40. package/dist/types/kind6.d.ts.map +1 -0
  41. package/dist/types/kind7.d.ts +18 -0
  42. package/dist/types/kind7.d.ts.map +1 -0
  43. package/dist/types/kind7374.d.ts +6 -0
  44. package/dist/types/kind7374.d.ts.map +1 -0
  45. package/dist/types/kind7375.d.ts +9 -0
  46. package/dist/types/kind7375.d.ts.map +1 -0
  47. package/dist/types/kind7376.d.ts +16 -0
  48. package/dist/types/kind7376.d.ts.map +1 -0
  49. package/dist/types/kind9321.d.ts +11 -0
  50. package/dist/types/kind9321.d.ts.map +1 -0
  51. package/dist/types/kind9735.d.ts +15 -0
  52. package/dist/types/kind9735.d.ts.map +1 -0
  53. package/dist/types/proofs.d.ts +21 -0
  54. package/dist/types/proofs.d.ts.map +1 -0
  55. package/dist/utils.d.ts +23 -0
  56. package/dist/utils.d.ts.map +1 -0
  57. package/dist/utils.js +1 -0
  58. package/dist/utils.js.map +1 -0
  59. package/package.json +72 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Thibaut Duchene
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 ADDED
@@ -0,0 +1,343 @@
1
+ # @candypoets/nipworker
2
+
3
+ A high-performance Nostr client library with worker-based architecture using Rust WebAssembly for optimal performance and non-blocking operations.
4
+
5
+ [![npm version](https://badge.fury.io/js/@candypoets%2Fnipworker.svg)](https://badge.fury.io/js/@candypoets%2Fnipworker)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ ![NipWorker](static/nipworker.jpg)
9
+
10
+ ## 🚀 Features
11
+
12
+ - **High Performance**: Rust WASM core for cryptographic operations and message processing
13
+ - **Worker-Based Architecture**: Non-blocking operations using Web Workers
14
+ - **TypeScript Support**: Full TypeScript definitions included
15
+ - **Dual Module Support**: Both ES modules and UMD builds
16
+ - **Efficient Serialization**: Uses MessagePack for optimal data transfer
17
+ - **Comprehensive NIP Support**: Implements 12+ standard Nostr Implementation Possibilities (NIPs)
18
+
19
+
20
+ ## 📋 Supported NIPs
21
+
22
+ | NIP | Name | Description | Event Kinds | Status |
23
+ |-----|------|-------------|-------------|---------|
24
+ | [NIP-01](https://github.com/nostr-protocol/nips/blob/master/01.md) | Basic Protocol | Core protocol flow description | 0, 1 | ✅ Full |
25
+ | [NIP-02](https://github.com/nostr-protocol/nips/blob/master/02.md) | Contact List | Contact lists and petnames | 3 | ✅ Full |
26
+ | [NIP-04](https://github.com/nostr-protocol/nips/blob/master/04.md) | Encrypted DMs | Encrypted direct messages | 4 | ✅ Full |
27
+ | [NIP-05](https://github.com/nostr-protocol/nips/blob/master/05.md) | DNS Identifiers | Mapping keys to DNS identifiers | - | 🔄 Partial |
28
+ | [NIP-10](https://github.com/nostr-protocol/nips/blob/master/10.md) | Text Note References | Threading and replies | - | ✅ Full |
29
+ | [NIP-18](https://github.com/nostr-protocol/nips/blob/master/18.md) | Reposts | Event reposts | 6 | ✅ Full |
30
+ | [NIP-19](https://github.com/nostr-protocol/nips/blob/master/19.md) | bech32 Entities | npub, note, nevent, nprofile encoding | - | ✅ Full |
31
+ | [NIP-25](https://github.com/nostr-protocol/nips/blob/master/25.md) | Reactions | Event reactions and emoji | 7 | ✅ Full |
32
+ | [NIP-27](https://github.com/nostr-protocol/nips/blob/master/27.md) | Text References | Mentions and references in content | - | ✅ Full |
33
+ | [NIP-44](https://github.com/nostr-protocol/nips/blob/master/44.md) | Versioned Encryption | Advanced encryption for private events | - | ✅ Full |
34
+ | [NIP-51](https://github.com/nostr-protocol/nips/blob/master/51.md) | Lists | Categorized lists (people, bookmarks) | 39089 | ✅ Full |
35
+ | [NIP-57](https://github.com/nostr-protocol/nips/blob/master/57.md) | Lightning Zaps | Bitcoin Lightning Network integration | 9735 | ✅ Full |
36
+ | [NIP-60](https://github.com/nostr-protocol/nips/blob/master/60.md) | Cashu Wallet | Cashu ecash wallet functionality | 7374, 7375, 7376, 10019, 17375 | ✅ Full |
37
+ | [NIP-61](https://github.com/nostr-protocol/nips/blob/master/61.md) | Nutzaps | Cashu token zaps | 9321 | ✅ Full |
38
+ | [NIP-65](https://github.com/nostr-protocol/nips/blob/master/65.md) | Relay Lists | User relay preferences | 10002 | ✅ Full |
39
+
40
+ ### NIP-60 Cashu Wallet Events
41
+
42
+ The library provides complete support for **NIP-60 Cashu wallet functionality**:
43
+
44
+ | Event Kind | Description | Encryption | Purpose |
45
+ |------------|-------------|------------|---------|
46
+ | 7374 | Quote events | NIP-44 | Token redemption quotes |
47
+ | 7375 | Token events | NIP-44 | Cashu proofs and tokens |
48
+ | 7376 | Spending history | NIP-44 | Transaction history |
49
+ | 9321 | Nutzaps | - | Cashu token zaps |
50
+ | 10019 | Wallet settings | - | User wallet preferences |
51
+ | 17375 | Encrypted wallets | NIP-44 | Private wallet data |
52
+
53
+
54
+ ### Legend
55
+ - ✅ **Full**: Complete implementation with all features
56
+ - 🔄 **Partial**: Basic support, some features may be limited
57
+ - ❌ **Not Supported**: NIP is not implemented
58
+
59
+ ## 📦 Installation
60
+
61
+ ```bash
62
+ npm install @candypoets/nipworker
63
+ ```
64
+
65
+ That's it! No additional dependencies needed.
66
+
67
+ ## 🔧 Usage
68
+
69
+ ### Import Structure
70
+
71
+ The library is organized into logical modules for better tree-shaking:
72
+
73
+ ```javascript
74
+ // Core API - main functionality
75
+ import { nostrManager, createNostrManager } from '@candypoets/nipworker';
76
+
77
+ // Hooks - React-style subscription hooks
78
+ import { useSubscription } from '@candypoets/nipworker/hooks';
79
+
80
+ // Types - TypeScript type definitions
81
+ import type {
82
+ ParsedEvent,
83
+ Kind1Parsed,
84
+ NostrManagerConfig,
85
+ Request
86
+ } from '@candypoets/nipworker/types';
87
+
88
+ // Utils - type guards and helper functions
89
+ import { isKind1, isKind0, SignerTypes } from '@candypoets/nipworker/utils';
90
+ ```
91
+
92
+ ### Basic Usage
93
+
94
+ ```javascript
95
+ import { nostrManager } from '@candypoets/nipworker';
96
+ import { useSubscription } from '@candypoets/nipworker/hooks';
97
+
98
+ // Login by setting up a signer (private key)
99
+ nostrManager.setSigner('privkey', 'your-private-key-hex');
100
+
101
+ // Subscribe to events using the hook
102
+ const unsubscribe = useSubscription(
103
+ 'my-subscription',
104
+ [{
105
+ kinds: [1], // Text notes
106
+ limit: 10
107
+ }],
108
+ (events, eventType) => {
109
+ if (eventType === 'EVENTS') {
110
+ console.log('Received events:', events);
111
+ } else if (eventType === 'EOSE') {
112
+ console.log('End of stored events');
113
+ }
114
+ },
115
+ { closeOnEose: false }
116
+ );
117
+
118
+ // Publish an event
119
+ const event = {
120
+ kind: 1,
121
+ content: 'Hello Nostr!',
122
+ tags: [],
123
+ created_at: Math.floor(Date.now() / 1000)
124
+ };
125
+
126
+ nostrManager.publish('publish-id-123', event, (status, type) => {
127
+ console.log('Publish status:', status);
128
+ });
129
+
130
+ // Clean up subscription when done
131
+ // unsubscribe();
132
+ ```
133
+
134
+ ### Advanced Usage
135
+
136
+ ```javascript
137
+ import { createNostrManager } from '@candypoets/nipworker';
138
+ import { useSubscription } from '@candypoets/nipworker/hooks';
139
+
140
+ // Create a custom manager instance with configuration
141
+ const customManager = createNostrManager();
142
+
143
+ // Set up multiple signers
144
+ customManager.setSigner('privkey', 'main-private-key-hex');
145
+
146
+ // Subscribe to specific authors with multiple filters
147
+ const unsubscribe = useSubscription(
148
+ 'author-feed',
149
+ [
150
+ {
151
+ kinds: [1, 6, 7],
152
+ authors: ['pubkey1', 'pubkey2'],
153
+ since: Math.floor(Date.now() / 1000) - 3600, // Last hour
154
+ limit: 50
155
+ },
156
+ {
157
+ kinds: [30023], // Long-form content
158
+ authors: ['pubkey1'],
159
+ limit: 10
160
+ }
161
+ ],
162
+ (events, eventType) => {
163
+ if (eventType === 'EVENTS') {
164
+ events.forEach(event => {
165
+ switch (event.kind) {
166
+ case 1:
167
+ console.log('Text note:', event.content);
168
+ break;
169
+ case 6:
170
+ console.log('Repost:', event);
171
+ break;
172
+ case 7:
173
+ console.log('Reaction:', event);
174
+ break;
175
+ case 30023:
176
+ console.log('Long-form content:', event);
177
+ break;
178
+ }
179
+ });
180
+ } else if (eventType === 'EOSE') {
181
+ console.log('End of stored events for author feed');
182
+ }
183
+ },
184
+ { closeOnEose: false }
185
+ );
186
+
187
+ // Global publish status monitoring
188
+ customManager.addPublishCallbackAll((status, eventId) => {
189
+ console.log(`Event ${eventId} status:`, status);
190
+ });
191
+
192
+ // Clean up when done
193
+ setTimeout(() => {
194
+ unsubscribe();
195
+ customManager.cleanup();
196
+ }, 60000);
197
+ ```
198
+
199
+ ## 🏗️ Architecture
200
+
201
+ NipWorker uses a multi-layered architecture:
202
+
203
+ 1. **Main Thread**: Your application code
204
+ 2. **Web Worker**: Handles network operations and message routing
205
+ 3. **Rust WASM Core**: Performs cryptographic operations and message validation
206
+
207
+ This architecture ensures that heavy operations don't block your main thread, providing a smooth user experience.
208
+
209
+ ## 📚 API Reference
210
+
211
+ ### NostrManager
212
+
213
+ #### Factory Function
214
+
215
+ ```typescript
216
+ createNostrManager(config?: NostrManagerConfig): NostrManager
217
+ ```
218
+
219
+ #### Methods
220
+
221
+ - `setSigner(name: string, secretKeyHex: string): void` - Set up a signer for publishing
222
+ - `subscribe(subscriptionId: string, requests: Request[], options?: SubscriptionOptions): SharedArrayBuffer` - Subscribe to events
223
+ - `publish(publishId: string, event: NostrEvent, callback?: PublishCallback): void` - Publish an event
224
+ - `signEvent(event: NostrEvent): void` - Sign an event without publishing
225
+ - `getPublicKey(): void` - Get the public key of the current signer
226
+ - `unsubscribe(subscriptionId: string): void` - Unsubscribe from events
227
+ - `cleanup(): void` - Clean up unused subscriptions
228
+ - `addPublishCallbackAll(callback: Function): void` - Monitor all publish statuses
229
+
230
+ ### useSubscription Hook
231
+
232
+ ```typescript
233
+ useSubscription(
234
+ subId: string,
235
+ requests: Request[],
236
+ callback: SubscriptionCallback,
237
+ options?: SubscriptionOptions
238
+ ): () => void
239
+ ```
240
+
241
+ #### Parameters
242
+
243
+ - `subId` - Unique subscription identifier
244
+ - `requests` - Array of Nostr filter objects
245
+ - `callback` - Function called when events are received
246
+ - `options` - Subscription options (closeOnEose, skipCache, force)
247
+
248
+ #### Returns
249
+
250
+ - Function to unsubscribe and clean up resources
251
+
252
+ ### Event Types
253
+
254
+ - `EVENTS` - New events received
255
+ - `EOSE` - End of stored events
256
+ - `EOCE` - End of cached events
257
+ - `PUBLISH_STATUS` - Status update for published events
258
+
259
+ ## 🛠️ Development
260
+
261
+ ### Prerequisites
262
+
263
+ - Node.js 18+
264
+ - Rust 1.70+
265
+ - wasm-pack
266
+
267
+ ### Building from Source
268
+
269
+ ```bash
270
+ # Clone the repository
271
+ git clone https://github.com/candypoets/nipworker.git
272
+ cd nipworker
273
+
274
+ # Install dependencies
275
+ npm install
276
+
277
+ # Build WASM modules
278
+ npm run build:wasm
279
+
280
+ # Build the library
281
+ npm run build
282
+ ```
283
+
284
+ ### Scripts
285
+
286
+ - `npm run dev` - Start development server
287
+ - `npm run build` - Build for production
288
+ - `npm run build:wasm` - Build WASM modules only
289
+ - `npm run build:types` - Generate TypeScript declarations
290
+ - `npm run clean` - Clean build artifacts
291
+
292
+ ## 🤝 Contributing
293
+
294
+ We welcome contributions! Please see our [Contributing Guidelines](CONTRIBUTING.md) for details.
295
+
296
+ ### Development Workflow
297
+
298
+ 1. Fork the repository
299
+ 2. Create a feature branch: `git checkout -b feature/amazing-feature`
300
+ 3. Make your changes
301
+ 4. Add tests if applicable
302
+ 5. Commit your changes: `git commit -m 'Add amazing feature'`
303
+ 6. Push to the branch: `git push origin feature/amazing-feature`
304
+ 7. Open a Pull Request
305
+
306
+ ## 📄 License
307
+
308
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
309
+
310
+ ## 🔗 Links
311
+
312
+ - [GitHub Repository](https://github.com/candypoets/nipworker)
313
+ - [Issue Tracker](https://github.com/candypoets/nipworker/issues)
314
+ - [Nostr Protocol](https://github.com/nostr-protocol/nostr)
315
+ - [NIPs Repository](https://github.com/nostr-protocol/nips)
316
+
317
+ ## 🙏 Acknowledgments
318
+
319
+ - [Nostr Protocol](https://nostr.com/) - The decentralized social protocol
320
+ - [nostr-tools](https://github.com/nbd-wtf/nostr-tools) - Essential Nostr utilities
321
+ - [wasm-pack](https://rustwasm.github.io/wasm-pack/) - Rust to WebAssembly workflow
322
+
323
+ ## 📊 Performance
324
+
325
+ NipWorker is designed for high performance:
326
+
327
+ - **Fast Event Processing**: Rust WASM core processes events up to 10x faster than pure JavaScript
328
+ - **Non-blocking Operations**: Web Worker architecture prevents UI freezing
329
+ - **Efficient Serialization**: MessagePack reduces bandwidth usage by ~30%
330
+ - **Connection Pooling**: Intelligent relay connection management
331
+
332
+
333
+
334
+ ## 🔒 Security
335
+
336
+ - All cryptographic operations are handled by the Rust WASM core
337
+ - Private keys never leave the worker thread
338
+ - Event validation is performed at the WASM level
339
+ - Secure random number generation for key operations
340
+
341
+ ---
342
+
343
+ Made with ❤️ by [So Tachi](mailto:sotachi@proton.me)