@unicitylabs/nostr-js-sdk 0.2.0 → 0.2.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.
package/README.md CHANGED
@@ -190,8 +190,28 @@ const event = await TokenTransferProtocol.createTokenTransferEvent(
190
190
 
191
191
  await client.publishEvent(event);
192
192
 
193
+ // Create token transfer in response to a payment request
194
+ const paymentRequestEventId = '...'; // Event ID of the original payment request
195
+ const event = await TokenTransferProtocol.createTokenTransferEvent(
196
+ keyManager,
197
+ recipientPubkey,
198
+ tokenJson,
199
+ {
200
+ amount: 100n,
201
+ symbol: 'UNIT',
202
+ replyToEventId: paymentRequestEventId // Links transfer to the payment request
203
+ }
204
+ );
205
+
193
206
  // Parse received token transfer
194
207
  const tokenJson = await TokenTransferProtocol.parseTokenTransfer(event, keyManager);
208
+
209
+ // Get reply-to event ID (for payment request correlation)
210
+ const replyToId = TokenTransferProtocol.getReplyToEventId(event);
211
+ if (replyToId) {
212
+ // This transfer is in response to a payment request
213
+ const originalRequest = pendingRequests.get(replyToId);
214
+ }
195
215
  ```
196
216
 
197
217
  ### Payment Requests
@@ -286,6 +306,7 @@ Token transfers use Nostr event kind 31113 with NIP-04 encryption.
286
306
  | `type` | Yes | Always `"token_transfer"` |
287
307
  | `amount` | No | Transfer amount (metadata for filtering) |
288
308
  | `symbol` | No | Token symbol (metadata for filtering) |
309
+ | `e` | No | Reply-to event ID (for payment request correlation) |
289
310
 
290
311
  ### Encrypted Content
291
312
 
@@ -310,10 +331,11 @@ gz:<base64_ciphertext>?iv=<base64_iv>
310
331
  TokenTransferProtocol.isTokenTransfer(event); // boolean
311
332
 
312
333
  // 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
334
+ TokenTransferProtocol.getAmount(event); // bigint | undefined
335
+ TokenTransferProtocol.getSymbol(event); // string | undefined
336
+ TokenTransferProtocol.getRecipient(event); // string | undefined
337
+ TokenTransferProtocol.getSender(event); // string
338
+ TokenTransferProtocol.getReplyToEventId(event); // string | undefined (payment request correlation)
317
339
  ```
318
340
 
319
341
  ## Payment Request Format
@@ -477,13 +499,13 @@ To test payment requests against a real wallet:
477
499
 
478
500
  ```bash
479
501
  # Send a single payment request
480
- TARGET_NAMETAG=mp-6 npm test -- --testNamePattern="send single payment request"
502
+ TARGET_NAMETAG=mp-9 npm test -- --testNamePattern="send single payment request"
481
503
 
482
504
  # Send multiple payment requests (for UI testing)
483
- TARGET_NAMETAG=mp-6 npm test -- --testNamePattern="send multiple payment requests"
505
+ TARGET_NAMETAG=mp-9 npm test -- --testNamePattern="send multiple payment requests"
484
506
 
485
507
  # Full flow with token transfer verification (requires wallet interaction)
486
- TARGET_NAMETAG=mp-6 npm test -- --testNamePattern="full payment request flow"
508
+ TARGET_NAMETAG=mp-9 npm test -- --testNamePattern="full payment request flow"
487
509
  ```
488
510
 
489
511
  Environment variables:
@@ -9824,16 +9824,32 @@ const MESSAGE_PREFIX$1 = 'token_transfer:';
9824
9824
  * - ["type", "token_transfer"] - Event type
9825
9825
  * - ["amount", "<amount>"] - Optional amount
9826
9826
  * - ["symbol", "<symbol>"] - Optional token symbol
9827
+ * - ["e", "<event_id>", "", "reply"] - Optional reply-to event (for payment request correlation)
9827
9828
  * - Content: NIP-04 encrypted "token_transfer:{tokenJson}"
9828
9829
  *
9829
9830
  * @param keyManager Key manager with signing keys
9830
9831
  * @param recipientPubkeyHex Recipient's public key (hex)
9831
9832
  * @param tokenJson Token JSON string
9832
- * @param amount Optional amount for metadata
9833
- * @param symbol Optional token symbol for metadata
9833
+ * @param amountOrOptions Optional amount for metadata, or options object
9834
+ * @param symbol Optional token symbol for metadata (ignored if options object used)
9834
9835
  * @returns Signed event
9835
9836
  */
9836
- async function createTokenTransferEvent(keyManager, recipientPubkeyHex, tokenJson, amount, symbol) {
9837
+ async function createTokenTransferEvent(keyManager, recipientPubkeyHex, tokenJson, amountOrOptions, symbol) {
9838
+ // Parse options (support both old and new signatures)
9839
+ let amount;
9840
+ let tokenSymbol;
9841
+ let replyToEventId;
9842
+ if (amountOrOptions !== undefined && typeof amountOrOptions === 'object') {
9843
+ // New options object signature
9844
+ amount = amountOrOptions.amount;
9845
+ tokenSymbol = amountOrOptions.symbol;
9846
+ replyToEventId = amountOrOptions.replyToEventId;
9847
+ }
9848
+ else {
9849
+ // Old positional arguments signature
9850
+ amount = amountOrOptions;
9851
+ tokenSymbol = symbol;
9852
+ }
9837
9853
  // Encrypt the token data
9838
9854
  const message = MESSAGE_PREFIX$1 + tokenJson;
9839
9855
  const encryptedContent = await keyManager.encryptHex(message, recipientPubkeyHex);
@@ -9845,8 +9861,12 @@ async function createTokenTransferEvent(keyManager, recipientPubkeyHex, tokenJso
9845
9861
  if (amount !== undefined) {
9846
9862
  tags.push(['amount', String(amount)]);
9847
9863
  }
9848
- if (symbol !== undefined) {
9849
- tags.push(['symbol', symbol]);
9864
+ if (tokenSymbol !== undefined) {
9865
+ tags.push(['symbol', tokenSymbol]);
9866
+ }
9867
+ // Add optional reply-to event reference (for payment request correlation)
9868
+ if (replyToEventId !== undefined && replyToEventId.length > 0) {
9869
+ tags.push(['e', replyToEventId, '', 'reply']);
9850
9870
  }
9851
9871
  const event = Event.create(keyManager, {
9852
9872
  kind: TOKEN_TRANSFER,
@@ -9934,6 +9954,15 @@ function getAmount$1(event) {
9934
9954
  function getSymbol(event) {
9935
9955
  return event.getTagValue('symbol');
9936
9956
  }
9957
+ /**
9958
+ * Get the reply-to event ID from a token transfer event.
9959
+ * Used to correlate token transfers with payment requests.
9960
+ * @param event Token transfer event
9961
+ * @returns Referenced event ID, or undefined if not present
9962
+ */
9963
+ function getReplyToEventId(event) {
9964
+ return event.getTagValue('e');
9965
+ }
9937
9966
  /**
9938
9967
  * Check if an event is a token transfer.
9939
9968
  * @param event Event to check
@@ -9965,6 +9994,7 @@ var TokenTransferProtocol = /*#__PURE__*/Object.freeze({
9965
9994
  createTokenTransferEvent: createTokenTransferEvent,
9966
9995
  getAmount: getAmount$1,
9967
9996
  getRecipient: getRecipient,
9997
+ getReplyToEventId: getReplyToEventId,
9968
9998
  getSender: getSender$1,
9969
9999
  getSymbol: getSymbol,
9970
10000
  isTokenTransfer: isTokenTransfer,