@bsv/message-box-client 1.3.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.3.0",
3
+ "version": "1.4.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -75,6 +75,6 @@
75
75
  },
76
76
  "dependencies": {
77
77
  "@bsv/authsocket-client": "^1.0.12",
78
- "@bsv/sdk": "^1.7.6"
78
+ "@bsv/sdk": "^1.8.2"
79
79
  }
80
80
  }
@@ -39,7 +39,7 @@ import {
39
39
  InternalizeOutput,
40
40
  Random,
41
41
  OriginatorDomainNameStringUnder250Bytes
42
-
42
+
43
43
  } from '@bsv/sdk'
44
44
  import { AuthSocketClient } from '@bsv/authsocket-client'
45
45
  import * as Logger from './Utils/logger.js'
@@ -315,7 +315,7 @@ export class MessageBoxClient {
315
315
  if (typeof targetHost !== 'string' || targetHost.trim() === '') {
316
316
  throw new Error('Cannot initialize WebSocket: No valid host provided')
317
317
  }
318
- this.socket = AuthSocketClient(targetHost, { wallet: this.walletClient, originator: this.originator}, )
318
+ this.socket = AuthSocketClient(targetHost, { wallet: this.walletClient, originator: this.originator })
319
319
 
320
320
  let identitySent = false
321
321
  let authenticated = false
@@ -410,7 +410,7 @@ export class MessageBoxClient {
410
410
  */
411
411
  async queryAdvertisements (
412
412
  identityKey?: string,
413
- host?: string,
413
+ host?: string
414
414
  ): Promise<AdvertisementToken[]> {
415
415
  const hosts: AdvertisementToken[] = []
416
416
  try {
@@ -639,7 +639,7 @@ export class MessageBoxClient {
639
639
  body,
640
640
  messageId,
641
641
  skipEncryption,
642
- checkPermissions,
642
+ checkPermissions
643
643
  }: SendMessageParams, overrideHost?: string): Promise<SendMessageResponse> {
644
644
  if (recipient == null || recipient.trim() === '') {
645
645
  throw new Error('[MB CLIENT ERROR] Recipient identity key is required')
@@ -861,7 +861,7 @@ export class MessageBoxClient {
861
861
  */
862
862
  async sendMessage (
863
863
  message: SendMessageParams,
864
- overrideHost?: string,
864
+ overrideHost?: string
865
865
  ): Promise<SendMessageResponse> {
866
866
  await this.assertInitialized()
867
867
  if (message.recipient == null || message.recipient.trim() === '') {
@@ -1219,7 +1219,7 @@ export class MessageBoxClient {
1219
1219
  if (hosts.length === 0) {
1220
1220
  const advertisedHosts = await this.queryAdvertisements(
1221
1221
  await this.getIdentityKey(),
1222
- undefined,
1222
+ undefined
1223
1223
  )
1224
1224
  hosts = Array.from(new Set([this.host, ...advertisedHosts.map(h => h.host)]))
1225
1225
  }
@@ -2157,7 +2157,7 @@ export class MessageBoxClient {
2157
2157
  private async createMessagePayment (
2158
2158
  recipient: string,
2159
2159
  quote: MessageBoxQuote,
2160
- description: string = 'MessageBox delivery payment',
2160
+ description: string = 'MessageBox delivery payment'
2161
2161
  ): Promise<Payment> {
2162
2162
  if (quote.recipientFee <= 0 && quote.deliveryFee <= 0) {
2163
2163
  throw new Error('No payment required')
@@ -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,19 +76,22 @@ 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
- this._authFetchInstance = new AuthFetch(this.peerPayWalletClient, undefined, undefined, this.originator)
94
+ this._authFetchInstance = new AuthFetch(this.peerPayWalletClient, undefined, undefined, this.originator)
91
95
  }
92
96
  return this._authFetchInstance
93
97
  }
@@ -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
  };
@@ -136,6 +140,7 @@ export class PeerPayClient extends MessageBoxClient {
136
140
  // Create the payment action
137
141
  const paymentAction = await this.peerPayWalletClient.createAction({
138
142
  description: 'PeerPay payment',
143
+ labels: ['peerpay'],
139
144
  outputs: [{
140
145
  satoshis: payment.amount,
141
146
  lockingScript,
@@ -180,7 +185,7 @@ export class PeerPayClient extends MessageBoxClient {
180
185
  * @returns {Promise<any>} Resolves with the payment result.
181
186
  * @throws {Error} If the recipient is missing or the amount is invalid.
182
187
  */
183
- async sendPayment (payment: PaymentParams, hostOverride?: string): Promise<any> {
188
+ async sendPayment(payment: PaymentParams, hostOverride?: string): Promise<any> {
184
189
  if (payment.recipient == null || payment.recipient.trim() === '' || payment.amount <= 0) {
185
190
  throw new Error('Invalid payment details: recipient and valid amount are required')
186
191
  }
@@ -190,7 +195,7 @@ export class PeerPayClient extends MessageBoxClient {
190
195
  // Ensure the recipient is included before sendings
191
196
  await this.sendMessage({
192
197
  recipient: payment.recipient,
193
- messageBox: STANDARD_PAYMENT_MESSAGEBOX,
198
+ messageBox: this.messageBox,
194
199
  body: JSON.stringify(paymentToken)
195
200
  }, hostOverride)
196
201
  }
@@ -209,15 +214,15 @@ export class PeerPayClient extends MessageBoxClient {
209
214
  * @returns {Promise<void>} Resolves when the payment has been sent.
210
215
  * @throws {Error} If payment token generation fails.
211
216
  */
212
- async sendLivePayment (payment: PaymentParams, overrideHost?: string): Promise<void> {
217
+ async sendLivePayment(payment: PaymentParams, overrideHost?: string): Promise<void> {
213
218
  const paymentToken = await this.createPaymentToken(payment)
214
219
 
215
220
  try {
216
221
  // Attempt WebSocket first
217
222
  await this.sendLiveMessage({
218
223
  recipient: payment.recipient,
219
- messageBox: STANDARD_PAYMENT_MESSAGEBOX,
220
- body: JSON.stringify(paymentToken),
224
+ messageBox: this.messageBox,
225
+ body: JSON.stringify(paymentToken)
221
226
  }, overrideHost)
222
227
  } catch (err) {
223
228
  Logger.warn('[PP CLIENT] sendLiveMessage failed, falling back to HTTP:', err)
@@ -225,8 +230,8 @@ export class PeerPayClient extends MessageBoxClient {
225
230
  // Fallback to HTTP if WebSocket fails
226
231
  await this.sendMessage({
227
232
  recipient: payment.recipient,
228
- messageBox: STANDARD_PAYMENT_MESSAGEBOX,
229
- body: JSON.stringify(paymentToken),
233
+ messageBox: this.messageBox,
234
+ body: JSON.stringify(paymentToken)
230
235
  }, overrideHost)
231
236
  }
232
237
  }
@@ -243,16 +248,16 @@ export class PeerPayClient extends MessageBoxClient {
243
248
  * @param {string} [obj.overrideHost] - Optional host override for WebSocket connection.
244
249
  * @returns {Promise<void>} Resolves when the listener is successfully set up.
245
250
  */
246
- async listenForLivePayments ({
251
+ async listenForLivePayments({
247
252
  onPayment,
248
- overrideHost,
253
+ overrideHost
249
254
  }: {
250
255
  onPayment: (payment: IncomingPayment) => void
251
256
  overrideHost?: string
252
257
  }): Promise<void> {
253
258
  await this.listenForLiveMessages({
254
- messageBox: STANDARD_PAYMENT_MESSAGEBOX,
255
- overrideHost,
259
+ messageBox: this.messageBox,
260
+ overrideHost,
256
261
 
257
262
  // Convert PeerMessage → IncomingPayment before calling onPayment
258
263
  onMessage: (message: PeerMessage) => {
@@ -279,7 +284,7 @@ export class PeerPayClient extends MessageBoxClient {
279
284
  * @returns {Promise<any>} Resolves with the payment result if successful.
280
285
  * @throws {Error} If payment processing fails.
281
286
  */
282
- async acceptPayment (payment: IncomingPayment): Promise<any> {
287
+ async acceptPayment(payment: IncomingPayment): Promise<any> {
283
288
  try {
284
289
  Logger.log(`[PP CLIENT] Processing payment: ${JSON.stringify(payment, null, 2)}`)
285
290
 
@@ -294,13 +299,14 @@ export class PeerPayClient extends MessageBoxClient {
294
299
  outputIndex: STANDARD_PAYMENT_OUTPUT_INDEX,
295
300
  protocol: 'wallet payment'
296
301
  }],
302
+ labels: ['peerpay'],
297
303
  description: 'PeerPay Payment'
298
304
  }, this.originator)
299
305
 
300
306
  Logger.log(`[PP CLIENT] Payment internalized successfully: ${JSON.stringify(paymentResult, null, 2)}`)
301
307
  Logger.log(`[PP CLIENT] Acknowledging payment with messageId: ${payment.messageId}`)
302
308
 
303
- await this.acknowledgeMessage({ messageIds: [payment.messageId]})
309
+ await this.acknowledgeMessage({ messageIds: [payment.messageId] })
304
310
 
305
311
  return { payment, paymentResult }
306
312
  } catch (error) {
@@ -319,7 +325,7 @@ export class PeerPayClient extends MessageBoxClient {
319
325
  * @param {IncomingPayment} payment - The payment object containing transaction details.
320
326
  * @returns {Promise<void>} Resolves when the payment is either acknowledged or refunded.
321
327
  */
322
- async rejectPayment (payment: IncomingPayment): Promise<void> {
328
+ async rejectPayment(payment: IncomingPayment): Promise<void> {
323
329
  Logger.log(`[PP CLIENT] Rejecting payment: ${JSON.stringify(payment, null, 2)}`)
324
330
 
325
331
  if (payment.token.amount - 1000 < 1000) {
@@ -380,8 +386,8 @@ export class PeerPayClient extends MessageBoxClient {
380
386
  * @param {string} [overrideHost] - Optional host override to list payments from
381
387
  * @returns {Promise<IncomingPayment[]>} Resolves with an array of pending payments.
382
388
  */
383
- async listIncomingPayments (overrideHost?: string): Promise<IncomingPayment[]> {
384
- 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 })
385
391
  return messages.map((msg: any) => {
386
392
  const parsedToken = safeParse<PaymentToken>(msg.body)
387
393