@vinzsocket/baileys 2.8.10 → 2.8.30

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 (83) hide show
  1. package/lib/Defaults/index.js +75 -73
  2. package/lib/Signal/Group/ciphertext-message.js +9 -9
  3. package/lib/Signal/Group/group-session-builder.js +21 -21
  4. package/lib/Signal/Group/group_cipher.js +79 -81
  5. package/lib/Signal/Group/index.js +2 -2
  6. package/lib/Signal/Group/keyhelper.js +9 -9
  7. package/lib/Signal/Group/queue-job.js +43 -46
  8. package/lib/Signal/Group/sender-chain-key.js +24 -25
  9. package/lib/Signal/Group/sender-key-distribution-message.js +50 -58
  10. package/lib/Signal/Group/sender-key-message.js +55 -60
  11. package/lib/Signal/Group/sender-key-name.js +38 -39
  12. package/lib/Signal/Group/sender-key-record.js +39 -41
  13. package/lib/Signal/Group/sender-key-state.js +85 -89
  14. package/lib/Signal/Group/sender-message-key.js +22 -22
  15. package/lib/Signal/libsignal.js +387 -292
  16. package/lib/Signal/lid-mapping.js +98 -93
  17. package/lib/Socket/Client/types.js +6 -6
  18. package/lib/Socket/Client/websocket.js +44 -44
  19. package/lib/Socket/business.js +350 -365
  20. package/lib/Socket/chats.js +1019 -914
  21. package/lib/Socket/communities.js +454 -376
  22. package/lib/Socket/groups.js +378 -310
  23. package/lib/Socket/index.js +11 -11
  24. package/lib/Socket/messages-recv.js +1355 -1182
  25. package/lib/Socket/messages-send.js +1224 -1081
  26. package/lib/Socket/mex.js +40 -34
  27. package/lib/Socket/newsletter.js +212 -169
  28. package/lib/Socket/socket.js +896 -757
  29. package/lib/Store/index.js +1 -1
  30. package/lib/Store/make-cache-manager-store.js +75 -79
  31. package/lib/Store/make-in-memory-store.js +450 -389
  32. package/lib/Store/make-ordered-dictionary.js +73 -76
  33. package/lib/Store/object-repository.js +24 -22
  34. package/lib/Types/Chat.js +1 -7
  35. package/lib/Types/Label.js +20 -20
  36. package/lib/Types/LabelAssociation.js +2 -2
  37. package/lib/Types/Message.js +2 -2
  38. package/lib/Types/Newsletter.js +24 -24
  39. package/lib/Types/Socket.js +1 -1
  40. package/lib/Types/State.js +8 -8
  41. package/lib/Types/index.js +10 -10
  42. package/lib/Utils/auth-utils.js +408 -401
  43. package/lib/Utils/baileys-event-stream.js +44 -35
  44. package/lib/Utils/business.js +200 -197
  45. package/lib/Utils/chat-utils.js +775 -708
  46. package/lib/Utils/crypto.js +97 -85
  47. package/lib/Utils/decode-wa-message.js +240 -227
  48. package/lib/Utils/event-buffer.js +547 -518
  49. package/lib/Utils/extended-messages.js +250 -203
  50. package/lib/Utils/generics.js +300 -289
  51. package/lib/Utils/history.js +76 -69
  52. package/lib/Utils/lid-resolver.js +47 -42
  53. package/lib/Utils/link-preview.js +75 -65
  54. package/lib/Utils/logger.js +3 -1
  55. package/lib/Utils/lt-hash.js +38 -34
  56. package/lib/Utils/make-mutex.js +32 -34
  57. package/lib/Utils/mention.js +79 -86
  58. package/lib/Utils/message-retry-manager.js +148 -136
  59. package/lib/Utils/messages-media.js +652 -562
  60. package/lib/Utils/messages.js +753 -707
  61. package/lib/Utils/noise-handler.js +156 -135
  62. package/lib/Utils/process-message.js +404 -341
  63. package/lib/Utils/signal.js +180 -118
  64. package/lib/Utils/use-multi-file-auth-state.js +87 -91
  65. package/lib/Utils/validate-connection.js +167 -142
  66. package/lib/WABinary/constants.js +28 -1293
  67. package/lib/WABinary/decode.js +220 -218
  68. package/lib/WABinary/encode.js +199 -201
  69. package/lib/WABinary/generic-utils.js +80 -77
  70. package/lib/WABinary/jid-utils.js +41 -35
  71. package/lib/WAM/BinaryInfo.js +7 -7
  72. package/lib/WAM/constants.js +1 -15241
  73. package/lib/WAM/encode.js +124 -131
  74. package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
  75. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +51 -47
  76. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -22
  77. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -32
  78. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -19
  79. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -43
  80. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +17 -17
  81. package/lib/WAUSync/USyncQuery.js +82 -84
  82. package/lib/WAUSync/USyncUser.js +20 -20
  83. package/package.json +4 -2
@@ -1,63 +1,55 @@
1
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);
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 typeof this.chainKey === 'string' ? Buffer.from(this.chainKey, 'base64') : this.chainKey;
55
- }
56
- getSignatureKey() {
57
- return typeof this.signatureKey === 'string' ? Buffer.from(this.signatureKey, 'base64') : this.signatureKey;
58
- }
59
- getId() {
60
- return this.id;
61
- }
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);
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 typeof this.chainKey === 'string' ? Buffer.from(this.chainKey, 'base64') : this.chainKey;
47
+ }
48
+ getSignatureKey() {
49
+ return typeof this.signatureKey === 'string' ? Buffer.from(this.signatureKey, 'base64') : this.signatureKey;
50
+ }
51
+ getId() {
52
+ return this.id;
53
+ }
62
54
  }
63
55
  //# sourceMappingURL=sender-key-distribution-message.js.map
@@ -3,65 +3,60 @@ import { calculateSignature, verifySignature } from 'libsignal/src/curve.js';
3
3
  import { proto } from '../../../WAProto/index.js';
4
4
  import { CiphertextMessage } from './ciphertext-message.js';
5
5
  export class SenderKeyMessage extends CiphertextMessage {
6
- constructor(keyId, iteration, ciphertext, signatureKey, serialized) {
7
- super();
8
- this.SIGNATURE_LENGTH = 64;
9
- if (serialized) {
10
- const version = serialized[0];
11
- const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH);
12
- const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH);
13
- const senderKeyMessage = proto.SenderKeyMessage.decode(message);
14
- this.serialized = serialized;
15
- this.messageVersion = (version & 0xff) >> 4;
16
- this.keyId = senderKeyMessage.id;
17
- this.iteration = senderKeyMessage.iteration;
18
- this.ciphertext =
19
- typeof senderKeyMessage.ciphertext === 'string'
20
- ? Buffer.from(senderKeyMessage.ciphertext, 'base64')
21
- : senderKeyMessage.ciphertext;
22
- this.signature = signature;
23
- }
24
- else {
25
- const version = (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 0xff) % 256;
26
- const ciphertextBuffer = Buffer.from(ciphertext);
27
- const message = proto.SenderKeyMessage.encode(proto.SenderKeyMessage.create({
28
- id: keyId,
29
- iteration: iteration,
30
- ciphertext: ciphertextBuffer
31
- })).finish();
32
- const signature = this.getSignature(signatureKey, Buffer.concat([Buffer.from([version]), message]));
33
- this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)]);
34
- this.messageVersion = this.CURRENT_VERSION;
35
- this.keyId = keyId;
36
- this.iteration = iteration;
37
- this.ciphertext = ciphertextBuffer;
38
- this.signature = signature;
39
- }
40
- }
41
- getKeyId() {
42
- return this.keyId;
43
- }
44
- getIteration() {
45
- return this.iteration;
46
- }
47
- getCipherText() {
48
- return this.ciphertext;
49
- }
50
- verifySignature(signatureKey) {
51
- const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH);
52
- const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH);
53
- const res = verifySignature(signatureKey, part1, part2);
54
- if (!res)
55
- throw new Error('Invalid signature!');
56
- }
57
- getSignature(signatureKey, serialized) {
58
- return Buffer.from(calculateSignature(signatureKey, serialized));
59
- }
60
- serialize() {
61
- return this.serialized;
62
- }
63
- getType() {
64
- return 4;
65
- }
6
+ constructor(keyId, iteration, ciphertext, signatureKey, serialized) {
7
+ super();
8
+ this.SIGNATURE_LENGTH = 64;
9
+ if (serialized) {
10
+ const version = serialized[0];
11
+ const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH);
12
+ const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH);
13
+ const senderKeyMessage = proto.SenderKeyMessage.decode(message);
14
+ this.serialized = serialized;
15
+ this.messageVersion = (version & 0xff) >> 4;
16
+ this.keyId = senderKeyMessage.id;
17
+ this.iteration = senderKeyMessage.iteration;
18
+ this.ciphertext = typeof senderKeyMessage.ciphertext === 'string' ? Buffer.from(senderKeyMessage.ciphertext, 'base64') : senderKeyMessage.ciphertext;
19
+ this.signature = signature;
20
+ } else {
21
+ const version = ((this.CURRENT_VERSION << 4 | this.CURRENT_VERSION) & 0xff) % 256;
22
+ const ciphertextBuffer = Buffer.from(ciphertext);
23
+ const message = proto.SenderKeyMessage.encode(proto.SenderKeyMessage.create({
24
+ id: keyId,
25
+ iteration: iteration,
26
+ ciphertext: ciphertextBuffer
27
+ })).finish();
28
+ const signature = this.getSignature(signatureKey, Buffer.concat([Buffer.from([version]), message]));
29
+ this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)]);
30
+ this.messageVersion = this.CURRENT_VERSION;
31
+ this.keyId = keyId;
32
+ this.iteration = iteration;
33
+ this.ciphertext = ciphertextBuffer;
34
+ this.signature = signature;
35
+ }
36
+ }
37
+ getKeyId() {
38
+ return this.keyId;
39
+ }
40
+ getIteration() {
41
+ return this.iteration;
42
+ }
43
+ getCipherText() {
44
+ return this.ciphertext;
45
+ }
46
+ verifySignature(signatureKey) {
47
+ const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH);
48
+ const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH);
49
+ const res = verifySignature(signatureKey, part1, part2);
50
+ if (!res) throw new Error('Invalid signature!');
51
+ }
52
+ getSignature(signatureKey, serialized) {
53
+ return Buffer.from(calculateSignature(signatureKey, serialized));
54
+ }
55
+ serialize() {
56
+ return this.serialized;
57
+ }
58
+ getType() {
59
+ return 4;
60
+ }
66
61
  }
67
62
  //# sourceMappingURL=sender-key-message.js.map
@@ -1,48 +1,47 @@
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)
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
- }
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
+ }
47
46
  }
48
47
  //# sourceMappingURL=sender-key-name.js.map
@@ -1,50 +1,48 @@
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
- }
11
- }
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
+ }
12
11
  }
13
- isEmpty() {
14
- return this.senderKeyStates.length === 0;
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];
15
19
  }
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);
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();
21
26
  }
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
- let parsed;
37
- if (typeof data === 'string') {
38
- parsed = JSON.parse(data, BufferJSON.reviver);
39
- }
40
- else if (data instanceof Uint8Array) {
41
- const str = Buffer.from(data).toString('utf-8');
42
- parsed = JSON.parse(str, BufferJSON.reviver);
43
- }
44
- else {
45
- parsed = data;
46
- }
47
- return new SenderKeyRecord(parsed);
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
+ let parsed;
37
+ if (typeof data === 'string') {
38
+ parsed = JSON.parse(data, BufferJSON.reviver);
39
+ } else if (data instanceof Uint8Array) {
40
+ const str = Buffer.from(data).toString('utf-8');
41
+ parsed = JSON.parse(str, BufferJSON.reviver);
42
+ } else {
43
+ parsed = data;
48
44
  }
45
+ return new SenderKeyRecord(parsed);
46
+ }
49
47
  }
50
48
  //# sourceMappingURL=sender-key-record.js.map
@@ -1,101 +1,97 @@
1
1
  import { SenderChainKey } from './sender-chain-key.js';
2
2
  import { SenderMessageKey } from './sender-message-key.js';
3
3
  export class SenderKeyState {
4
- constructor(id, iteration, chainKey, signatureKeyPair, signatureKeyPublic, signatureKeyPrivate, senderKeyStateStructure) {
5
- this.MAX_MESSAGE_KEYS = 2000;
6
- if (senderKeyStateStructure) {
7
- if (!Array.isArray(senderKeyStateStructure.senderMessageKeys)) {
8
- this.senderKeyStateStructure = { ...senderKeyStateStructure, senderMessageKeys: [] };
9
- }
10
- else {
11
- this.senderKeyStateStructure = senderKeyStateStructure;
12
- }
13
- }
14
- else {
15
- if (signatureKeyPair) {
16
- signatureKeyPublic = signatureKeyPair.public;
17
- signatureKeyPrivate = signatureKeyPair.private;
18
- }
19
- chainKey = typeof chainKey === 'string' ? Buffer.from(chainKey, 'base64') : chainKey;
20
- const senderChainKeyStructure = {
21
- iteration: iteration || 0,
22
- seed: chainKey || Buffer.alloc(0)
23
- };
24
- const signingKeyStructure = {
25
- public: typeof signatureKeyPublic === 'string'
26
- ? Buffer.from(signatureKeyPublic, 'base64')
27
- : signatureKeyPublic || Buffer.alloc(0)
28
- };
29
- if (signatureKeyPrivate) {
30
- signingKeyStructure.private =
31
- typeof signatureKeyPrivate === 'string' ? Buffer.from(signatureKeyPrivate, 'base64') : signatureKeyPrivate;
32
- }
33
- this.senderKeyStateStructure = {
34
- senderKeyId: id || 0,
35
- senderChainKey: senderChainKeyStructure,
36
- senderSigningKey: signingKeyStructure,
37
- senderMessageKeys: []
38
- };
39
- }
40
- }
41
- getKeyId() {
42
- return this.senderKeyStateStructure.senderKeyId;
43
- }
44
- getSenderChainKey() {
45
- return new SenderChainKey(this.senderKeyStateStructure.senderChainKey.iteration, this.senderKeyStateStructure.senderChainKey.seed);
46
- }
47
- setSenderChainKey(chainKey) {
48
- this.senderKeyStateStructure.senderChainKey = {
49
- iteration: chainKey.getIteration(),
50
- seed: chainKey.getSeed()
4
+ constructor(id, iteration, chainKey, signatureKeyPair, signatureKeyPublic, signatureKeyPrivate, senderKeyStateStructure) {
5
+ this.MAX_MESSAGE_KEYS = 2000;
6
+ if (senderKeyStateStructure) {
7
+ if (!Array.isArray(senderKeyStateStructure.senderMessageKeys)) {
8
+ this.senderKeyStateStructure = {
9
+ ...senderKeyStateStructure,
10
+ senderMessageKeys: []
51
11
  };
12
+ } else {
13
+ this.senderKeyStateStructure = senderKeyStateStructure;
14
+ }
15
+ } else {
16
+ if (signatureKeyPair) {
17
+ signatureKeyPublic = signatureKeyPair.public;
18
+ signatureKeyPrivate = signatureKeyPair.private;
19
+ }
20
+ chainKey = typeof chainKey === 'string' ? Buffer.from(chainKey, 'base64') : chainKey;
21
+ const senderChainKeyStructure = {
22
+ iteration: iteration || 0,
23
+ seed: chainKey || Buffer.alloc(0)
24
+ };
25
+ const signingKeyStructure = {
26
+ public: typeof signatureKeyPublic === 'string' ? Buffer.from(signatureKeyPublic, 'base64') : signatureKeyPublic || Buffer.alloc(0)
27
+ };
28
+ if (signatureKeyPrivate) {
29
+ signingKeyStructure.private = typeof signatureKeyPrivate === 'string' ? Buffer.from(signatureKeyPrivate, 'base64') : signatureKeyPrivate;
30
+ }
31
+ this.senderKeyStateStructure = {
32
+ senderKeyId: id || 0,
33
+ senderChainKey: senderChainKeyStructure,
34
+ senderSigningKey: signingKeyStructure,
35
+ senderMessageKeys: []
36
+ };
52
37
  }
53
- getSigningKeyPublic() {
54
- const publicKey = this.senderKeyStateStructure.senderSigningKey.public;
55
- if (publicKey instanceof Buffer) {
56
- return publicKey;
57
- }
58
- else if (typeof publicKey === 'string') {
59
- return Buffer.from(publicKey, 'base64');
60
- }
61
- return Buffer.from(publicKey || []);
62
- }
63
- getSigningKeyPrivate() {
64
- const privateKey = this.senderKeyStateStructure.senderSigningKey.private;
65
- if (!privateKey) {
66
- return undefined;
67
- }
68
- if (privateKey instanceof Buffer) {
69
- return privateKey;
70
- }
71
- else if (typeof privateKey === 'string') {
72
- return Buffer.from(privateKey, 'base64');
73
- }
74
- return Buffer.from(privateKey || []);
38
+ }
39
+ getKeyId() {
40
+ return this.senderKeyStateStructure.senderKeyId;
41
+ }
42
+ getSenderChainKey() {
43
+ return new SenderChainKey(this.senderKeyStateStructure.senderChainKey.iteration, this.senderKeyStateStructure.senderChainKey.seed);
44
+ }
45
+ setSenderChainKey(chainKey) {
46
+ this.senderKeyStateStructure.senderChainKey = {
47
+ iteration: chainKey.getIteration(),
48
+ seed: chainKey.getSeed()
49
+ };
50
+ }
51
+ getSigningKeyPublic() {
52
+ const publicKey = this.senderKeyStateStructure.senderSigningKey.public;
53
+ if (publicKey instanceof Buffer) {
54
+ return publicKey;
55
+ } else if (typeof publicKey === 'string') {
56
+ return Buffer.from(publicKey, 'base64');
75
57
  }
76
- hasSenderMessageKey(iteration) {
77
- return this.senderKeyStateStructure.senderMessageKeys.some(key => key.iteration === iteration);
58
+ return Buffer.from(publicKey || []);
59
+ }
60
+ getSigningKeyPrivate() {
61
+ const privateKey = this.senderKeyStateStructure.senderSigningKey.private;
62
+ if (!privateKey) {
63
+ return undefined;
78
64
  }
79
- addSenderMessageKey(senderMessageKey) {
80
- this.senderKeyStateStructure.senderMessageKeys.push({
81
- iteration: senderMessageKey.getIteration(),
82
- seed: senderMessageKey.getSeed()
83
- });
84
- if (this.senderKeyStateStructure.senderMessageKeys.length > this.MAX_MESSAGE_KEYS) {
85
- this.senderKeyStateStructure.senderMessageKeys.shift();
86
- }
65
+ if (privateKey instanceof Buffer) {
66
+ return privateKey;
67
+ } else if (typeof privateKey === 'string') {
68
+ return Buffer.from(privateKey, 'base64');
87
69
  }
88
- removeSenderMessageKey(iteration) {
89
- const index = this.senderKeyStateStructure.senderMessageKeys.findIndex(key => key.iteration === iteration);
90
- if (index !== -1) {
91
- const messageKey = this.senderKeyStateStructure.senderMessageKeys[index];
92
- this.senderKeyStateStructure.senderMessageKeys.splice(index, 1);
93
- return new SenderMessageKey(messageKey.iteration, messageKey.seed);
94
- }
95
- return null;
70
+ return Buffer.from(privateKey || []);
71
+ }
72
+ hasSenderMessageKey(iteration) {
73
+ return this.senderKeyStateStructure.senderMessageKeys.some(key => key.iteration === iteration);
74
+ }
75
+ addSenderMessageKey(senderMessageKey) {
76
+ this.senderKeyStateStructure.senderMessageKeys.push({
77
+ iteration: senderMessageKey.getIteration(),
78
+ seed: senderMessageKey.getSeed()
79
+ });
80
+ if (this.senderKeyStateStructure.senderMessageKeys.length > this.MAX_MESSAGE_KEYS) {
81
+ this.senderKeyStateStructure.senderMessageKeys.shift();
96
82
  }
97
- getStructure() {
98
- return this.senderKeyStateStructure;
83
+ }
84
+ removeSenderMessageKey(iteration) {
85
+ const index = this.senderKeyStateStructure.senderMessageKeys.findIndex(key => key.iteration === iteration);
86
+ if (index !== -1) {
87
+ const messageKey = this.senderKeyStateStructure.senderMessageKeys[index];
88
+ this.senderKeyStateStructure.senderMessageKeys.splice(index, 1);
89
+ return new SenderMessageKey(messageKey.iteration, messageKey.seed);
99
90
  }
91
+ return null;
92
+ }
93
+ getStructure() {
94
+ return this.senderKeyStateStructure;
95
+ }
100
96
  }
101
97
  //# sourceMappingURL=sender-key-state.js.map
@@ -1,27 +1,27 @@
1
1
  /* @ts-ignore */
2
2
  import { deriveSecrets } from 'libsignal/src/crypto.js';
3
3
  export class SenderMessageKey {
4
- constructor(iteration, seed) {
5
- const derivative = deriveSecrets(seed, Buffer.alloc(32), Buffer.from('WhisperGroup'));
6
- const keys = new Uint8Array(32);
7
- keys.set(new Uint8Array(derivative[0].slice(16)));
8
- keys.set(new Uint8Array(derivative[1].slice(0, 16)), 16);
9
- this.iv = Buffer.from(derivative[0].slice(0, 16));
10
- this.cipherKey = Buffer.from(keys.buffer);
11
- this.iteration = iteration;
12
- this.seed = seed;
13
- }
14
- getIteration() {
15
- return this.iteration;
16
- }
17
- getIv() {
18
- return this.iv;
19
- }
20
- getCipherKey() {
21
- return this.cipherKey;
22
- }
23
- getSeed() {
24
- return this.seed;
25
- }
4
+ constructor(iteration, seed) {
5
+ const derivative = deriveSecrets(seed, Buffer.alloc(32), Buffer.from('WhisperGroup'));
6
+ const keys = new Uint8Array(32);
7
+ keys.set(new Uint8Array(derivative[0].slice(16)));
8
+ keys.set(new Uint8Array(derivative[1].slice(0, 16)), 16);
9
+ this.iv = Buffer.from(derivative[0].slice(0, 16));
10
+ this.cipherKey = Buffer.from(keys.buffer);
11
+ this.iteration = iteration;
12
+ this.seed = seed;
13
+ }
14
+ getIteration() {
15
+ return this.iteration;
16
+ }
17
+ getIv() {
18
+ return this.iv;
19
+ }
20
+ getCipherKey() {
21
+ return this.cipherKey;
22
+ }
23
+ getSeed() {
24
+ return this.seed;
25
+ }
26
26
  }
27
27
  //# sourceMappingURL=sender-message-key.js.map