Skip to content

Commit 8358542

Browse files
Merge pull request #21 from bsv-blockchain/fix/support-custom-messagebox
Fix/support custom messagebox
2 parents 44a2c8a + d36906e commit 8358542

File tree

3 files changed

+23
-39
lines changed

3 files changed

+23
-39
lines changed

package-lock.json

Lines changed: 3 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bsv/message-box-client",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"publishConfig": {
55
"access": "public"
66
},

src/PeerPayClient.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { WalletInterface, P2PKH, PublicKey, createNonce, AtomicBEEF, AuthFetch,
1616

1717
import * as Logger from './Utils/logger.js'
1818

19-
function safeParse<T> (input: any): T {
19+
function safeParse<T>(input: any): T {
2020
try {
2121
return typeof input === 'string' ? JSON.parse(input) : input
2222
} catch (e) {
@@ -35,6 +35,7 @@ const STANDARD_PAYMENT_OUTPUT_INDEX = 0
3535
*/
3636
export interface PeerPayClientConfig {
3737
messageBoxHost?: string
38+
messageBox?: string
3839
walletClient: WalletInterface
3940
enableLogging?: boolean // Added optional logging flag,
4041
originator?: OriginatorDomainNameStringUnder250Bytes
@@ -75,17 +76,20 @@ export interface IncomingPayment {
7576
export class PeerPayClient extends MessageBoxClient {
7677
private readonly peerPayWalletClient: WalletInterface
7778
private _authFetchInstance?: AuthFetch
78-
constructor (config: PeerPayClientConfig) {
79+
private messageBox: string
80+
81+
constructor(config: PeerPayClientConfig) {
7982
const { messageBoxHost = 'https://messagebox.babbage.systems', walletClient, enableLogging = false, originator } = config
8083

8184
// 🔹 Pass enableLogging to MessageBoxClient
8285
super({ host: messageBoxHost, walletClient, enableLogging, originator })
8386

87+
this.messageBox = config.messageBox ?? STANDARD_PAYMENT_MESSAGEBOX
8488
this.peerPayWalletClient = walletClient
8589
this.originator = originator
8690
}
8791

88-
private get authFetchInstance (): AuthFetch {
92+
private get authFetchInstance(): AuthFetch {
8993
if (this._authFetchInstance === null || this._authFetchInstance === undefined) {
9094
this._authFetchInstance = new AuthFetch(this.peerPayWalletClient, undefined, undefined, this.originator)
9195
}
@@ -104,7 +108,7 @@ export class PeerPayClient extends MessageBoxClient {
104108
* @returns {Promise<PaymentToken>} A valid payment token containing transaction details.
105109
* @throws {Error} If the recipient's public key cannot be derived.
106110
*/
107-
async createPaymentToken (payment: PaymentParams): Promise<PaymentToken> {
111+
async createPaymentToken(payment: PaymentParams): Promise<PaymentToken> {
108112
if (payment.amount <= 0) {
109113
throw new Error('Invalid payment details: recipient and valid amount are required')
110114
};
@@ -181,7 +185,7 @@ export class PeerPayClient extends MessageBoxClient {
181185
* @returns {Promise<any>} Resolves with the payment result.
182186
* @throws {Error} If the recipient is missing or the amount is invalid.
183187
*/
184-
async sendPayment (payment: PaymentParams, hostOverride?: string): Promise<any> {
188+
async sendPayment(payment: PaymentParams, hostOverride?: string): Promise<any> {
185189
if (payment.recipient == null || payment.recipient.trim() === '' || payment.amount <= 0) {
186190
throw new Error('Invalid payment details: recipient and valid amount are required')
187191
}
@@ -191,7 +195,7 @@ export class PeerPayClient extends MessageBoxClient {
191195
// Ensure the recipient is included before sendings
192196
await this.sendMessage({
193197
recipient: payment.recipient,
194-
messageBox: STANDARD_PAYMENT_MESSAGEBOX,
198+
messageBox: this.messageBox,
195199
body: JSON.stringify(paymentToken)
196200
}, hostOverride)
197201
}
@@ -210,14 +214,14 @@ export class PeerPayClient extends MessageBoxClient {
210214
* @returns {Promise<void>} Resolves when the payment has been sent.
211215
* @throws {Error} If payment token generation fails.
212216
*/
213-
async sendLivePayment (payment: PaymentParams, overrideHost?: string): Promise<void> {
217+
async sendLivePayment(payment: PaymentParams, overrideHost?: string): Promise<void> {
214218
const paymentToken = await this.createPaymentToken(payment)
215219

216220
try {
217221
// Attempt WebSocket first
218222
await this.sendLiveMessage({
219223
recipient: payment.recipient,
220-
messageBox: STANDARD_PAYMENT_MESSAGEBOX,
224+
messageBox: this.messageBox,
221225
body: JSON.stringify(paymentToken)
222226
}, overrideHost)
223227
} catch (err) {
@@ -226,7 +230,7 @@ export class PeerPayClient extends MessageBoxClient {
226230
// Fallback to HTTP if WebSocket fails
227231
await this.sendMessage({
228232
recipient: payment.recipient,
229-
messageBox: STANDARD_PAYMENT_MESSAGEBOX,
233+
messageBox: this.messageBox,
230234
body: JSON.stringify(paymentToken)
231235
}, overrideHost)
232236
}
@@ -244,15 +248,15 @@ export class PeerPayClient extends MessageBoxClient {
244248
* @param {string} [obj.overrideHost] - Optional host override for WebSocket connection.
245249
* @returns {Promise<void>} Resolves when the listener is successfully set up.
246250
*/
247-
async listenForLivePayments ({
251+
async listenForLivePayments({
248252
onPayment,
249253
overrideHost
250254
}: {
251255
onPayment: (payment: IncomingPayment) => void
252256
overrideHost?: string
253257
}): Promise<void> {
254258
await this.listenForLiveMessages({
255-
messageBox: STANDARD_PAYMENT_MESSAGEBOX,
259+
messageBox: this.messageBox,
256260
overrideHost,
257261

258262
// Convert PeerMessage → IncomingPayment before calling onPayment
@@ -280,7 +284,7 @@ export class PeerPayClient extends MessageBoxClient {
280284
* @returns {Promise<any>} Resolves with the payment result if successful.
281285
* @throws {Error} If payment processing fails.
282286
*/
283-
async acceptPayment (payment: IncomingPayment): Promise<any> {
287+
async acceptPayment(payment: IncomingPayment): Promise<any> {
284288
try {
285289
Logger.log(`[PP CLIENT] Processing payment: ${JSON.stringify(payment, null, 2)}`)
286290

@@ -321,7 +325,7 @@ export class PeerPayClient extends MessageBoxClient {
321325
* @param {IncomingPayment} payment - The payment object containing transaction details.
322326
* @returns {Promise<void>} Resolves when the payment is either acknowledged or refunded.
323327
*/
324-
async rejectPayment (payment: IncomingPayment): Promise<void> {
328+
async rejectPayment(payment: IncomingPayment): Promise<void> {
325329
Logger.log(`[PP CLIENT] Rejecting payment: ${JSON.stringify(payment, null, 2)}`)
326330

327331
if (payment.token.amount - 1000 < 1000) {
@@ -382,8 +386,8 @@ export class PeerPayClient extends MessageBoxClient {
382386
* @param {string} [overrideHost] - Optional host override to list payments from
383387
* @returns {Promise<IncomingPayment[]>} Resolves with an array of pending payments.
384388
*/
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 })
387391
return messages.map((msg: any) => {
388392
const parsedToken = safeParse<PaymentToken>(msg.body)
389393

0 commit comments

Comments
 (0)