@bsv/message-box-client 1.4.0 → 1.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/message-box-client",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -16,7 +16,7 @@ import { WalletInterface, P2PKH, PublicKey, createNonce, AtomicBEEF, AuthFetch,
16
16
 
17
17
  import * as Logger from './Utils/logger.js'
18
18
 
19
- function safeParse<T> (input: any): T {
19
+ function safeParse<T>(input: any): T {
20
20
  try {
21
21
  return typeof input === 'string' ? JSON.parse(input) : input
22
22
  } catch (e) {
@@ -35,6 +35,7 @@ const STANDARD_PAYMENT_OUTPUT_INDEX = 0
35
35
  */
36
36
  export interface PeerPayClientConfig {
37
37
  messageBoxHost?: string
38
+ messageBox?: string
38
39
  walletClient: WalletInterface
39
40
  enableLogging?: boolean // Added optional logging flag,
40
41
  originator?: OriginatorDomainNameStringUnder250Bytes
@@ -75,17 +76,20 @@ export interface IncomingPayment {
75
76
  export class PeerPayClient extends MessageBoxClient {
76
77
  private readonly peerPayWalletClient: WalletInterface
77
78
  private _authFetchInstance?: AuthFetch
78
- constructor (config: PeerPayClientConfig) {
79
+ private messageBox: string
80
+
81
+ constructor(config: PeerPayClientConfig) {
79
82
  const { messageBoxHost = 'https://messagebox.babbage.systems', walletClient, enableLogging = false, originator } = config
80
83
 
81
84
  // 🔹 Pass enableLogging to MessageBoxClient
82
85
  super({ host: messageBoxHost, walletClient, enableLogging, originator })
83
86
 
87
+ this.messageBox = config.messageBox ?? STANDARD_PAYMENT_MESSAGEBOX
84
88
  this.peerPayWalletClient = walletClient
85
89
  this.originator = originator
86
90
  }
87
91
 
88
- private get authFetchInstance (): AuthFetch {
92
+ private get authFetchInstance(): AuthFetch {
89
93
  if (this._authFetchInstance === null || this._authFetchInstance === undefined) {
90
94
  this._authFetchInstance = new AuthFetch(this.peerPayWalletClient, undefined, undefined, this.originator)
91
95
  }
@@ -104,7 +108,7 @@ export class PeerPayClient extends MessageBoxClient {
104
108
  * @returns {Promise<PaymentToken>} A valid payment token containing transaction details.
105
109
  * @throws {Error} If the recipient's public key cannot be derived.
106
110
  */
107
- async createPaymentToken (payment: PaymentParams): Promise<PaymentToken> {
111
+ async createPaymentToken(payment: PaymentParams): Promise<PaymentToken> {
108
112
  if (payment.amount <= 0) {
109
113
  throw new Error('Invalid payment details: recipient and valid amount are required')
110
114
  };
@@ -181,7 +185,7 @@ export class PeerPayClient extends MessageBoxClient {
181
185
  * @returns {Promise<any>} Resolves with the payment result.
182
186
  * @throws {Error} If the recipient is missing or the amount is invalid.
183
187
  */
184
- async sendPayment (payment: PaymentParams, hostOverride?: string): Promise<any> {
188
+ async sendPayment(payment: PaymentParams, hostOverride?: string): Promise<any> {
185
189
  if (payment.recipient == null || payment.recipient.trim() === '' || payment.amount <= 0) {
186
190
  throw new Error('Invalid payment details: recipient and valid amount are required')
187
191
  }
@@ -191,7 +195,7 @@ export class PeerPayClient extends MessageBoxClient {
191
195
  // Ensure the recipient is included before sendings
192
196
  await this.sendMessage({
193
197
  recipient: payment.recipient,
194
- messageBox: STANDARD_PAYMENT_MESSAGEBOX,
198
+ messageBox: this.messageBox,
195
199
  body: JSON.stringify(paymentToken)
196
200
  }, hostOverride)
197
201
  }
@@ -210,14 +214,14 @@ export class PeerPayClient extends MessageBoxClient {
210
214
  * @returns {Promise<void>} Resolves when the payment has been sent.
211
215
  * @throws {Error} If payment token generation fails.
212
216
  */
213
- async sendLivePayment (payment: PaymentParams, overrideHost?: string): Promise<void> {
217
+ async sendLivePayment(payment: PaymentParams, overrideHost?: string): Promise<void> {
214
218
  const paymentToken = await this.createPaymentToken(payment)
215
219
 
216
220
  try {
217
221
  // Attempt WebSocket first
218
222
  await this.sendLiveMessage({
219
223
  recipient: payment.recipient,
220
- messageBox: STANDARD_PAYMENT_MESSAGEBOX,
224
+ messageBox: this.messageBox,
221
225
  body: JSON.stringify(paymentToken)
222
226
  }, overrideHost)
223
227
  } catch (err) {
@@ -226,7 +230,7 @@ export class PeerPayClient extends MessageBoxClient {
226
230
  // Fallback to HTTP if WebSocket fails
227
231
  await this.sendMessage({
228
232
  recipient: payment.recipient,
229
- messageBox: STANDARD_PAYMENT_MESSAGEBOX,
233
+ messageBox: this.messageBox,
230
234
  body: JSON.stringify(paymentToken)
231
235
  }, overrideHost)
232
236
  }
@@ -244,7 +248,7 @@ export class PeerPayClient extends MessageBoxClient {
244
248
  * @param {string} [obj.overrideHost] - Optional host override for WebSocket connection.
245
249
  * @returns {Promise<void>} Resolves when the listener is successfully set up.
246
250
  */
247
- async listenForLivePayments ({
251
+ async listenForLivePayments({
248
252
  onPayment,
249
253
  overrideHost
250
254
  }: {
@@ -252,7 +256,7 @@ export class PeerPayClient extends MessageBoxClient {
252
256
  overrideHost?: string
253
257
  }): Promise<void> {
254
258
  await this.listenForLiveMessages({
255
- messageBox: STANDARD_PAYMENT_MESSAGEBOX,
259
+ messageBox: this.messageBox,
256
260
  overrideHost,
257
261
 
258
262
  // Convert PeerMessage → IncomingPayment before calling onPayment
@@ -280,7 +284,7 @@ export class PeerPayClient extends MessageBoxClient {
280
284
  * @returns {Promise<any>} Resolves with the payment result if successful.
281
285
  * @throws {Error} If payment processing fails.
282
286
  */
283
- async acceptPayment (payment: IncomingPayment): Promise<any> {
287
+ async acceptPayment(payment: IncomingPayment): Promise<any> {
284
288
  try {
285
289
  Logger.log(`[PP CLIENT] Processing payment: ${JSON.stringify(payment, null, 2)}`)
286
290
 
@@ -321,7 +325,7 @@ export class PeerPayClient extends MessageBoxClient {
321
325
  * @param {IncomingPayment} payment - The payment object containing transaction details.
322
326
  * @returns {Promise<void>} Resolves when the payment is either acknowledged or refunded.
323
327
  */
324
- async rejectPayment (payment: IncomingPayment): Promise<void> {
328
+ async rejectPayment(payment: IncomingPayment): Promise<void> {
325
329
  Logger.log(`[PP CLIENT] Rejecting payment: ${JSON.stringify(payment, null, 2)}`)
326
330
 
327
331
  if (payment.token.amount - 1000 < 1000) {
@@ -382,8 +386,8 @@ export class PeerPayClient extends MessageBoxClient {
382
386
  * @param {string} [overrideHost] - Optional host override to list payments from
383
387
  * @returns {Promise<IncomingPayment[]>} Resolves with an array of pending payments.
384
388
  */
385
- async listIncomingPayments (overrideHost?: string): Promise<IncomingPayment[]> {
386
- const messages = await this.listMessages({ messageBox: STANDARD_PAYMENT_MESSAGEBOX, host: overrideHost })
389
+ async listIncomingPayments(overrideHost?: string): Promise<IncomingPayment[]> {
390
+ const messages = await this.listMessages({ messageBox: this.messageBox, host: overrideHost })
387
391
  return messages.map((msg: any) => {
388
392
  const parsedToken = safeParse<PaymentToken>(msg.body)
389
393