@bsv/message-box-client 2.0.5 → 2.0.7

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 (32) hide show
  1. package/dist/cjs/package.json +2 -2
  2. package/dist/cjs/src/PeerPayClient.js +463 -62
  3. package/dist/cjs/src/PeerPayClient.js.map +1 -1
  4. package/dist/cjs/src/__tests/PeerPayClientRequestIntegration.test.js +317 -0
  5. package/dist/cjs/src/__tests/PeerPayClientRequestIntegration.test.js.map +1 -0
  6. package/dist/cjs/src/__tests/PeerPayClientUnit.test.js +505 -1
  7. package/dist/cjs/src/__tests/PeerPayClientUnit.test.js.map +1 -1
  8. package/dist/cjs/src/types.js +5 -0
  9. package/dist/cjs/src/types.js.map +1 -1
  10. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  11. package/dist/esm/src/PeerPayClient.js +459 -61
  12. package/dist/esm/src/PeerPayClient.js.map +1 -1
  13. package/dist/esm/src/__tests/PeerPayClientRequestIntegration.test.js +312 -0
  14. package/dist/esm/src/__tests/PeerPayClientRequestIntegration.test.js.map +1 -0
  15. package/dist/esm/src/__tests/PeerPayClientUnit.test.js +505 -1
  16. package/dist/esm/src/__tests/PeerPayClientUnit.test.js.map +1 -1
  17. package/dist/esm/src/types.js +4 -1
  18. package/dist/esm/src/types.js.map +1 -1
  19. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  20. package/dist/types/src/PeerPayClient.d.ts +160 -0
  21. package/dist/types/src/PeerPayClient.d.ts.map +1 -1
  22. package/dist/types/src/__tests/PeerPayClientRequestIntegration.test.d.ts +10 -0
  23. package/dist/types/src/__tests/PeerPayClientRequestIntegration.test.d.ts.map +1 -0
  24. package/dist/types/src/types.d.ts +88 -0
  25. package/dist/types/src/types.d.ts.map +1 -1
  26. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  27. package/dist/umd/bundle.js +1 -1
  28. package/package.json +2 -2
  29. package/src/PeerPayClient.ts +526 -69
  30. package/src/__tests/PeerPayClientRequestIntegration.test.ts +364 -0
  31. package/src/__tests/PeerPayClientUnit.test.ts +594 -1
  32. package/src/types.ts +95 -0
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/message-box-client",
3
- "version": "2.0.5",
3
+ "version": "2.0.7",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -72,6 +72,6 @@
72
72
  },
73
73
  "dependencies": {
74
74
  "@bsv/authsocket-client": "^2.0.2",
75
- "@bsv/sdk": "^2.0.4"
75
+ "@bsv/sdk": "^2.0.11"
76
76
  }
77
77
  }
@@ -44,22 +44,47 @@ var __importStar = (this && this.__importStar) || (function () {
44
44
  };
45
45
  })();
46
46
  Object.defineProperty(exports, "__esModule", { value: true });
47
- exports.PeerPayClient = exports.STANDARD_PAYMENT_MESSAGEBOX = void 0;
47
+ exports.PeerPayClient = exports.PAYMENT_REQUEST_RESPONSES_MESSAGEBOX = exports.PAYMENT_REQUESTS_MESSAGEBOX = exports.STANDARD_PAYMENT_MESSAGEBOX = void 0;
48
48
  const MessageBoxClient_js_1 = require("./MessageBoxClient.js");
49
+ const types_js_1 = require("./types.js");
49
50
  const sdk_1 = require("@bsv/sdk");
50
51
  const Logger = __importStar(require("./Utils/logger.js"));
52
+ function toNumberArray(tx) {
53
+ return Array.isArray(tx) ? tx : Array.from(tx);
54
+ }
55
+ function hexToBytes(hex) {
56
+ const matches = hex.match(/.{1,2}/g);
57
+ return matches != null ? matches.map(byte => parseInt(byte, 16)) : [];
58
+ }
51
59
  function safeParse(input) {
52
60
  try {
53
61
  return typeof input === 'string' ? JSON.parse(input) : input;
54
62
  }
55
63
  catch (e) {
56
64
  Logger.error('[PP CLIENT] Failed to parse input in safeParse:', input);
57
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
58
- const fallback = {};
59
- return fallback;
65
+ return undefined;
60
66
  }
61
67
  }
68
+ /**
69
+ * Validates that a parsed object has the required fields for a PaymentRequestMessage.
70
+ * Returns true for both new requests (has amount, description, expiresAt) and cancellations (has cancelled: true).
71
+ */
72
+ function isValidPaymentRequestMessage(obj) {
73
+ if (typeof obj !== 'object' || obj === null)
74
+ return false;
75
+ if (typeof obj.requestId !== 'string')
76
+ return false;
77
+ if (typeof obj.senderIdentityKey !== 'string')
78
+ return false;
79
+ if (typeof obj.requestProof !== 'string')
80
+ return false;
81
+ if (obj.cancelled === true)
82
+ return true;
83
+ return typeof obj.amount === 'number' && typeof obj.description === 'string' && typeof obj.expiresAt === 'number';
84
+ }
62
85
  exports.STANDARD_PAYMENT_MESSAGEBOX = 'payment_inbox';
86
+ exports.PAYMENT_REQUESTS_MESSAGEBOX = 'payment_requests';
87
+ exports.PAYMENT_REQUEST_RESPONSES_MESSAGEBOX = 'payment_request_responses';
63
88
  const STANDARD_PAYMENT_OUTPUT_INDEX = 0;
64
89
  /**
65
90
  * PeerPayClient enables peer-to-peer Bitcoin payments using MessageBox.
@@ -73,6 +98,15 @@ class PeerPayClient extends MessageBoxClient_js_1.MessageBoxClient {
73
98
  this.messageBox = (_a = config.messageBox) !== null && _a !== void 0 ? _a : exports.STANDARD_PAYMENT_MESSAGEBOX;
74
99
  this.peerPayWalletClient = walletClient;
75
100
  this.originator = originator;
101
+ this.settlementModule = new sdk_1.Brc29RemittanceModule({
102
+ protocolID: [2, '3241645161d8'],
103
+ labels: ['peerpay'],
104
+ description: 'PeerPay payment',
105
+ outputDescription: 'Payment for PeerPay transaction',
106
+ internalizeProtocol: 'wallet payment',
107
+ refundFeeSatoshis: 1000,
108
+ minRefundSatoshis: 1000
109
+ });
76
110
  }
77
111
  get authFetchInstance() {
78
112
  if (this._authFetchInstance === null || this._authFetchInstance === undefined) {
@@ -80,6 +114,59 @@ class PeerPayClient extends MessageBoxClient_js_1.MessageBoxClient {
80
114
  }
81
115
  return this._authFetchInstance;
82
116
  }
117
+ /**
118
+ * Allows payment requests from a specific identity key by setting
119
+ * the recipientFee to 0 for the payment_requests message box.
120
+ *
121
+ * @param {Object} params - Parameters.
122
+ * @param {string} params.identityKey - The identity key to allow payment requests from.
123
+ * @returns {Promise<void>} Resolves when the permission is set.
124
+ */
125
+ async allowPaymentRequestsFrom({ identityKey }) {
126
+ await this.setMessageBoxPermission({
127
+ messageBox: exports.PAYMENT_REQUESTS_MESSAGEBOX,
128
+ sender: identityKey,
129
+ recipientFee: 0
130
+ });
131
+ }
132
+ /**
133
+ * Blocks payment requests from a specific identity key by setting
134
+ * the recipientFee to -1 for the payment_requests message box.
135
+ *
136
+ * @param {Object} params - Parameters.
137
+ * @param {string} params.identityKey - The identity key to block payment requests from.
138
+ * @returns {Promise<void>} Resolves when the permission is set.
139
+ */
140
+ async blockPaymentRequestsFrom({ identityKey }) {
141
+ await this.setMessageBoxPermission({
142
+ messageBox: exports.PAYMENT_REQUESTS_MESSAGEBOX,
143
+ sender: identityKey,
144
+ recipientFee: -1
145
+ });
146
+ }
147
+ /**
148
+ * Lists all permissions for the payment_requests message box, mapped to
149
+ * a simplified { identityKey, allowed } structure.
150
+ *
151
+ * A permission is considered "allowed" if recipientFee >= 0 (0 = always allow,
152
+ * positive = payment required). A recipientFee of -1 means blocked.
153
+ *
154
+ * @returns {Promise<Array<{ identityKey: string, allowed: boolean }>>} Resolved with the list of permissions.
155
+ */
156
+ async listPaymentRequestPermissions() {
157
+ const permissions = await this.listMessageBoxPermissions({ messageBox: exports.PAYMENT_REQUESTS_MESSAGEBOX });
158
+ // Filter to only per-sender entries (sender is not null/empty).
159
+ // Use the status field returned by the server to determine allowed state.
160
+ return permissions
161
+ .filter(p => p.sender != null && p.sender !== '')
162
+ .map(p => {
163
+ var _a;
164
+ return ({
165
+ identityKey: (_a = p.sender) !== null && _a !== void 0 ? _a : '',
166
+ allowed: p.status !== 'blocked'
167
+ });
168
+ });
169
+ }
83
170
  /**
84
171
  * Generates a valid payment token for a recipient.
85
172
  *
@@ -97,53 +184,34 @@ class PeerPayClient extends MessageBoxClient_js_1.MessageBoxClient {
97
184
  throw new Error('Invalid payment details: recipient and valid amount are required');
98
185
  }
99
186
  ;
100
- // Generate derivation paths using correct nonce function
101
- const derivationPrefix = await (0, sdk_1.createNonce)(this.peerPayWalletClient, 'self', this.originator);
102
- const derivationSuffix = await (0, sdk_1.createNonce)(this.peerPayWalletClient, 'self', this.originator);
103
- Logger.log(`[PP CLIENT] Derivation Prefix: ${derivationPrefix}`);
104
- Logger.log(`[PP CLIENT] Derivation Suffix: ${derivationSuffix}`);
105
- // Get recipient's derived public key
106
- const { publicKey: derivedKeyResult } = await this.peerPayWalletClient.getPublicKey({
107
- protocolID: [2, '3241645161d8'],
108
- keyID: `${derivationPrefix} ${derivationSuffix}`,
109
- counterparty: payment.recipient
110
- }, this.originator);
111
- Logger.log(`[PP CLIENT] Derived Public Key: ${derivedKeyResult}`);
112
- if (derivedKeyResult == null || derivedKeyResult.trim() === '') {
113
- throw new Error('Failed to derive recipient’s public key');
114
- }
115
- // Create locking script using recipient's public key
116
- const lockingScript = new sdk_1.P2PKH().lock(sdk_1.PublicKey.fromString(derivedKeyResult).toAddress()).toHex();
117
- Logger.log(`[PP CLIENT] Locking Script: ${lockingScript}`);
118
- // Create the payment action
119
- const paymentAction = await this.peerPayWalletClient.createAction({
120
- description: 'PeerPay payment',
121
- labels: ['peerpay'],
122
- outputs: [{
123
- satoshis: payment.amount,
124
- lockingScript,
125
- customInstructions: JSON.stringify({
126
- derivationPrefix,
127
- derivationSuffix,
128
- payee: payment.recipient
129
- }),
130
- outputDescription: 'Payment for PeerPay transaction'
131
- }],
132
- options: {
133
- randomizeOutputs: false
187
+ const result = await this.settlementModule.buildSettlement({
188
+ threadId: 'peerpay',
189
+ option: {
190
+ amountSatoshis: payment.amount,
191
+ payee: payment.recipient,
192
+ labels: ['peerpay'],
193
+ description: 'PeerPay payment'
134
194
  }
135
- }, this.originator);
136
- if (paymentAction.tx === undefined) {
137
- throw new Error('Transaction creation failed!');
195
+ }, {
196
+ wallet: this.peerPayWalletClient,
197
+ originator: this.originator,
198
+ now: () => Date.now(),
199
+ logger: Logger
200
+ });
201
+ if (result.action === 'terminate') {
202
+ if (result.termination.code === 'brc29.public_key_missing') {
203
+ throw new Error('Failed to derive recipient’s public key');
204
+ }
205
+ throw new Error(result.termination.message);
138
206
  }
139
- Logger.log('[PP CLIENT] Payment Action:', paymentAction);
207
+ Logger.log('[PP CLIENT] Payment Action Settlement Artifact:', result.artifact);
140
208
  return {
141
209
  customInstructions: {
142
- derivationPrefix,
143
- derivationSuffix
210
+ derivationPrefix: result.artifact.customInstructions.derivationPrefix,
211
+ derivationSuffix: result.artifact.customInstructions.derivationSuffix
144
212
  },
145
- transaction: paymentAction.tx,
146
- amount: payment.amount
213
+ transaction: result.artifact.transaction,
214
+ amount: result.artifact.amountSatoshis
147
215
  };
148
216
  }
149
217
  /**
@@ -224,10 +292,13 @@ class PeerPayClient extends MessageBoxClient_js_1.MessageBoxClient {
224
292
  // Convert PeerMessage → IncomingPayment before calling onPayment
225
293
  onMessage: (message) => {
226
294
  Logger.log('[MB CLIENT] Received Live Payment:', message);
295
+ const token = safeParse(message.body);
296
+ if (token == null)
297
+ return;
227
298
  const incomingPayment = {
228
299
  messageId: message.messageId,
229
300
  sender: message.sender,
230
- token: safeParse(message.body)
301
+ token
231
302
  };
232
303
  Logger.log('[PP CLIENT] Converted PeerMessage to IncomingPayment:', incomingPayment);
233
304
  onPayment(incomingPayment);
@@ -246,23 +317,31 @@ class PeerPayClient extends MessageBoxClient_js_1.MessageBoxClient {
246
317
  * @throws {Error} If payment processing fails.
247
318
  */
248
319
  async acceptPayment(payment) {
249
- var _a;
320
+ var _a, _b;
250
321
  try {
251
322
  Logger.log(`[PP CLIENT] Processing payment: ${JSON.stringify(payment, null, 2)}`);
252
- const paymentResult = await this.peerPayWalletClient.internalizeAction({
253
- tx: payment.token.transaction,
254
- outputs: [{
255
- paymentRemittance: {
256
- derivationPrefix: payment.token.customInstructions.derivationPrefix,
257
- derivationSuffix: payment.token.customInstructions.derivationSuffix,
258
- senderIdentityKey: payment.sender
259
- },
260
- outputIndex: (_a = payment.token.outputIndex) !== null && _a !== void 0 ? _a : STANDARD_PAYMENT_OUTPUT_INDEX,
261
- protocol: 'wallet payment'
262
- }],
263
- labels: ['peerpay'],
264
- description: 'PeerPay Payment'
265
- }, this.originator);
323
+ const acceptResult = await this.settlementModule.acceptSettlement({
324
+ threadId: 'peerpay',
325
+ sender: payment.sender,
326
+ settlement: {
327
+ customInstructions: {
328
+ derivationPrefix: payment.token.customInstructions.derivationPrefix,
329
+ derivationSuffix: payment.token.customInstructions.derivationSuffix
330
+ },
331
+ transaction: toNumberArray(payment.token.transaction),
332
+ amountSatoshis: payment.token.amount,
333
+ outputIndex: (_a = payment.token.outputIndex) !== null && _a !== void 0 ? _a : STANDARD_PAYMENT_OUTPUT_INDEX
334
+ }
335
+ }, {
336
+ wallet: this.peerPayWalletClient,
337
+ originator: this.originator,
338
+ now: () => Date.now(),
339
+ logger: Logger
340
+ });
341
+ if (acceptResult.action === 'terminate') {
342
+ throw new Error(acceptResult.termination.message);
343
+ }
344
+ const paymentResult = (_b = acceptResult.receiptData) === null || _b === void 0 ? void 0 : _b.internalizeResult;
266
345
  Logger.log(`[PP CLIENT] Payment internalized successfully: ${JSON.stringify(paymentResult, null, 2)}`);
267
346
  Logger.log(`[PP CLIENT] Acknowledging payment with messageId: ${payment.messageId}`);
268
347
  await this.acknowledgeMessage({ messageIds: [payment.messageId] });
@@ -341,13 +420,335 @@ class PeerPayClient extends MessageBoxClient_js_1.MessageBoxClient {
341
420
  const messages = await this.listMessages({ messageBox: this.messageBox, host: overrideHost });
342
421
  return messages.map((msg) => {
343
422
  const parsedToken = safeParse(msg.body);
423
+ if (parsedToken == null)
424
+ return null;
344
425
  return {
345
426
  messageId: msg.messageId,
346
427
  sender: msg.sender,
347
428
  token: parsedToken
348
429
  };
430
+ }).filter((p) => p != null);
431
+ }
432
+ /**
433
+ * Lists all responses to payment requests from the payment_request_responses message box.
434
+ *
435
+ * Retrieves messages and parses each as a PaymentRequestResponse.
436
+ *
437
+ * @param {string} [hostOverride] - Optional host override for the message box server.
438
+ * @returns {Promise<PaymentRequestResponse[]>} Resolves with an array of payment request responses.
439
+ */
440
+ async listPaymentRequestResponses(hostOverride) {
441
+ const messages = await this.listMessages({ messageBox: exports.PAYMENT_REQUEST_RESPONSES_MESSAGEBOX, host: hostOverride });
442
+ return messages.map((msg) => safeParse(msg.body))
443
+ .filter((r) => r != null);
444
+ }
445
+ /**
446
+ * Listens for incoming payment requests in real time via WebSocket.
447
+ *
448
+ * Wraps listenForLiveMessages on the payment_requests box and converts each
449
+ * incoming PeerMessage into an IncomingPaymentRequest before calling onRequest.
450
+ *
451
+ * @param {Object} params - Listener configuration.
452
+ * @param {Function} params.onRequest - Callback invoked when a new payment request arrives.
453
+ * @param {string} [params.overrideHost] - Optional host override for the WebSocket connection.
454
+ * @returns {Promise<void>} Resolves when the listener is established.
455
+ */
456
+ async listenForLivePaymentRequests({ onRequest, overrideHost }) {
457
+ await this.listenForLiveMessages({
458
+ messageBox: exports.PAYMENT_REQUESTS_MESSAGEBOX,
459
+ overrideHost,
460
+ onMessage: (message) => {
461
+ const body = safeParse(message.body);
462
+ if (body == null || body.cancelled === true)
463
+ return; // Skip cancellations and parse failures
464
+ const request = {
465
+ messageId: message.messageId,
466
+ sender: message.sender,
467
+ requestId: body.requestId,
468
+ amount: body.amount,
469
+ description: body.description,
470
+ expiresAt: body.expiresAt
471
+ };
472
+ onRequest(request);
473
+ }
349
474
  });
350
475
  }
476
+ /**
477
+ * Listens for payment request responses in real time via WebSocket.
478
+ *
479
+ * Wraps listenForLiveMessages on the payment_request_responses box and converts each
480
+ * incoming PeerMessage into a PaymentRequestResponse before calling onResponse.
481
+ *
482
+ * @param {Object} params - Listener configuration.
483
+ * @param {Function} params.onResponse - Callback invoked when a new response arrives.
484
+ * @param {string} [params.overrideHost] - Optional host override for the WebSocket connection.
485
+ * @returns {Promise<void>} Resolves when the listener is established.
486
+ */
487
+ async listenForLivePaymentRequestResponses({ onResponse, overrideHost }) {
488
+ await this.listenForLiveMessages({
489
+ messageBox: exports.PAYMENT_REQUEST_RESPONSES_MESSAGEBOX,
490
+ overrideHost,
491
+ onMessage: (message) => {
492
+ const response = safeParse(message.body);
493
+ if (response == null)
494
+ return;
495
+ onResponse(response);
496
+ }
497
+ });
498
+ }
499
+ /**
500
+ * Fulfills an incoming payment request by sending the requested payment and
501
+ * notifying the requester with a 'paid' response in the payment_request_responses box.
502
+ * Also acknowledges the original request message.
503
+ *
504
+ * @param {Object} params - Fulfillment parameters.
505
+ * @param {IncomingPaymentRequest} params.request - The incoming payment request to fulfill.
506
+ * @param {string} [params.note] - Optional note to include in the response.
507
+ * @param {string} [hostOverride] - Optional host override for the message box server.
508
+ * @returns {Promise<void>} Resolves when payment is sent and acknowledgment is complete.
509
+ */
510
+ async fulfillPaymentRequest(params, hostOverride) {
511
+ const { request, note } = params;
512
+ await this.sendPayment({ recipient: request.sender, amount: request.amount }, hostOverride);
513
+ const response = {
514
+ requestId: request.requestId,
515
+ status: 'paid',
516
+ amountPaid: request.amount,
517
+ ...(note != null && { note })
518
+ };
519
+ await this.sendMessage({
520
+ recipient: request.sender,
521
+ messageBox: exports.PAYMENT_REQUEST_RESPONSES_MESSAGEBOX,
522
+ body: JSON.stringify(response)
523
+ }, hostOverride);
524
+ await this.acknowledgeMessage({ messageIds: [request.messageId], host: hostOverride });
525
+ }
526
+ /**
527
+ * Declines an incoming payment request by notifying the requester with a 'declined'
528
+ * response in the payment_request_responses box and acknowledging the original request.
529
+ *
530
+ * @param {Object} params - Decline parameters.
531
+ * @param {IncomingPaymentRequest} params.request - The incoming payment request to decline.
532
+ * @param {string} [params.note] - Optional note explaining why the request was declined.
533
+ * @param {string} [hostOverride] - Optional host override for the message box server.
534
+ * @returns {Promise<void>} Resolves when the response is sent and request is acknowledged.
535
+ */
536
+ async declinePaymentRequest(params, hostOverride) {
537
+ const { request, note } = params;
538
+ const response = {
539
+ requestId: request.requestId,
540
+ status: 'declined',
541
+ ...(note != null && { note })
542
+ };
543
+ await this.sendMessage({
544
+ recipient: request.sender,
545
+ messageBox: exports.PAYMENT_REQUEST_RESPONSES_MESSAGEBOX,
546
+ body: JSON.stringify(response)
547
+ }, hostOverride);
548
+ await this.acknowledgeMessage({ messageIds: [request.messageId], host: hostOverride });
549
+ }
550
+ /**
551
+ * Sends a payment request to a recipient via the payment_requests message box.
552
+ *
553
+ * Generates a unique requestId using createNonce, looks up the caller's identity key,
554
+ * and sends a PaymentRequestMessage to the recipient.
555
+ *
556
+ * @param {Object} params - Payment request parameters.
557
+ * @param {string} params.recipient - The identity key of the intended payer.
558
+ * @param {number} params.amount - The amount in satoshis being requested (must be > 0).
559
+ * @param {string} params.description - Human-readable reason for the payment request.
560
+ * @param {number} params.expiresAt - Unix timestamp (ms) when the request expires.
561
+ * @param {string} [hostOverride] - Optional host override for the message box server.
562
+ * @returns {Promise<{ requestId: string }>} The generated requestId for this request.
563
+ * @throws {Error} If amount is <= 0.
564
+ */
565
+ async requestPayment(params, hostOverride) {
566
+ if (params.amount <= 0) {
567
+ throw new Error('Invalid payment request: amount must be greater than 0');
568
+ }
569
+ const requestId = await (0, sdk_1.createNonce)(this.peerPayWalletClient, 'self', this.originator);
570
+ const senderIdentityKey = await this.getIdentityKey();
571
+ const proofData = Array.from(new TextEncoder().encode(requestId + params.recipient));
572
+ const { hmac } = await this.peerPayWalletClient.createHmac({
573
+ data: proofData,
574
+ protocolID: [2, 'payment request auth'],
575
+ keyID: requestId,
576
+ counterparty: params.recipient
577
+ }, this.originator);
578
+ const requestProof = Array.from(hmac).map(b => b.toString(16).padStart(2, '0')).join('');
579
+ const body = {
580
+ requestId,
581
+ amount: params.amount,
582
+ description: params.description,
583
+ expiresAt: params.expiresAt,
584
+ senderIdentityKey,
585
+ requestProof
586
+ };
587
+ try {
588
+ await this.sendMessage({
589
+ recipient: params.recipient,
590
+ messageBox: exports.PAYMENT_REQUESTS_MESSAGEBOX,
591
+ body: JSON.stringify(body)
592
+ }, hostOverride);
593
+ }
594
+ catch (err) {
595
+ // Translate HTTP 403 (permission denied) into a user-friendly message.
596
+ if (typeof (err === null || err === void 0 ? void 0 : err.message) === 'string' && err.message.includes('403')) {
597
+ throw new Error('Payment request blocked — you are not on the recipient\'s whitelist.');
598
+ }
599
+ throw err;
600
+ }
601
+ return { requestId, requestProof };
602
+ }
603
+ /**
604
+ * Lists all incoming payment requests from the payment_requests message box.
605
+ *
606
+ * Automatically filters out:
607
+ * - Expired requests (expiresAt < now), which are acknowledged and discarded.
608
+ * - Cancelled requests (a cancellation message with the same requestId exists),
609
+ * both the original and cancellation messages are acknowledged and discarded.
610
+ * - Out-of-range requests (when limits are provided), which are acknowledged and discarded.
611
+ *
612
+ * @param {string} [hostOverride] - Optional host override for the message box server.
613
+ * @param {PaymentRequestLimits} [limits] - Optional min/max satoshi limits for filtering.
614
+ * @returns {Promise<IncomingPaymentRequest[]>} Resolves with active, valid payment requests.
615
+ */
616
+ async listIncomingPaymentRequests(hostOverride, limits) {
617
+ var _a, _b;
618
+ const messages = await this.listMessages({ messageBox: exports.PAYMENT_REQUESTS_MESSAGEBOX, host: hostOverride });
619
+ const myIdentityKey = await this.getIdentityKey();
620
+ const now = Date.now();
621
+ // Parse and validate all messages, collecting malformed ones for ack
622
+ const malformedMessageIds = [];
623
+ const parsed = [];
624
+ for (const msg of messages) {
625
+ const body = safeParse(msg.body);
626
+ if (body != null && isValidPaymentRequestMessage(body)) {
627
+ parsed.push({ messageId: msg.messageId, sender: msg.sender, body });
628
+ }
629
+ else {
630
+ malformedMessageIds.push(msg.messageId);
631
+ }
632
+ }
633
+ // Collect cancelled requestIds — verify HMAC proof before accepting
634
+ const cancelledRequests = new Map(); // requestId → sender
635
+ const cancelMessageIds = [];
636
+ for (const item of parsed) {
637
+ if (item.body.cancelled === true) {
638
+ // Verify cancellation HMAC proof
639
+ try {
640
+ const proofData = Array.from(new TextEncoder().encode(item.body.requestId + myIdentityKey));
641
+ await this.peerPayWalletClient.verifyHmac({
642
+ data: proofData,
643
+ hmac: hexToBytes(item.body.requestProof),
644
+ protocolID: [2, 'payment request auth'],
645
+ keyID: item.body.requestId,
646
+ counterparty: item.sender
647
+ }, this.originator);
648
+ cancelledRequests.set(item.body.requestId, item.sender);
649
+ cancelMessageIds.push(item.messageId);
650
+ }
651
+ catch {
652
+ Logger.warn(`[PP CLIENT] Invalid cancellation proof for requestId=${item.body.requestId}, discarding`);
653
+ malformedMessageIds.push(item.messageId);
654
+ }
655
+ continue;
656
+ }
657
+ }
658
+ const expiredMessageIds = [];
659
+ const outOfRangeMessageIds = [];
660
+ const cancelledOriginalMessageIds = [];
661
+ const active = [];
662
+ for (const item of parsed) {
663
+ // Skip cancellation messages themselves (already collected above)
664
+ if (item.body.cancelled === true)
665
+ continue;
666
+ const { requestId, amount, description, expiresAt } = item.body;
667
+ // Filter expired
668
+ if (expiresAt < now) {
669
+ expiredMessageIds.push(item.messageId);
670
+ continue;
671
+ }
672
+ // Filter cancelled originals — only if cancellation came from the same sender
673
+ if (cancelledRequests.has(requestId) && cancelledRequests.get(requestId) === item.sender) {
674
+ cancelledOriginalMessageIds.push(item.messageId);
675
+ continue;
676
+ }
677
+ // Filter out-of-range — apply defaults for any missing limit fields
678
+ const effectiveMin = (_a = limits === null || limits === void 0 ? void 0 : limits.minAmount) !== null && _a !== void 0 ? _a : types_js_1.DEFAULT_PAYMENT_REQUEST_MIN_AMOUNT;
679
+ const effectiveMax = (_b = limits === null || limits === void 0 ? void 0 : limits.maxAmount) !== null && _b !== void 0 ? _b : types_js_1.DEFAULT_PAYMENT_REQUEST_MAX_AMOUNT;
680
+ if (amount < effectiveMin || amount > effectiveMax) {
681
+ outOfRangeMessageIds.push(item.messageId);
682
+ continue;
683
+ }
684
+ // Verify HMAC proof — ensures message came from claimed sender
685
+ try {
686
+ const proofData = Array.from(new TextEncoder().encode(requestId + myIdentityKey));
687
+ await this.peerPayWalletClient.verifyHmac({
688
+ data: proofData,
689
+ hmac: hexToBytes(item.body.requestProof),
690
+ protocolID: [2, 'payment request auth'],
691
+ keyID: requestId,
692
+ counterparty: item.sender
693
+ }, this.originator);
694
+ }
695
+ catch {
696
+ Logger.warn(`[PP CLIENT] Invalid requestProof for requestId=${requestId}, discarding`);
697
+ malformedMessageIds.push(item.messageId);
698
+ continue;
699
+ }
700
+ active.push({
701
+ messageId: item.messageId,
702
+ sender: item.sender,
703
+ requestId,
704
+ amount,
705
+ description,
706
+ expiresAt
707
+ });
708
+ }
709
+ // Acknowledge expired
710
+ if (expiredMessageIds.length > 0) {
711
+ await this.acknowledgeMessage({ messageIds: expiredMessageIds, host: hostOverride });
712
+ }
713
+ // Acknowledge cancelled originals + cancel messages together
714
+ const cancelAckIds = [...cancelledOriginalMessageIds, ...cancelMessageIds];
715
+ if (cancelAckIds.length > 0) {
716
+ await this.acknowledgeMessage({ messageIds: cancelAckIds, host: hostOverride });
717
+ }
718
+ // Acknowledge out-of-range
719
+ if (outOfRangeMessageIds.length > 0) {
720
+ await this.acknowledgeMessage({ messageIds: outOfRangeMessageIds, host: hostOverride });
721
+ }
722
+ // Acknowledge malformed messages so they don't reappear
723
+ if (malformedMessageIds.length > 0) {
724
+ await this.acknowledgeMessage({ messageIds: malformedMessageIds, host: hostOverride });
725
+ }
726
+ return active;
727
+ }
728
+ /**
729
+ * Cancels a previously sent payment request by sending a cancellation message
730
+ * with the same requestId and `cancelled: true`.
731
+ *
732
+ * @param {Object} params - Cancellation parameters.
733
+ * @param {string} params.recipient - The identity key of the recipient of the original request.
734
+ * @param {string} params.requestId - The requestId of the payment request to cancel.
735
+ * @param {string} [hostOverride] - Optional host override for the message box server.
736
+ * @returns {Promise<void>} Resolves when the cancellation message has been sent.
737
+ */
738
+ async cancelPaymentRequest(params, hostOverride) {
739
+ const senderIdentityKey = await this.getIdentityKey();
740
+ const body = {
741
+ requestId: params.requestId,
742
+ senderIdentityKey,
743
+ requestProof: params.requestProof,
744
+ cancelled: true
745
+ };
746
+ await this.sendMessage({
747
+ recipient: params.recipient,
748
+ messageBox: exports.PAYMENT_REQUESTS_MESSAGEBOX,
749
+ body: JSON.stringify(body)
750
+ }, hostOverride);
751
+ }
351
752
  }
352
753
  exports.PeerPayClient = PeerPayClient;
353
754
  //# sourceMappingURL=PeerPayClient.js.map