@bsv/message-box-client 1.1.9 → 1.1.11

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 (36) hide show
  1. package/dist/cjs/package.json +5 -5
  2. package/dist/cjs/src/MessageBoxClient.js +707 -87
  3. package/dist/cjs/src/MessageBoxClient.js.map +1 -1
  4. package/dist/cjs/src/PeerPayClient.js +61 -28
  5. package/dist/cjs/src/PeerPayClient.js.map +1 -1
  6. package/dist/cjs/src/Utils/logger.js +22 -21
  7. package/dist/cjs/src/Utils/logger.js.map +1 -1
  8. package/dist/cjs/src/types/permissions.js +6 -0
  9. package/dist/cjs/src/types/permissions.js.map +1 -0
  10. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  11. package/dist/esm/src/MessageBoxClient.js +593 -12
  12. package/dist/esm/src/MessageBoxClient.js.map +1 -1
  13. package/dist/esm/src/PeerPayClient.js +1 -1
  14. package/dist/esm/src/PeerPayClient.js.map +1 -1
  15. package/dist/esm/src/Utils/logger.js +17 -19
  16. package/dist/esm/src/Utils/logger.js.map +1 -1
  17. package/dist/esm/src/types/permissions.js +5 -0
  18. package/dist/esm/src/types/permissions.js.map +1 -0
  19. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  20. package/dist/types/src/MessageBoxClient.d.ts +218 -13
  21. package/dist/types/src/MessageBoxClient.d.ts.map +1 -1
  22. package/dist/types/src/PeerPayClient.d.ts.map +1 -1
  23. package/dist/types/src/Utils/logger.d.ts +5 -8
  24. package/dist/types/src/Utils/logger.d.ts.map +1 -1
  25. package/dist/types/src/types/permissions.d.ts +75 -0
  26. package/dist/types/src/types/permissions.d.ts.map +1 -0
  27. package/dist/types/src/types.d.ts +71 -2
  28. package/dist/types/src/types.d.ts.map +1 -1
  29. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  30. package/dist/umd/bundle.js +1 -1
  31. package/package.json +5 -5
  32. package/src/MessageBoxClient.ts +732 -24
  33. package/src/PeerPayClient.ts +11 -11
  34. package/src/Utils/logger.ts +17 -19
  35. package/src/types/permissions.ts +81 -0
  36. package/src/types.ts +77 -2
@@ -13,9 +13,9 @@
13
13
  import { MessageBoxClient } from './MessageBoxClient.js'
14
14
  import { PeerMessage } from './types.js'
15
15
  import { WalletClient, P2PKH, PublicKey, createNonce, AtomicBEEF, AuthFetch, Base64String } from '@bsv/sdk'
16
- import { Logger } from './Utils/logger.js'
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
  };
@@ -178,7 +178,7 @@ export class PeerPayClient extends MessageBoxClient {
178
178
  * @returns {Promise<any>} Resolves with the payment result.
179
179
  * @throws {Error} If the recipient is missing or the amount is invalid.
180
180
  */
181
- async sendPayment (payment: PaymentParams): Promise<any> {
181
+ async sendPayment(payment: PaymentParams): Promise<any> {
182
182
  if (payment.recipient == null || payment.recipient.trim() === '' || payment.amount <= 0) {
183
183
  throw new Error('Invalid payment details: recipient and valid amount are required')
184
184
  }
@@ -206,7 +206,7 @@ export class PeerPayClient extends MessageBoxClient {
206
206
  * @returns {Promise<void>} Resolves when the payment has been sent.
207
207
  * @throws {Error} If payment token generation fails.
208
208
  */
209
- async sendLivePayment (payment: PaymentParams): Promise<void> {
209
+ async sendLivePayment(payment: PaymentParams): Promise<void> {
210
210
  const paymentToken = await this.createPaymentToken(payment)
211
211
 
212
212
  try {
@@ -239,7 +239,7 @@ export class PeerPayClient extends MessageBoxClient {
239
239
  * @param {Function} obj.onPayment - Callback function triggered when a payment is received.
240
240
  * @returns {Promise<void>} Resolves when the listener is successfully set up.
241
241
  */
242
- async listenForLivePayments ({
242
+ async listenForLivePayments({
243
243
  onPayment
244
244
  }: { onPayment: (payment: IncomingPayment) => void }): Promise<void> {
245
245
  await this.listenForLiveMessages({
@@ -270,7 +270,7 @@ export class PeerPayClient extends MessageBoxClient {
270
270
  * @returns {Promise<any>} Resolves with the payment result if successful.
271
271
  * @throws {Error} If payment processing fails.
272
272
  */
273
- async acceptPayment (payment: IncomingPayment): Promise<any> {
273
+ async acceptPayment(payment: IncomingPayment): Promise<any> {
274
274
  try {
275
275
  Logger.log(`[PP CLIENT] Processing payment: ${JSON.stringify(payment, null, 2)}`)
276
276
 
@@ -310,7 +310,7 @@ export class PeerPayClient extends MessageBoxClient {
310
310
  * @param {IncomingPayment} payment - The payment object containing transaction details.
311
311
  * @returns {Promise<void>} Resolves when the payment is either acknowledged or refunded.
312
312
  */
313
- async rejectPayment (payment: IncomingPayment): Promise<void> {
313
+ async rejectPayment(payment: IncomingPayment): Promise<void> {
314
314
  Logger.log(`[PP CLIENT] Rejecting payment: ${JSON.stringify(payment, null, 2)}`)
315
315
 
316
316
  if (payment.token.amount - 1000 < 1000) {
@@ -370,7 +370,7 @@ export class PeerPayClient extends MessageBoxClient {
370
370
  *
371
371
  * @returns {Promise<IncomingPayment[]>} Resolves with an array of pending payments.
372
372
  */
373
- async listIncomingPayments (): Promise<IncomingPayment[]> {
373
+ async listIncomingPayments(): Promise<IncomingPayment[]> {
374
374
  const messages = await this.listMessages({ messageBox: STANDARD_PAYMENT_MESSAGEBOX })
375
375
 
376
376
  return messages.map((msg: any) => {
@@ -1,27 +1,25 @@
1
- export class Logger {
2
- private static isEnabled = false
1
+ let isEnabled = false
3
2
 
4
- static enable (): void {
5
- this.isEnabled = true
6
- }
3
+ export function enable (): void {
4
+ isEnabled = true
5
+ }
7
6
 
8
- static disable (): void {
9
- this.isEnabled = false
10
- }
7
+ export function disable (): void {
8
+ isEnabled = false
9
+ }
11
10
 
12
- static log (...args: unknown[]): void {
13
- if (this.isEnabled) {
14
- console.log(...args)
15
- }
11
+ export function log (...args: unknown[]): void {
12
+ if (isEnabled) {
13
+ console.log(...args)
16
14
  }
15
+ }
17
16
 
18
- static warn (...args: unknown[]): void {
19
- if (this.isEnabled) {
20
- console.warn(...args)
21
- }
17
+ export function warn (...args: unknown[]): void {
18
+ if (isEnabled) {
19
+ console.warn(...args)
22
20
  }
21
+ }
23
22
 
24
- static error (...args: unknown[]): void {
25
- console.error(...args)
26
- }
23
+ export function error (...args: unknown[]): void {
24
+ console.error(...args)
27
25
  }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Permission and fee management types for MessageBox system
3
+ */
4
+
5
+ import { PubKeyHex } from '@bsv/sdk'
6
+
7
+ /**
8
+ * Parameters for setting message box permissions
9
+ */
10
+ export interface SetMessageBoxPermissionParams {
11
+ /** The messageBox type (e.g., 'notifications', 'inbox') */
12
+ messageBox: string
13
+ /** Optional sender - if omitted, sets box-wide default */
14
+ sender?: string
15
+ /** Recipient fee: -1=block all, 0=always allow, >0=satoshi amount required */
16
+ recipientFee: number
17
+ }
18
+
19
+ /**
20
+ * Parameters for getting message box permissions
21
+ */
22
+ export interface GetMessageBoxPermissionParams {
23
+ /** The recipient's identity key */
24
+ recipient: string
25
+ /** The messageBox type */
26
+ messageBox: string
27
+ /** Optional sender - if omitted, gets box-wide default */
28
+ sender?: string
29
+ }
30
+
31
+ /**
32
+ * Permission response from server
33
+ */
34
+ export interface MessageBoxPermission {
35
+ /** Sender identity key (null for box-wide defaults) */
36
+ sender: string | null
37
+ /** MessageBox type */
38
+ messageBox: string
39
+ /** Recipient fee setting */
40
+ recipientFee: number
41
+ /** Permission status derived from recipientFee */
42
+ status: 'always_allow' | 'blocked' | 'payment_required'
43
+ /** Creation timestamp */
44
+ createdAt: string
45
+ /** Last update timestamp */
46
+ updatedAt: string
47
+ }
48
+
49
+ /**
50
+ * Fee quote response
51
+ */
52
+ export interface MessageBoxQuote {
53
+ /** Server delivery fee */
54
+ deliveryFee: number
55
+ /** Recipient fee */
56
+ recipientFee: number
57
+ /** Delivery agent identity key */
58
+ deliveryAgentIdentityKey: PubKeyHex
59
+ }
60
+
61
+ /**
62
+ * Parameters for listing permissions
63
+ */
64
+ export interface ListPermissionsParams {
65
+ /** Optional messageBox filter */
66
+ messageBox?: string
67
+ /** Optional pagination limit */
68
+ limit?: number
69
+ /** Optional pagination offset */
70
+ offset?: number
71
+ }
72
+
73
+ /**
74
+ * Parameters for getting fee quote
75
+ */
76
+ export interface GetQuoteParams {
77
+ /** Recipient identity key */
78
+ recipient: string
79
+ /** MessageBox type */
80
+ messageBox: string
81
+ }
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Base64String, WalletClient } from '@bsv/sdk'
1
+ import { AtomicBEEF, Base64String, BasketStringUnder300Bytes, BEEF, BooleanDefaultTrue, DescriptionString5to50Bytes, HexString, LabelStringUnder300Bytes, LockingScript, OutputTagStringUnder300Bytes, PositiveIntegerOrZero, PubKeyHex, WalletInterface } from '@bsv/sdk'
2
2
 
3
3
  /**
4
4
  * Configuration options for initializing a MessageBoxClient.
@@ -8,7 +8,7 @@ export interface MessageBoxClientOptions {
8
8
  * Wallet instance used for auth, identity, and encryption.
9
9
  * If not provided, a new WalletClient will be created.
10
10
  */
11
- walletClient?: WalletClient
11
+ walletClient?: WalletInterface
12
12
 
13
13
  /**
14
14
  * Base URL of the MessageBox server.
@@ -62,6 +62,8 @@ export interface SendMessageParams {
62
62
  body: string | object
63
63
  messageId?: string
64
64
  skipEncryption?: boolean
65
+ /** Optional: Enable permission and fee checking (default: false for backwards compatibility) */
66
+ checkPermissions?: boolean
65
67
  }
66
68
 
67
69
  /**
@@ -106,3 +108,76 @@ export interface ListMessagesParams {
106
108
  export interface EncryptedMessage {
107
109
  encryptedMessage: Base64String
108
110
  }
111
+
112
+ export interface AdvertisementToken {
113
+ host: string
114
+ txid: HexString
115
+ outputIndex: number
116
+ lockingScript: LockingScript
117
+ beef: BEEF
118
+ }
119
+
120
+ export interface Payment {
121
+ tx: AtomicBEEF
122
+ outputs: Array<{
123
+ outputIndex: PositiveIntegerOrZero
124
+ protocol: 'wallet payment' | 'basket insertion'
125
+ paymentRemittance?: {
126
+ derivationPrefix: Base64String
127
+ derivationSuffix: Base64String
128
+ senderIdentityKey: PubKeyHex
129
+ }
130
+ insertionRemittance?: {
131
+ basket: BasketStringUnder300Bytes
132
+ customInstructions?: string
133
+ tags?: OutputTagStringUnder300Bytes[]
134
+ }
135
+ }>
136
+ description: DescriptionString5to50Bytes
137
+ labels?: LabelStringUnder300Bytes[]
138
+ seekPermission?: BooleanDefaultTrue
139
+ }
140
+
141
+ /**
142
+ * Device registration parameters for FCM notifications
143
+ */
144
+ export interface DeviceRegistrationParams {
145
+ /** FCM token from Firebase SDK */
146
+ fcmToken: string
147
+ /** Optional device identifier */
148
+ deviceId?: string
149
+ /** Optional platform type */
150
+ platform?: 'ios' | 'android' | 'web'
151
+ }
152
+
153
+ /**
154
+ * Device registration response
155
+ */
156
+ export interface DeviceRegistrationResponse {
157
+ status: string
158
+ message: string
159
+ deviceId: number
160
+ }
161
+
162
+ /**
163
+ * Registered device information
164
+ */
165
+ export interface RegisteredDevice {
166
+ id: number
167
+ deviceId: string | null
168
+ platform: string | null
169
+ fcmToken: string // Truncated for security (shows only last 10 characters)
170
+ active: boolean
171
+ createdAt: string
172
+ updatedAt: string
173
+ lastUsed: string
174
+ }
175
+
176
+ /**
177
+ * Response from listing registered devices
178
+ */
179
+ export interface ListDevicesResponse {
180
+ status: string
181
+ devices: RegisteredDevice[]
182
+ description?: string // For error responses
183
+ }