@sixcore/baileys 1.0.0 → 1.0.2

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 (228) hide show
  1. package/WAProto/index.js +14270 -302
  2. package/jessica.js +91 -0
  3. package/lib/Defaults/baileys-version.json +1 -1
  4. package/lib/Defaults/index.js +117 -79
  5. package/lib/Defaults/phonenumber-mcc.json +223 -0
  6. package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
  7. package/lib/Signal/Group/ciphertext-message.js +15 -0
  8. package/lib/Signal/Group/group-session-builder.d.ts +14 -0
  9. package/lib/Signal/Group/group-session-builder.js +64 -0
  10. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  11. package/lib/Signal/Group/group_cipher.js +96 -0
  12. package/lib/Signal/Group/index.d.ts +11 -0
  13. package/lib/Signal/Group/index.js +57 -0
  14. package/lib/Signal/Group/keyhelper.d.ts +10 -0
  15. package/lib/Signal/Group/keyhelper.js +55 -0
  16. package/lib/Signal/Group/queue-job.d.ts +1 -0
  17. package/lib/Signal/Group/queue-job.js +57 -0
  18. package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
  19. package/lib/Signal/Group/sender-chain-key.js +34 -0
  20. package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
  21. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  22. package/lib/Signal/Group/sender-key-message.d.ts +18 -0
  23. package/lib/Signal/Group/sender-key-message.js +69 -0
  24. package/lib/Signal/Group/sender-key-name.d.ts +17 -0
  25. package/lib/Signal/Group/sender-key-name.js +51 -0
  26. package/lib/Signal/Group/sender-key-record.d.ts +30 -0
  27. package/lib/Signal/Group/sender-key-record.js +53 -0
  28. package/lib/Signal/Group/sender-key-state.d.ts +38 -0
  29. package/lib/Signal/Group/sender-key-state.js +99 -0
  30. package/lib/Signal/Group/sender-message-key.d.ts +11 -0
  31. package/{WASignalGroup/sender_message_key.js → lib/Signal/Group/sender-message-key.js} +6 -16
  32. package/lib/Signal/libsignal.js +51 -29
  33. package/lib/Socket/business.d.ts +43 -42
  34. package/lib/Socket/chats.d.ts +222 -36
  35. package/lib/Socket/chats.js +173 -153
  36. package/lib/Socket/dugong.d.ts +254 -0
  37. package/lib/Socket/dugong.js +484 -0
  38. package/lib/Socket/groups.d.ts +7 -7
  39. package/lib/Socket/groups.js +37 -35
  40. package/lib/Socket/index.d.ts +52 -51
  41. package/lib/Socket/index.js +1 -0
  42. package/lib/Socket/messages-recv.d.ts +37 -34
  43. package/lib/Socket/messages-recv.js +175 -37
  44. package/lib/Socket/messages-send.d.ts +12 -18
  45. package/lib/Socket/messages-send.js +396 -574
  46. package/lib/Socket/newsletter.d.ts +28 -26
  47. package/lib/Socket/newsletter.js +132 -121
  48. package/lib/Socket/registration.d.ts +52 -49
  49. package/lib/Socket/registration.js +7 -7
  50. package/lib/Socket/socket.d.ts +0 -1
  51. package/lib/Socket/socket.js +49 -27
  52. package/lib/Socket/usync.d.ts +10 -11
  53. package/lib/Store/make-cache-manager-store.d.ts +1 -2
  54. package/lib/Store/make-in-memory-store.d.ts +2 -2
  55. package/lib/Store/make-in-memory-store.js +1 -5
  56. package/lib/Store/make-ordered-dictionary.js +2 -2
  57. package/lib/Types/Auth.d.ts +1 -0
  58. package/lib/Types/Call.d.ts +1 -1
  59. package/lib/Types/Chat.d.ts +7 -12
  60. package/lib/Types/Events.d.ts +2 -17
  61. package/lib/Types/GroupMetadata.d.ts +2 -3
  62. package/lib/Types/Label.d.ts +0 -11
  63. package/lib/Types/Label.js +1 -1
  64. package/lib/Types/LabelAssociation.js +1 -1
  65. package/lib/Types/Message.d.ts +10 -170
  66. package/lib/Types/Newsletter.d.ts +97 -86
  67. package/lib/Types/Newsletter.js +38 -32
  68. package/lib/Types/Socket.d.ts +2 -7
  69. package/lib/Types/index.d.ts +0 -9
  70. package/lib/Types/index.js +1 -1
  71. package/lib/Utils/auth-utils.js +14 -35
  72. package/lib/Utils/business.d.ts +1 -1
  73. package/lib/Utils/business.js +2 -2
  74. package/lib/Utils/chat-utils.d.ts +12 -11
  75. package/lib/Utils/chat-utils.js +36 -52
  76. package/lib/Utils/crypto.d.ts +16 -15
  77. package/lib/Utils/crypto.js +26 -74
  78. package/lib/Utils/decode-wa-message.d.ts +0 -17
  79. package/lib/Utils/decode-wa-message.js +17 -53
  80. package/lib/Utils/event-buffer.js +7 -10
  81. package/lib/Utils/generics.d.ts +17 -13
  82. package/lib/Utils/generics.js +79 -58
  83. package/lib/Utils/history.d.ts +2 -6
  84. package/lib/Utils/history.js +6 -4
  85. package/lib/Utils/logger.d.ts +3 -1
  86. package/lib/Utils/lt-hash.js +12 -12
  87. package/lib/Utils/make-mutex.d.ts +2 -2
  88. package/lib/Utils/messages-media.d.ts +28 -25
  89. package/lib/Utils/messages-media.js +733 -557
  90. package/lib/Utils/messages.js +68 -473
  91. package/lib/Utils/noise-handler.d.ts +5 -4
  92. package/lib/Utils/noise-handler.js +14 -19
  93. package/lib/Utils/process-message.d.ts +5 -5
  94. package/lib/Utils/process-message.js +23 -75
  95. package/lib/Utils/signal.d.ts +1 -2
  96. package/lib/Utils/signal.js +26 -32
  97. package/lib/Utils/use-multi-file-auth-state.d.ts +1 -0
  98. package/lib/Utils/use-multi-file-auth-state.js +66 -242
  99. package/lib/Utils/validate-connection.d.ts +1 -1
  100. package/lib/Utils/validate-connection.js +88 -64
  101. package/lib/WABinary/constants.d.ts +27 -24
  102. package/lib/WABinary/decode.d.ts +2 -1
  103. package/lib/WABinary/decode.js +11 -23
  104. package/lib/WABinary/encode.d.ts +2 -1
  105. package/lib/WABinary/encode.js +147 -134
  106. package/lib/WABinary/generic-utils.d.ts +5 -2
  107. package/lib/WABinary/generic-utils.js +125 -37
  108. package/lib/WABinary/jid-utils.d.ts +1 -1
  109. package/lib/WAM/BinaryInfo.d.ts +11 -2
  110. package/lib/WAM/encode.d.ts +2 -1
  111. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +3 -3
  112. package/lib/WAUSync/USyncUser.d.ts +2 -0
  113. package/lib/index.d.ts +12 -0
  114. package/lib/index.js +64 -1
  115. package/package.json +113 -51
  116. package/WAProto/GenerateStatics.sh +0 -4
  117. package/WAProto/WAProto.proto +0 -4357
  118. package/WAProto/index.d.ts +0 -50383
  119. package/WASignalGroup/GroupProtocol.js +0 -1697
  120. package/WASignalGroup/ciphertext_message.js +0 -16
  121. package/WASignalGroup/generate-proto.sh +0 -1
  122. package/WASignalGroup/group.proto +0 -42
  123. package/WASignalGroup/group_cipher.js +0 -120
  124. package/WASignalGroup/group_session_builder.js +0 -46
  125. package/WASignalGroup/index.js +0 -5
  126. package/WASignalGroup/keyhelper.js +0 -21
  127. package/WASignalGroup/protobufs.js +0 -3
  128. package/WASignalGroup/queue_job.js +0 -69
  129. package/WASignalGroup/sender_chain_key.js +0 -50
  130. package/WASignalGroup/sender_key_distribution_message.js +0 -78
  131. package/WASignalGroup/sender_key_message.js +0 -92
  132. package/WASignalGroup/sender_key_name.js +0 -70
  133. package/WASignalGroup/sender_key_record.js +0 -56
  134. package/WASignalGroup/sender_key_state.js +0 -129
  135. package/lib/Utils/use-single-file-auth-state.d.ts +0 -12
  136. package/lib/Utils/use-single-file-auth-state.js +0 -75
  137. package/src/Defaults/baileys-version.json +0 -3
  138. package/src/Defaults/index.ts +0 -133
  139. package/src/Signal/Group/ciphertext-message.ts +0 -9
  140. package/src/Signal/Group/group-session-builder.ts +0 -56
  141. package/src/Signal/Group/group_cipher.ts +0 -117
  142. package/src/Signal/Group/index.ts +0 -11
  143. package/src/Signal/Group/keyhelper.ts +0 -28
  144. package/src/Signal/Group/sender-chain-key.ts +0 -34
  145. package/src/Signal/Group/sender-key-distribution-message.ts +0 -95
  146. package/src/Signal/Group/sender-key-message.ts +0 -96
  147. package/src/Signal/Group/sender-key-name.ts +0 -66
  148. package/src/Signal/Group/sender-key-record.ts +0 -69
  149. package/src/Signal/Group/sender-key-state.ts +0 -134
  150. package/src/Signal/Group/sender-message-key.ts +0 -36
  151. package/src/Signal/libsignal.ts +0 -447
  152. package/src/Signal/lid-mapping.ts +0 -209
  153. package/src/Socket/Client/index.ts +0 -2
  154. package/src/Socket/Client/types.ts +0 -22
  155. package/src/Socket/Client/websocket.ts +0 -56
  156. package/src/Socket/business.ts +0 -421
  157. package/src/Socket/chats.ts +0 -1223
  158. package/src/Socket/communities.ts +0 -477
  159. package/src/Socket/groups.ts +0 -361
  160. package/src/Socket/index.ts +0 -22
  161. package/src/Socket/messages-recv.ts +0 -1563
  162. package/src/Socket/messages-send.ts +0 -1210
  163. package/src/Socket/mex.ts +0 -58
  164. package/src/Socket/newsletter.ts +0 -229
  165. package/src/Socket/socket.ts +0 -1072
  166. package/src/Types/Auth.ts +0 -115
  167. package/src/Types/Bussines.ts +0 -20
  168. package/src/Types/Call.ts +0 -14
  169. package/src/Types/Chat.ts +0 -138
  170. package/src/Types/Contact.ts +0 -24
  171. package/src/Types/Events.ts +0 -132
  172. package/src/Types/GroupMetadata.ts +0 -70
  173. package/src/Types/Label.ts +0 -48
  174. package/src/Types/LabelAssociation.ts +0 -35
  175. package/src/Types/Message.ts +0 -424
  176. package/src/Types/Newsletter.ts +0 -98
  177. package/src/Types/Product.ts +0 -85
  178. package/src/Types/Signal.ts +0 -76
  179. package/src/Types/Socket.ts +0 -150
  180. package/src/Types/State.ts +0 -43
  181. package/src/Types/USync.ts +0 -27
  182. package/src/Types/globals.d.ts +0 -8
  183. package/src/Types/index.ts +0 -67
  184. package/src/Utils/auth-utils.ts +0 -331
  185. package/src/Utils/browser-utils.ts +0 -31
  186. package/src/Utils/business.ts +0 -286
  187. package/src/Utils/chat-utils.ts +0 -933
  188. package/src/Utils/crypto.ts +0 -184
  189. package/src/Utils/decode-wa-message.ts +0 -355
  190. package/src/Utils/event-buffer.ts +0 -662
  191. package/src/Utils/generics.ts +0 -470
  192. package/src/Utils/history.ts +0 -114
  193. package/src/Utils/index.ts +0 -18
  194. package/src/Utils/link-preview.ts +0 -111
  195. package/src/Utils/logger.ts +0 -13
  196. package/src/Utils/lt-hash.ts +0 -65
  197. package/src/Utils/make-mutex.ts +0 -45
  198. package/src/Utils/message-retry-manager.ts +0 -229
  199. package/src/Utils/messages-media.ts +0 -820
  200. package/src/Utils/messages.ts +0 -1137
  201. package/src/Utils/noise-handler.ts +0 -192
  202. package/src/Utils/pre-key-manager.ts +0 -126
  203. package/src/Utils/process-message.ts +0 -622
  204. package/src/Utils/signal.ts +0 -214
  205. package/src/Utils/use-multi-file-auth-state.ts +0 -136
  206. package/src/Utils/validate-connection.ts +0 -253
  207. package/src/WABinary/constants.ts +0 -1305
  208. package/src/WABinary/decode.ts +0 -281
  209. package/src/WABinary/encode.ts +0 -253
  210. package/src/WABinary/generic-utils.ts +0 -127
  211. package/src/WABinary/index.ts +0 -5
  212. package/src/WABinary/jid-utils.ts +0 -128
  213. package/src/WABinary/types.ts +0 -17
  214. package/src/WAM/BinaryInfo.ts +0 -12
  215. package/src/WAM/constants.ts +0 -22889
  216. package/src/WAM/encode.ts +0 -169
  217. package/src/WAM/index.ts +0 -3
  218. package/src/WAUSync/Protocols/USyncContactProtocol.ts +0 -32
  219. package/src/WAUSync/Protocols/USyncDeviceProtocol.ts +0 -78
  220. package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.ts +0 -35
  221. package/src/WAUSync/Protocols/USyncStatusProtocol.ts +0 -44
  222. package/src/WAUSync/Protocols/UsyncBotProfileProtocol.ts +0 -76
  223. package/src/WAUSync/Protocols/UsyncLIDProtocol.ts +0 -33
  224. package/src/WAUSync/Protocols/index.ts +0 -4
  225. package/src/WAUSync/USyncQuery.ts +0 -133
  226. package/src/WAUSync/USyncUser.ts +0 -32
  227. package/src/WAUSync/index.ts +0 -3
  228. package/src/index.ts +0 -13
@@ -1,95 +0,0 @@
1
- import { proto } from '../../../WAProto/index.js'
2
- import { CiphertextMessage } from './ciphertext-message'
3
-
4
- interface SenderKeyDistributionMessageStructure {
5
- id: number
6
- iteration: number
7
- chainKey: string | Uint8Array
8
- signingKey: string | Uint8Array
9
- }
10
-
11
- export class SenderKeyDistributionMessage extends CiphertextMessage {
12
- private readonly id: number
13
- private readonly iteration: number
14
- private readonly chainKey: Uint8Array
15
- private readonly signatureKey: Uint8Array
16
- private readonly serialized: Uint8Array
17
-
18
- constructor(
19
- id?: number | null,
20
- iteration?: number | null,
21
- chainKey?: Uint8Array | null,
22
- signatureKey?: Uint8Array | null,
23
- serialized?: Uint8Array | null
24
- ) {
25
- super()
26
-
27
- if (serialized) {
28
- try {
29
- const message = serialized.slice(1)
30
- const distributionMessage = proto.SenderKeyDistributionMessage.decode(
31
- message
32
- ).toJSON() as SenderKeyDistributionMessageStructure
33
-
34
- this.serialized = serialized
35
- this.id = distributionMessage.id
36
- this.iteration = distributionMessage.iteration
37
- this.chainKey =
38
- typeof distributionMessage.chainKey === 'string'
39
- ? Buffer.from(distributionMessage.chainKey, 'base64')
40
- : distributionMessage.chainKey
41
- this.signatureKey =
42
- typeof distributionMessage.signingKey === 'string'
43
- ? Buffer.from(distributionMessage.signingKey, 'base64')
44
- : distributionMessage.signingKey
45
- } catch (e) {
46
- throw new Error(String(e))
47
- }
48
- } else {
49
- const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION)
50
- this.id = id!
51
- this.iteration = iteration!
52
- this.chainKey = chainKey!
53
- this.signatureKey = signatureKey!
54
-
55
- const message = proto.SenderKeyDistributionMessage.encode(
56
- proto.SenderKeyDistributionMessage.create({
57
- id,
58
- iteration,
59
- chainKey,
60
- signingKey: this.signatureKey
61
- })
62
- ).finish()
63
-
64
- this.serialized = Buffer.concat([Buffer.from([version]), message])
65
- }
66
- }
67
-
68
- private intsToByteHighAndLow(highValue: number, lowValue: number): number {
69
- return (((highValue << 4) | lowValue) & 0xff) % 256
70
- }
71
-
72
- public serialize(): Uint8Array {
73
- return this.serialized
74
- }
75
-
76
- public getType(): number {
77
- return this.SENDERKEY_DISTRIBUTION_TYPE
78
- }
79
-
80
- public getIteration(): number {
81
- return this.iteration
82
- }
83
-
84
- public getChainKey(): Uint8Array {
85
- return this.chainKey
86
- }
87
-
88
- public getSignatureKey(): Uint8Array {
89
- return this.signatureKey
90
- }
91
-
92
- public getId(): number {
93
- return this.id
94
- }
95
- }
@@ -1,96 +0,0 @@
1
- import { calculateSignature, verifySignature } from 'libsignal/src/curve'
2
- import { proto } from '../../../WAProto/index.js'
3
- import { CiphertextMessage } from './ciphertext-message'
4
-
5
- interface SenderKeyMessageStructure {
6
- id: number
7
- iteration: number
8
- ciphertext: string | Buffer
9
- }
10
-
11
- export class SenderKeyMessage extends CiphertextMessage {
12
- private readonly SIGNATURE_LENGTH = 64
13
- private readonly messageVersion: number
14
- private readonly keyId: number
15
- private readonly iteration: number
16
- private readonly ciphertext: Uint8Array
17
- private readonly signature: Uint8Array
18
- private readonly serialized: Uint8Array
19
-
20
- constructor(
21
- keyId?: number | null,
22
- iteration?: number | null,
23
- ciphertext?: Uint8Array | null,
24
- signatureKey?: Uint8Array | null,
25
- serialized?: Uint8Array | null
26
- ) {
27
- super()
28
-
29
- if (serialized) {
30
- const version = serialized[0]!
31
- const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH)
32
- const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH)
33
- const senderKeyMessage = proto.SenderKeyMessage.decode(message).toJSON() as SenderKeyMessageStructure
34
-
35
- this.serialized = serialized
36
- this.messageVersion = (version & 0xff) >> 4
37
- this.keyId = senderKeyMessage.id
38
- this.iteration = senderKeyMessage.iteration
39
- this.ciphertext =
40
- typeof senderKeyMessage.ciphertext === 'string'
41
- ? Buffer.from(senderKeyMessage.ciphertext, 'base64')
42
- : senderKeyMessage.ciphertext
43
- this.signature = signature
44
- } else {
45
- const version = (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 0xff) % 256
46
- const ciphertextBuffer = Buffer.from(ciphertext!)
47
- const message = proto.SenderKeyMessage.encode(
48
- proto.SenderKeyMessage.create({
49
- id: keyId!,
50
- iteration: iteration!,
51
- ciphertext: ciphertextBuffer
52
- })
53
- ).finish()
54
-
55
- const signature = this.getSignature(signatureKey!, Buffer.concat([Buffer.from([version]), message]))
56
-
57
- this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)])
58
- this.messageVersion = this.CURRENT_VERSION
59
- this.keyId = keyId!
60
- this.iteration = iteration!
61
- this.ciphertext = ciphertextBuffer
62
- this.signature = signature
63
- }
64
- }
65
-
66
- public getKeyId(): number {
67
- return this.keyId
68
- }
69
-
70
- public getIteration(): number {
71
- return this.iteration
72
- }
73
-
74
- public getCipherText(): Uint8Array {
75
- return this.ciphertext
76
- }
77
-
78
- public verifySignature(signatureKey: Uint8Array): void {
79
- const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH)
80
- const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH)
81
- const res = verifySignature(signatureKey, part1, part2)
82
- if (!res) throw new Error('Invalid signature!')
83
- }
84
-
85
- private getSignature(signatureKey: Uint8Array, serialized: Uint8Array): Uint8Array {
86
- return Buffer.from(calculateSignature(signatureKey, serialized))
87
- }
88
-
89
- public serialize(): Uint8Array {
90
- return this.serialized
91
- }
92
-
93
- public getType(): number {
94
- return 4
95
- }
96
- }
@@ -1,66 +0,0 @@
1
- interface Sender {
2
- id: string
3
- deviceId: number
4
- toString(): string
5
- }
6
-
7
- function isNull(str: string | null): boolean {
8
- return str === null || str === ''
9
- }
10
-
11
- function intValue(num: number): number {
12
- const MAX_VALUE = 0x7fffffff
13
- const MIN_VALUE = -0x80000000
14
- if (num > MAX_VALUE || num < MIN_VALUE) {
15
- return num & 0xffffffff
16
- }
17
-
18
- return num
19
- }
20
-
21
- function hashCode(strKey: string): number {
22
- let hash = 0
23
- if (!isNull(strKey)) {
24
- for (let i = 0; i < strKey.length; i++) {
25
- hash = hash * 31 + strKey.charCodeAt(i)
26
- hash = intValue(hash)
27
- }
28
- }
29
-
30
- return hash
31
- }
32
-
33
- export class SenderKeyName {
34
- private readonly groupId: string
35
- private readonly sender: Sender
36
-
37
- constructor(groupId: string, sender: Sender) {
38
- this.groupId = groupId
39
- this.sender = sender
40
- }
41
-
42
- public getGroupId(): string {
43
- return this.groupId
44
- }
45
-
46
- public getSender(): Sender {
47
- return this.sender
48
- }
49
-
50
- public serialize(): string {
51
- return `${this.groupId}::${this.sender.id}::${this.sender.deviceId}`
52
- }
53
-
54
- public toString(): string {
55
- return this.serialize()
56
- }
57
-
58
- public equals(other: SenderKeyName | null): boolean {
59
- if (other === null) return false
60
- return this.groupId === other.groupId && this.sender.toString() === other.sender.toString()
61
- }
62
-
63
- public hashCode(): number {
64
- return hashCode(this.groupId) ^ hashCode(this.sender.toString())
65
- }
66
- }
@@ -1,69 +0,0 @@
1
- import { BufferJSON } from '../../Utils/generics'
2
- import { SenderKeyState } from './sender-key-state'
3
-
4
- export interface SenderKeyStateStructure {
5
- senderKeyId: number
6
- senderChainKey: {
7
- iteration: number
8
- seed: Uint8Array
9
- }
10
- senderSigningKey: {
11
- public: Uint8Array
12
- private?: Uint8Array
13
- }
14
- senderMessageKeys: Array<{
15
- iteration: number
16
- seed: Uint8Array
17
- }>
18
- }
19
-
20
- export class SenderKeyRecord {
21
- private readonly MAX_STATES = 5
22
- private readonly senderKeyStates: SenderKeyState[] = []
23
-
24
- constructor(serialized?: SenderKeyStateStructure[]) {
25
- if (serialized) {
26
- for (const structure of serialized) {
27
- this.senderKeyStates.push(new SenderKeyState(null, null, null, null, null, null, structure))
28
- }
29
- }
30
- }
31
-
32
- public isEmpty(): boolean {
33
- return this.senderKeyStates.length === 0
34
- }
35
-
36
- public getSenderKeyState(keyId?: number): SenderKeyState | undefined {
37
- if (keyId === undefined && this.senderKeyStates.length) {
38
- return this.senderKeyStates[this.senderKeyStates.length - 1]
39
- }
40
-
41
- return this.senderKeyStates.find(state => state.getKeyId() === keyId)
42
- }
43
-
44
- public addSenderKeyState(id: number, iteration: number, chainKey: Uint8Array, signatureKey: Uint8Array): void {
45
- this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, null, signatureKey))
46
- if (this.senderKeyStates.length > this.MAX_STATES) {
47
- this.senderKeyStates.shift()
48
- }
49
- }
50
-
51
- public setSenderKeyState(
52
- id: number,
53
- iteration: number,
54
- chainKey: Uint8Array,
55
- keyPair: { public: Uint8Array; private: Uint8Array }
56
- ): void {
57
- this.senderKeyStates.length = 0
58
- this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, keyPair))
59
- }
60
-
61
- public serialize(): SenderKeyStateStructure[] {
62
- return this.senderKeyStates.map(state => state.getStructure())
63
- }
64
- static deserialize(data: Uint8Array): SenderKeyRecord {
65
- const str = Buffer.from(data).toString('utf-8')
66
- const parsed = JSON.parse(str, BufferJSON.reviver)
67
- return new SenderKeyRecord(parsed)
68
- }
69
- }
@@ -1,134 +0,0 @@
1
- import { SenderChainKey } from './sender-chain-key'
2
- import { SenderMessageKey } from './sender-message-key'
3
-
4
- interface SenderChainKeyStructure {
5
- iteration: number
6
- seed: Uint8Array
7
- }
8
-
9
- interface SenderSigningKeyStructure {
10
- public: Uint8Array
11
- private?: Uint8Array
12
- }
13
-
14
- interface SenderMessageKeyStructure {
15
- iteration: number
16
- seed: Uint8Array
17
- }
18
-
19
- interface SenderKeyStateStructure {
20
- senderKeyId: number
21
- senderChainKey: SenderChainKeyStructure
22
- senderSigningKey: SenderSigningKeyStructure
23
- senderMessageKeys: SenderMessageKeyStructure[]
24
- }
25
-
26
- export class SenderKeyState {
27
- private readonly MAX_MESSAGE_KEYS = 2000
28
- private readonly senderKeyStateStructure: SenderKeyStateStructure
29
-
30
- constructor(
31
- id?: number | null,
32
- iteration?: number | null,
33
- chainKey?: Uint8Array | null | string,
34
- signatureKeyPair?: { public: Uint8Array | string; private: Uint8Array | string } | null,
35
- signatureKeyPublic?: Uint8Array | string | null,
36
- signatureKeyPrivate?: Uint8Array | string | null,
37
- senderKeyStateStructure?: SenderKeyStateStructure | null
38
- ) {
39
- if (senderKeyStateStructure) {
40
- this.senderKeyStateStructure = {
41
- ...senderKeyStateStructure,
42
- senderMessageKeys: Array.isArray(senderKeyStateStructure.senderMessageKeys)
43
- ? senderKeyStateStructure.senderMessageKeys
44
- : []
45
- }
46
- } else {
47
- if (signatureKeyPair) {
48
- signatureKeyPublic = signatureKeyPair.public
49
- signatureKeyPrivate = signatureKeyPair.private
50
- }
51
-
52
- this.senderKeyStateStructure = {
53
- senderKeyId: id || 0,
54
- senderChainKey: {
55
- iteration: iteration || 0,
56
- seed: Buffer.from(chainKey || [])
57
- },
58
- senderSigningKey: {
59
- public: Buffer.from(signatureKeyPublic || []),
60
- private: Buffer.from(signatureKeyPrivate || [])
61
- },
62
- senderMessageKeys: []
63
- }
64
- }
65
- }
66
-
67
- public getKeyId(): number {
68
- return this.senderKeyStateStructure.senderKeyId
69
- }
70
-
71
- public getSenderChainKey(): SenderChainKey {
72
- return new SenderChainKey(
73
- this.senderKeyStateStructure.senderChainKey.iteration,
74
- this.senderKeyStateStructure.senderChainKey.seed
75
- )
76
- }
77
-
78
- public setSenderChainKey(chainKey: SenderChainKey): void {
79
- this.senderKeyStateStructure.senderChainKey = {
80
- iteration: chainKey.getIteration(),
81
- seed: chainKey.getSeed()
82
- }
83
- }
84
-
85
- public getSigningKeyPublic(): Buffer {
86
- const publicKey = Buffer.from(this.senderKeyStateStructure.senderSigningKey.public)
87
-
88
- if (publicKey.length === 32) {
89
- const fixed = Buffer.alloc(33)
90
- fixed[0] = 0x05
91
- publicKey.copy(fixed, 1)
92
- return fixed
93
- }
94
-
95
- return publicKey
96
- }
97
-
98
- public getSigningKeyPrivate(): Buffer | undefined {
99
- const privateKey = this.senderKeyStateStructure.senderSigningKey.private
100
-
101
- return Buffer.from(privateKey || [])
102
- }
103
-
104
- public hasSenderMessageKey(iteration: number): boolean {
105
- return this.senderKeyStateStructure.senderMessageKeys.some(key => key.iteration === iteration)
106
- }
107
-
108
- public addSenderMessageKey(senderMessageKey: SenderMessageKey): void {
109
- this.senderKeyStateStructure.senderMessageKeys.push({
110
- iteration: senderMessageKey.getIteration(),
111
- seed: senderMessageKey.getSeed()
112
- })
113
-
114
- if (this.senderKeyStateStructure.senderMessageKeys.length > this.MAX_MESSAGE_KEYS) {
115
- this.senderKeyStateStructure.senderMessageKeys.shift()
116
- }
117
- }
118
-
119
- public removeSenderMessageKey(iteration: number): SenderMessageKey | null {
120
- const index = this.senderKeyStateStructure.senderMessageKeys.findIndex(key => key.iteration === iteration)
121
-
122
- if (index !== -1) {
123
- const messageKey = this.senderKeyStateStructure.senderMessageKeys[index]!
124
- this.senderKeyStateStructure.senderMessageKeys.splice(index, 1)
125
- return new SenderMessageKey(messageKey.iteration, messageKey.seed)
126
- }
127
-
128
- return null
129
- }
130
-
131
- public getStructure(): SenderKeyStateStructure {
132
- return this.senderKeyStateStructure
133
- }
134
- }
@@ -1,36 +0,0 @@
1
- import { deriveSecrets } from 'libsignal/src/crypto'
2
-
3
- export class SenderMessageKey {
4
- private readonly iteration: number
5
- private readonly iv: Uint8Array
6
- private readonly cipherKey: Uint8Array
7
- private readonly seed: Uint8Array
8
-
9
- constructor(iteration: number, seed: Uint8Array) {
10
- const derivative = deriveSecrets(seed, Buffer.alloc(32), Buffer.from('WhisperGroup'))
11
- const keys = new Uint8Array(32)
12
- keys.set(new Uint8Array(derivative[0].slice(16)))
13
- keys.set(new Uint8Array(derivative[1].slice(0, 16)), 16)
14
-
15
- this.iv = Buffer.from(derivative[0].slice(0, 16))
16
- this.cipherKey = Buffer.from(keys.buffer)
17
- this.iteration = iteration
18
- this.seed = seed
19
- }
20
-
21
- public getIteration(): number {
22
- return this.iteration
23
- }
24
-
25
- public getIv(): Uint8Array {
26
- return this.iv
27
- }
28
-
29
- public getCipherKey(): Uint8Array {
30
- return this.cipherKey
31
- }
32
-
33
- public getSeed(): Uint8Array {
34
- return this.seed
35
- }
36
- }