whalibmob 5.40.45 → 5.40.47

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 (51) hide show
  1. package/index.d.ts +4 -6
  2. package/index.js +16 -14
  3. package/lib/Client.js +77 -28
  4. package/lib/signal/SenderKey.js +1 -1
  5. package/lib/signal/SignalProtocol.js +204 -65
  6. package/lib/signal/SignalStore.js +9 -5
  7. package/lib/signal/core/LICENSE +27 -0
  8. package/lib/signal/core/THIRD_PARTY_LICENSES.md +4225 -0
  9. package/lib/signal/core/index.cjs +2805 -0
  10. package/lib/signal/core/wasm/nosimd.wasm +0 -0
  11. package/lib/signal/core/wasm/simd.wasm +0 -0
  12. package/lib/signal/libsignal/LICENSE +21 -0
  13. package/lib/signal/libsignal/address.js +27 -0
  14. package/lib/signal/libsignal/bytes.js +16 -0
  15. package/lib/signal/libsignal/crypto.js +60 -93
  16. package/lib/signal/libsignal/curve.js +47 -138
  17. package/lib/signal/libsignal/errors.js +68 -27
  18. package/lib/signal/libsignal/group.js +85 -0
  19. package/lib/signal/libsignal/index.js +35 -10
  20. package/lib/signal/libsignal/keys.js +27 -0
  21. package/lib/signal/libsignal/legacy-session-codec.js +135 -0
  22. package/lib/signal/libsignal/legacy-session.js +31 -0
  23. package/lib/signal/libsignal/lock.js +46 -0
  24. package/lib/signal/libsignal/prekey-header.js +59 -0
  25. package/lib/signal/libsignal/session-format.js +43 -0
  26. package/lib/signal/libsignal/session.js +198 -0
  27. package/lib/signal/libsignal/store.js +2 -0
  28. package/package.json +7 -2
  29. package/lib/signal/WaSignalGroup/ciphertext-message.js +0 -15
  30. package/lib/signal/WaSignalGroup/group-session-builder.js +0 -46
  31. package/lib/signal/WaSignalGroup/group_cipher.js +0 -93
  32. package/lib/signal/WaSignalGroup/index.js +0 -27
  33. package/lib/signal/WaSignalGroup/keyhelper.js +0 -24
  34. package/lib/signal/WaSignalGroup/sender-chain-key.js +0 -35
  35. package/lib/signal/WaSignalGroup/sender-key-distribution-message.js +0 -137
  36. package/lib/signal/WaSignalGroup/sender-key-message.js +0 -140
  37. package/lib/signal/WaSignalGroup/sender-key-name.js +0 -59
  38. package/lib/signal/WaSignalGroup/sender-key-record.js +0 -63
  39. package/lib/signal/WaSignalGroup/sender-key-state.js +0 -101
  40. package/lib/signal/WaSignalGroup/sender-message-key.js +0 -34
  41. package/lib/signal/libsignal/WhisperTextProtocol.js +0 -933
  42. package/lib/signal/libsignal/base_key_type.js +0 -7
  43. package/lib/signal/libsignal/chain_type.js +0 -6
  44. package/lib/signal/libsignal/keyhelper.js +0 -45
  45. package/lib/signal/libsignal/numeric_fingerprint.js +0 -83
  46. package/lib/signal/libsignal/protobufs.js +0 -10
  47. package/lib/signal/libsignal/protocol_address.js +0 -40
  48. package/lib/signal/libsignal/queue_job.js +0 -71
  49. package/lib/signal/libsignal/session_builder.js +0 -171
  50. package/lib/signal/libsignal/session_cipher.js +0 -334
  51. package/lib/signal/libsignal/session_record.js +0 -316
@@ -1,93 +0,0 @@
1
- 'use strict';
2
-
3
- const { Boom } = require('@hapi/boom');
4
- const { decrypt, encrypt } = require('../libsignal/crypto');
5
- const { SenderKeyMessage } = require('./sender-key-message');
6
- const { SenderKeyRecord } = require('./sender-key-record');
7
-
8
- class GroupCipher {
9
- constructor(senderKeyStore, senderKeyName) {
10
- this.senderKeyStore = senderKeyStore;
11
- this.senderKeyName = senderKeyName;
12
- }
13
-
14
- async encrypt(paddedPlaintext) {
15
- const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
16
- if (!record) {
17
- throw new Boom('No SenderKeyRecord found for encryption', { statusCode: 404 });
18
- }
19
- const senderKeyState = record.getSenderKeyState();
20
- if (!senderKeyState) {
21
- throw new Boom('No session to encrypt message', { statusCode: 404 });
22
- }
23
- const iteration = senderKeyState.getSenderChainKey().getIteration();
24
- const senderKey = this.getSenderKey(senderKeyState, iteration === 0 ? 0 : iteration + 1);
25
- const ciphertext = await this.getCipherText(senderKey.getIv(), senderKey.getCipherKey(), paddedPlaintext);
26
- const senderKeyMessage = new SenderKeyMessage(
27
- senderKeyState.getKeyId(),
28
- senderKey.getIteration(),
29
- ciphertext,
30
- senderKeyState.getSigningKeyPrivate()
31
- );
32
- await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
33
- return senderKeyMessage.serialize();
34
- }
35
-
36
- async decrypt(senderKeyMessageBytes) {
37
- const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
38
- if (!record) {
39
- throw new Boom('No SenderKeyRecord found for decryption', { statusCode: 404 });
40
- }
41
- const senderKeyMessage = new SenderKeyMessage(null, null, null, null, senderKeyMessageBytes);
42
- const senderKeyState = record.getSenderKeyState(senderKeyMessage.getKeyId());
43
- if (!senderKeyState) {
44
- throw new Boom('No session found to decrypt message', { statusCode: 404 });
45
- }
46
- senderKeyMessage.verifySignature(senderKeyState.getSigningKeyPublic());
47
- const senderKey = this.getSenderKey(senderKeyState, senderKeyMessage.getIteration());
48
- const plaintext = await this.getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText());
49
- await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
50
- return plaintext;
51
- }
52
-
53
- getSenderKey(senderKeyState, iteration) {
54
- let senderChainKey = senderKeyState.getSenderChainKey();
55
- if (senderChainKey.getIteration() > iteration) {
56
- if (senderKeyState.hasSenderMessageKey(iteration)) {
57
- const messageKey = senderKeyState.removeSenderMessageKey(iteration);
58
- if (!messageKey) {
59
- throw new Boom('No sender message key found for iteration', { statusCode: 404 });
60
- }
61
- return messageKey;
62
- }
63
- throw new Boom(`Received message with old counter: ${senderChainKey.getIteration()}, ${iteration}`, { statusCode: 400 });
64
- }
65
- if (iteration - senderChainKey.getIteration() > 2000) {
66
- throw new Boom('Over 2000 messages into the future!', { statusCode: 400 });
67
- }
68
- while (senderChainKey.getIteration() < iteration) {
69
- senderKeyState.addSenderMessageKey(senderChainKey.getSenderMessageKey());
70
- senderChainKey = senderChainKey.getNext();
71
- }
72
- senderKeyState.setSenderChainKey(senderChainKey.getNext());
73
- return senderChainKey.getSenderMessageKey();
74
- }
75
-
76
- async getPlainText(iv, key, ciphertext) {
77
- try {
78
- return decrypt(key, ciphertext, iv);
79
- } catch (e) {
80
- throw new Boom('InvalidMessageException', { statusCode: 400 });
81
- }
82
- }
83
-
84
- async getCipherText(iv, key, plaintext) {
85
- try {
86
- return encrypt(key, plaintext, iv);
87
- } catch (e) {
88
- throw new Boom('InvalidMessageException', { statusCode: 400 });
89
- }
90
- }
91
- }
92
-
93
- module.exports = { GroupCipher };
@@ -1,27 +0,0 @@
1
- 'use strict';
2
-
3
- const { GroupSessionBuilder } = require('./group-session-builder');
4
- const { SenderKeyDistributionMessage } = require('./sender-key-distribution-message');
5
- const { SenderKeyRecord } = require('./sender-key-record');
6
- const { SenderKeyName } = require('./sender-key-name');
7
- const { GroupCipher } = require('./group_cipher');
8
- const { SenderKeyState } = require('./sender-key-state');
9
- const { SenderKeyMessage } = require('./sender-key-message');
10
- const { SenderMessageKey } = require('./sender-message-key');
11
- const { SenderChainKey } = require('./sender-chain-key');
12
- const { CiphertextMessage } = require('./ciphertext-message');
13
- const keyhelper = require('./keyhelper');
14
-
15
- module.exports = {
16
- GroupSessionBuilder,
17
- SenderKeyDistributionMessage,
18
- SenderKeyRecord,
19
- SenderKeyName,
20
- GroupCipher,
21
- SenderKeyState,
22
- SenderKeyMessage,
23
- SenderMessageKey,
24
- SenderChainKey,
25
- CiphertextMessage,
26
- keyhelper
27
- };
@@ -1,24 +0,0 @@
1
- 'use strict';
2
-
3
- const crypto = require('crypto');
4
- const { generateKeyPair } = require('../libsignal/curve');
5
-
6
- function generateSenderKey() {
7
- return crypto.randomBytes(32);
8
- }
9
-
10
- function generateSenderKeyId() {
11
- return crypto.randomInt(2147483647);
12
- }
13
-
14
- function generateSenderSigningKey(key) {
15
- if (!key) {
16
- key = generateKeyPair();
17
- }
18
- return {
19
- public: Buffer.from(key.pubKey),
20
- private: Buffer.from(key.privKey)
21
- };
22
- }
23
-
24
- module.exports = { generateSenderKey, generateSenderKeyId, generateSenderSigningKey };
@@ -1,35 +0,0 @@
1
- 'use strict';
2
-
3
- const { calculateMAC } = require('../libsignal/crypto');
4
- const { SenderMessageKey } = require('./sender-message-key');
5
-
6
- class SenderChainKey {
7
- constructor(iteration, chainKey) {
8
- this.MESSAGE_KEY_SEED = Buffer.from([0x01]);
9
- this.CHAIN_KEY_SEED = Buffer.from([0x02]);
10
- this.iteration = iteration;
11
- this.chainKey = Buffer.from(chainKey);
12
- }
13
-
14
- getIteration() {
15
- return this.iteration;
16
- }
17
-
18
- getSenderMessageKey() {
19
- return new SenderMessageKey(this.iteration, this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey));
20
- }
21
-
22
- getNext() {
23
- return new SenderChainKey(this.iteration + 1, this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey));
24
- }
25
-
26
- getSeed() {
27
- return this.chainKey;
28
- }
29
-
30
- getDerivative(seed, key) {
31
- return calculateMAC(key, seed);
32
- }
33
- }
34
-
35
- module.exports = { SenderChainKey };
@@ -1,137 +0,0 @@
1
- 'use strict';
2
-
3
- const { Boom } = require('@hapi/boom');
4
- const { CiphertextMessage } = require('./ciphertext-message');
5
-
6
- // ─── Minimal protobuf encode/decode for SenderKeyDistributionMessage ──────────
7
- // message SenderKeyDistributionMessage { uint32 id=1; uint32 iteration=2; bytes chainKey=3; bytes signingKey=4; }
8
-
9
- function encodeVarint(n) {
10
- const out = [];
11
- n = n >>> 0;
12
- do {
13
- let b = n & 0x7f;
14
- n >>>= 7;
15
- if (n) b |= 0x80;
16
- out.push(b);
17
- } while (n);
18
- return Buffer.from(out);
19
- }
20
-
21
- function fieldVarint(num, val) {
22
- return Buffer.concat([encodeVarint((num << 3) | 0), encodeVarint(val >>> 0)]);
23
- }
24
-
25
- function fieldBytes(num, buf) {
26
- return Buffer.concat([encodeVarint((num << 3) | 2), encodeVarint(buf.length), buf]);
27
- }
28
-
29
- function encodeSKDM(id, iteration, chainKey, signingKey) {
30
- return Buffer.concat([
31
- fieldVarint(1, id),
32
- fieldVarint(2, iteration),
33
- fieldBytes(3, Buffer.from(chainKey)),
34
- fieldBytes(4, Buffer.from(signingKey))
35
- ]);
36
- }
37
-
38
- function readVarint(buf, offset) {
39
- let result = 0, shift = 0;
40
- while (offset < buf.length) {
41
- const byte = buf[offset++];
42
- result |= (byte & 0x7f) << shift;
43
- shift += 7;
44
- if (!(byte & 0x80)) break;
45
- }
46
- return { value: result >>> 0, offset };
47
- }
48
-
49
- function decodeSKDM(buf) {
50
- const fields = {};
51
- let offset = 0;
52
- while (offset < buf.length) {
53
- const tag = readVarint(buf, offset); offset = tag.offset;
54
- const fieldNum = tag.value >>> 3;
55
- const wire = tag.value & 0x07;
56
- if (wire === 0) {
57
- const v = readVarint(buf, offset); offset = v.offset;
58
- fields[fieldNum] = v.value;
59
- } else if (wire === 2) {
60
- const len = readVarint(buf, offset); offset = len.offset;
61
- fields[fieldNum] = buf.slice(offset, offset + len.value);
62
- offset += len.value;
63
- } else {
64
- break;
65
- }
66
- }
67
- return {
68
- id: fields[1] !== undefined ? fields[1] : 0,
69
- iteration: fields[2] !== undefined ? fields[2] : 0,
70
- chainKey: fields[3] || Buffer.alloc(32),
71
- signingKey: fields[4] || Buffer.alloc(33)
72
- };
73
- }
74
-
75
- // ─── SenderKeyDistributionMessage ─────────────────────────────────────────────
76
-
77
- class SenderKeyDistributionMessage extends CiphertextMessage {
78
- constructor(id, iteration, chainKey, signatureKey, serialized) {
79
- super();
80
-
81
- if (serialized) {
82
- try {
83
- const message = serialized.slice(1);
84
- const decoded = decodeSKDM(message);
85
- this.serialized = serialized;
86
- this.id = decoded.id;
87
- this.iteration = decoded.iteration;
88
- this.chainKey = Buffer.isBuffer(decoded.chainKey)
89
- ? decoded.chainKey
90
- : Buffer.from(decoded.chainKey);
91
- this.signatureKey = Buffer.isBuffer(decoded.signingKey)
92
- ? decoded.signingKey
93
- : Buffer.from(decoded.signingKey);
94
- } catch (e) {
95
- throw new Boom(String(e));
96
- }
97
- } else {
98
- const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION);
99
- this.id = id;
100
- this.iteration = iteration;
101
- this.chainKey = Buffer.from(chainKey);
102
- this.signatureKey = Buffer.from(signatureKey);
103
- const message = encodeSKDM(id, iteration, this.chainKey, this.signatureKey);
104
- this.serialized = Buffer.concat([Buffer.from([version]), message]);
105
- }
106
- }
107
-
108
- intsToByteHighAndLow(highValue, lowValue) {
109
- return (((highValue << 4) | lowValue) & 0xff) % 256;
110
- }
111
-
112
- serialize() {
113
- return this.serialized;
114
- }
115
-
116
- getType() {
117
- return this.SENDERKEY_DISTRIBUTION_TYPE;
118
- }
119
-
120
- getIteration() {
121
- return this.iteration;
122
- }
123
-
124
- getChainKey() {
125
- return this.chainKey;
126
- }
127
-
128
- getSignatureKey() {
129
- return this.signatureKey;
130
- }
131
-
132
- getId() {
133
- return this.id;
134
- }
135
- }
136
-
137
- module.exports = { SenderKeyDistributionMessage };
@@ -1,140 +0,0 @@
1
- 'use strict';
2
-
3
- const { Boom } = require('@hapi/boom');
4
- const { calculateSignature, verifySignature } = require('../libsignal/curve');
5
- const { CiphertextMessage } = require('./ciphertext-message');
6
-
7
- // ─── Minimal protobuf encode/decode for SenderKeyMessage ─────────────────────
8
- // message SenderKeyMessage { uint32 id=1; uint32 iteration=2; bytes ciphertext=3; }
9
-
10
- function encodeVarint(n) {
11
- const out = [];
12
- n = n >>> 0;
13
- do {
14
- let b = n & 0x7f;
15
- n >>>= 7;
16
- if (n) b |= 0x80;
17
- out.push(b);
18
- } while (n);
19
- return Buffer.from(out);
20
- }
21
-
22
- function fieldVarint(num, val) {
23
- return Buffer.concat([encodeVarint((num << 3) | 0), encodeVarint(val >>> 0)]);
24
- }
25
-
26
- function fieldBytes(num, buf) {
27
- return Buffer.concat([encodeVarint((num << 3) | 2), encodeVarint(buf.length), buf]);
28
- }
29
-
30
- function encodeSKM(id, iteration, ciphertext) {
31
- return Buffer.concat([
32
- fieldVarint(1, id),
33
- fieldVarint(2, iteration),
34
- fieldBytes(3, Buffer.from(ciphertext))
35
- ]);
36
- }
37
-
38
- function readVarint(buf, offset) {
39
- let result = 0, shift = 0;
40
- while (offset < buf.length) {
41
- const byte = buf[offset++];
42
- result |= (byte & 0x7f) << shift;
43
- shift += 7;
44
- if (!(byte & 0x80)) break;
45
- }
46
- return { value: result >>> 0, offset };
47
- }
48
-
49
- function decodeSKM(buf) {
50
- const fields = {};
51
- let offset = 0;
52
- while (offset < buf.length) {
53
- const tag = readVarint(buf, offset); offset = tag.offset;
54
- const fieldNum = tag.value >>> 3;
55
- const wire = tag.value & 0x07;
56
- if (wire === 0) {
57
- const v = readVarint(buf, offset); offset = v.offset;
58
- fields[fieldNum] = v.value;
59
- } else if (wire === 2) {
60
- const len = readVarint(buf, offset); offset = len.offset;
61
- fields[fieldNum] = buf.slice(offset, offset + len.value);
62
- offset += len.value;
63
- } else {
64
- break;
65
- }
66
- }
67
- return {
68
- id: fields[1] !== undefined ? fields[1] : 0,
69
- iteration: fields[2] !== undefined ? fields[2] : 0,
70
- ciphertext: fields[3] || Buffer.alloc(0)
71
- };
72
- }
73
-
74
- // ─── SenderKeyMessage ─────────────────────────────────────────────────────────
75
-
76
- class SenderKeyMessage extends CiphertextMessage {
77
- constructor(keyId, iteration, ciphertext, signatureKey, serialized) {
78
- super();
79
- this.SIGNATURE_LENGTH = 64;
80
-
81
- if (serialized) {
82
- const version = serialized[0];
83
- const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH);
84
- const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH);
85
- const decoded = decodeSKM(message);
86
- this.serialized = serialized;
87
- this.messageVersion = (version & 0xff) >> 4;
88
- this.keyId = decoded.id;
89
- this.iteration = decoded.iteration;
90
- this.ciphertext = Buffer.isBuffer(decoded.ciphertext)
91
- ? decoded.ciphertext
92
- : Buffer.from(decoded.ciphertext);
93
- this.signature = signature;
94
- } else {
95
- const version = (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 0xff) % 256;
96
- const ciphertextBuffer = Buffer.from(ciphertext);
97
- const message = encodeSKM(keyId, iteration, ciphertextBuffer);
98
- const signature = this.getSignature(signatureKey, Buffer.concat([Buffer.from([version]), message]));
99
- this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)]);
100
- this.messageVersion = this.CURRENT_VERSION;
101
- this.keyId = keyId;
102
- this.iteration = iteration;
103
- this.ciphertext = ciphertextBuffer;
104
- this.signature = signature;
105
- }
106
- }
107
-
108
- getKeyId() {
109
- return this.keyId;
110
- }
111
-
112
- getIteration() {
113
- return this.iteration;
114
- }
115
-
116
- getCipherText() {
117
- return this.ciphertext;
118
- }
119
-
120
- verifySignature(signatureKey) {
121
- const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH);
122
- const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH);
123
- const res = verifySignature(signatureKey, part1, part2);
124
- if (!res) throw new Boom('Invalid signature!', { statusCode: 400 });
125
- }
126
-
127
- getSignature(signatureKey, serialized) {
128
- return Buffer.from(calculateSignature(signatureKey, serialized));
129
- }
130
-
131
- serialize() {
132
- return this.serialized;
133
- }
134
-
135
- getType() {
136
- return 4;
137
- }
138
- }
139
-
140
- module.exports = { SenderKeyMessage };
@@ -1,59 +0,0 @@
1
- 'use strict';
2
-
3
- function isNull(str) {
4
- return str === null || str === '';
5
- }
6
-
7
- function intValue(num) {
8
- const MAX_VALUE = 0x7fffffff;
9
- const MIN_VALUE = -0x80000000;
10
- if (num > MAX_VALUE || num < MIN_VALUE) {
11
- return num & 0xffffffff;
12
- }
13
- return num;
14
- }
15
-
16
- function hashCode(strKey) {
17
- let hash = 0;
18
- if (!isNull(strKey)) {
19
- for (let i = 0; i < strKey.length; i++) {
20
- hash = hash * 31 + strKey.charCodeAt(i);
21
- hash = intValue(hash);
22
- }
23
- }
24
- return hash;
25
- }
26
-
27
- class SenderKeyName {
28
- constructor(groupId, sender) {
29
- this.groupId = groupId;
30
- this.sender = sender;
31
- }
32
-
33
- getGroupId() {
34
- return this.groupId;
35
- }
36
-
37
- getSender() {
38
- return this.sender;
39
- }
40
-
41
- serialize() {
42
- return `${this.groupId}::${this.sender.id}::${this.sender.deviceId}`;
43
- }
44
-
45
- toString() {
46
- return this.serialize();
47
- }
48
-
49
- equals(other) {
50
- if (other === null) return false;
51
- return this.groupId === other.groupId && this.sender.toString() === other.sender.toString();
52
- }
53
-
54
- hashCode() {
55
- return hashCode(this.groupId) ^ hashCode(this.sender.toString());
56
- }
57
- }
58
-
59
- module.exports = { SenderKeyName };
@@ -1,63 +0,0 @@
1
- 'use strict';
2
-
3
- const { SenderKeyState } = require('./sender-key-state');
4
-
5
- // JSON reviver that restores Buffers from their serialized form.
6
- // Records are written with a plain JSON.stringify, so Node's default
7
- // Buffer.toJSON() shape ({ type: 'Buffer', data: [..] }) is the one produced
8
- // here; the base64-string form is also accepted so records written by any
9
- // other serializer still load.
10
- function bufferReviver(key, value) {
11
- if (value && typeof value === 'object' && value.type === 'Buffer') {
12
- if (Array.isArray(value.data)) return Buffer.from(value.data);
13
- if (typeof value.data === 'string') return Buffer.from(value.data, 'base64');
14
- }
15
- return value;
16
- }
17
-
18
- class SenderKeyRecord {
19
- constructor(serialized) {
20
- this.MAX_STATES = 5;
21
- this.senderKeyStates = [];
22
- if (serialized) {
23
- for (const structure of serialized) {
24
- this.senderKeyStates.push(new SenderKeyState(null, null, null, null, null, null, structure));
25
- }
26
- }
27
- }
28
-
29
- isEmpty() {
30
- return this.senderKeyStates.length === 0;
31
- }
32
-
33
- getSenderKeyState(keyId) {
34
- if (keyId === undefined && this.senderKeyStates.length) {
35
- return this.senderKeyStates[this.senderKeyStates.length - 1];
36
- }
37
- return this.senderKeyStates.find(state => state.getKeyId() === keyId);
38
- }
39
-
40
- addSenderKeyState(id, iteration, chainKey, signatureKey) {
41
- this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, null, signatureKey));
42
- if (this.senderKeyStates.length > this.MAX_STATES) {
43
- this.senderKeyStates.shift();
44
- }
45
- }
46
-
47
- setSenderKeyState(id, iteration, chainKey, keyPair) {
48
- this.senderKeyStates.length = 0;
49
- this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, keyPair));
50
- }
51
-
52
- serialize() {
53
- return this.senderKeyStates.map(state => state.getStructure());
54
- }
55
-
56
- static deserialize(data) {
57
- const str = Buffer.from(data).toString('utf-8');
58
- const parsed = JSON.parse(str, bufferReviver);
59
- return new SenderKeyRecord(parsed);
60
- }
61
- }
62
-
63
- module.exports = { SenderKeyRecord };
@@ -1,101 +0,0 @@
1
- 'use strict';
2
-
3
- const { SenderChainKey } = require('./sender-chain-key');
4
- const { SenderMessageKey } = require('./sender-message-key');
5
-
6
- class SenderKeyState {
7
- constructor(id, iteration, chainKey, signatureKeyPair, signatureKeyPublic, signatureKeyPrivate, senderKeyStateStructure) {
8
- this.MAX_MESSAGE_KEYS = 2000;
9
- if (senderKeyStateStructure) {
10
- this.senderKeyStateStructure = {
11
- ...senderKeyStateStructure,
12
- senderMessageKeys: Array.isArray(senderKeyStateStructure.senderMessageKeys)
13
- ? senderKeyStateStructure.senderMessageKeys
14
- : []
15
- };
16
- } else {
17
- if (signatureKeyPair) {
18
- signatureKeyPublic = signatureKeyPair.public;
19
- signatureKeyPrivate = signatureKeyPair.private;
20
- }
21
- this.senderKeyStateStructure = {
22
- senderKeyId: id || 0,
23
- senderChainKey: {
24
- iteration: iteration || 0,
25
- seed: Buffer.from(chainKey || [])
26
- },
27
- senderSigningKey: {
28
- public: Buffer.from(signatureKeyPublic || []),
29
- private: Buffer.from(signatureKeyPrivate || [])
30
- },
31
- senderMessageKeys: []
32
- };
33
- }
34
- }
35
-
36
- getKeyId() {
37
- return this.senderKeyStateStructure.senderKeyId;
38
- }
39
-
40
- getSenderChainKey() {
41
- return new SenderChainKey(
42
- this.senderKeyStateStructure.senderChainKey.iteration,
43
- this.senderKeyStateStructure.senderChainKey.seed
44
- );
45
- }
46
-
47
- setSenderChainKey(chainKey) {
48
- this.senderKeyStateStructure.senderChainKey = {
49
- iteration: chainKey.getIteration(),
50
- seed: chainKey.getSeed()
51
- };
52
- }
53
-
54
- getSigningKeyPublic() {
55
- const publicKey = Buffer.from(this.senderKeyStateStructure.senderSigningKey.public);
56
- if (publicKey.length === 32) {
57
- const fixed = Buffer.alloc(33);
58
- fixed[0] = 0x05;
59
- publicKey.copy(fixed, 1);
60
- return fixed;
61
- }
62
- return publicKey;
63
- }
64
-
65
- getSigningKeyPrivate() {
66
- const privateKey = this.senderKeyStateStructure.senderSigningKey.private;
67
- return Buffer.from(privateKey || []);
68
- }
69
-
70
- hasSenderMessageKey(iteration) {
71
- return this.senderKeyStateStructure.senderMessageKeys.some(key => key.iteration === iteration);
72
- }
73
-
74
- addSenderMessageKey(senderMessageKey) {
75
- this.senderKeyStateStructure.senderMessageKeys.push({
76
- iteration: senderMessageKey.getIteration(),
77
- seed: senderMessageKey.getSeed()
78
- });
79
- if (this.senderKeyStateStructure.senderMessageKeys.length > this.MAX_MESSAGE_KEYS) {
80
- this.senderKeyStateStructure.senderMessageKeys.shift();
81
- }
82
- }
83
-
84
- removeSenderMessageKey(iteration) {
85
- const index = this.senderKeyStateStructure.senderMessageKeys.findIndex(
86
- key => key.iteration === iteration
87
- );
88
- if (index !== -1) {
89
- const messageKey = this.senderKeyStateStructure.senderMessageKeys[index];
90
- this.senderKeyStateStructure.senderMessageKeys.splice(index, 1);
91
- return new SenderMessageKey(messageKey.iteration, messageKey.seed);
92
- }
93
- return null;
94
- }
95
-
96
- getStructure() {
97
- return this.senderKeyStateStructure;
98
- }
99
- }
100
-
101
- module.exports = { SenderKeyState };