fca-nx 6.6.6
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.
- package/LICENCE +21 -0
- package/README.md +446 -0
- package/e2ee/config.json +0 -0
- package/e2ee/crypto.js +173 -0
- package/e2ee/index.js +1032 -0
- package/e2ee/proto/ArmadilloApplication.proto +281 -0
- package/e2ee/proto/ArmadilloICDC.proto +14 -0
- package/e2ee/proto/ConsumerApplication.proto +232 -0
- package/e2ee/proto/MessageApplication.proto +82 -0
- package/e2ee/proto/MessageTransport.proto +77 -0
- package/e2ee/proto/WACommon.proto +66 -0
- package/e2ee/proto/WAMediaTransport.proto +176 -0
- package/e2ee/protocol.js +196 -0
- package/e2ee/ratchet.js +219 -0
- package/e2ee/store.js +182 -0
- package/e2ee/vendor/fb-e2ee.cjs +9159 -0
- package/e2ee/vendor/proto/ArmadilloApplication.proto +281 -0
- package/e2ee/vendor/proto/ArmadilloICDC.proto +14 -0
- package/e2ee/vendor/proto/ConsumerApplication.proto +232 -0
- package/e2ee/vendor/proto/MessageApplication.proto +82 -0
- package/e2ee/vendor/proto/MessageTransport.proto +77 -0
- package/e2ee/vendor/proto/WACommon.proto +66 -0
- package/e2ee/vendor/proto/WAMediaTransport.proto +176 -0
- package/func/checkUpdate.js +7 -0
- package/func/logAdapter.js +33 -0
- package/func/logger.js +134 -0
- package/index.js +22 -0
- package/module/config.js +45 -0
- package/module/login.js +183 -0
- package/module/loginHelper.js +1382 -0
- package/module/options.js +52 -0
- package/package.json +101 -0
- package/src/api/action/addExternalModule.js +25 -0
- package/src/api/action/addFriend.js +145 -0
- package/src/api/action/changeAvatar.js +137 -0
- package/src/api/action/changeBio.js +75 -0
- package/src/api/action/changeCover.js +277 -0
- package/src/api/action/enableAutoSaveAppState.js +73 -0
- package/src/api/action/getCurrentUserID.js +7 -0
- package/src/api/action/handleFriendRequest.js +57 -0
- package/src/api/action/logout.js +76 -0
- package/src/api/action/refreshFb_dtsg.js +48 -0
- package/src/api/action/setPostReaction.js +106 -0
- package/src/api/action/unfriend.js +54 -0
- package/src/api/http/httpGet.js +46 -0
- package/src/api/http/httpPost.js +52 -0
- package/src/api/http/postFormData.js +47 -0
- package/src/api/messaging/addUserToGroup.js +68 -0
- package/src/api/messaging/changeAdminStatus.js +126 -0
- package/src/api/messaging/changeArchivedStatus.js +55 -0
- package/src/api/messaging/changeBlockedStatus.js +48 -0
- package/src/api/messaging/changeGroupImage.js +91 -0
- package/src/api/messaging/changeNickname.js +70 -0
- package/src/api/messaging/changeThreadColor.js +79 -0
- package/src/api/messaging/changeThreadEmoji.js +111 -0
- package/src/api/messaging/createNewGroup.js +88 -0
- package/src/api/messaging/createPoll.js +46 -0
- package/src/api/messaging/createThemeAI.js +98 -0
- package/src/api/messaging/deleteMessage.js +136 -0
- package/src/api/messaging/deleteThread.js +56 -0
- package/src/api/messaging/editMessage.js +90 -0
- package/src/api/messaging/forwardAttachment.js +57 -0
- package/src/api/messaging/forwardMessage.js +134 -0
- package/src/api/messaging/getEmojiUrl.js +29 -0
- package/src/api/messaging/getFriendsList.js +82 -0
- package/src/api/messaging/getMessage.js +829 -0
- package/src/api/messaging/getThemePictures.js +62 -0
- package/src/api/messaging/handleMessageRequest.js +65 -0
- package/src/api/messaging/markAsDelivered.js +57 -0
- package/src/api/messaging/markAsRead.js +88 -0
- package/src/api/messaging/markAsReadAll.js +49 -0
- package/src/api/messaging/markAsSeen.js +61 -0
- package/src/api/messaging/muteThread.js +50 -0
- package/src/api/messaging/pinMessage.js +115 -0
- package/src/api/messaging/removeUserFromGroup.js +62 -0
- package/src/api/messaging/resolvePhotoUrl.js +43 -0
- package/src/api/messaging/scheduler.js +264 -0
- package/src/api/messaging/searchForThread.js +53 -0
- package/src/api/messaging/sendBroadcast.js +93 -0
- package/src/api/messaging/sendMessage.js +269 -0
- package/src/api/messaging/sendTypingIndicator.js +74 -0
- package/src/api/messaging/sessionGuard.js +208 -0
- package/src/api/messaging/setMessageReaction.js +95 -0
- package/src/api/messaging/setTitle.js +124 -0
- package/src/api/messaging/shareContact.js +49 -0
- package/src/api/messaging/threadColors.js +128 -0
- package/src/api/messaging/unsendMessage.js +81 -0
- package/src/api/messaging/uploadAttachment.js +523 -0
- package/src/api/socket/OldMessage.js +186 -0
- package/src/api/socket/core/connectMqtt.js +320 -0
- package/src/api/socket/core/emitAuth.js +103 -0
- package/src/api/socket/core/getSeqID.js +338 -0
- package/src/api/socket/core/getTaskResponseData.js +25 -0
- package/src/api/socket/core/parseDelta.js +387 -0
- package/src/api/socket/detail/buildStream.js +215 -0
- package/src/api/socket/detail/constants.js +28 -0
- package/src/api/socket/e2ee/crypto.js +173 -0
- package/src/api/socket/e2ee/e2ee/crypto.js +173 -0
- package/src/api/socket/e2ee/e2ee/index.js +421 -0
- package/src/api/socket/e2ee/e2ee/proto/ArmadilloApplication.proto +281 -0
- package/src/api/socket/e2ee/e2ee/proto/ArmadilloICDC.proto +14 -0
- package/src/api/socket/e2ee/e2ee/proto/ConsumerApplication.proto +232 -0
- package/src/api/socket/e2ee/e2ee/proto/MessageApplication.proto +82 -0
- package/src/api/socket/e2ee/e2ee/proto/MessageTransport.proto +77 -0
- package/src/api/socket/e2ee/e2ee/proto/WACommon.proto +66 -0
- package/src/api/socket/e2ee/e2ee/proto/WAMediaTransport.proto +176 -0
- package/src/api/socket/e2ee/e2ee/protocol.js +196 -0
- package/src/api/socket/e2ee/e2ee/ratchet.js +219 -0
- package/src/api/socket/e2ee/e2ee/store.js +182 -0
- package/src/api/socket/e2ee/e2ee/vendor/fb-e2ee.cjs +9162 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/ArmadilloApplication.proto +281 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/ArmadilloICDC.proto +14 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/ConsumerApplication.proto +232 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/MessageApplication.proto +82 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/MessageTransport.proto +77 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/WACommon.proto +66 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/WAMediaTransport.proto +176 -0
- package/src/api/socket/e2ee/index.js +437 -0
- package/src/api/socket/e2ee/proto/ArmadilloApplication.proto +281 -0
- package/src/api/socket/e2ee/proto/ArmadilloICDC.proto +14 -0
- package/src/api/socket/e2ee/proto/ConsumerApplication.proto +232 -0
- package/src/api/socket/e2ee/proto/MessageApplication.proto +82 -0
- package/src/api/socket/e2ee/proto/MessageTransport.proto +77 -0
- package/src/api/socket/e2ee/proto/WACommon.proto +66 -0
- package/src/api/socket/e2ee/proto/WAMediaTransport.proto +176 -0
- package/src/api/socket/e2ee/protocol.js +196 -0
- package/src/api/socket/e2ee/ratchet.js +219 -0
- package/src/api/socket/e2ee/store.js +182 -0
- package/src/api/socket/e2ee/vendor/fb-e2ee.cjs +9162 -0
- package/src/api/socket/e2ee/vendor/proto/ArmadilloApplication.proto +281 -0
- package/src/api/socket/e2ee/vendor/proto/ArmadilloICDC.proto +14 -0
- package/src/api/socket/e2ee/vendor/proto/ConsumerApplication.proto +232 -0
- package/src/api/socket/e2ee/vendor/proto/MessageApplication.proto +82 -0
- package/src/api/socket/e2ee/vendor/proto/MessageTransport.proto +77 -0
- package/src/api/socket/e2ee/vendor/proto/WACommon.proto +66 -0
- package/src/api/socket/e2ee/vendor/proto/WAMediaTransport.proto +176 -0
- package/src/api/socket/listenE2EE.js +75 -0
- package/src/api/socket/listenMqtt.js +551 -0
- package/src/api/socket/middleware/index.js +216 -0
- package/src/api/socket/sendMessage.js +309 -0
- package/src/api/socket/sendMessageMqtt.js +69 -0
- package/src/api/threads/getThreadHistory.js +664 -0
- package/src/api/threads/getThreadInfo.js +296 -0
- package/src/api/threads/getThreadList.js +293 -0
- package/src/api/threads/getThreadPictures.js +78 -0
- package/src/api/users/getUserID.js +65 -0
- package/src/api/users/getUserInfo.js +402 -0
- package/src/api/users/getUserInfoV2.js +911 -0
- package/src/app/MessengerBot.js +209 -0
- package/src/app/MessengerContext.js +32 -0
- package/src/app/createFcaClient.js +139 -0
- package/src/app/threadInfoRealtimeSync.js +284 -0
- package/src/core/sendReqMqtt.js +96 -0
- package/src/database/helpers.js +53 -0
- package/src/database/models/index.js +88 -0
- package/src/database/models/thread.js +50 -0
- package/src/database/models/user.js +46 -0
- package/src/database/threadData.js +94 -0
- package/src/database/userData.js +98 -0
- package/src/remote/remoteClient.js +123 -0
- package/src/utils/broadcast.js +51 -0
- package/src/utils/client.js +10 -0
- package/src/utils/clientIdentity.js +118 -0
- package/src/utils/constants.js +23 -0
- package/src/utils/cookies.js +68 -0
- package/src/utils/format/attachment.js +357 -0
- package/src/utils/format/cookie.js +9 -0
- package/src/utils/format/date.js +50 -0
- package/src/utils/format/decode.js +44 -0
- package/src/utils/format/delta.js +194 -0
- package/src/utils/format/ids.js +64 -0
- package/src/utils/format/index.js +64 -0
- package/src/utils/format/message.js +88 -0
- package/src/utils/format/presence.js +132 -0
- package/src/utils/format/readTyp.js +44 -0
- package/src/utils/format/thread.js +42 -0
- package/src/utils/format/utils.js +141 -0
- package/src/utils/headers.js +124 -0
- package/src/utils/logger.js +144 -0
- package/src/utils/loginParser/autoLogin.js +125 -0
- package/src/utils/loginParser/helpers.js +43 -0
- package/src/utils/loginParser/index.js +10 -0
- package/src/utils/loginParser/parseAndCheckLogin.js +220 -0
- package/src/utils/loginParser/textUtils.js +28 -0
- package/src/utils/request/client.js +26 -0
- package/src/utils/request/config.js +29 -0
- package/src/utils/request/defaults.js +46 -0
- package/src/utils/request/helpers.js +46 -0
- package/src/utils/request/index.js +17 -0
- package/src/utils/request/methods.js +163 -0
- package/src/utils/request/proxy.js +46 -0
- package/src/utils/request/retry.js +77 -0
- package/src/utils/request/sanitize.js +49 -0
- package/src/utils/utils.js +686 -0
- package/src/utils/versionCheck.js +47 -0
- package/src/vendor/fca-unofficial/index.js +11 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
syntax = "proto2";
|
|
2
|
+
package WACommon;
|
|
3
|
+
|
|
4
|
+
enum FutureProofBehavior {
|
|
5
|
+
PLACEHOLDER = 0;
|
|
6
|
+
NO_PLACEHOLDER = 1;
|
|
7
|
+
IGNORE = 2;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
message MessageKey {
|
|
11
|
+
optional string remoteJID = 1;
|
|
12
|
+
optional bool fromMe = 2;
|
|
13
|
+
optional string ID = 3;
|
|
14
|
+
optional string participant = 4;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
message Command {
|
|
18
|
+
enum CommandType {
|
|
19
|
+
EVERYONE = 1;
|
|
20
|
+
SILENT = 2;
|
|
21
|
+
AI = 3;
|
|
22
|
+
AI_IMAGINE = 4;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
optional CommandType commandType = 1;
|
|
26
|
+
optional uint32 offset = 2;
|
|
27
|
+
optional uint32 length = 3;
|
|
28
|
+
optional string validationToken = 4;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
message Mention {
|
|
32
|
+
enum MentionType {
|
|
33
|
+
PROFILE = 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
optional MentionType mentionType = 1;
|
|
37
|
+
optional string mentionedJID = 2;
|
|
38
|
+
optional uint32 offset = 3;
|
|
39
|
+
optional uint32 length = 4;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
message MessageText {
|
|
43
|
+
optional string text = 1;
|
|
44
|
+
repeated string mentionedJID = 2;
|
|
45
|
+
repeated Command commands = 3;
|
|
46
|
+
repeated Mention mentions = 4;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
message SubProtocol {
|
|
50
|
+
optional bytes payload = 1;
|
|
51
|
+
optional int32 version = 2;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
message LimitSharing {
|
|
55
|
+
enum Trigger {
|
|
56
|
+
UNKNOWN = 0;
|
|
57
|
+
CHAT_SETTING = 1;
|
|
58
|
+
BIZ_SUPPORTS_FB_HOSTING = 2;
|
|
59
|
+
UNKNOWN_GROUP = 3;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
optional bool sharingLimited = 1;
|
|
63
|
+
optional Trigger trigger = 2;
|
|
64
|
+
optional int64 limitSharingSettingTimestamp = 3;
|
|
65
|
+
optional bool initiatedByMe = 4;
|
|
66
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
syntax = "proto2";
|
|
2
|
+
package WAMediaTransport;
|
|
3
|
+
|
|
4
|
+
import "WACommon.proto";
|
|
5
|
+
|
|
6
|
+
message WAMediaTransport {
|
|
7
|
+
message Ancillary {
|
|
8
|
+
message Thumbnail {
|
|
9
|
+
message DownloadableThumbnail {
|
|
10
|
+
optional bytes fileSHA256 = 1;
|
|
11
|
+
optional bytes fileEncSHA256 = 2;
|
|
12
|
+
optional string directPath = 3;
|
|
13
|
+
optional bytes mediaKey = 4;
|
|
14
|
+
optional int64 mediaKeyTimestamp = 5;
|
|
15
|
+
optional string objectID = 6;
|
|
16
|
+
optional bytes thumbnailScansSidecar = 7;
|
|
17
|
+
repeated uint32 thumbnailScanLengths = 8;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
optional bytes JPEGThumbnail = 1;
|
|
21
|
+
optional DownloadableThumbnail downloadableThumbnail = 2;
|
|
22
|
+
optional uint32 thumbnailWidth = 3;
|
|
23
|
+
optional uint32 thumbnailHeight = 4;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
optional uint64 fileLength = 1;
|
|
27
|
+
optional string mimetype = 2;
|
|
28
|
+
optional Thumbnail thumbnail = 3;
|
|
29
|
+
optional string objectID = 4;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
message Integral {
|
|
33
|
+
optional bytes fileSHA256 = 1;
|
|
34
|
+
optional bytes mediaKey = 2;
|
|
35
|
+
optional bytes fileEncSHA256 = 3;
|
|
36
|
+
optional string directPath = 4;
|
|
37
|
+
optional int64 mediaKeyTimestamp = 5;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
optional Integral integral = 1;
|
|
41
|
+
optional Ancillary ancillary = 2;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
message ImageTransport {
|
|
45
|
+
message Ancillary {
|
|
46
|
+
enum HdType {
|
|
47
|
+
NONE = 0;
|
|
48
|
+
LQ_4K = 1;
|
|
49
|
+
HQ_4K = 2;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
optional uint32 height = 1;
|
|
53
|
+
optional uint32 width = 2;
|
|
54
|
+
optional bytes scansSidecar = 3;
|
|
55
|
+
repeated uint32 scanLengths = 4;
|
|
56
|
+
optional bytes midQualityFileSHA256 = 5;
|
|
57
|
+
optional HdType hdType = 6;
|
|
58
|
+
repeated float memoriesConceptScores = 7 [packed=true];
|
|
59
|
+
repeated uint32 memoriesConceptIDs = 8 [packed=true];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
message Integral {
|
|
63
|
+
optional WAMediaTransport transport = 1;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
optional Integral integral = 1;
|
|
67
|
+
optional Ancillary ancillary = 2;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
message VideoTransport {
|
|
71
|
+
message Ancillary {
|
|
72
|
+
enum Attribution {
|
|
73
|
+
NONE = 0;
|
|
74
|
+
GIPHY = 1;
|
|
75
|
+
TENOR = 2;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
optional uint32 seconds = 1;
|
|
79
|
+
optional WACommon.MessageText caption = 2;
|
|
80
|
+
optional bool gifPlayback = 3;
|
|
81
|
+
optional uint32 height = 4;
|
|
82
|
+
optional uint32 width = 5;
|
|
83
|
+
optional bytes sidecar = 6;
|
|
84
|
+
optional Attribution gifAttribution = 7;
|
|
85
|
+
optional string accessibilityLabel = 8;
|
|
86
|
+
optional bool isHd = 9;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
message Integral {
|
|
90
|
+
optional WAMediaTransport transport = 1;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
optional Integral integral = 1;
|
|
94
|
+
optional Ancillary ancillary = 2;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
message AudioTransport {
|
|
98
|
+
message Ancillary {
|
|
99
|
+
message AvatarAudio {
|
|
100
|
+
enum AnimationsType {
|
|
101
|
+
TALKING_A = 0;
|
|
102
|
+
IDLE_A = 1;
|
|
103
|
+
TALKING_B = 2;
|
|
104
|
+
IDLE_B = 3;
|
|
105
|
+
BACKGROUND = 4;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
message DownloadableAvatarAnimations {
|
|
109
|
+
optional bytes fileSHA256 = 1;
|
|
110
|
+
optional bytes fileEncSHA256 = 2;
|
|
111
|
+
optional string directPath = 3;
|
|
112
|
+
optional bytes mediaKey = 4;
|
|
113
|
+
optional int64 mediaKeyTimestamp = 5;
|
|
114
|
+
optional string objectID = 6;
|
|
115
|
+
optional AnimationsType animationsType = 7;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
optional uint32 poseID = 1;
|
|
119
|
+
repeated DownloadableAvatarAnimations avatarAnimations = 2;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
optional uint32 seconds = 1;
|
|
123
|
+
optional AvatarAudio avatarAudio = 2;
|
|
124
|
+
optional string waveformData = 3;
|
|
125
|
+
optional bytes waveform = 4;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
message Integral {
|
|
129
|
+
enum AudioFormat {
|
|
130
|
+
UNKNOWN = 0;
|
|
131
|
+
OPUS = 1;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
optional WAMediaTransport transport = 1;
|
|
135
|
+
optional AudioFormat audioFormat = 2;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
optional Integral integral = 1;
|
|
139
|
+
optional Ancillary ancillary = 2;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
message DocumentTransport {
|
|
143
|
+
message Ancillary {
|
|
144
|
+
optional uint32 pageCount = 1;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
message Integral {
|
|
148
|
+
optional WAMediaTransport transport = 1;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
optional Integral integral = 1;
|
|
152
|
+
optional Ancillary ancillary = 2;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
message StickerTransport {
|
|
156
|
+
message Ancillary {
|
|
157
|
+
optional uint32 pageCount = 1;
|
|
158
|
+
optional uint32 height = 2;
|
|
159
|
+
optional uint32 width = 3;
|
|
160
|
+
optional uint32 firstFrameLength = 4;
|
|
161
|
+
optional bytes firstFrameSidecar = 5;
|
|
162
|
+
optional string mustacheText = 6;
|
|
163
|
+
optional bool isThirdParty = 7;
|
|
164
|
+
optional string receiverFetchID = 8;
|
|
165
|
+
optional string accessibilityLabel = 9;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
message Integral {
|
|
169
|
+
optional WAMediaTransport transport = 1;
|
|
170
|
+
optional bool isAnimated = 2;
|
|
171
|
+
optional string receiverFetchID = 3;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
optional Integral integral = 1;
|
|
175
|
+
optional Ancillary ancillary = 2;
|
|
176
|
+
}
|
package/e2ee/protocol.js
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Facebook-specific E2EE wire protocol handling.
|
|
5
|
+
*
|
|
6
|
+
* Facebook Secret Conversations use Signal Protocol (X3DH + Double Ratchet)
|
|
7
|
+
* with their own framing on top. This module handles:
|
|
8
|
+
* - Key bundle upload/download to Facebook's graph endpoint
|
|
9
|
+
* - Message framing (encode/decode FCANX_E2EE frame format)
|
|
10
|
+
* - Signed pre-key signatures using HMAC-SHA256 (FB uses Ed25519 in practice;
|
|
11
|
+
* we sign with HMAC for our own inter-device sessions)
|
|
12
|
+
* - E2EE-capable thread detection from message metadata
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const crypto = require("crypto");
|
|
16
|
+
const { hmacSha256, generateKeyPair, randomBytes } = require("./crypto");
|
|
17
|
+
|
|
18
|
+
// ─── Frame format ──────────────────────────────────────────────────────────────
|
|
19
|
+
// 2 bytes: magic (0x4E58 = "NX")
|
|
20
|
+
// 1 byte: version (0x01)
|
|
21
|
+
// 1 byte: type (0=init, 1=message, 2=reaction, 3=receipt, 4=typing)
|
|
22
|
+
// 4 bytes: sender ID length
|
|
23
|
+
// N bytes: sender ID (utf8)
|
|
24
|
+
// 4 bytes: thread ID length
|
|
25
|
+
// N bytes: thread ID (utf8)
|
|
26
|
+
// 4 bytes: header length [only for type 0,1,2]
|
|
27
|
+
// N bytes: header
|
|
28
|
+
// 4 bytes: payload length
|
|
29
|
+
// N bytes: payload (encrypted or raw for receipt/typing)
|
|
30
|
+
|
|
31
|
+
const MAGIC = 0x4E58;
|
|
32
|
+
const VERSION = 0x01;
|
|
33
|
+
const TYPE = { INIT: 0, MESSAGE: 1, REACTION: 2, RECEIPT: 3, TYPING: 4 };
|
|
34
|
+
|
|
35
|
+
function encodeFrame(type, senderID, threadID, header, payload) {
|
|
36
|
+
const sidBuf = Buffer.from(String(senderID), "utf8");
|
|
37
|
+
const tidBuf = Buffer.from(String(threadID), "utf8");
|
|
38
|
+
const hdrBuf = header || Buffer.alloc(0);
|
|
39
|
+
const payBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload || "");
|
|
40
|
+
|
|
41
|
+
const totalSize = 2 + 1 + 1 + 4 + sidBuf.length + 4 + tidBuf.length + 4 + hdrBuf.length + 4 + payBuf.length;
|
|
42
|
+
const buf = Buffer.allocUnsafe(totalSize);
|
|
43
|
+
let offset = 0;
|
|
44
|
+
|
|
45
|
+
buf.writeUInt16BE(MAGIC, offset); offset += 2;
|
|
46
|
+
buf.writeUInt8(VERSION, offset); offset += 1;
|
|
47
|
+
buf.writeUInt8(type, offset); offset += 1;
|
|
48
|
+
|
|
49
|
+
buf.writeUInt32BE(sidBuf.length, offset); offset += 4;
|
|
50
|
+
sidBuf.copy(buf, offset); offset += sidBuf.length;
|
|
51
|
+
|
|
52
|
+
buf.writeUInt32BE(tidBuf.length, offset); offset += 4;
|
|
53
|
+
tidBuf.copy(buf, offset); offset += tidBuf.length;
|
|
54
|
+
|
|
55
|
+
buf.writeUInt32BE(hdrBuf.length, offset); offset += 4;
|
|
56
|
+
hdrBuf.copy(buf, offset); offset += hdrBuf.length;
|
|
57
|
+
|
|
58
|
+
buf.writeUInt32BE(payBuf.length, offset); offset += 4;
|
|
59
|
+
payBuf.copy(buf, offset);
|
|
60
|
+
|
|
61
|
+
return buf;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function decodeFrame(buf) {
|
|
65
|
+
if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf);
|
|
66
|
+
if (buf.length < 8) throw new Error("Frame too short");
|
|
67
|
+
let offset = 0;
|
|
68
|
+
|
|
69
|
+
const magic = buf.readUInt16BE(offset); offset += 2;
|
|
70
|
+
if (magic !== MAGIC) throw new Error("Invalid frame magic: " + magic.toString(16));
|
|
71
|
+
const version = buf.readUInt8(offset); offset += 1;
|
|
72
|
+
const type = buf.readUInt8(offset); offset += 1;
|
|
73
|
+
|
|
74
|
+
const sidLen = buf.readUInt32BE(offset); offset += 4;
|
|
75
|
+
const senderID = buf.slice(offset, offset + sidLen).toString("utf8"); offset += sidLen;
|
|
76
|
+
|
|
77
|
+
const tidLen = buf.readUInt32BE(offset); offset += 4;
|
|
78
|
+
const threadID = buf.slice(offset, offset + tidLen).toString("utf8"); offset += tidLen;
|
|
79
|
+
|
|
80
|
+
const hdrLen = buf.readUInt32BE(offset); offset += 4;
|
|
81
|
+
const header = buf.slice(offset, offset + hdrLen); offset += hdrLen;
|
|
82
|
+
|
|
83
|
+
const payLen = buf.readUInt32BE(offset); offset += 4;
|
|
84
|
+
const payload = buf.slice(offset, offset + payLen);
|
|
85
|
+
|
|
86
|
+
return { version, type, senderID, threadID, header, payload };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ─── Key Bundle ────────────────────────────────────────────────────────────────
|
|
90
|
+
|
|
91
|
+
function buildKeyBundle(identityKey, signedPreKey, oneTimePreKeys) {
|
|
92
|
+
return {
|
|
93
|
+
identityKey: identityKey.publicKeyRaw.toString("base64"),
|
|
94
|
+
signedPreKey: {
|
|
95
|
+
keyId: signedPreKey.keyId || 1,
|
|
96
|
+
publicKey: signedPreKey.publicKeyRaw.toString("base64"),
|
|
97
|
+
signature: signedPreKey.signature ? signedPreKey.signature.toString("base64") : ""
|
|
98
|
+
},
|
|
99
|
+
oneTimePreKeys: (oneTimePreKeys || []).map(k => ({
|
|
100
|
+
keyId: k.keyId,
|
|
101
|
+
publicKey: k.publicKeyRaw.toString("base64")
|
|
102
|
+
}))
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function signKey(identityPrivateKey, keyToSign) {
|
|
107
|
+
return hmacSha256(identityPrivateKey, keyToSign.publicKeyRaw);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ─── Facebook Graph API: key registration/fetching ───────────────────────────
|
|
111
|
+
|
|
112
|
+
async function uploadKeyBundle(defaultFuncs, ctx, keyBundle) {
|
|
113
|
+
try {
|
|
114
|
+
const res = await defaultFuncs.post(
|
|
115
|
+
"https://www.facebook.com/api/graphql/",
|
|
116
|
+
ctx.jar,
|
|
117
|
+
{
|
|
118
|
+
fb_api_req_friendly_name: "MessengerE2EERegistrationMutation",
|
|
119
|
+
fb_api_caller_class: "RelayModern",
|
|
120
|
+
doc_id: "6325661730877783",
|
|
121
|
+
variables: JSON.stringify({
|
|
122
|
+
input: {
|
|
123
|
+
actor_id: ctx.userID,
|
|
124
|
+
client_mutation_id: String(ctx.clientMutationId++),
|
|
125
|
+
identity_key: keyBundle.identityKey,
|
|
126
|
+
signed_pre_key: keyBundle.signedPreKey,
|
|
127
|
+
one_time_pre_keys: keyBundle.oneTimePreKeys
|
|
128
|
+
}
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
);
|
|
132
|
+
return res;
|
|
133
|
+
} catch (e) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async function fetchRecipientKeyBundle(defaultFuncs, ctx, recipientUserID) {
|
|
139
|
+
try {
|
|
140
|
+
const res = await defaultFuncs.post(
|
|
141
|
+
"https://www.facebook.com/api/graphql/",
|
|
142
|
+
ctx.jar,
|
|
143
|
+
{
|
|
144
|
+
fb_api_req_friendly_name: "MessengerE2EEKeyBundleQuery",
|
|
145
|
+
fb_api_caller_class: "RelayModern",
|
|
146
|
+
doc_id: "6325661730877784",
|
|
147
|
+
variables: JSON.stringify({ user_id: String(recipientUserID) })
|
|
148
|
+
}
|
|
149
|
+
);
|
|
150
|
+
return res;
|
|
151
|
+
} catch (e) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ─── Message payload builders ─────────────────────────────────────────────────
|
|
157
|
+
|
|
158
|
+
function buildInitPayload(senderEphemeralPub, senderOneTimeKeyId, recipientOneTimeKeyId) {
|
|
159
|
+
return JSON.stringify({
|
|
160
|
+
type: "x3dh_init",
|
|
161
|
+
ek: senderEphemeralPub.toString("base64"),
|
|
162
|
+
skid: senderOneTimeKeyId || null,
|
|
163
|
+
opk_id: recipientOneTimeKeyId || null,
|
|
164
|
+
ts: Date.now()
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function parseInitPayload(buf) {
|
|
169
|
+
const str = Buffer.isBuffer(buf) ? buf.toString("utf8") : buf;
|
|
170
|
+
return JSON.parse(str);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function buildMessagePayload(text, attachments, replyToMessageId) {
|
|
174
|
+
return JSON.stringify({
|
|
175
|
+
v: 1,
|
|
176
|
+
text: text || null,
|
|
177
|
+
attachments: attachments || [],
|
|
178
|
+
reply_to: replyToMessageId || null,
|
|
179
|
+
ts: Date.now(),
|
|
180
|
+
nonce: randomBytes(8).toString("hex")
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function parseMessagePayload(buf) {
|
|
185
|
+
const str = Buffer.isBuffer(buf) ? buf.toString("utf8") : buf;
|
|
186
|
+
return JSON.parse(str);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
module.exports = {
|
|
190
|
+
MAGIC, VERSION, TYPE,
|
|
191
|
+
encodeFrame, decodeFrame,
|
|
192
|
+
buildKeyBundle, signKey,
|
|
193
|
+
uploadKeyBundle, fetchRecipientKeyBundle,
|
|
194
|
+
buildInitPayload, parseInitPayload,
|
|
195
|
+
buildMessagePayload, parseMessagePayload
|
|
196
|
+
};
|
package/e2ee/ratchet.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Double Ratchet Algorithm (Signal Protocol)
|
|
5
|
+
* Based on the Signal Protocol specification:
|
|
6
|
+
* https://signal.org/docs/specifications/doubleratchet/
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const { generateKeyPair, dh, hkdf, hkdfExtract, hkdfExpand, encrypt, decrypt, hmacSha256 } = require("./crypto");
|
|
10
|
+
|
|
11
|
+
const MAX_SKIP = 1000;
|
|
12
|
+
const INFO_RATCHET = Buffer.from("WhisperRatchet", "utf8");
|
|
13
|
+
const INFO_MSG = Buffer.from("WhisperMessageKeys", "utf8");
|
|
14
|
+
|
|
15
|
+
function kdfCK(ck) {
|
|
16
|
+
const ck2 = hmacSha256(ck, Buffer.from([0x02]));
|
|
17
|
+
const mk = hmacSha256(ck, Buffer.from([0x01]));
|
|
18
|
+
return { ck: ck2, mk };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function kdfRK(rk, dh_out) {
|
|
22
|
+
const salt = rk;
|
|
23
|
+
const out = hkdf(dh_out, 64, salt, INFO_RATCHET);
|
|
24
|
+
return { rk: out.slice(0, 32), ck: out.slice(32, 64) };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function deriveMessageKeys(mk) {
|
|
28
|
+
const out = hkdf(mk, 80, Buffer.alloc(32, 0), INFO_MSG);
|
|
29
|
+
return {
|
|
30
|
+
encKey: out.slice(0, 32),
|
|
31
|
+
authKey: out.slice(32, 64),
|
|
32
|
+
iv: out.slice(64, 80)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
class DoubleRatchetSession {
|
|
37
|
+
constructor() {
|
|
38
|
+
this.DHs = null; // Our DH ratchet key pair
|
|
39
|
+
this.DHr = null; // Their DH ratchet public key (raw 32 bytes)
|
|
40
|
+
this.RK = null; // Root key (32 bytes)
|
|
41
|
+
this.CKs = null; // Sending chain key
|
|
42
|
+
this.CKr = null; // Receiving chain key
|
|
43
|
+
this.Ns = 0; // Send message number
|
|
44
|
+
this.Nr = 0; // Receive message number
|
|
45
|
+
this.PN = 0; // Previous sending chain length
|
|
46
|
+
this.MKSKIPPED = new Map(); // Skipped message keys
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Initialize as sender (Alice) after X3DH
|
|
51
|
+
*/
|
|
52
|
+
initAsSender(sk, recipientRatchetPublicKey) {
|
|
53
|
+
this.DHs = generateKeyPair();
|
|
54
|
+
this.DHr = Buffer.from(recipientRatchetPublicKey);
|
|
55
|
+
const dhOut = dh(this.DHs.privateKey, this.DHr);
|
|
56
|
+
const { rk, ck } = kdfRK(sk, dhOut);
|
|
57
|
+
this.RK = rk;
|
|
58
|
+
this.CKs = ck;
|
|
59
|
+
this.CKr = null;
|
|
60
|
+
this.Ns = 0; this.Nr = 0; this.PN = 0;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Initialize as receiver (Bob) after X3DH
|
|
65
|
+
*/
|
|
66
|
+
initAsReceiver(sk, ourRatchetKeyPair) {
|
|
67
|
+
this.DHs = ourRatchetKeyPair;
|
|
68
|
+
this.DHr = null;
|
|
69
|
+
this.RK = sk;
|
|
70
|
+
this.CKs = null;
|
|
71
|
+
this.CKr = null;
|
|
72
|
+
this.Ns = 0; this.Nr = 0; this.PN = 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Encrypt a message
|
|
77
|
+
* Returns { header, ciphertext }
|
|
78
|
+
*/
|
|
79
|
+
encrypt(plaintext, aad) {
|
|
80
|
+
const { ck, mk } = kdfCK(this.CKs);
|
|
81
|
+
this.CKs = ck;
|
|
82
|
+
const header = this._buildHeader(this.DHs.publicKeyRaw, this.PN, this.Ns);
|
|
83
|
+
this.Ns++;
|
|
84
|
+
const keys = deriveMessageKeys(mk);
|
|
85
|
+
const aadFull = aad ? Buffer.concat([aad, header]) : header;
|
|
86
|
+
const ciphertext = encrypt(keys.encKey, Buffer.isBuffer(plaintext) ? plaintext : Buffer.from(plaintext, "utf8"), aadFull);
|
|
87
|
+
return { header, ciphertext };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Decrypt a message
|
|
92
|
+
* @param {Buffer} header - message header
|
|
93
|
+
* @param {Buffer} ciphertext - encrypted payload
|
|
94
|
+
* @param {Buffer} [aad] - additional auth data
|
|
95
|
+
* @returns {Buffer} decrypted plaintext
|
|
96
|
+
*/
|
|
97
|
+
decrypt(header, ciphertext, aad) {
|
|
98
|
+
const { dh_pub, pn, n } = this._parseHeader(header);
|
|
99
|
+
const aadFull = aad ? Buffer.concat([aad, header]) : header;
|
|
100
|
+
|
|
101
|
+
// Check skipped keys first
|
|
102
|
+
const skipKey = `${dh_pub.toString("hex")}:${n}`;
|
|
103
|
+
if (this.MKSKIPPED.has(skipKey)) {
|
|
104
|
+
const mk = this.MKSKIPPED.get(skipKey);
|
|
105
|
+
this.MKSKIPPED.delete(skipKey);
|
|
106
|
+
const keys = deriveMessageKeys(mk);
|
|
107
|
+
return decrypt(keys.encKey, ciphertext, aadFull);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Check if we need to ratchet
|
|
111
|
+
const needsRatchet = !this.DHr || !dh_pub.equals(this.DHr);
|
|
112
|
+
if (needsRatchet) {
|
|
113
|
+
this._skipMessageKeys(pn);
|
|
114
|
+
this._dhRatchetStep(dh_pub);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
this._skipMessageKeys(n);
|
|
118
|
+
const { ck, mk } = kdfCK(this.CKr);
|
|
119
|
+
this.CKr = ck;
|
|
120
|
+
this.Nr++;
|
|
121
|
+
|
|
122
|
+
const keys = deriveMessageKeys(mk);
|
|
123
|
+
return decrypt(keys.encKey, ciphertext, aadFull);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
_skipMessageKeys(until) {
|
|
127
|
+
if (this.Nr + MAX_SKIP < until) throw new Error("Too many skipped messages: " + until);
|
|
128
|
+
if (this.CKr) {
|
|
129
|
+
while (this.Nr < until) {
|
|
130
|
+
const { ck, mk } = kdfCK(this.CKr);
|
|
131
|
+
this.CKr = ck;
|
|
132
|
+
const key = `${this.DHr.toString("hex")}:${this.Nr}`;
|
|
133
|
+
this.MKSKIPPED.set(key, mk);
|
|
134
|
+
this.Nr++;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
_dhRatchetStep(remoteDhPub) {
|
|
140
|
+
this.PN = this.Ns;
|
|
141
|
+
this.Ns = 0;
|
|
142
|
+
this.Nr = 0;
|
|
143
|
+
this.DHr = remoteDhPub;
|
|
144
|
+
|
|
145
|
+
// Receive ratchet
|
|
146
|
+
const dhOut1 = dh(this.DHs.privateKey, this.DHr);
|
|
147
|
+
const { rk: rk1, ck: ckr } = kdfRK(this.RK, dhOut1);
|
|
148
|
+
this.RK = rk1;
|
|
149
|
+
this.CKr = ckr;
|
|
150
|
+
|
|
151
|
+
// Send ratchet
|
|
152
|
+
this.DHs = generateKeyPair();
|
|
153
|
+
const dhOut2 = dh(this.DHs.privateKey, this.DHr);
|
|
154
|
+
const { rk: rk2, ck: cks } = kdfRK(this.RK, dhOut2);
|
|
155
|
+
this.RK = rk2;
|
|
156
|
+
this.CKs = cks;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
_buildHeader(dhPub, pn, n) {
|
|
160
|
+
const buf = Buffer.allocUnsafe(32 + 4 + 4);
|
|
161
|
+
Buffer.from(dhPub).copy(buf, 0);
|
|
162
|
+
buf.writeUInt32BE(pn, 32);
|
|
163
|
+
buf.writeUInt32BE(n, 36);
|
|
164
|
+
return buf;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
_parseHeader(header) {
|
|
168
|
+
if (header.length < 40) throw new Error("Invalid ratchet header length");
|
|
169
|
+
return {
|
|
170
|
+
dh_pub: header.slice(0, 32),
|
|
171
|
+
pn: header.readUInt32BE(32),
|
|
172
|
+
n: header.readUInt32BE(36)
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Serialize session state for persistence
|
|
178
|
+
*/
|
|
179
|
+
serialize() {
|
|
180
|
+
return JSON.stringify({
|
|
181
|
+
DHs_priv: this.DHs ? this.DHs.privateKey.toString("base64") : null,
|
|
182
|
+
DHs_pub: this.DHs ? this.DHs.publicKeyRaw.toString("base64") : null,
|
|
183
|
+
DHr: this.DHr ? this.DHr.toString("base64") : null,
|
|
184
|
+
RK: this.RK ? this.RK.toString("base64") : null,
|
|
185
|
+
CKs: this.CKs ? this.CKs.toString("base64") : null,
|
|
186
|
+
CKr: this.CKr ? this.CKr.toString("base64") : null,
|
|
187
|
+
Ns: this.Ns,
|
|
188
|
+
Nr: this.Nr,
|
|
189
|
+
PN: this.PN,
|
|
190
|
+
MKSKIPPED: Array.from(this.MKSKIPPED.entries()).map(([k, v]) => [k, v.toString("base64")])
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Restore session state from serialized string
|
|
196
|
+
*/
|
|
197
|
+
static deserialize(json) {
|
|
198
|
+
const d = JSON.parse(json);
|
|
199
|
+
const session = new DoubleRatchetSession();
|
|
200
|
+
if (d.DHs_priv) {
|
|
201
|
+
session.DHs = {
|
|
202
|
+
privateKey: Buffer.from(d.DHs_priv, "base64"),
|
|
203
|
+
publicKey: null,
|
|
204
|
+
publicKeyRaw: Buffer.from(d.DHs_pub, "base64")
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
session.DHr = d.DHr ? Buffer.from(d.DHr, "base64") : null;
|
|
208
|
+
session.RK = d.RK ? Buffer.from(d.RK, "base64") : null;
|
|
209
|
+
session.CKs = d.CKs ? Buffer.from(d.CKs, "base64") : null;
|
|
210
|
+
session.CKr = d.CKr ? Buffer.from(d.CKr, "base64") : null;
|
|
211
|
+
session.Ns = d.Ns || 0;
|
|
212
|
+
session.Nr = d.Nr || 0;
|
|
213
|
+
session.PN = d.PN || 0;
|
|
214
|
+
session.MKSKIPPED = new Map((d.MKSKIPPED || []).map(([k, v]) => [k, Buffer.from(v, "base64")]));
|
|
215
|
+
return session;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
module.exports = { DoubleRatchetSession, kdfCK, kdfRK, deriveMessageKeys };
|