nostr-mcp-server 2.0.0

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 (36) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +498 -0
  3. package/build/__tests__/basic.test.js +87 -0
  4. package/build/__tests__/error-handling.test.js +145 -0
  5. package/build/__tests__/format-conversion.test.js +137 -0
  6. package/build/__tests__/integration.test.js +163 -0
  7. package/build/__tests__/mocks.js +109 -0
  8. package/build/__tests__/nip19-conversion.test.js +268 -0
  9. package/build/__tests__/nips-search.test.js +109 -0
  10. package/build/__tests__/note-creation.test.js +148 -0
  11. package/build/__tests__/note-tools-functions.test.js +173 -0
  12. package/build/__tests__/note-tools-unit.test.js +97 -0
  13. package/build/__tests__/profile-notes-simple.test.js +78 -0
  14. package/build/__tests__/profile-postnote.test.js +120 -0
  15. package/build/__tests__/profile-tools.test.js +90 -0
  16. package/build/__tests__/relay-specification.test.js +136 -0
  17. package/build/__tests__/search-nips-simple.test.js +96 -0
  18. package/build/__tests__/websocket-integration.test.js +257 -0
  19. package/build/__tests__/zap-tools-simple.test.js +72 -0
  20. package/build/__tests__/zap-tools-tests.test.js +197 -0
  21. package/build/index.js +1285 -0
  22. package/build/nips/nips-tools.js +567 -0
  23. package/build/nips-tools.js +421 -0
  24. package/build/note/note-tools.js +296 -0
  25. package/build/note-tools.js +53 -0
  26. package/build/profile/profile-tools.js +260 -0
  27. package/build/utils/constants.js +27 -0
  28. package/build/utils/conversion.js +332 -0
  29. package/build/utils/ephemeral-relay.js +438 -0
  30. package/build/utils/formatting.js +34 -0
  31. package/build/utils/index.js +6 -0
  32. package/build/utils/nip19-tools.js +117 -0
  33. package/build/utils/pool.js +55 -0
  34. package/build/zap/zap-tools.js +980 -0
  35. package/build/zap-tools.js +989 -0
  36. package/package.json +59 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Austin Kelsay
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,498 @@
1
+ # Nostr MCP Server
2
+
3
+ A Model Context Protocol (MCP) server that provides Nostr capabilities to LLMs like Claude.
4
+
5
+ https://github.com/user-attachments/assets/1d2d47d0-c61b-44e2-85be-5985d2a81c64
6
+
7
+ ## Features
8
+
9
+ This server implements 18 tools for interacting with the Nostr network:
10
+
11
+ ### Reading & Querying Tools
12
+ 1. `getProfile`: Fetches a user's profile information by public key
13
+ 2. `getKind1Notes`: Fetches text notes (kind 1) authored by a user
14
+ 3. `getLongFormNotes`: Fetches long-form content (kind 30023) authored by a user
15
+ 4. `getReceivedZaps`: Fetches zaps received by a user, including detailed payment information
16
+ 5. `getSentZaps`: Fetches zaps sent by a user, including detailed payment information
17
+ 6. `getAllZaps`: Fetches both sent and received zaps for a user, clearly labeled with direction and totals
18
+ 7. `searchNips`: Search through Nostr Implementation Possibilities (NIPs) with relevance scoring
19
+
20
+ ### Identity & Profile Management Tools
21
+ 8. `createKeypair`: Generate new Nostr keypairs in hex and/or npub/nsec format
22
+ 9. `createProfile`: Create a new Nostr profile (kind 0 event) with metadata
23
+ 10. `updateProfile`: Update an existing Nostr profile with new metadata
24
+
25
+ ### Note Creation & Publishing Tools
26
+ 11. `createNote`: Create unsigned kind 1 note events with specified content and tags
27
+ 12. `signNote`: Sign note events with a private key, generating cryptographically valid signatures
28
+ 13. `publishNote`: Publish signed notes to specified Nostr relays
29
+ 14. `postNote`: All-in-one authenticated note posting using an existing private key (nsec/hex)
30
+
31
+ ### Anonymous Tools
32
+ 15. `sendAnonymousZap`: Prepare an anonymous zap to a profile or event, generating a lightning invoice for payment
33
+ 16. `postAnonymousNote`: Post an anonymous note using a randomly generated one-time keypair
34
+
35
+ ### NIP-19 Entity Tools
36
+ 17. `convertNip19`: Convert between different NIP-19 entity formats (hex, npub, nsec, note, nprofile, nevent, naddr)
37
+ 18. `analyzeNip19`: Analyze and decode any NIP-19 entity to understand its type and contents
38
+
39
+ All tools fully support both hex public keys and npub format, with user-friendly display of Nostr identifiers.
40
+
41
+ ## Installation
42
+
43
+ ### Option 1: Install from npm (Recommended)
44
+
45
+ ```bash
46
+ npm install -g nostr-mcp-server
47
+ ```
48
+
49
+ ### Option 2: Install from source
50
+
51
+ ```bash
52
+ # Clone the repository
53
+ git clone https://github.com/austinkelsay/nostr-mcp-server.git
54
+ cd nostr-mcp-server
55
+
56
+ # Install dependencies
57
+ npm install
58
+
59
+ # Build the project
60
+ npm run build
61
+ ```
62
+
63
+ ## Connecting to Claude for Desktop
64
+
65
+ 1. Make sure you have [Claude for Desktop](https://claude.ai/desktop) installed and updated to the latest version.
66
+
67
+ 2. Configure Claude for Desktop by editing or creating the configuration file:
68
+
69
+ For macOS:
70
+ ```bash
71
+ vim ~/Library/Application\ Support/Claude/claude_desktop_config.json
72
+ ```
73
+
74
+ For Windows:
75
+ ```bash
76
+ notepad %AppData%\Claude\claude_desktop_config.json
77
+ ```
78
+
79
+ 3. Add the Nostr server to your configuration:
80
+
81
+ **If installed via npm:**
82
+ ```json
83
+ {
84
+ "mcpServers": {
85
+ "nostr": {
86
+ "command": "npx",
87
+ "args": [
88
+ "nostr-mcp-server"
89
+ ]
90
+ }
91
+ }
92
+ }
93
+ ```
94
+
95
+ **If installed from source:**
96
+ ```json
97
+ {
98
+ "mcpServers": {
99
+ "nostr": {
100
+ "command": "node",
101
+ "args": [
102
+ "/ABSOLUTE/PATH/TO/nostr-mcp-server/build/index.js"
103
+ ]
104
+ }
105
+ }
106
+ }
107
+ ```
108
+
109
+ For source installations, replace `/ABSOLUTE/PATH/TO/` with the actual path to your project.
110
+
111
+ 4. Restart Claude for Desktop.
112
+
113
+ ## Connecting to Cursor
114
+
115
+ 1. Make sure you have [Cursor](https://cursor.sh/) installed and updated to the latest version.
116
+
117
+ 2. Configure Cursor by creating or editing the configuration file:
118
+
119
+ For macOS:
120
+ ```bash
121
+ vim ~/.cursor/config.json
122
+ ```
123
+
124
+ For Windows:
125
+ ```bash
126
+ notepad %USERPROFILE%\.cursor\config.json
127
+ ```
128
+
129
+ 3. Add the Nostr server to your configuration:
130
+
131
+ **If installed via npm:**
132
+ ```json
133
+ {
134
+ "mcpServers": {
135
+ "nostr": {
136
+ "command": "npx",
137
+ "args": [
138
+ "nostr-mcp-server"
139
+ ]
140
+ }
141
+ }
142
+ }
143
+ ```
144
+
145
+ **If installed from source:**
146
+ ```json
147
+ {
148
+ "mcpServers": {
149
+ "nostr": {
150
+ "command": "node",
151
+ "args": [
152
+ "/ABSOLUTE/PATH/TO/nostr-mcp-server/build/index.js"
153
+ ]
154
+ }
155
+ }
156
+ }
157
+ ```
158
+
159
+ For source installations, replace `/ABSOLUTE/PATH/TO/` with the actual path to your project.
160
+
161
+ 4. Restart Cursor.
162
+
163
+ ## Connecting to Goose
164
+
165
+ 1. Make sure you have [Goose](https://github.com/block/goose) installed and properly configured.
166
+
167
+ 2. Add the Nostr MCP server to your Goose configuration:
168
+
169
+ Open your Goose configuration file (typically `~/.config/goose/profiles.yaml`) and add the following to your profile's `mcpServers` section:
170
+
171
+ **If installed via npm:**
172
+ ```yaml
173
+ profiles:
174
+ default:
175
+ provider: # your existing provider config
176
+ model: # your existing model config
177
+ mcpServers:
178
+ - name: nostr
179
+ command: npx
180
+ args:
181
+ - nostr-mcp-server
182
+ ```
183
+
184
+ **If installed from source:**
185
+ ```yaml
186
+ profiles:
187
+ default:
188
+ provider: # your existing provider config
189
+ model: # your existing model config
190
+ mcpServers:
191
+ - name: nostr
192
+ command: node
193
+ args:
194
+ - /ABSOLUTE/PATH/TO/nostr-mcp-server/build/index.js
195
+ ```
196
+
197
+ For source installations, replace `/ABSOLUTE/PATH/TO/` with the actual path to your project.
198
+
199
+ 3. Restart Goose or reload your configuration for the changes to take effect.
200
+
201
+ 4. You can verify the MCP server is connected by asking Goose:
202
+ ```
203
+ What MCP tools do you have available for Nostr?
204
+ ```
205
+
206
+ ## Usage in Claude
207
+
208
+ Once configured, you can ask Claude to use the Nostr tools by making requests like:
209
+
210
+ ### Reading & Querying
211
+ - "Show me the profile information for npub1qny3tkh0acurzla8x3zy4nhrjz5zd8ne6dvrjehx9n9hr3lnj08qwuzwc8"
212
+ - "What are the recent posts from npub1qny3tkh0acurzla8x3zy4nhrjz5zd8ne6dvrjehx9n9hr3lnj08qwuzwc8?"
213
+ - "Show me the long-form articles from npub1qny3tkh0acurzla8x3zy4nhrjz5zd8ne6dvrjehx9n9hr3lnj08qwuzwc8"
214
+ - "How many zaps has npub1qny3tkh0acurzla8x3zy4nhrjz5zd8ne6dvrjehx9n9hr3lnj08qwuzwc8 received?"
215
+ - "Show me the zaps sent by npub1qny3tkh0acurzla8x3zy4nhrjz5zd8ne6dvrjehx9n9hr3lnj08qwuzwc8"
216
+ - "Show me all zaps (both sent and received) for npub1qny3tkh0acurzla8x3zy4nhrjz5zd8ne6dvrjehx9n9hr3lnj08qwuzwc8"
217
+ - "Search for NIPs about zaps"
218
+ - "What NIPs are related to long-form content?"
219
+ - "Show me NIP-23 with full content"
220
+
221
+ ### Identity & Profile Management
222
+ - "Generate a new Nostr keypair for me"
223
+ - "Create a keypair in both hex and npub format"
224
+ - "Create a new profile with name 'Alice' and about 'Bitcoiner and developer'"
225
+ - "Update my profile with picture 'https://example.com/avatar.jpg' and website 'https://alice.dev'"
226
+
227
+ ### Note Creation & Publishing
228
+ - "Create a note event with content 'Hello Nostr!' and tags #intro #nostr"
229
+ - "Sign this note event with my private key nsec1xyz..."
230
+ - "Publish this signed note to wss://relay.damus.io and wss://nos.lol"
231
+ - "Post a note saying 'GM Nostr! ☀️' using my private key nsec1xyz..."
232
+
233
+ ### Anonymous Operations
234
+ - "Send an anonymous zap of 100 sats to npub1qny3tkh0acurzla8x3zy4nhrjz5zd8ne6dvrjehx9n9hr3lnj08qwuzwc8"
235
+ - "Send 1000 sats to note1abcdef... with a comment saying 'Great post!'"
236
+ - "Post an anonymous note saying 'Hello Nostr world!'"
237
+ - "Create an anonymous post with tags #bitcoin and #nostr"
238
+
239
+ ### NIP-19 Entity Conversion & Analysis
240
+ - "Convert this hex pubkey to npub: 06639334b39dd9cf4aa1323375931bec1d6cd43b5de30af7b70b08262e5f6e3f"
241
+ - "Convert npub1qny3tkh0acurzla8x3zy4nhrjz5zd8ne6dvrjehx9n9hr3lnj08qwuzwc8 to hex format"
242
+ - "Convert this note ID to nevent format with relay hints"
243
+ - "What type of entity is nprofile1qqsw3dy8cpu...? Analyze it for me"
244
+ - "Decode and analyze this NIP-19 entity: nevent1qqs..."
245
+
246
+ The server automatically handles conversion between npub and hex formats, so you can use either format in your queries. Results are displayed with user-friendly npub identifiers.
247
+
248
+ ## Anonymous Notes
249
+
250
+ The `postAnonymousNote` tool allows users to post notes to the Nostr network without revealing their identity. Key points about anonymous notes:
251
+
252
+ - The note will be published using a random one-time keypair generated just for this post
253
+ - The private key is never stored or saved anywhere - it's used only for signing the note
254
+ - You receive the public key and note ID in the response if you want to reference them
255
+ - You can optionally specify custom tags to include with your note
256
+ - By default, the note is published to several popular relays to ensure good propagation
257
+
258
+ Examples:
259
+ ```
260
+ "Post an anonymous note saying 'Just trying out the anonymous posting feature!'"
261
+ "Create an anonymous note with the content 'Testing the Nostr anonymity features' and tags #test #anonymous"
262
+ "Post anonymously to Nostr: 'I can share thoughts without linking to my identity'"
263
+ ```
264
+
265
+ For more control, you can specify custom relays:
266
+ ```
267
+ "Post an anonymous note to relay wss://relay.damus.io saying 'Hello Nostr world!'"
268
+ ```
269
+
270
+ This feature is useful for:
271
+ - Testing posts without affecting your main identity
272
+ - Sharing information anonymously
273
+ - Creating temporary or throwaway content
274
+
275
+ ## Identity & Profile Management
276
+
277
+ The server provides comprehensive tools for managing Nostr identities and profiles:
278
+
279
+ ### Keypair Generation
280
+ The `createKeypair` tool generates cryptographically secure Nostr keypairs using the secp256k1 curve. You can choose to receive keys in hex format, npub/nsec format, or both:
281
+
282
+ ```
283
+ "Generate a new Nostr keypair in both hex and npub format"
284
+ "Create a keypair with only hex keys"
285
+ "Generate keys in npub format only"
286
+ ```
287
+
288
+ ### Profile Creation & Updates
289
+ Create and manage Nostr profiles (kind 0 events) with full metadata support:
290
+
291
+ - **Names & Bio**: Set display names, usernames, and about text
292
+ - **Media**: Add profile pictures and banners
293
+ - **Identity**: Configure NIP-05 identifiers for verification
294
+ - **Lightning**: Set up Lightning addresses (LUD-16) and LNURL (LUD-06) for payments
295
+ - **Web Presence**: Add personal websites and social links
296
+
297
+ Examples:
298
+ ```
299
+ "Create a profile with name 'Alice', about 'Bitcoin developer', and picture 'https://example.com/alice.jpg'"
300
+ "Update my profile to add website 'https://alice.dev' and Lightning address 'alice@getalby.com'"
301
+ ```
302
+
303
+ ## Authenticated Note Posting
304
+
305
+ ### Individual Note Operations
306
+ For advanced users who need granular control over the note creation process:
307
+
308
+ - **`createNote`**: Creates unsigned note events with your content and tags
309
+ - **`signNote`**: Signs note events with your private key, generating cryptographically valid signatures
310
+ - **`publishNote`**: Publishes signed notes to your chosen relays
311
+
312
+ This modular approach allows for:
313
+ - Offline note creation and later signing
314
+ - Batch operations across multiple notes
315
+ - Integration with external signing workflows
316
+ - Publishing to different relay sets
317
+
318
+ ### All-in-One Posting
319
+ The `postNote` tool provides a convenient single-command approach for authenticated posting:
320
+
321
+ ```
322
+ "Post a note saying 'GM Nostr! ☀️' using my private key nsec1xyz..."
323
+ "Create a post with content 'Just shipped a new feature!' and tags #development #nostr"
324
+ "Post 'Beautiful sunset today 🌅' with tags #photography #nature to relay wss://relay.damus.io"
325
+ ```
326
+
327
+ Key features:
328
+ - **Authenticated Identity**: Posts appear under your established Nostr identity
329
+ - **Format Flexibility**: Accepts both hex and nsec private key formats
330
+ - **Tag Support**: Add hashtags, mentions, and custom metadata
331
+ - **Relay Control**: Publish to specific relays or use defaults
332
+
333
+ ## Advanced Usage
334
+
335
+ You can specify custom relays for any query:
336
+
337
+ - "Show me the profile for npub1qny3tkh0acurzla8x3zy4nhrjz5zd8ne6dvrjehx9n9hr3lnj08qwuzwc8 using relay wss://relay.damus.io"
338
+
339
+ You can also specify the number of notes or zaps to fetch:
340
+
341
+ - "Show me the latest 20 notes from npub1qny3tkh0acurzla8x3zy4nhrjz5zd8ne6dvrjehx9n9hr3lnj08qwuzwc8"
342
+
343
+ For anonymous zaps, you can include optional comments and specify the target type:
344
+
345
+ - "Send an anonymous zap of 500 sats to note1abcdef... with the comment 'Great post!'"
346
+ - "Send 1000 sats anonymously to nevent1qys... using relay wss://relay.damus.io"
347
+
348
+ For zap queries, you can enable extra validation and debugging:
349
+
350
+ - "Show me all zaps for npub1qny3tkh0acurzla8x3zy4nhrjz5zd8ne6dvrjehx9n9hr3lnj08qwuzwc8 with validation and debug enabled"
351
+
352
+ For NIP searches, you can control the number of results and include full content:
353
+
354
+ - "Search for NIPs about zaps with full content"
355
+ - "Show me the top 5 NIPs about relays"
356
+ - "What NIPs are related to encryption? Show me 15 results"
357
+
358
+ ## Limitations
359
+
360
+ - The server has a default 8-second timeout for queries to prevent hanging
361
+ - Only public keys in hex format or npub format are supported
362
+ - Only a subset of relays is used by default
363
+
364
+ ## Implementation Details
365
+
366
+ - Built with **[snstr](https://github.com/austinkelsay/snstr)** - a lightweight, modern TypeScript library for Nostr protocol implementation
367
+ - Native support for npub format using NIP-19 encoding/decoding
368
+ - NIP-57 compliant zap receipt detection with direction-awareness (sent/received/self)
369
+ - Advanced bolt11 invoice parsing with payment amount extraction
370
+ - Smart caching system for improved performance with large volumes of zaps
371
+ - Total sats calculations for sent/received/self zaps with net balance
372
+ - Optional NIP-57 validation for ensuring zap receipt integrity
373
+ - Anonymous zap support with lightning invoice generation
374
+ - Support for zapping profiles, events (note IDs), and replaceable events (naddr)
375
+ - Each tool call creates a fresh connection to the relays, ensuring reliable data retrieval
376
+ - Comprehensive test suite with clean execution and proper resource cleanup
377
+
378
+ ## Anonymous Zaps
379
+
380
+ The `sendAnonymousZap` tool lets users send zaps without revealing their Nostr identity. Key points about anonymous zaps:
381
+
382
+ - The zap will appear to come from an anonymous user in the recipient's wallet
383
+ - The zap follows the NIP-57 protocol but without a sender signature
384
+ - The recipient can still receive the payment and any included message
385
+ - You can zap profiles (using npub/hex pubkey), specific events (using note/nevent/hex ID), or replaceable events (using naddr)
386
+ - The server generates a lightning invoice for payment that you can copy into your Lightning wallet
387
+
388
+ Examples:
389
+ ```
390
+ "Send an anonymous zap of 100 sats to npub1qny3tkh0acurzla8x3zy4nhrjz5zd8ne6dvrjehx9n9hr3lnj08qwuzwc8"
391
+ "Send 1000 sats anonymously to note1abcdef... with the comment 'Great post!'"
392
+ ```
393
+
394
+ The server fully validates LNURL services according to LNURL-pay (LUD-06) and Lightning Address (LUD-16) specifications, ensuring compatibility with various wallet implementations.
395
+
396
+ ## Troubleshooting
397
+
398
+ - If queries time out, try increasing the `QUERY_TIMEOUT` value in the source code (currently 8 seconds)
399
+ - If no data is found, try specifying different relays that might have the data
400
+ - Check Claude's MCP logs for detailed error information
401
+
402
+ ## Default Relays
403
+
404
+ The server uses the following relays by default:
405
+ - wss://relay.damus.io
406
+ - wss://relay.nostr.band
407
+ - wss://relay.primal.net
408
+ - wss://nos.lol
409
+ - wss://purplerelay.com
410
+ - wss://nostr.land
411
+
412
+ ## Development
413
+
414
+ To modify or extend this server:
415
+
416
+ 1. Edit the relevant file:
417
+ - `index.ts`: Main server and tool registration
418
+ - `profile/profile-tools.ts`: Identity management, keypair generation, profile creation ([Documentation](./profile/README.md))
419
+ - `note/note-tools.ts`: Note creation, signing, publishing, and reading functionality ([Documentation](./note/README.md))
420
+ - `zap/zap-tools.ts`: Zap-related functionality ([Documentation](./zap/README.md))
421
+ - `nips/nips-tools.ts`: Functions for searching NIPs ([Documentation](./nips/README.md))
422
+ - `utils/`: Shared utility functions
423
+ - `constants.ts`: Global constants and relay configurations
424
+ - `conversion.ts`: NIP-19 entity conversion utilities (hex/npub/nprofile/nevent/naddr)
425
+ - `formatting.ts`: Output formatting helpers
426
+ - `nip19-tools.ts`: NIP-19 entity conversion and analysis tools
427
+ - `pool.ts`: Nostr connection pool management
428
+ - `ephemeral-relay.ts`: In-memory Nostr relay for testing
429
+
430
+ 2. Run `npm run build` to compile
431
+
432
+ 3. Restart Claude for Desktop or Cursor to pick up your changes
433
+
434
+ ## Testing
435
+
436
+ We've implemented a comprehensive test suite using Jest to test both basic functionality and integration with the Nostr protocol:
437
+
438
+ ```bash
439
+ # Run all tests
440
+ npm test
441
+
442
+ # Run a specific test file
443
+ npm test -- __tests__/basic.test.ts
444
+
445
+ # Run integration tests
446
+ npm test -- __tests__/integration.test.ts
447
+ ```
448
+
449
+ The test suite includes:
450
+
451
+ ### Unit Tests
452
+ - `basic.test.ts` - Tests simple profile formatting and zap receipt processing
453
+ - `profile-notes-simple.test.ts` - Tests profile and note data structures
454
+ - `profile-tools.test.ts` - Tests keypair generation, profile creation, and identity management
455
+ - `note-creation.test.ts` - Tests note creation, signing, and publishing workflows
456
+ - `note-tools-functions.test.ts` - Tests note formatting, creation, signing, and publishing functions
457
+ - `note-tools-unit.test.ts` - Unit tests for note formatting functions
458
+ - `profile-postnote.test.ts` - Tests authenticated note posting with existing private keys
459
+ - `zap-tools-simple.test.ts` - Tests zap processing and anonymous zap preparation
460
+ - `zap-tools-tests.test.ts` - Tests zap validation, parsing, and direction determination
461
+ - `search-nips-simple.test.ts` - Tests NIPs search functionality with relevance scoring
462
+ - `nip19-conversion.test.ts` - Tests NIP-19 entity conversion and analysis (28 test cases)
463
+
464
+ ### Integration Tests
465
+ - `integration.test.ts` - Tests interaction with an ephemeral Nostr relay including:
466
+ - Publishing profile events
467
+ - Creating and retrieving text notes
468
+ - Publishing zap receipts
469
+ - Filtering events
470
+
471
+ - `websocket-integration.test.ts` - Tests WebSocket communication with a Nostr relay:
472
+ - Publishing events over WebSocket
473
+ - Subscribing to events with filters
474
+ - Managing multiple subscriptions
475
+ - Closing subscriptions
476
+ - Verifying that events with invalid signatures are rejected
477
+
478
+ ### Test Infrastructure
479
+ - All integration tests use our `ephemeral-relay.ts` implementation—a fully functional in-memory Nostr relay
480
+ - Tests use **snstr** for cryptographic event signing and verification
481
+ - Clean test execution with proper resource cleanup and no console warnings
482
+ - Automated WebSocket connection management with timeout handling
483
+ - Isolated test environment requiring no external network connections
484
+
485
+ For more details about the test suite, see [__tests__/README.md](./__tests__/README.md).
486
+
487
+ ## Codebase Organization
488
+
489
+ The codebase is organized into modules:
490
+ - Core server setup in `index.ts`
491
+ - Specialized functionality in dedicated directories:
492
+ - [`profile/`](./profile/README.md): Identity management, keypair generation, and profile creation
493
+ - [`note/`](./note/README.md): Note creation, signing, publishing, and reading functionality
494
+ - [`zap/`](./zap/README.md): Zap handling and anonymous zapping
495
+ - [`nips/`](./nips/README.md): NIPs search and caching functionality
496
+ - Common utilities in the `utils/` directory
497
+
498
+ This modular structure makes the codebase more maintainable, reduces duplication, and enables easier feature extensions. For detailed information about each module's features and implementation, see their respective documentation.
@@ -0,0 +1,87 @@
1
+ import { jest } from '@jest/globals';
2
+ // Mock the formatProfile function
3
+ const mockFormatProfile = jest.fn((profile) => {
4
+ const content = typeof profile.content === 'string'
5
+ ? JSON.parse(profile.content)
6
+ : profile.content;
7
+ return `Name: ${content.name || 'Anonymous'}
8
+ Display Name: ${content.display_name || ''}
9
+ About: ${content.about || ''}`;
10
+ });
11
+ // Test a simple nostr profile formatting function
12
+ describe('Basic Nostr Functionality', () => {
13
+ test('profile formatting should work correctly', () => {
14
+ // Arrange - create a mock profile
15
+ const mockProfile = {
16
+ id: '1234',
17
+ pubkey: '7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e',
18
+ created_at: Math.floor(Date.now() / 1000) - 3600,
19
+ kind: 0,
20
+ tags: [],
21
+ content: JSON.stringify({
22
+ name: 'Test User',
23
+ display_name: 'Tester',
24
+ about: 'A test profile for unit tests'
25
+ }),
26
+ sig: 'mock_signature'
27
+ };
28
+ // Act - call the function
29
+ const result = mockFormatProfile(mockProfile);
30
+ // Assert - check the result
31
+ expect(result).toContain('Name: Test User');
32
+ expect(result).toContain('Display Name: Tester');
33
+ expect(result).toContain('About: A test profile for unit tests');
34
+ });
35
+ test('profile formatting should handle empty fields', () => {
36
+ // Arrange - create a mock profile with minimal data
37
+ const mockProfile = {
38
+ id: '5678',
39
+ pubkey: '7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e',
40
+ created_at: Math.floor(Date.now() / 1000) - 3600,
41
+ kind: 0,
42
+ tags: [],
43
+ content: JSON.stringify({
44
+ name: 'Minimal User'
45
+ }),
46
+ sig: 'mock_signature'
47
+ };
48
+ // Act - call the function
49
+ const result = mockFormatProfile(mockProfile);
50
+ // Assert - check the result
51
+ expect(result).toContain('Name: Minimal User');
52
+ expect(result).toContain('Display Name:'); // Empty but exists
53
+ expect(result).toContain('About:'); // Empty but exists
54
+ });
55
+ test('zap receipt processing', () => {
56
+ // Implement a simple zap test here
57
+ const mockProcessZap = (receipt, targetPubkey) => {
58
+ const targetTag = receipt.tags.find(tag => tag[0] === 'p' && tag[1] === targetPubkey);
59
+ const direction = targetTag ? 'received' : 'sent';
60
+ const amountTag = receipt.tags.find(tag => tag[0] === 'amount');
61
+ const amountSats = amountTag ? parseInt(amountTag[1]) / 1000 : 0; // Convert millisats to sats
62
+ return {
63
+ id: receipt.id,
64
+ direction,
65
+ amountSats,
66
+ created_at: receipt.created_at
67
+ };
68
+ };
69
+ // Create mock zap receipt
70
+ const mockZapReceipt = {
71
+ id: 'abcd',
72
+ pubkey: 'lightning_service_pubkey',
73
+ created_at: Math.floor(Date.now() / 1000) - 900,
74
+ kind: 9735,
75
+ tags: [
76
+ ['p', '7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e'],
77
+ ['amount', '10000'], // 100 sats in millisats
78
+ ],
79
+ content: '',
80
+ sig: 'mock_signature'
81
+ };
82
+ // Test zap processing
83
+ const result = mockProcessZap(mockZapReceipt, '7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e');
84
+ expect(result.direction).toBe('received');
85
+ expect(result.amountSats).toBe(10);
86
+ });
87
+ });