@unicitylabs/nostr-js-sdk 0.2.1 → 0.2.3

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
@@ -47,8 +47,15 @@ console.log(keyManager.getPublicKeyHex());
47
47
  import { NostrClient, NostrKeyManager } from '@unicitylabs/nostr-sdk';
48
48
 
49
49
  const keyManager = NostrKeyManager.generate();
50
+
51
+ // Create client with default options
50
52
  const client = new NostrClient(keyManager);
51
53
 
54
+ // Or configure with custom options
55
+ const client = new NostrClient(keyManager, {
56
+ queryTimeoutMs: 15000, // Increase timeout for slow relays (default: 5000ms)
57
+ });
58
+
52
59
  // Connect to relays
53
60
  await client.connect(
54
61
  'wss://relay.damus.io',
@@ -58,6 +65,9 @@ await client.connect(
58
65
  // Check connection status
59
66
  console.log(client.isConnected());
60
67
  console.log(client.getConnectedRelays());
68
+
69
+ // You can also adjust timeout dynamically
70
+ client.setQueryTimeout(30000); // 30 seconds
61
71
  ```
62
72
 
63
73
  ### Publishing Events
@@ -177,31 +187,24 @@ client.unsubscribe(subId);
177
187
  ### Token Transfers
178
188
 
179
189
  ```typescript
180
- import { TokenTransferProtocol } from '@unicitylabs/nostr-sdk';
190
+ import { NostrClient, TokenTransferProtocol } from '@unicitylabs/nostr-sdk';
181
191
 
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
- );
192
+ // Simple token transfer using NostrClient
193
+ const eventId = await client.sendTokenTransfer(recipientPubkey, tokenJson);
190
194
 
191
- await client.publishEvent(event);
195
+ // Token transfer with metadata
196
+ const eventId = await client.sendTokenTransfer(recipientPubkey, tokenJson, {
197
+ amount: 100n,
198
+ symbol: 'UNIT'
199
+ });
192
200
 
193
- // Create token transfer in response to a payment request
201
+ // Token transfer in response to a payment request (with correlation)
194
202
  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
- );
203
+ const eventId = await client.sendTokenTransfer(recipientPubkey, tokenJson, {
204
+ amount: 100n,
205
+ symbol: 'UNIT',
206
+ replyToEventId: paymentRequestEventId // Links transfer to the payment request
207
+ });
205
208
 
206
209
  // Parse received token transfer
207
210
  const tokenJson = await TokenTransferProtocol.parseTokenTransfer(event, keyManager);
@@ -5990,8 +5990,8 @@ var nip17 = /*#__PURE__*/Object.freeze({
5990
5990
  const CONNECTION_TIMEOUT_MS = 30000;
5991
5991
  /** Reconnection delay in milliseconds */
5992
5992
  const RECONNECT_DELAY_MS = 5000;
5993
- /** Query timeout in milliseconds */
5994
- const QUERY_TIMEOUT_MS = 5000;
5993
+ /** Default query timeout in milliseconds */
5994
+ const DEFAULT_QUERY_TIMEOUT_MS = 5000;
5995
5995
  /**
5996
5996
  * NostrClient provides the main interface for Nostr protocol operations.
5997
5997
  */
@@ -6003,12 +6003,15 @@ class NostrClient {
6003
6003
  pendingOks = new Map();
6004
6004
  subscriptionCounter = 0;
6005
6005
  closed = false;
6006
+ queryTimeoutMs;
6006
6007
  /**
6007
6008
  * Create a NostrClient instance.
6008
6009
  * @param keyManager Key manager with signing keys
6010
+ * @param options Optional configuration options
6009
6011
  */
6010
- constructor(keyManager) {
6012
+ constructor(keyManager, options) {
6011
6013
  this.keyManager = keyManager;
6014
+ this.queryTimeoutMs = options?.queryTimeoutMs ?? DEFAULT_QUERY_TIMEOUT_MS;
6012
6015
  }
6013
6016
  /**
6014
6017
  * Get the key manager.
@@ -6017,6 +6020,20 @@ class NostrClient {
6017
6020
  getKeyManager() {
6018
6021
  return this.keyManager;
6019
6022
  }
6023
+ /**
6024
+ * Get the current query timeout in milliseconds.
6025
+ * @returns Query timeout in milliseconds
6026
+ */
6027
+ getQueryTimeout() {
6028
+ return this.queryTimeoutMs;
6029
+ }
6030
+ /**
6031
+ * Set the query timeout for nametag lookups and other queries.
6032
+ * @param timeoutMs Timeout in milliseconds
6033
+ */
6034
+ setQueryTimeout(timeoutMs) {
6035
+ this.queryTimeoutMs = timeoutMs;
6036
+ }
6020
6037
  /**
6021
6038
  * Connect to one or more relay WebSocket URLs.
6022
6039
  * @param relayUrls Relay URLs to connect to
@@ -6354,11 +6371,12 @@ class NostrClient {
6354
6371
  * Send a token transfer (encrypted).
6355
6372
  * @param recipientPubkeyHex Recipient's public key (hex)
6356
6373
  * @param tokenJson Token JSON string
6374
+ * @param options Optional parameters (amount, symbol, replyToEventId)
6357
6375
  * @returns Promise that resolves with the event ID
6358
6376
  */
6359
- async sendTokenTransfer(recipientPubkeyHex, tokenJson) {
6377
+ async sendTokenTransfer(recipientPubkeyHex, tokenJson, options) {
6360
6378
  const TokenTransferProtocol$1 = await Promise.resolve().then(function () { return TokenTransferProtocol; });
6361
- const event = await TokenTransferProtocol$1.createTokenTransferEvent(this.keyManager, recipientPubkeyHex, tokenJson);
6379
+ const event = await TokenTransferProtocol$1.createTokenTransferEvent(this.keyManager, recipientPubkeyHex, tokenJson, options);
6362
6380
  return this.publishEvent(event);
6363
6381
  }
6364
6382
  /**
@@ -6441,7 +6459,7 @@ class NostrClient {
6441
6459
  const timeoutId = setTimeout(() => {
6442
6460
  this.unsubscribe(subscriptionId);
6443
6461
  resolve(null);
6444
- }, QUERY_TIMEOUT_MS);
6462
+ }, this.queryTimeoutMs);
6445
6463
  let result = null;
6446
6464
  let latestCreatedAt = 0;
6447
6465
  const subscriptionId = this.subscribe(filter, {