@bsv/message-box-client 1.2.3 → 1.2.5
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/dist/cjs/package.json +1 -1
- package/dist/cjs/src/MessageBoxClient.js +175 -43
- package/dist/cjs/src/MessageBoxClient.js.map +1 -1
- package/dist/cjs/src/PeerPayClient.js +8 -5
- package/dist/cjs/src/PeerPayClient.js.map +1 -1
- package/dist/cjs/src/__tests/MessageBoxClient.test.js +0 -15
- package/dist/cjs/src/__tests/MessageBoxClient.test.js.map +1 -1
- package/dist/cjs/src/__tests/PeerPayClientUnit.test.js +2 -2
- package/dist/cjs/src/__tests/PeerPayClientUnit.test.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/MessageBoxClient.js +174 -43
- package/dist/esm/src/MessageBoxClient.js.map +1 -1
- package/dist/esm/src/PeerPayClient.js +8 -5
- package/dist/esm/src/PeerPayClient.js.map +1 -1
- package/dist/esm/src/__tests/MessageBoxClient.test.js +0 -15
- package/dist/esm/src/__tests/MessageBoxClient.test.js.map +1 -1
- package/dist/esm/src/__tests/PeerPayClientUnit.test.js +2 -2
- package/dist/esm/src/__tests/PeerPayClientUnit.test.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/MessageBoxClient.d.ts +101 -11
- package/dist/types/src/MessageBoxClient.d.ts.map +1 -1
- package/dist/types/src/PeerPayClient.d.ts +6 -3
- package/dist/types/src/PeerPayClient.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/dist/umd/bundle.js +1 -1
- package/package.json +1 -1
- package/src/MessageBoxClient.ts +235 -77
- package/src/PeerPayClient.ts +21 -14
- package/src/__tests/MessageBoxClient.test.ts +0 -23
- package/src/__tests/PeerPayClientUnit.test.ts +2 -2
package/src/PeerPayClient.ts
CHANGED
|
@@ -15,7 +15,7 @@ import { PeerMessage } from './types.js'
|
|
|
15
15
|
import { WalletClient, P2PKH, PublicKey, createNonce, AtomicBEEF, AuthFetch, Base64String } from '@bsv/sdk'
|
|
16
16
|
import * as Logger from './Utils/logger.js'
|
|
17
17
|
|
|
18
|
-
function safeParse<T>(input: any): T {
|
|
18
|
+
function safeParse<T> (input: any): T {
|
|
19
19
|
try {
|
|
20
20
|
return typeof input === 'string' ? JSON.parse(input) : input
|
|
21
21
|
} catch (e) {
|
|
@@ -74,7 +74,7 @@ export class PeerPayClient extends MessageBoxClient {
|
|
|
74
74
|
private readonly peerPayWalletClient: WalletClient
|
|
75
75
|
private _authFetchInstance?: AuthFetch
|
|
76
76
|
|
|
77
|
-
constructor(config: PeerPayClientConfig) {
|
|
77
|
+
constructor (config: PeerPayClientConfig) {
|
|
78
78
|
const { messageBoxHost = 'https://messagebox.babbage.systems', walletClient, enableLogging = false } = config
|
|
79
79
|
|
|
80
80
|
// 🔹 Pass enableLogging to MessageBoxClient
|
|
@@ -83,7 +83,7 @@ export class PeerPayClient extends MessageBoxClient {
|
|
|
83
83
|
this.peerPayWalletClient = walletClient
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
private get authFetchInstance(): AuthFetch {
|
|
86
|
+
private get authFetchInstance (): AuthFetch {
|
|
87
87
|
if (this._authFetchInstance === null || this._authFetchInstance === undefined) {
|
|
88
88
|
this._authFetchInstance = new AuthFetch(this.peerPayWalletClient)
|
|
89
89
|
}
|
|
@@ -102,7 +102,7 @@ export class PeerPayClient extends MessageBoxClient {
|
|
|
102
102
|
* @returns {Promise<PaymentToken>} A valid payment token containing transaction details.
|
|
103
103
|
* @throws {Error} If the recipient's public key cannot be derived.
|
|
104
104
|
*/
|
|
105
|
-
async createPaymentToken(payment: PaymentParams): Promise<PaymentToken> {
|
|
105
|
+
async createPaymentToken (payment: PaymentParams): Promise<PaymentToken> {
|
|
106
106
|
if (payment.amount <= 0) {
|
|
107
107
|
throw new Error('Invalid payment details: recipient and valid amount are required')
|
|
108
108
|
};
|
|
@@ -175,10 +175,11 @@ export class PeerPayClient extends MessageBoxClient {
|
|
|
175
175
|
* @param {PaymentParams} payment - The payment details.
|
|
176
176
|
* @param {string} payment.recipient - The recipient's identity key.
|
|
177
177
|
* @param {number} payment.amount - The amount in satoshis to send.
|
|
178
|
+
* @param {string} [hostOverride] - Optional host override for the message box server.
|
|
178
179
|
* @returns {Promise<any>} Resolves with the payment result.
|
|
179
180
|
* @throws {Error} If the recipient is missing or the amount is invalid.
|
|
180
181
|
*/
|
|
181
|
-
async sendPayment(payment: PaymentParams): Promise<any> {
|
|
182
|
+
async sendPayment (payment: PaymentParams, hostOverride?: string): Promise<any> {
|
|
182
183
|
if (payment.recipient == null || payment.recipient.trim() === '' || payment.amount <= 0) {
|
|
183
184
|
throw new Error('Invalid payment details: recipient and valid amount are required')
|
|
184
185
|
}
|
|
@@ -190,7 +191,7 @@ export class PeerPayClient extends MessageBoxClient {
|
|
|
190
191
|
recipient: payment.recipient,
|
|
191
192
|
messageBox: STANDARD_PAYMENT_MESSAGEBOX,
|
|
192
193
|
body: JSON.stringify(paymentToken)
|
|
193
|
-
})
|
|
194
|
+
}, hostOverride)
|
|
194
195
|
}
|
|
195
196
|
|
|
196
197
|
/**
|
|
@@ -206,7 +207,7 @@ export class PeerPayClient extends MessageBoxClient {
|
|
|
206
207
|
* @returns {Promise<void>} Resolves when the payment has been sent.
|
|
207
208
|
* @throws {Error} If payment token generation fails.
|
|
208
209
|
*/
|
|
209
|
-
async sendLivePayment(payment: PaymentParams): Promise<void> {
|
|
210
|
+
async sendLivePayment (payment: PaymentParams): Promise<void> {
|
|
210
211
|
const paymentToken = await this.createPaymentToken(payment)
|
|
211
212
|
|
|
212
213
|
try {
|
|
@@ -239,11 +240,16 @@ export class PeerPayClient extends MessageBoxClient {
|
|
|
239
240
|
* @param {Function} obj.onPayment - Callback function triggered when a payment is received.
|
|
240
241
|
* @returns {Promise<void>} Resolves when the listener is successfully set up.
|
|
241
242
|
*/
|
|
242
|
-
async listenForLivePayments({
|
|
243
|
-
onPayment
|
|
244
|
-
|
|
243
|
+
async listenForLivePayments ({
|
|
244
|
+
onPayment,
|
|
245
|
+
overrideHost
|
|
246
|
+
}: {
|
|
247
|
+
onPayment: (payment: IncomingPayment) => void
|
|
248
|
+
overrideHost?: string
|
|
249
|
+
}): Promise<void> {
|
|
245
250
|
await this.listenForLiveMessages({
|
|
246
251
|
messageBox: STANDARD_PAYMENT_MESSAGEBOX,
|
|
252
|
+
overrideHost,
|
|
247
253
|
|
|
248
254
|
// Convert PeerMessage → IncomingPayment before calling onPayment
|
|
249
255
|
onMessage: (message: PeerMessage) => {
|
|
@@ -270,7 +276,7 @@ export class PeerPayClient extends MessageBoxClient {
|
|
|
270
276
|
* @returns {Promise<any>} Resolves with the payment result if successful.
|
|
271
277
|
* @throws {Error} If payment processing fails.
|
|
272
278
|
*/
|
|
273
|
-
async acceptPayment(payment: IncomingPayment): Promise<any> {
|
|
279
|
+
async acceptPayment (payment: IncomingPayment): Promise<any> {
|
|
274
280
|
try {
|
|
275
281
|
Logger.log(`[PP CLIENT] Processing payment: ${JSON.stringify(payment, null, 2)}`)
|
|
276
282
|
|
|
@@ -310,7 +316,7 @@ export class PeerPayClient extends MessageBoxClient {
|
|
|
310
316
|
* @param {IncomingPayment} payment - The payment object containing transaction details.
|
|
311
317
|
* @returns {Promise<void>} Resolves when the payment is either acknowledged or refunded.
|
|
312
318
|
*/
|
|
313
|
-
async rejectPayment(payment: IncomingPayment): Promise<void> {
|
|
319
|
+
async rejectPayment (payment: IncomingPayment): Promise<void> {
|
|
314
320
|
Logger.log(`[PP CLIENT] Rejecting payment: ${JSON.stringify(payment, null, 2)}`)
|
|
315
321
|
|
|
316
322
|
if (payment.token.amount - 1000 < 1000) {
|
|
@@ -368,10 +374,11 @@ export class PeerPayClient extends MessageBoxClient {
|
|
|
368
374
|
* This function queries the message box for new messages and transforms
|
|
369
375
|
* them into `IncomingPayment` objects by extracting relevant fields.
|
|
370
376
|
*
|
|
377
|
+
* @param {string} [overrideHost] - Optional host override to list payments from
|
|
371
378
|
* @returns {Promise<IncomingPayment[]>} Resolves with an array of pending payments.
|
|
372
379
|
*/
|
|
373
|
-
async listIncomingPayments(): Promise<IncomingPayment[]> {
|
|
374
|
-
const messages = await this.listMessages({ messageBox: STANDARD_PAYMENT_MESSAGEBOX })
|
|
380
|
+
async listIncomingPayments (overrideHost?: string): Promise<IncomingPayment[]> {
|
|
381
|
+
const messages = await this.listMessages({ messageBox: STANDARD_PAYMENT_MESSAGEBOX, host: overrideHost })
|
|
375
382
|
|
|
376
383
|
return messages.map((msg: any) => {
|
|
377
384
|
const parsedToken = safeParse<PaymentToken>(msg.body)
|
|
@@ -320,7 +320,6 @@ describe('MessageBoxClient', () => {
|
|
|
320
320
|
|
|
321
321
|
const result = await messageBoxClient.listMessages({ messageBox: 'test_inbox' })
|
|
322
322
|
|
|
323
|
-
|
|
324
323
|
expect(result).toEqual(JSON.parse(VALID_LIST_AND_READ_RESULT.body).messages)
|
|
325
324
|
})
|
|
326
325
|
|
|
@@ -438,28 +437,6 @@ describe('MessageBoxClient', () => {
|
|
|
438
437
|
.rejects.toThrow('Failed to acknowledge messages')
|
|
439
438
|
})
|
|
440
439
|
|
|
441
|
-
it('Throws an error when WebSocket is not initialized before listening for messages', async () => {
|
|
442
|
-
const messageBoxClient = new MessageBoxClient({
|
|
443
|
-
walletClient: mockWalletClient,
|
|
444
|
-
host: 'https://messagebox.babbage.systems',
|
|
445
|
-
enableLogging: true
|
|
446
|
-
})
|
|
447
|
-
await messageBoxClient.init()
|
|
448
|
-
|
|
449
|
-
// Stub out the identity key to pass that check
|
|
450
|
-
; (messageBoxClient as any).myIdentityKey = '02b463b8ef7f03c47fba2679c7334d13e4939b8ca30dbb6bbd22e34ea3e9b1b0e4'
|
|
451
|
-
|
|
452
|
-
// Stub out joinRoom to throw like the real one might
|
|
453
|
-
jest.spyOn(messageBoxClient, 'joinRoom').mockRejectedValue(new Error('WebSocket connection not initialized'))
|
|
454
|
-
|
|
455
|
-
await expect(
|
|
456
|
-
messageBoxClient.listenForLiveMessages({
|
|
457
|
-
onMessage: jest.fn(),
|
|
458
|
-
messageBox: 'test_inbox'
|
|
459
|
-
})
|
|
460
|
-
).rejects.toThrow('WebSocket connection not initialized')
|
|
461
|
-
})
|
|
462
|
-
|
|
463
440
|
it('Emits joinRoom event and listens for incoming messages', async () => {
|
|
464
441
|
const messageBoxClient = new MessageBoxClient({
|
|
465
442
|
walletClient: mockWalletClient,
|
|
@@ -119,7 +119,7 @@ describe('PeerPayClient Unit Tests', () => {
|
|
|
119
119
|
recipient: 'recipientKey',
|
|
120
120
|
messageBox: 'payment_inbox',
|
|
121
121
|
body: expect.any(String)
|
|
122
|
-
})
|
|
122
|
+
}, undefined)
|
|
123
123
|
}, 10000)
|
|
124
124
|
})
|
|
125
125
|
|
|
@@ -147,7 +147,7 @@ describe('PeerPayClient Unit Tests', () => {
|
|
|
147
147
|
expect(peerPayClient.sendLiveMessage).toHaveBeenCalledWith({
|
|
148
148
|
recipient: 'recipientKey',
|
|
149
149
|
messageBox: 'payment_inbox',
|
|
150
|
-
body:
|
|
150
|
+
body: '{"customInstructions":{"derivationPrefix":"prefix","derivationSuffix":"suffix"},"transaction":[1,2,3,4,5],"amount":2}'
|
|
151
151
|
})
|
|
152
152
|
})
|
|
153
153
|
})
|