@bsv/message-box-client 1.2.1 → 1.2.3

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.2.1",
3
+ "version": "1.2.3",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -877,7 +877,7 @@ export class MessageBoxClient {
877
877
  const quote = await this.getMessageBoxQuote({
878
878
  recipient: message.recipient,
879
879
  messageBox: message.messageBox
880
- })
880
+ }, overrideHost)
881
881
 
882
882
  if (quote.recipientFee === -1) {
883
883
  throw new Error('You have been blocked from sending messages to this recipient.')
@@ -1200,11 +1200,13 @@ export class MessageBoxClient {
1200
1200
  * messages.forEach(msg => console.log(msg.sender, msg.body))
1201
1201
  * // Payments included with messages are automatically received
1202
1202
  */
1203
- async listMessages({ messageBox, host, originator, acceptPayments }: ListMessagesParams): Promise<PeerMessage[]> {
1203
+ async listMessages ({ messageBox, host, originator, acceptPayments }: ListMessagesParams): Promise<PeerMessage[]> {
1204
1204
  if (typeof acceptPayments !== 'boolean') {
1205
1205
  acceptPayments = true
1206
1206
  }
1207
- await this.assertInitialized()
1207
+ if (typeof host !== 'string') {
1208
+ await this.assertInitialized()
1209
+ }
1208
1210
  if (messageBox.trim() === '') {
1209
1211
  throw new Error('MessageBox cannot be empty')
1210
1212
  }
@@ -1386,6 +1388,70 @@ export class MessageBoxClient {
1386
1388
  return messages
1387
1389
  }
1388
1390
 
1391
+ async listMessagesLite ({ messageBox, host }: ListMessagesParams): Promise<PeerMessage[]> {
1392
+ const res = await this.authFetch.fetch(`${host as string}/listMessages`, {
1393
+ method: 'POST',
1394
+ headers: { 'Content-Type': 'application/json' },
1395
+ body: JSON.stringify({ messageBox })
1396
+ })
1397
+ const data = await res.json()
1398
+ if (data.status === 'error') throw new Error(data.description ?? 'Unknown server error')
1399
+ const messages = data.messages as PeerMessage[]
1400
+ const tryParse = (raw: string): any => {
1401
+ try {
1402
+ return JSON.parse(raw)
1403
+ } catch {
1404
+ return raw
1405
+ }
1406
+ }
1407
+ for (const message of messages) {
1408
+ try {
1409
+ const parsedBody: unknown =
1410
+ typeof message.body === 'string' ? tryParse(message.body) : message.body
1411
+ let messageContent: any = parsedBody
1412
+ if (
1413
+ parsedBody != null &&
1414
+ typeof parsedBody === 'object' &&
1415
+ 'message' in parsedBody
1416
+ ) {
1417
+ // Handle wrapped message format (with payment data)
1418
+ const wrappedMessage = (parsedBody as any).message
1419
+ messageContent = typeof wrappedMessage === 'string'
1420
+ ? tryParse(wrappedMessage)
1421
+ : wrappedMessage
1422
+ }
1423
+ // Handle message decryption
1424
+ if (
1425
+ messageContent != null &&
1426
+ typeof messageContent === 'object' &&
1427
+ typeof messageContent.encryptedMessage === 'string'
1428
+ ) {
1429
+ const decrypted = await this.walletClient.decrypt({
1430
+ protocolID: [1, 'messagebox'],
1431
+ keyID: '1',
1432
+ counterparty: message.sender,
1433
+ ciphertext: Utils.toArray(
1434
+ messageContent.encryptedMessage,
1435
+ 'base64'
1436
+ )
1437
+ })
1438
+ const decryptedText = Utils.toUTF8(decrypted.plaintext)
1439
+ message.body = tryParse(decryptedText)
1440
+ } else {
1441
+ // For non-encrypted messages, use the processed content
1442
+ message.body = messageContent ?? parsedBody
1443
+ }
1444
+ } catch (err) {
1445
+ Logger.error(
1446
+ '[MB CLIENT ERROR] Failed to parse or decrypt message in list:',
1447
+ err
1448
+ )
1449
+ message.body = '[Error: Failed to decrypt or parse message]'
1450
+ }
1451
+ }
1452
+ return messages
1453
+ }
1454
+
1389
1455
  /**
1390
1456
  * @method acknowledgeMessage
1391
1457
  * @async