@bsv/message-box-client 1.1.10 → 1.2.0

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 +4 -4
  2. package/dist/cjs/src/MessageBoxClient.js +747 -129
  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 +636 -57
  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 +235 -24
  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 +80 -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 +4 -4
  32. package/src/MessageBoxClient.ts +781 -68
  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 +87 -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.
@@ -27,6 +27,11 @@ export interface MessageBoxClientOptions {
27
27
  * @default 'local'
28
28
  */
29
29
  networkPreset?: 'local' | 'mainnet' | 'testnet'
30
+
31
+ /**
32
+ * Originator of the message box client.
33
+ */
34
+ originator?: string
30
35
  }
31
36
 
32
37
  /**
@@ -62,6 +67,9 @@ export interface SendMessageParams {
62
67
  body: string | object
63
68
  messageId?: string
64
69
  skipEncryption?: boolean
70
+ /** Optional: Enable permission and fee checking (default: false for backwards compatibility) */
71
+ checkPermissions?: boolean
72
+ originator?: string
65
73
  }
66
74
 
67
75
  /**
@@ -81,10 +89,12 @@ export interface SendMessageResponse {
81
89
  *
82
90
  * @property {string[]} messageIds - An array of message IDs to acknowledge.
83
91
  * @property {string} [host] - Optional host URL where the messages originated.
92
+ * @property {string} [originator] - Optional originator of the message box client.
84
93
  */
85
94
  export interface AcknowledgeMessageParams {
86
95
  messageIds: string[]
87
96
  host?: string
97
+ originator?: string
88
98
  }
89
99
 
90
100
  /**
@@ -92,10 +102,12 @@ export interface AcknowledgeMessageParams {
92
102
  *
93
103
  * @property messageBox - The identifier of the message box to retrieve messages from.
94
104
  * @property host - (Optional) The host URL to connect to for retrieving messages.
105
+ * @property originator - (Optional) The originator of the message box client.
95
106
  */
96
107
  export interface ListMessagesParams {
97
108
  messageBox: string
98
109
  host?: string
110
+ originator?: string
99
111
  }
100
112
 
101
113
  /**
@@ -106,3 +118,76 @@ export interface ListMessagesParams {
106
118
  export interface EncryptedMessage {
107
119
  encryptedMessage: Base64String
108
120
  }
121
+
122
+ export interface AdvertisementToken {
123
+ host: string
124
+ txid: HexString
125
+ outputIndex: number
126
+ lockingScript: LockingScript
127
+ beef: BEEF
128
+ }
129
+
130
+ export interface Payment {
131
+ tx: AtomicBEEF
132
+ outputs: Array<{
133
+ outputIndex: PositiveIntegerOrZero
134
+ protocol: 'wallet payment' | 'basket insertion'
135
+ paymentRemittance?: {
136
+ derivationPrefix: Base64String
137
+ derivationSuffix: Base64String
138
+ senderIdentityKey: PubKeyHex
139
+ }
140
+ insertionRemittance?: {
141
+ basket: BasketStringUnder300Bytes
142
+ customInstructions?: string
143
+ tags?: OutputTagStringUnder300Bytes[]
144
+ }
145
+ }>
146
+ description: DescriptionString5to50Bytes
147
+ labels?: LabelStringUnder300Bytes[]
148
+ seekPermission?: BooleanDefaultTrue
149
+ }
150
+
151
+ /**
152
+ * Device registration parameters for FCM notifications
153
+ */
154
+ export interface DeviceRegistrationParams {
155
+ /** FCM token from Firebase SDK */
156
+ fcmToken: string
157
+ /** Optional device identifier */
158
+ deviceId?: string
159
+ /** Optional platform type */
160
+ platform?: 'ios' | 'android' | 'web'
161
+ }
162
+
163
+ /**
164
+ * Device registration response
165
+ */
166
+ export interface DeviceRegistrationResponse {
167
+ status: string
168
+ message: string
169
+ deviceId: number
170
+ }
171
+
172
+ /**
173
+ * Registered device information
174
+ */
175
+ export interface RegisteredDevice {
176
+ id: number
177
+ deviceId: string | null
178
+ platform: string | null
179
+ fcmToken: string // Truncated for security (shows only last 10 characters)
180
+ active: boolean
181
+ createdAt: string
182
+ updatedAt: string
183
+ lastUsed: string
184
+ }
185
+
186
+ /**
187
+ * Response from listing registered devices
188
+ */
189
+ export interface ListDevicesResponse {
190
+ status: string
191
+ devices: RegisteredDevice[]
192
+ description?: string // For error responses
193
+ }