@mrlegendbot/baileys 1.2.5 → 1.2.7

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 (212) hide show
  1. package/WAProto/GenerateStatics.sh +3 -0
  2. package/WAProto/fix-imports.js +85 -0
  3. package/WAProto/index.d.ts +14017 -0
  4. package/WAProto/index.js +97691 -2
  5. package/lib/Defaults/index.d.ts +75 -0
  6. package/lib/Defaults/index.js +92 -89
  7. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  8. package/lib/Signal/Group/ciphertext-message.js +10 -9
  9. package/lib/Signal/Group/group-session-builder.d.ts +15 -0
  10. package/lib/Signal/Group/group-session-builder.js +22 -21
  11. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  12. package/lib/Signal/Group/group_cipher.js +69 -75
  13. package/lib/Signal/Group/index.d.ts +12 -0
  14. package/lib/Signal/Group/index.js +1 -0
  15. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  16. package/lib/Signal/Group/keyhelper.js +10 -9
  17. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  18. package/lib/Signal/Group/sender-chain-key.js +22 -21
  19. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  20. package/lib/Signal/Group/sender-key-distribution-message.js +60 -51
  21. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  22. package/lib/Signal/Group/sender-key-message.js +62 -56
  23. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  24. package/lib/Signal/Group/sender-key-name.js +40 -38
  25. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  26. package/lib/Signal/Group/sender-key-record.js +34 -33
  27. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  28. package/lib/Signal/Group/sender-key-state.js +76 -72
  29. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  30. package/lib/Signal/Group/sender-message-key.js +23 -22
  31. package/lib/Signal/libsignal.d.ts +5 -0
  32. package/lib/Signal/libsignal.js +405 -506
  33. package/lib/Signal/lid-mapping.d.ts +23 -0
  34. package/lib/Signal/lid-mapping.js +258 -276
  35. package/lib/Socket/Client/index.d.ts +3 -0
  36. package/lib/Socket/Client/index.js +1 -0
  37. package/lib/Socket/Client/types.d.ts +16 -0
  38. package/lib/Socket/Client/types.js +7 -6
  39. package/lib/Socket/Client/websocket.d.ts +13 -0
  40. package/lib/Socket/Client/websocket.js +49 -48
  41. package/lib/Socket/business.d.ts +217 -0
  42. package/lib/Socket/business.js +369 -349
  43. package/lib/Socket/chats.d.ts +125 -0
  44. package/lib/Socket/chats.js +1148 -1172
  45. package/lib/Socket/communities.d.ts +273 -0
  46. package/lib/Socket/communities.js +413 -473
  47. package/lib/Socket/groups.d.ts +161 -0
  48. package/lib/Socket/groups.js +331 -387
  49. package/lib/Socket/index.d.ts +260 -0
  50. package/lib/Socket/index.js +8 -6
  51. package/lib/Socket/messages-recv.d.ts +203 -0
  52. package/lib/Socket/messages-recv.js +1719 -1782
  53. package/lib/Socket/messages-send.d.ts +200 -0
  54. package/lib/Socket/messages-send.js +2508 -1384
  55. package/lib/Socket/mex.d.ts +3 -0
  56. package/lib/Socket/mex.js +35 -40
  57. package/lib/Socket/newsletter.d.ts +170 -0
  58. package/lib/Socket/newsletter.js +191 -239
  59. package/lib/Socket/socket.d.ts +59 -0
  60. package/lib/Socket/socket.js +923 -992
  61. package/lib/Types/Auth.d.ts +117 -0
  62. package/lib/Types/Auth.js +1 -0
  63. package/lib/Types/Bussines.d.ts +25 -0
  64. package/lib/Types/Bussines.js +1 -0
  65. package/lib/Types/Call.d.ts +15 -0
  66. package/lib/Types/Call.js +1 -0
  67. package/lib/Types/Chat.d.ts +124 -0
  68. package/lib/Types/Chat.js +8 -1
  69. package/lib/Types/Contact.d.ts +26 -0
  70. package/lib/Types/Contact.js +1 -0
  71. package/lib/Types/Events.d.ts +256 -0
  72. package/lib/Types/Events.js +2 -1
  73. package/lib/Types/GroupMetadata.d.ts +71 -0
  74. package/lib/Types/GroupMetadata.js +1 -0
  75. package/lib/Types/Label.d.ts +47 -0
  76. package/lib/Types/Label.js +22 -20
  77. package/lib/Types/LabelAssociation.d.ts +30 -0
  78. package/lib/Types/LabelAssociation.js +4 -2
  79. package/lib/Types/Message.d.ts +320 -0
  80. package/lib/Types/Message.js +5 -3
  81. package/lib/Types/Mex.d.ts +141 -0
  82. package/lib/Types/Mex.js +31 -26
  83. package/lib/Types/Product.d.ts +79 -0
  84. package/lib/Types/Product.js +1 -0
  85. package/lib/Types/Signal.d.ts +87 -0
  86. package/lib/Types/Signal.js +2 -1
  87. package/lib/Types/Socket.d.ts +136 -0
  88. package/lib/Types/Socket.js +3 -2
  89. package/lib/Types/State.d.ts +97 -0
  90. package/lib/Types/State.js +52 -4
  91. package/lib/Types/USync.d.ts +26 -0
  92. package/lib/Types/USync.js +1 -0
  93. package/lib/Types/index.d.ts +66 -0
  94. package/lib/Types/index.js +11 -10
  95. package/lib/Utils/auth-utils.d.ts +24 -0
  96. package/lib/Utils/auth-utils.js +277 -240
  97. package/lib/Utils/browser-utils.d.ts +4 -0
  98. package/lib/Utils/browser-utils.js +23 -20
  99. package/lib/Utils/business.d.ts +23 -0
  100. package/lib/Utils/business.js +204 -200
  101. package/lib/Utils/chat-utils.d.ts +100 -0
  102. package/lib/Utils/chat-utils.js +810 -836
  103. package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
  104. package/lib/Utils/companion-reg-client-utils.js +25 -23
  105. package/lib/Utils/crypto.d.ts +37 -0
  106. package/lib/Utils/crypto.js +86 -70
  107. package/lib/Utils/decode-wa-message.d.ts +66 -0
  108. package/lib/Utils/decode-wa-message.js +284 -277
  109. package/lib/Utils/event-buffer.d.ts +36 -0
  110. package/lib/Utils/event-buffer.js +595 -592
  111. package/lib/Utils/generics.d.ts +92 -0
  112. package/lib/Utils/generics.js +335 -312
  113. package/lib/Utils/history.d.ts +24 -0
  114. package/lib/Utils/history.js +115 -123
  115. package/lib/Utils/identity-change-handler.d.ts +44 -0
  116. package/lib/Utils/identity-change-handler.js +50 -0
  117. package/lib/Utils/index.d.ts +22 -0
  118. package/lib/Utils/index.js +2 -1
  119. package/lib/Utils/link-preview.d.ts +21 -0
  120. package/lib/Utils/link-preview.js +73 -74
  121. package/lib/Utils/logger.d.ts +12 -0
  122. package/lib/Utils/logger.js +2 -3
  123. package/lib/Utils/lt-hash.d.ts +8 -0
  124. package/lib/Utils/lt-hash.js +6 -0
  125. package/lib/Utils/make-mutex.d.ts +9 -0
  126. package/lib/Utils/make-mutex.js +27 -27
  127. package/lib/Utils/message-retry-manager.d.ts +115 -0
  128. package/lib/Utils/message-retry-manager.js +254 -213
  129. package/lib/Utils/messages-media.d.ts +133 -0
  130. package/lib/Utils/messages-media.js +715 -741
  131. package/lib/Utils/messages.d.ts +91 -0
  132. package/lib/Utils/messages.js +827 -791
  133. package/lib/Utils/noise-handler.d.ts +20 -0
  134. package/lib/Utils/noise-handler.js +187 -215
  135. package/lib/Utils/offline-node-processor.d.ts +17 -0
  136. package/lib/Utils/offline-node-processor.js +40 -0
  137. package/lib/Utils/pre-key-manager.d.ts +28 -0
  138. package/lib/Utils/pre-key-manager.js +98 -69
  139. package/lib/Utils/process-message.d.ts +60 -0
  140. package/lib/Utils/process-message.js +586 -617
  141. package/lib/Utils/reporting-utils.d.ts +11 -0
  142. package/lib/Utils/reporting-utils.js +234 -553
  143. package/lib/Utils/signal.d.ts +47 -0
  144. package/lib/Utils/signal.js +170 -217
  145. package/lib/Utils/stanza-ack.d.ts +11 -0
  146. package/lib/Utils/stanza-ack.js +35 -27
  147. package/lib/Utils/sync-action-utils.d.ts +19 -0
  148. package/lib/Utils/sync-action-utils.js +41 -37
  149. package/lib/Utils/tc-token-utils.d.ts +37 -0
  150. package/lib/Utils/tc-token-utils.js +141 -120
  151. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  152. package/lib/Utils/use-multi-file-auth-state.js +105 -87
  153. package/lib/Utils/validate-connection.d.ts +11 -0
  154. package/lib/Utils/validate-connection.js +188 -190
  155. package/lib/WABinary/constants.d.ts +28 -0
  156. package/lib/WABinary/constants.js +1294 -28
  157. package/lib/WABinary/decode.d.ts +7 -0
  158. package/lib/WABinary/decode.js +249 -245
  159. package/lib/WABinary/encode.d.ts +3 -0
  160. package/lib/WABinary/encode.js +206 -201
  161. package/lib/WABinary/generic-utils.d.ts +15 -0
  162. package/lib/WABinary/generic-utils.js +89 -86
  163. package/lib/WABinary/index.d.ts +6 -0
  164. package/lib/WABinary/index.js +1 -0
  165. package/lib/WABinary/jid-utils.d.ts +48 -0
  166. package/lib/WABinary/jid-utils.js +77 -68
  167. package/lib/WABinary/types.d.ts +19 -0
  168. package/lib/WABinary/types.js +1 -0
  169. package/lib/WAM/BinaryInfo.d.ts +9 -0
  170. package/lib/WAM/BinaryInfo.js +8 -7
  171. package/lib/WAM/constants.d.ts +40 -0
  172. package/lib/WAM/constants.js +22853 -19892
  173. package/lib/WAM/encode.d.ts +3 -0
  174. package/lib/WAM/encode.js +132 -121
  175. package/lib/WAM/index.d.ts +4 -0
  176. package/lib/WAM/index.js +1 -0
  177. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  178. package/lib/WAUSync/Protocols/USyncContactProtocol.js +43 -46
  179. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  180. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +48 -47
  181. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  182. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +23 -22
  183. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  184. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +33 -30
  185. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  186. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +20 -19
  187. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  188. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +44 -53
  189. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  190. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
  191. package/lib/WAUSync/Protocols/index.d.ts +6 -0
  192. package/lib/WAUSync/Protocols/index.js +1 -0
  193. package/lib/WAUSync/USyncQuery.d.ts +30 -0
  194. package/lib/WAUSync/USyncQuery.js +88 -79
  195. package/lib/WAUSync/USyncUser.d.ts +17 -0
  196. package/lib/WAUSync/USyncUser.js +29 -28
  197. package/lib/WAUSync/index.d.ts +4 -0
  198. package/lib/WAUSync/index.js +1 -0
  199. package/lib/index.d.ts +12 -0
  200. package/lib/index.js +2 -2
  201. package/package.json +86 -22
  202. package/README.upstream.md +0 -1312
  203. package/WAProto/WAProto.json +0 -1
  204. package/WAProto/WAProto.proto +0 -6275
  205. package/WAProto/compiler.js +0 -550
  206. package/lib/Store/index.js +0 -8
  207. package/lib/Store/keyed-db.js +0 -108
  208. package/lib/Store/make-cache-manager-store.js +0 -70
  209. package/lib/Store/make-in-memory-store.js +0 -417
  210. package/lib/Store/make-ordered-dictionary.js +0 -69
  211. package/lib/Store/object-repository.js +0 -27
  212. package/lib/Utils/use-sqlite-auth-state.js +0 -162
@@ -1,25 +1,26 @@
1
1
  import { calculateMAC } from 'libsignal/src/crypto.js';
2
2
  import { SenderMessageKey } from './sender-message-key.js';
3
3
  export class SenderChainKey {
4
- constructor(iteration, chainKey) {
5
- this.MESSAGE_KEY_SEED = Buffer.from([0x01]);
6
- this.CHAIN_KEY_SEED = Buffer.from([0x02]);
7
- this.iteration = iteration;
8
- this.chainKey = Buffer.from(chainKey);
9
- }
10
- getIteration() {
11
- return this.iteration;
12
- }
13
- getSenderMessageKey() {
14
- return new SenderMessageKey(this.iteration, this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey));
15
- }
16
- getNext() {
17
- return new SenderChainKey(this.iteration + 1, this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey));
18
- }
19
- getSeed() {
20
- return this.chainKey;
21
- }
22
- getDerivative(seed, key) {
23
- return calculateMAC(key, seed);
24
- }
4
+ constructor(iteration, chainKey) {
5
+ this.MESSAGE_KEY_SEED = Buffer.from([0x01]);
6
+ this.CHAIN_KEY_SEED = Buffer.from([0x02]);
7
+ this.iteration = iteration;
8
+ this.chainKey = Buffer.from(chainKey);
9
+ }
10
+ getIteration() {
11
+ return this.iteration;
12
+ }
13
+ getSenderMessageKey() {
14
+ return new SenderMessageKey(this.iteration, this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey));
15
+ }
16
+ getNext() {
17
+ return new SenderChainKey(this.iteration + 1, this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey));
18
+ }
19
+ getSeed() {
20
+ return this.chainKey;
21
+ }
22
+ getDerivative(seed, key) {
23
+ return calculateMAC(key, seed);
24
+ }
25
25
  }
26
+ //# sourceMappingURL=sender-chain-key.js.map
@@ -0,0 +1,17 @@
1
+ import { CiphertextMessage } from './ciphertext-message.js';
2
+ export declare class SenderKeyDistributionMessage extends CiphertextMessage {
3
+ private readonly id;
4
+ private readonly iteration;
5
+ private readonly chainKey;
6
+ private readonly signatureKey;
7
+ private readonly serialized;
8
+ constructor(id?: number | null, iteration?: number | null, chainKey?: Uint8Array | null, signatureKey?: Uint8Array | null, serialized?: Uint8Array | null);
9
+ private intsToByteHighAndLow;
10
+ serialize(): Uint8Array;
11
+ getType(): number;
12
+ getIteration(): number;
13
+ getChainKey(): Uint8Array;
14
+ getSignatureKey(): Uint8Array;
15
+ getId(): number;
16
+ }
17
+ //# sourceMappingURL=sender-key-distribution-message.d.ts.map
@@ -1,54 +1,63 @@
1
- import { proto } from '../../../WAProto/compiler.js';
1
+ import { proto } from '../../../WAProto/index.js';
2
2
  import { CiphertextMessage } from './ciphertext-message.js';
3
3
  export class SenderKeyDistributionMessage extends CiphertextMessage {
4
- constructor(id, iteration, chainKey, signatureKey, serialized) {
5
- super();
6
- if (serialized) {
7
- try {
8
- const message = serialized.slice(1);
9
- const distributionMessage = proto.SenderKeyDistributionMessage.decode(message).toJSON();
10
- this.serialized = serialized;
11
- this.id = distributionMessage.id;
12
- this.iteration = distributionMessage.iteration;
13
- this.chainKey = typeof distributionMessage.chainKey === 'string' ? Buffer.from(distributionMessage.chainKey, 'base64') : distributionMessage.chainKey;
14
- this.signatureKey = typeof distributionMessage.signingKey === 'string' ? Buffer.from(distributionMessage.signingKey, 'base64') : distributionMessage.signingKey;
15
- } catch (e) {
16
- throw new Error(String(e));
17
- }
18
- } else {
19
- const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION);
20
- this.id = id;
21
- this.iteration = iteration;
22
- this.chainKey = chainKey;
23
- this.signatureKey = signatureKey;
24
- const message = proto.SenderKeyDistributionMessage.encode(proto.SenderKeyDistributionMessage.create({
25
- id,
26
- iteration,
27
- chainKey,
28
- signingKey: this.signatureKey
29
- })).finish();
30
- this.serialized = Buffer.concat([Buffer.from([version]), message]);
31
- }
32
- }
33
- intsToByteHighAndLow(highValue, lowValue) {
34
- return ((highValue << 4 | lowValue) & 0xff) % 256;
35
- }
36
- serialize() {
37
- return this.serialized;
38
- }
39
- getType() {
40
- return this.SENDERKEY_DISTRIBUTION_TYPE;
41
- }
42
- getIteration() {
43
- return this.iteration;
44
- }
45
- getChainKey() {
46
- return this.chainKey;
47
- }
48
- getSignatureKey() {
49
- return this.signatureKey;
50
- }
51
- getId() {
52
- return this.id;
53
- }
4
+ constructor(id, iteration, chainKey, signatureKey, serialized) {
5
+ super();
6
+ if (serialized) {
7
+ try {
8
+ const message = serialized.slice(1);
9
+ const distributionMessage = proto.SenderKeyDistributionMessage.decode(message).toJSON();
10
+ this.serialized = serialized;
11
+ this.id = distributionMessage.id;
12
+ this.iteration = distributionMessage.iteration;
13
+ this.chainKey =
14
+ typeof distributionMessage.chainKey === 'string'
15
+ ? Buffer.from(distributionMessage.chainKey, 'base64')
16
+ : distributionMessage.chainKey;
17
+ this.signatureKey =
18
+ typeof distributionMessage.signingKey === 'string'
19
+ ? Buffer.from(distributionMessage.signingKey, 'base64')
20
+ : distributionMessage.signingKey;
21
+ }
22
+ catch (e) {
23
+ throw new Error(String(e));
24
+ }
25
+ }
26
+ else {
27
+ const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION);
28
+ this.id = id;
29
+ this.iteration = iteration;
30
+ this.chainKey = chainKey;
31
+ this.signatureKey = signatureKey;
32
+ const message = proto.SenderKeyDistributionMessage.encode(proto.SenderKeyDistributionMessage.create({
33
+ id,
34
+ iteration,
35
+ chainKey,
36
+ signingKey: this.signatureKey
37
+ })).finish();
38
+ this.serialized = Buffer.concat([Buffer.from([version]), message]);
39
+ }
40
+ }
41
+ intsToByteHighAndLow(highValue, lowValue) {
42
+ return (((highValue << 4) | lowValue) & 0xff) % 256;
43
+ }
44
+ serialize() {
45
+ return this.serialized;
46
+ }
47
+ getType() {
48
+ return this.SENDERKEY_DISTRIBUTION_TYPE;
49
+ }
50
+ getIteration() {
51
+ return this.iteration;
52
+ }
53
+ getChainKey() {
54
+ return this.chainKey;
55
+ }
56
+ getSignatureKey() {
57
+ return this.signatureKey;
58
+ }
59
+ getId() {
60
+ return this.id;
61
+ }
54
62
  }
63
+ //# sourceMappingURL=sender-key-distribution-message.js.map
@@ -0,0 +1,19 @@
1
+ import { CiphertextMessage } from './ciphertext-message.js';
2
+ export declare class SenderKeyMessage extends CiphertextMessage {
3
+ private readonly SIGNATURE_LENGTH;
4
+ private readonly messageVersion;
5
+ private readonly keyId;
6
+ private readonly iteration;
7
+ private readonly ciphertext;
8
+ private readonly signature;
9
+ private readonly serialized;
10
+ constructor(keyId?: number | null, iteration?: number | null, ciphertext?: Uint8Array | null, signatureKey?: Uint8Array | null, serialized?: Uint8Array | null);
11
+ getKeyId(): number;
12
+ getIteration(): number;
13
+ getCipherText(): Uint8Array;
14
+ verifySignature(signatureKey: Uint8Array): void;
15
+ private getSignature;
16
+ serialize(): Uint8Array;
17
+ getType(): number;
18
+ }
19
+ //# sourceMappingURL=sender-key-message.d.ts.map
@@ -1,60 +1,66 @@
1
1
  import { calculateSignature, verifySignature } from 'libsignal/src/curve.js';
2
- import { proto } from '../../../WAProto/compiler.js';
2
+ import { proto } from '../../../WAProto/index.js';
3
3
  import { CiphertextMessage } from './ciphertext-message.js';
4
4
  export class SenderKeyMessage extends CiphertextMessage {
5
- constructor(keyId, iteration, ciphertext, signatureKey, serialized) {
6
- super();
7
- this.SIGNATURE_LENGTH = 64;
8
- if (serialized) {
9
- const version = serialized[0];
10
- const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH);
11
- const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH);
12
- const senderKeyMessage = proto.SenderKeyMessage.decode(message).toJSON();
13
- this.serialized = serialized;
14
- this.messageVersion = (version & 0xff) >> 4;
15
- this.keyId = senderKeyMessage.id;
16
- this.iteration = senderKeyMessage.iteration;
17
- this.ciphertext = typeof senderKeyMessage.ciphertext === 'string' ? Buffer.from(senderKeyMessage.ciphertext, 'base64') : senderKeyMessage.ciphertext;
18
- this.signature = signature;
19
- } else {
20
- const version = ((this.CURRENT_VERSION << 4 | this.CURRENT_VERSION) & 0xff) % 256;
21
- const ciphertextBuffer = Buffer.from(ciphertext);
22
- const message = proto.SenderKeyMessage.encode(proto.SenderKeyMessage.create({
23
- id: keyId,
24
- iteration: iteration,
25
- ciphertext: ciphertextBuffer
26
- })).finish();
27
- const signature = this.getSignature(signatureKey, Buffer.concat([Buffer.from([version]), message]));
28
- this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)]);
29
- this.messageVersion = this.CURRENT_VERSION;
30
- this.keyId = keyId;
31
- this.iteration = iteration;
32
- this.ciphertext = ciphertextBuffer;
33
- this.signature = signature;
34
- }
35
- }
36
- getKeyId() {
37
- return this.keyId;
38
- }
39
- getIteration() {
40
- return this.iteration;
41
- }
42
- getCipherText() {
43
- return this.ciphertext;
44
- }
45
- verifySignature(signatureKey) {
46
- const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH);
47
- const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH);
48
- const res = verifySignature(signatureKey, part1, part2);
49
- if (!res) throw new Error('Invalid signature!');
50
- }
51
- getSignature(signatureKey, serialized) {
52
- return Buffer.from(calculateSignature(signatureKey, serialized));
53
- }
54
- serialize() {
55
- return this.serialized;
56
- }
57
- getType() {
58
- return 4;
59
- }
5
+ constructor(keyId, iteration, ciphertext, signatureKey, serialized) {
6
+ super();
7
+ this.SIGNATURE_LENGTH = 64;
8
+ if (serialized) {
9
+ const version = serialized[0];
10
+ const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH);
11
+ const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH);
12
+ const senderKeyMessage = proto.SenderKeyMessage.decode(message).toJSON();
13
+ this.serialized = serialized;
14
+ this.messageVersion = (version & 0xff) >> 4;
15
+ this.keyId = senderKeyMessage.id;
16
+ this.iteration = senderKeyMessage.iteration;
17
+ this.ciphertext =
18
+ typeof senderKeyMessage.ciphertext === 'string'
19
+ ? Buffer.from(senderKeyMessage.ciphertext, 'base64')
20
+ : senderKeyMessage.ciphertext;
21
+ this.signature = signature;
22
+ }
23
+ else {
24
+ const version = (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 0xff) % 256;
25
+ const ciphertextBuffer = Buffer.from(ciphertext);
26
+ const message = proto.SenderKeyMessage.encode(proto.SenderKeyMessage.create({
27
+ id: keyId,
28
+ iteration: iteration,
29
+ ciphertext: ciphertextBuffer
30
+ })).finish();
31
+ const signature = this.getSignature(signatureKey, Buffer.concat([Buffer.from([version]), message]));
32
+ this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)]);
33
+ this.messageVersion = this.CURRENT_VERSION;
34
+ this.keyId = keyId;
35
+ this.iteration = iteration;
36
+ this.ciphertext = ciphertextBuffer;
37
+ this.signature = signature;
38
+ }
39
+ }
40
+ getKeyId() {
41
+ return this.keyId;
42
+ }
43
+ getIteration() {
44
+ return this.iteration;
45
+ }
46
+ getCipherText() {
47
+ return this.ciphertext;
48
+ }
49
+ verifySignature(signatureKey) {
50
+ const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH);
51
+ const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH);
52
+ const res = verifySignature(signatureKey, part1, part2);
53
+ if (!res)
54
+ throw new Error('Invalid signature!');
55
+ }
56
+ getSignature(signatureKey, serialized) {
57
+ return Buffer.from(calculateSignature(signatureKey, serialized));
58
+ }
59
+ serialize() {
60
+ return this.serialized;
61
+ }
62
+ getType() {
63
+ return 4;
64
+ }
60
65
  }
66
+ //# sourceMappingURL=sender-key-message.js.map
@@ -0,0 +1,18 @@
1
+ interface Sender {
2
+ id: string;
3
+ deviceId: number;
4
+ toString(): string;
5
+ }
6
+ export declare class SenderKeyName {
7
+ private readonly groupId;
8
+ private readonly sender;
9
+ constructor(groupId: string, sender: Sender);
10
+ getGroupId(): string;
11
+ getSender(): Sender;
12
+ serialize(): string;
13
+ toString(): string;
14
+ equals(other: SenderKeyName | null): boolean;
15
+ hashCode(): number;
16
+ }
17
+ export {};
18
+ //# sourceMappingURL=sender-key-name.d.ts.map
@@ -1,46 +1,48 @@
1
1
  function isNull(str) {
2
- return str === null || str === '';
2
+ return str === null || str === '';
3
3
  }
4
4
  function intValue(num) {
5
- const MAX_VALUE = 0x7fffffff;
6
- const MIN_VALUE = -0x80000000;
7
- if (num > MAX_VALUE || num < MIN_VALUE) {
8
- return num & 0xffffffff;
9
- }
10
- return num;
5
+ const MAX_VALUE = 0x7fffffff;
6
+ const MIN_VALUE = -0x80000000;
7
+ if (num > MAX_VALUE || num < MIN_VALUE) {
8
+ return num & 0xffffffff;
9
+ }
10
+ return num;
11
11
  }
12
12
  function hashCode(strKey) {
13
- let hash = 0;
14
- if (!isNull(strKey)) {
15
- for (let i = 0; i < strKey.length; i++) {
16
- hash = hash * 31 + strKey.charCodeAt(i);
17
- hash = intValue(hash);
18
- }
19
- }
20
- return hash;
13
+ let hash = 0;
14
+ if (!isNull(strKey)) {
15
+ for (let i = 0; i < strKey.length; i++) {
16
+ hash = hash * 31 + strKey.charCodeAt(i);
17
+ hash = intValue(hash);
18
+ }
19
+ }
20
+ return hash;
21
21
  }
22
22
  export class SenderKeyName {
23
- constructor(groupId, sender) {
24
- this.groupId = groupId;
25
- this.sender = sender;
26
- }
27
- getGroupId() {
28
- return this.groupId;
29
- }
30
- getSender() {
31
- return this.sender;
32
- }
33
- serialize() {
34
- return `${this.groupId}::${this.sender.id}::${this.sender.deviceId}`;
35
- }
36
- toString() {
37
- return this.serialize();
38
- }
39
- equals(other) {
40
- if (other === null) return false;
41
- return this.groupId === other.groupId && this.sender.toString() === other.sender.toString();
42
- }
43
- hashCode() {
44
- return hashCode(this.groupId) ^ hashCode(this.sender.toString());
45
- }
23
+ constructor(groupId, sender) {
24
+ this.groupId = groupId;
25
+ this.sender = sender;
26
+ }
27
+ getGroupId() {
28
+ return this.groupId;
29
+ }
30
+ getSender() {
31
+ return this.sender;
32
+ }
33
+ serialize() {
34
+ return `${this.groupId}::${this.sender.id}::${this.sender.deviceId}`;
35
+ }
36
+ toString() {
37
+ return this.serialize();
38
+ }
39
+ equals(other) {
40
+ if (other === null)
41
+ return false;
42
+ return this.groupId === other.groupId && this.sender.toString() === other.sender.toString();
43
+ }
44
+ hashCode() {
45
+ return hashCode(this.groupId) ^ hashCode(this.sender.toString());
46
+ }
46
47
  }
48
+ //# sourceMappingURL=sender-key-name.js.map
@@ -0,0 +1,31 @@
1
+ import { SenderKeyState } from './sender-key-state.js';
2
+ export interface SenderKeyStateStructure {
3
+ senderKeyId: number;
4
+ senderChainKey: {
5
+ iteration: number;
6
+ seed: Uint8Array;
7
+ };
8
+ senderSigningKey: {
9
+ public: Uint8Array;
10
+ private?: Uint8Array;
11
+ };
12
+ senderMessageKeys: Array<{
13
+ iteration: number;
14
+ seed: Uint8Array;
15
+ }>;
16
+ }
17
+ export declare class SenderKeyRecord {
18
+ private readonly MAX_STATES;
19
+ private readonly senderKeyStates;
20
+ constructor(serialized?: SenderKeyStateStructure[]);
21
+ isEmpty(): boolean;
22
+ getSenderKeyState(keyId?: number): SenderKeyState | undefined;
23
+ addSenderKeyState(id: number, iteration: number, chainKey: Uint8Array, signatureKey: Uint8Array): void;
24
+ setSenderKeyState(id: number, iteration: number, chainKey: Uint8Array, keyPair: {
25
+ public: Uint8Array;
26
+ private: Uint8Array;
27
+ }): void;
28
+ serialize(): SenderKeyStateStructure[];
29
+ static deserialize(data: Uint8Array): SenderKeyRecord;
30
+ }
31
+ //# sourceMappingURL=sender-key-record.d.ts.map
@@ -1,40 +1,41 @@
1
1
  import { BufferJSON } from '../../Utils/generics.js';
2
2
  import { SenderKeyState } from './sender-key-state.js';
3
3
  export class SenderKeyRecord {
4
- constructor(serialized) {
5
- this.MAX_STATES = 5;
6
- this.senderKeyStates = [];
7
- if (serialized) {
8
- for (const structure of serialized) {
9
- this.senderKeyStates.push(new SenderKeyState(null, null, null, null, null, null, structure));
10
- }
4
+ constructor(serialized) {
5
+ this.MAX_STATES = 5;
6
+ this.senderKeyStates = [];
7
+ if (serialized) {
8
+ for (const structure of serialized) {
9
+ this.senderKeyStates.push(new SenderKeyState(null, null, null, null, null, null, structure));
10
+ }
11
+ }
11
12
  }
12
- }
13
- isEmpty() {
14
- return this.senderKeyStates.length === 0;
15
- }
16
- getSenderKeyState(keyId) {
17
- if (keyId === undefined && this.senderKeyStates.length) {
18
- return this.senderKeyStates[this.senderKeyStates.length - 1];
13
+ isEmpty() {
14
+ return this.senderKeyStates.length === 0;
19
15
  }
20
- return this.senderKeyStates.find(state => state.getKeyId() === keyId);
21
- }
22
- addSenderKeyState(id, iteration, chainKey, signatureKey) {
23
- this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, null, signatureKey));
24
- if (this.senderKeyStates.length > this.MAX_STATES) {
25
- this.senderKeyStates.shift();
16
+ getSenderKeyState(keyId) {
17
+ if (keyId === undefined && this.senderKeyStates.length) {
18
+ return this.senderKeyStates[this.senderKeyStates.length - 1];
19
+ }
20
+ return this.senderKeyStates.find(state => state.getKeyId() === keyId);
21
+ }
22
+ addSenderKeyState(id, iteration, chainKey, signatureKey) {
23
+ this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, null, signatureKey));
24
+ if (this.senderKeyStates.length > this.MAX_STATES) {
25
+ this.senderKeyStates.shift();
26
+ }
27
+ }
28
+ setSenderKeyState(id, iteration, chainKey, keyPair) {
29
+ this.senderKeyStates.length = 0;
30
+ this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, keyPair));
31
+ }
32
+ serialize() {
33
+ return this.senderKeyStates.map(state => state.getStructure());
34
+ }
35
+ static deserialize(data) {
36
+ const str = Buffer.from(data).toString('utf-8');
37
+ const parsed = JSON.parse(str, BufferJSON.reviver);
38
+ return new SenderKeyRecord(parsed);
26
39
  }
27
- }
28
- setSenderKeyState(id, iteration, chainKey, keyPair) {
29
- this.senderKeyStates.length = 0;
30
- this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, keyPair));
31
- }
32
- serialize() {
33
- return this.senderKeyStates.map(state => state.getStructure());
34
- }
35
- static deserialize(data) {
36
- const str = Buffer.from(data).toString('utf-8');
37
- const parsed = JSON.parse(str, BufferJSON.reviver);
38
- return new SenderKeyRecord(parsed);
39
- }
40
40
  }
41
+ //# sourceMappingURL=sender-key-record.js.map
@@ -0,0 +1,39 @@
1
+ import { SenderChainKey } from './sender-chain-key.js';
2
+ import { SenderMessageKey } from './sender-message-key.js';
3
+ interface SenderChainKeyStructure {
4
+ iteration: number;
5
+ seed: Uint8Array;
6
+ }
7
+ interface SenderSigningKeyStructure {
8
+ public: Uint8Array;
9
+ private?: Uint8Array;
10
+ }
11
+ interface SenderMessageKeyStructure {
12
+ iteration: number;
13
+ seed: Uint8Array;
14
+ }
15
+ interface SenderKeyStateStructure {
16
+ senderKeyId: number;
17
+ senderChainKey: SenderChainKeyStructure;
18
+ senderSigningKey: SenderSigningKeyStructure;
19
+ senderMessageKeys: SenderMessageKeyStructure[];
20
+ }
21
+ export declare class SenderKeyState {
22
+ private readonly MAX_MESSAGE_KEYS;
23
+ private readonly senderKeyStateStructure;
24
+ constructor(id?: number | null, iteration?: number | null, chainKey?: Uint8Array | null | string, signatureKeyPair?: {
25
+ public: Uint8Array | string;
26
+ private: Uint8Array | string;
27
+ } | null, signatureKeyPublic?: Uint8Array | string | null, signatureKeyPrivate?: Uint8Array | string | null, senderKeyStateStructure?: SenderKeyStateStructure | null);
28
+ getKeyId(): number;
29
+ getSenderChainKey(): SenderChainKey;
30
+ setSenderChainKey(chainKey: SenderChainKey): void;
31
+ getSigningKeyPublic(): Buffer;
32
+ getSigningKeyPrivate(): Buffer | undefined;
33
+ hasSenderMessageKey(iteration: number): boolean;
34
+ addSenderMessageKey(senderMessageKey: SenderMessageKey): void;
35
+ removeSenderMessageKey(iteration: number): SenderMessageKey | null;
36
+ getStructure(): SenderKeyStateStructure;
37
+ }
38
+ export {};
39
+ //# sourceMappingURL=sender-key-state.d.ts.map