@areumtecnologia/wasockets 1.0.0 → 1.0.1

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.
@@ -39,15 +39,35 @@ function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
39
39
  })
40
40
 
41
41
  const repository = {
42
- decryptGroupMessage({ group, authorJid, msg }) {
43
- const senderName = jidToSignalSenderKeyName(group, authorJid)
44
- const cipher = new GroupCipher(storage, senderName)
45
-
46
- // Use transaction to ensure atomicity
47
- return parsedKeys.transaction(async () => {
48
- return cipher.decrypt(msg)
49
- }, group)
50
- },
42
+ decryptGroupMessage({ group, authorJid, msg }) {
43
+ const decryptWithAuthor = (author) => {
44
+ const senderName = jidToSignalSenderKeyName(group, author)
45
+ const cipher = new GroupCipher(storage, senderName)
46
+ return cipher.decrypt(msg)
47
+ }
48
+
49
+ // Use transaction to ensure atomicity
50
+ return parsedKeys.transaction(async () => {
51
+ try {
52
+ return await decryptWithAuthor(authorJid)
53
+ }
54
+
55
+ catch (error) {
56
+ if (!isMissingSenderKeySessionError(error)) {
57
+ throw error
58
+ }
59
+
60
+ const alternateAuthorJid = await getAlternateSenderKeyAuthor(authorJid, lidMapping)
61
+
62
+ if (!alternateAuthorJid || alternateAuthorJid === authorJid) {
63
+ throw error
64
+ }
65
+
66
+ logger.debug({ group, authorJid, alternateAuthorJid }, 'retrying group decrypt with alternate sender key identity')
67
+ return await decryptWithAuthor(alternateAuthorJid)
68
+ }
69
+ }, group)
70
+ },
51
71
 
52
72
  async processSenderKeyDistributionMessage({ item, authorJid }) {
53
73
  const builder = new GroupSessionBuilder(storage)
@@ -68,13 +88,25 @@ function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
68
88
  return parsedKeys.transaction(async () => {
69
89
  const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr])
70
90
 
71
- if (!senderKey) {
72
- await storage.storeSenderKey(senderName, new SenderKeyRecord())
73
- }
74
-
75
- await builder.process(senderName, senderMsg)
76
- }, item.groupId)
77
- },
91
+ if (!senderKey) {
92
+ await storage.storeSenderKey(senderName, new SenderKeyRecord())
93
+ }
94
+
95
+ await builder.process(senderName, senderMsg)
96
+
97
+ const alternateAuthorJid = await getAlternateSenderKeyAuthor(authorJid, lidMapping)
98
+
99
+ if (alternateAuthorJid && alternateAuthorJid !== authorJid) {
100
+ const primarySenderName = senderName.toString()
101
+ const alternateSenderName = jidToSignalSenderKeyName(item.groupId, alternateAuthorJid).toString()
102
+ const { [primarySenderName]: primarySenderKey } = await auth.keys.get('sender-key', [primarySenderName])
103
+
104
+ if (primarySenderKey) {
105
+ await auth.keys.set({ 'sender-key': { [alternateSenderName]: primarySenderKey } })
106
+ }
107
+ }
108
+ }, item.groupId)
109
+ },
78
110
 
79
111
  async decryptMessage({ jid, type, ciphertext }) {
80
112
  const addr = jidToSignalProtocolAddress(jid)
@@ -351,11 +383,28 @@ const jidToSignalProtocolAddress = (jid) => {
351
383
  return new ProtocolAddress(signalUser, finalDevice)
352
384
  }
353
385
 
354
- const jidToSignalSenderKeyName = (group, user) => {
355
- return new SenderKeyName(group, jidToSignalProtocolAddress(user))
356
- }
357
-
358
- function signalStorage({ creds, keys }, lidMapping) {
386
+ const jidToSignalSenderKeyName = (group, user) => {
387
+ return new SenderKeyName(group, jidToSignalProtocolAddress(user))
388
+ }
389
+
390
+ const getAlternateSenderKeyAuthor = async (authorJid, lidMapping) => {
391
+ if (isPnUser(authorJid) || isHostedPnUser(authorJid)) {
392
+ return await lidMapping.getLIDForPN(authorJid)
393
+ }
394
+
395
+ if (isLidUser(authorJid) || isHostedLidUser(authorJid)) {
396
+ return await lidMapping.getPNForLID(authorJid)
397
+ }
398
+
399
+ return null
400
+ }
401
+
402
+ const isMissingSenderKeySessionError = (error) => {
403
+ const message = error?.message || error?.toString?.() || ''
404
+ return message.includes('No session found to decrypt message')
405
+ }
406
+
407
+ function signalStorage({ creds, keys }, lidMapping) {
359
408
  // Shared function to resolve PN signal address to LID if mapping exists
360
409
  const resolveLIDSignalAddress = async (id) => {
361
410
  if (id.includes('.')) {
@@ -448,4 +497,4 @@ function signalStorage({ creds, keys }, lidMapping) {
448
497
 
449
498
  module.exports = {
450
499
  makeLibSignalRepository
451
- }
500
+ }
@@ -27,11 +27,11 @@ class WebSocketClient extends AbstractSocketClient {
27
27
  }
28
28
 
29
29
  get isClosing() {
30
- this.socket?.readyState === CLOSING
30
+ return this.socket?.readyState === CLOSING
31
31
  }
32
32
 
33
33
  get isConnecting() {
34
- this.socket?.readyState === CONNECTING
34
+ return this.socket?.readyState === CONNECTING
35
35
  }
36
36
 
37
37
  connect() {
@@ -68,4 +68,4 @@ class WebSocketClient extends AbstractSocketClient {
68
68
 
69
69
  module.exports = {
70
70
  WebSocketClient
71
- }
71
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@areumtecnologia/wasockets",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "WhatsApp API Modification By areumtecnologia",
5
5
  "keywords": [
6
6
  "whatsapp",