@unicitylabs/nostr-js-sdk 0.2.0 → 0.2.2

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.
package/README.md CHANGED
@@ -177,21 +177,34 @@ client.unsubscribe(subId);
177
177
  ### Token Transfers
178
178
 
179
179
  ```typescript
180
- import { TokenTransferProtocol } from '@unicitylabs/nostr-sdk';
180
+ import { NostrClient, TokenTransferProtocol } from '@unicitylabs/nostr-sdk';
181
181
 
182
- // Create token transfer event
183
- const event = await TokenTransferProtocol.createTokenTransferEvent(
184
- keyManager,
185
- recipientPubkey,
186
- JSON.stringify({ tokenId: '...', amount: 100 }),
187
- 100n, // amount (optional metadata)
188
- 'UNIT' // symbol (optional metadata)
189
- );
182
+ // Simple token transfer using NostrClient
183
+ const eventId = await client.sendTokenTransfer(recipientPubkey, tokenJson);
190
184
 
191
- await client.publishEvent(event);
185
+ // Token transfer with metadata
186
+ const eventId = await client.sendTokenTransfer(recipientPubkey, tokenJson, {
187
+ amount: 100n,
188
+ symbol: 'UNIT'
189
+ });
190
+
191
+ // Token transfer in response to a payment request (with correlation)
192
+ const paymentRequestEventId = '...'; // Event ID of the original payment request
193
+ const eventId = await client.sendTokenTransfer(recipientPubkey, tokenJson, {
194
+ amount: 100n,
195
+ symbol: 'UNIT',
196
+ replyToEventId: paymentRequestEventId // Links transfer to the payment request
197
+ });
192
198
 
193
199
  // Parse received token transfer
194
200
  const tokenJson = await TokenTransferProtocol.parseTokenTransfer(event, keyManager);
201
+
202
+ // Get reply-to event ID (for payment request correlation)
203
+ const replyToId = TokenTransferProtocol.getReplyToEventId(event);
204
+ if (replyToId) {
205
+ // This transfer is in response to a payment request
206
+ const originalRequest = pendingRequests.get(replyToId);
207
+ }
195
208
  ```
196
209
 
197
210
  ### Payment Requests
@@ -286,6 +299,7 @@ Token transfers use Nostr event kind 31113 with NIP-04 encryption.
286
299
  | `type` | Yes | Always `"token_transfer"` |
287
300
  | `amount` | No | Transfer amount (metadata for filtering) |
288
301
  | `symbol` | No | Token symbol (metadata for filtering) |
302
+ | `e` | No | Reply-to event ID (for payment request correlation) |
289
303
 
290
304
  ### Encrypted Content
291
305
 
@@ -310,10 +324,11 @@ gz:<base64_ciphertext>?iv=<base64_iv>
310
324
  TokenTransferProtocol.isTokenTransfer(event); // boolean
311
325
 
312
326
  // Get metadata from tags
313
- TokenTransferProtocol.getAmount(event); // bigint | undefined
314
- TokenTransferProtocol.getSymbol(event); // string | undefined
315
- TokenTransferProtocol.getRecipient(event); // string | undefined
316
- TokenTransferProtocol.getSender(event); // string
327
+ TokenTransferProtocol.getAmount(event); // bigint | undefined
328
+ TokenTransferProtocol.getSymbol(event); // string | undefined
329
+ TokenTransferProtocol.getRecipient(event); // string | undefined
330
+ TokenTransferProtocol.getSender(event); // string
331
+ TokenTransferProtocol.getReplyToEventId(event); // string | undefined (payment request correlation)
317
332
  ```
318
333
 
319
334
  ## Payment Request Format
@@ -477,13 +492,13 @@ To test payment requests against a real wallet:
477
492
 
478
493
  ```bash
479
494
  # Send a single payment request
480
- TARGET_NAMETAG=mp-6 npm test -- --testNamePattern="send single payment request"
495
+ TARGET_NAMETAG=mp-9 npm test -- --testNamePattern="send single payment request"
481
496
 
482
497
  # Send multiple payment requests (for UI testing)
483
- TARGET_NAMETAG=mp-6 npm test -- --testNamePattern="send multiple payment requests"
498
+ TARGET_NAMETAG=mp-9 npm test -- --testNamePattern="send multiple payment requests"
484
499
 
485
500
  # Full flow with token transfer verification (requires wallet interaction)
486
- TARGET_NAMETAG=mp-6 npm test -- --testNamePattern="full payment request flow"
501
+ TARGET_NAMETAG=mp-9 npm test -- --testNamePattern="full payment request flow"
487
502
  ```
488
503
 
489
504
  Environment variables:
@@ -6354,11 +6354,12 @@ class NostrClient {
6354
6354
  * Send a token transfer (encrypted).
6355
6355
  * @param recipientPubkeyHex Recipient's public key (hex)
6356
6356
  * @param tokenJson Token JSON string
6357
+ * @param options Optional parameters (amount, symbol, replyToEventId)
6357
6358
  * @returns Promise that resolves with the event ID
6358
6359
  */
6359
- async sendTokenTransfer(recipientPubkeyHex, tokenJson) {
6360
+ async sendTokenTransfer(recipientPubkeyHex, tokenJson, options) {
6360
6361
  const TokenTransferProtocol$1 = await Promise.resolve().then(function () { return TokenTransferProtocol; });
6361
- const event = await TokenTransferProtocol$1.createTokenTransferEvent(this.keyManager, recipientPubkeyHex, tokenJson);
6362
+ const event = await TokenTransferProtocol$1.createTokenTransferEvent(this.keyManager, recipientPubkeyHex, tokenJson, options);
6362
6363
  return this.publishEvent(event);
6363
6364
  }
6364
6365
  /**
@@ -9824,16 +9825,32 @@ const MESSAGE_PREFIX$1 = 'token_transfer:';
9824
9825
  * - ["type", "token_transfer"] - Event type
9825
9826
  * - ["amount", "<amount>"] - Optional amount
9826
9827
  * - ["symbol", "<symbol>"] - Optional token symbol
9828
+ * - ["e", "<event_id>", "", "reply"] - Optional reply-to event (for payment request correlation)
9827
9829
  * - Content: NIP-04 encrypted "token_transfer:{tokenJson}"
9828
9830
  *
9829
9831
  * @param keyManager Key manager with signing keys
9830
9832
  * @param recipientPubkeyHex Recipient's public key (hex)
9831
9833
  * @param tokenJson Token JSON string
9832
- * @param amount Optional amount for metadata
9833
- * @param symbol Optional token symbol for metadata
9834
+ * @param amountOrOptions Optional amount for metadata, or options object
9835
+ * @param symbol Optional token symbol for metadata (ignored if options object used)
9834
9836
  * @returns Signed event
9835
9837
  */
9836
- async function createTokenTransferEvent(keyManager, recipientPubkeyHex, tokenJson, amount, symbol) {
9838
+ async function createTokenTransferEvent(keyManager, recipientPubkeyHex, tokenJson, amountOrOptions, symbol) {
9839
+ // Parse options (support both old and new signatures)
9840
+ let amount;
9841
+ let tokenSymbol;
9842
+ let replyToEventId;
9843
+ if (amountOrOptions !== undefined && typeof amountOrOptions === 'object') {
9844
+ // New options object signature
9845
+ amount = amountOrOptions.amount;
9846
+ tokenSymbol = amountOrOptions.symbol;
9847
+ replyToEventId = amountOrOptions.replyToEventId;
9848
+ }
9849
+ else {
9850
+ // Old positional arguments signature
9851
+ amount = amountOrOptions;
9852
+ tokenSymbol = symbol;
9853
+ }
9837
9854
  // Encrypt the token data
9838
9855
  const message = MESSAGE_PREFIX$1 + tokenJson;
9839
9856
  const encryptedContent = await keyManager.encryptHex(message, recipientPubkeyHex);
@@ -9845,8 +9862,12 @@ async function createTokenTransferEvent(keyManager, recipientPubkeyHex, tokenJso
9845
9862
  if (amount !== undefined) {
9846
9863
  tags.push(['amount', String(amount)]);
9847
9864
  }
9848
- if (symbol !== undefined) {
9849
- tags.push(['symbol', symbol]);
9865
+ if (tokenSymbol !== undefined) {
9866
+ tags.push(['symbol', tokenSymbol]);
9867
+ }
9868
+ // Add optional reply-to event reference (for payment request correlation)
9869
+ if (replyToEventId !== undefined && replyToEventId.length > 0) {
9870
+ tags.push(['e', replyToEventId, '', 'reply']);
9850
9871
  }
9851
9872
  const event = Event.create(keyManager, {
9852
9873
  kind: TOKEN_TRANSFER,
@@ -9934,6 +9955,15 @@ function getAmount$1(event) {
9934
9955
  function getSymbol(event) {
9935
9956
  return event.getTagValue('symbol');
9936
9957
  }
9958
+ /**
9959
+ * Get the reply-to event ID from a token transfer event.
9960
+ * Used to correlate token transfers with payment requests.
9961
+ * @param event Token transfer event
9962
+ * @returns Referenced event ID, or undefined if not present
9963
+ */
9964
+ function getReplyToEventId(event) {
9965
+ return event.getTagValue('e');
9966
+ }
9937
9967
  /**
9938
9968
  * Check if an event is a token transfer.
9939
9969
  * @param event Event to check
@@ -9965,6 +9995,7 @@ var TokenTransferProtocol = /*#__PURE__*/Object.freeze({
9965
9995
  createTokenTransferEvent: createTokenTransferEvent,
9966
9996
  getAmount: getAmount$1,
9967
9997
  getRecipient: getRecipient,
9998
+ getReplyToEventId: getReplyToEventId,
9968
9999
  getSender: getSender$1,
9969
10000
  getSymbol: getSymbol,
9970
10001
  isTokenTransfer: isTokenTransfer,