@badzz88/baileys 8.5.4 → 8.5.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/README.md +2 -2
- package/WAProto/fix-imports.js +69 -79
- package/WAProto/index.d.ts +15305 -87935
- package/WAProto/index.js +41109 -194076
- package/engine-requirements.js +15 -8
- package/package.json +23 -19
- package/src/Defaults/index.js +186 -0
- package/src/Signal/Group/ciphertext-message.js +15 -0
- package/src/Signal/Group/group-session-builder.js +86 -0
- package/src/Signal/Group/group_cipher.js +82 -0
- package/src/Signal/Group/index.js +140 -0
- package/src/Signal/Group/keyhelper.js +77 -0
- package/src/Signal/Group/sender-chain-key.js +29 -0
- package/src/Signal/Group/sender-key-distribution-message.js +66 -0
- package/src/Signal/Group/sender-key-message.js +69 -0
- package/src/Signal/Group/sender-key-name.js +51 -0
- package/src/Signal/Group/sender-key-record.js +44 -0
- package/src/Signal/Group/sender-key-state.js +87 -0
- package/src/Signal/Group/sender-message-key.js +29 -0
- package/src/Signal/libsignal.js +455 -0
- package/src/Signal/lid-mapping.js +273 -0
- package/src/Socket/Client/index.js +31 -0
- package/src/Socket/Client/types.js +13 -0
- package/src/Socket/Client/websocket.js +61 -0
- package/src/Socket/aigroups.js +221 -0
- package/src/Socket/business.js +411 -0
- package/src/Socket/chats.js +1449 -0
- package/src/Socket/communities.js +463 -0
- package/src/Socket/graphql.js +523 -0
- package/src/Socket/groups.js +516 -0
- package/src/Socket/index.js +31 -0
- package/src/Socket/interactive-handler.js +527 -0
- package/src/Socket/interop.js +339 -0
- package/src/Socket/luxu.js +349 -0
- package/src/Socket/managed-account.js +98 -0
- package/src/Socket/messages-recv.js +2685 -0
- package/src/Socket/messages-send.js +2047 -0
- package/src/Socket/mex.js +57 -0
- package/src/Socket/newsletter.js +455 -0
- package/src/Socket/privacy.js +125 -0
- package/src/Socket/registration.js +236 -0
- package/src/Socket/socket.js +983 -0
- package/src/Socket/text-router.js +65 -0
- package/src/Socket/username.js +130 -0
- package/src/Store/index.js +33 -0
- package/src/Store/keyed-db.js +118 -0
- package/src/Store/make-cache-manager-store.js +84 -0
- package/src/Store/make-in-memory-store.js +551 -0
- package/src/Store/make-ordered-dictionary.js +81 -0
- package/src/Store/object-repository.js +26 -0
- package/src/Types/Auth.js +31 -0
- package/src/Types/Bussines.js +2 -0
- package/src/Types/Call.js +2 -0
- package/src/Types/Chat.js +4 -0
- package/src/Types/Contact.js +2 -0
- package/src/Types/Events.js +2 -0
- package/src/Types/GroupMetadata.js +2 -0
- package/src/Types/Label.js +26 -0
- package/src/Types/LabelAssociation.js +8 -0
- package/src/Types/Message.js +93 -0
- package/src/Types/Mex.js +112 -0
- package/src/Types/Newsletter.js +109 -0
- package/src/Types/Product.js +2 -0
- package/src/Types/Signal.js +2 -0
- package/src/Types/Socket.js +2 -0
- package/src/Types/State.js +62 -0
- package/src/Types/USync.js +2 -0
- package/src/Types/index.js +56 -0
- package/src/Utils/auth-utils.js +254 -0
- package/src/Utils/browser-utils.js +112 -0
- package/src/Utils/business.js +240 -0
- package/src/Utils/chat-utils.js +1230 -0
- package/src/Utils/command-loader.d.ts +27 -0
- package/src/Utils/command-loader.js +89 -0
- package/src/Utils/companion-reg-client-utils.js +40 -0
- package/src/Utils/consumer-application.js +105 -0
- package/src/Utils/crypto.js +118 -0
- package/src/Utils/curve25519-js.js +242 -0
- package/src/Utils/decode-wa-message.js +529 -0
- package/src/Utils/event-buffer.js +580 -0
- package/src/Utils/generics.js +483 -0
- package/src/Utils/group-history.js +44 -0
- package/src/Utils/history.js +232 -0
- package/src/Utils/identity-change-handler.js +52 -0
- package/src/Utils/index.js +58 -0
- package/src/Utils/jid-display-normalization.js +195 -0
- package/src/Utils/link-preview.js +135 -0
- package/src/Utils/logger.js +8 -0
- package/src/Utils/lt-hash.js +25 -0
- package/src/Utils/make-mutex.js +36 -0
- package/src/Utils/message-composer.js +471 -0
- package/src/Utils/message-retry-manager.js +217 -0
- package/src/Utils/messages-media.js +843 -0
- package/src/Utils/messages.js +2817 -0
- package/src/Utils/meta-ai-msmsg.js +222 -0
- package/src/Utils/native-bridge.js +68 -0
- package/src/Utils/noise-handler.js +169 -0
- package/src/Utils/offline-node-processor.js +34 -0
- package/src/Utils/pre-key-manager.js +90 -0
- package/src/Utils/process-message.js +950 -0
- package/src/Utils/reporting-utils.js +261 -0
- package/src/Utils/session-pool.d.ts +14 -0
- package/src/Utils/session-pool.js +70 -0
- package/src/Utils/signal.js +217 -0
- package/src/Utils/stanza-ack.js +30 -0
- package/src/Utils/sticker.d.ts +13 -0
- package/src/Utils/sticker.js +123 -0
- package/src/Utils/sync-action-utils.js +45 -0
- package/src/Utils/tc-token-utils.js +158 -0
- package/src/Utils/use-multi-file-auth-state.js +111 -0
- package/src/Utils/validate-connection.js +209 -0
- package/src/Utils/view-once-cache.d.ts +12 -0
- package/src/Utils/view-once-cache.js +63 -0
- package/src/Utils/voip-rekey.js +22 -0
- package/src/WABinary/constants.js +1304 -0
- package/src/WABinary/decode.js +342 -0
- package/src/WABinary/encode.js +225 -0
- package/src/WABinary/generic-utils.js +238 -0
- package/src/WABinary/index.js +34 -0
- package/src/WABinary/jid-utils.js +351 -0
- package/src/WABinary/types.js +2 -0
- package/src/WAM/BinaryInfo.js +13 -0
- package/src/WAM/constants.js +39484 -0
- package/src/WAM/encode.js +149 -0
- package/src/WAM/index.js +32 -0
- package/src/WAUSync/Protocols/USyncBotProfileProtocol.js +53 -0
- package/src/WAUSync/Protocols/USyncBusinessProtocol.js +44 -0
- package/src/WAUSync/Protocols/USyncContactProtocol.js +55 -0
- package/src/WAUSync/Protocols/USyncDeviceProtocol.js +55 -0
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +31 -0
- package/src/WAUSync/Protocols/USyncFeatureProtocol.js +54 -0
- package/src/WAUSync/Protocols/USyncLIDProtocol.js +32 -0
- package/src/WAUSync/Protocols/USyncNewsletterProtocol.js +473 -0
- package/src/WAUSync/Protocols/USyncPictureProtocol.js +34 -0
- package/src/WAUSync/Protocols/USyncSidelistProtocol.js +29 -0
- package/src/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/src/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
- package/src/WAUSync/Protocols/USyncUsernameProtocol.js +28 -0
- package/src/WAUSync/Protocols/index.js +40 -0
- package/src/WAUSync/USyncQuery.js +126 -0
- package/src/WAUSync/USyncUser.js +50 -0
- package/src/WAUSync/index.js +32 -0
- package/src/antiban.js +4152 -0
- package/{lib → src}/index.js +51 -49
- package/lib/Defaults/index.js +0 -195
- package/lib/Signal/Group/ciphertext-message.js +0 -15
- package/lib/Signal/Group/group-session-builder.js +0 -92
- package/lib/Signal/Group/group_cipher.js +0 -89
- package/lib/Signal/Group/index.js +0 -136
- package/lib/Signal/Group/keyhelper.js +0 -73
- package/lib/Signal/Group/sender-chain-key.js +0 -32
- package/lib/Signal/Group/sender-key-distribution-message.js +0 -66
- package/lib/Signal/Group/sender-key-message.js +0 -69
- package/lib/Signal/Group/sender-key-name.js +0 -50
- package/lib/Signal/Group/sender-key-record.js +0 -44
- package/lib/Signal/Group/sender-key-state.js +0 -97
- package/lib/Signal/Group/sender-message-key.js +0 -30
- package/lib/Signal/libsignal.js +0 -470
- package/lib/Signal/lid-mapping.js +0 -280
- package/lib/Socket/Client/index.js +0 -30
- package/lib/Socket/Client/types.js +0 -13
- package/lib/Socket/Client/websocket.js +0 -62
- package/lib/Socket/aigroups.js +0 -240
- package/lib/Socket/business.js +0 -414
- package/lib/Socket/chats.js +0 -2165
- package/lib/Socket/communities.js +0 -545
- package/lib/Socket/graphql.js +0 -863
- package/lib/Socket/groups.js +0 -685
- package/lib/Socket/index.js +0 -41
- package/lib/Socket/interactive-handler.js +0 -579
- package/lib/Socket/interop.js +0 -430
- package/lib/Socket/managed-account.js +0 -214
- package/lib/Socket/messages-recv.js +0 -3000
- package/lib/Socket/messages-send.js +0 -2155
- package/lib/Socket/mex.js +0 -47
- package/lib/Socket/newsletter.js +0 -814
- package/lib/Socket/privacy.js +0 -261
- package/lib/Socket/registration.js +0 -434
- package/lib/Socket/socket.js +0 -1074
- package/lib/Socket/username.js +0 -160
- package/lib/Store/index.js +0 -36
- package/lib/Store/make-cache-manager-store.js +0 -90
- package/lib/Store/make-in-memory-store.js +0 -488
- package/lib/Store/make-ordered-dictionary.js +0 -81
- package/lib/Store/object-repository.js +0 -29
- package/lib/Types/Auth.js +0 -38
- package/lib/Types/Bussines.js +0 -2
- package/lib/Types/Call.js +0 -2
- package/lib/Types/Chat.js +0 -4
- package/lib/Types/Contact.js +0 -2
- package/lib/Types/Events.js +0 -2
- package/lib/Types/GroupMetadata.js +0 -2
- package/lib/Types/Label.js +0 -27
- package/lib/Types/LabelAssociation.js +0 -9
- package/lib/Types/Message.js +0 -95
- package/lib/Types/Newsletter.js +0 -152
- package/lib/Types/Product.js +0 -2
- package/lib/Types/Signal.js +0 -2
- package/lib/Types/Socket.js +0 -2
- package/lib/Types/State.js +0 -70
- package/lib/Types/USync.js +0 -2
- package/lib/Types/index.js +0 -55
- package/lib/Utils/auth-utils.js +0 -301
- package/lib/Utils/browser-utils.js +0 -114
- package/lib/Utils/business.js +0 -243
- package/lib/Utils/chat-utils.js +0 -1272
- package/lib/Utils/consumer-application.js +0 -107
- package/lib/Utils/crypto.js +0 -125
- package/lib/Utils/decode-wa-message.js +0 -793
- package/lib/Utils/event-buffer.js +0 -583
- package/lib/Utils/generics.js +0 -617
- package/lib/Utils/group-history.js +0 -51
- package/lib/Utils/history.js +0 -244
- package/lib/Utils/identity-change-handler.js +0 -52
- package/lib/Utils/index.js +0 -53
- package/lib/Utils/jid-display-normalization.js +0 -218
- package/lib/Utils/link-preview.js +0 -138
- package/lib/Utils/logger.js +0 -9
- package/lib/Utils/lt-hash.js +0 -6
- package/lib/Utils/make-mutex.js +0 -36
- package/lib/Utils/message-composer.js +0 -479
- package/lib/Utils/message-inspect.js +0 -393
- package/lib/Utils/message-retry-manager.js +0 -205
- package/lib/Utils/messages-media.js +0 -925
- package/lib/Utils/messages.js +0 -2482
- package/lib/Utils/meta-ai-msmsg.js +0 -122
- package/lib/Utils/noise-handler.js +0 -194
- package/lib/Utils/offline-node-processor.js +0 -37
- package/lib/Utils/pre-key-manager.js +0 -97
- package/lib/Utils/process-message.js +0 -1139
- package/lib/Utils/reporting-utils.js +0 -262
- package/lib/Utils/signal.js +0 -189
- package/lib/Utils/stanza-ack.js +0 -67
- package/lib/Utils/sync-action-utils.js +0 -51
- package/lib/Utils/tc-token-utils.js +0 -156
- package/lib/Utils/use-multi-file-auth-state.js +0 -137
- package/lib/Utils/validate-connection.js +0 -259
- package/lib/Utils/voip-rekey.js +0 -22
- package/lib/WABinary/constants.js +0 -1304
- package/lib/WABinary/decode.js +0 -377
- package/lib/WABinary/encode.js +0 -58
- package/lib/WABinary/generic-utils.js +0 -150
- package/lib/WABinary/index.js +0 -33
- package/lib/WABinary/jid-utils.js +0 -374
- package/lib/WABinary/types.js +0 -2
- package/lib/WAM/BinaryInfo.js +0 -13
- package/lib/WAM/constants.js +0 -39486
- package/lib/WAM/encode.js +0 -142
- package/lib/WAM/index.js +0 -31
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +0 -55
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +0 -100
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +0 -60
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +0 -65
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -27
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +0 -72
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -31
- package/lib/WAUSync/Protocols/USyncNewsletterProtocol.js +0 -865
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +0 -32
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +0 -29
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +0 -44
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +0 -38
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +0 -28
- package/lib/WAUSync/Protocols/index.js +0 -40
- package/lib/WAUSync/USyncBackoff.js +0 -26
- package/lib/WAUSync/USyncQuery.js +0 -204
- package/lib/WAUSync/USyncUser.js +0 -58
- package/lib/WAUSync/index.js +0 -32
- package/lib/antiban.js +0 -4391
- /package/{lib → src}/Defaults/phonenumber-mcc.json +0 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface CommandContext {
|
|
2
|
+
sock: any;
|
|
3
|
+
msg: any;
|
|
4
|
+
args: string[];
|
|
5
|
+
text: string;
|
|
6
|
+
jid: string;
|
|
7
|
+
sender: string;
|
|
8
|
+
isGroup: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface Command {
|
|
11
|
+
name: string;
|
|
12
|
+
aliases?: string[];
|
|
13
|
+
description?: string;
|
|
14
|
+
execute(ctx: CommandContext): Promise<void> | void;
|
|
15
|
+
}
|
|
16
|
+
export interface CommandHandlerOptions {
|
|
17
|
+
commandsDir: string;
|
|
18
|
+
prefix?: string;
|
|
19
|
+
logger?: any;
|
|
20
|
+
onError?: (error: any, msg: any) => void;
|
|
21
|
+
}
|
|
22
|
+
export interface CommandHandler {
|
|
23
|
+
commands: Map<string, Command>;
|
|
24
|
+
reload: () => void;
|
|
25
|
+
stop: () => void;
|
|
26
|
+
}
|
|
27
|
+
export function createCommandHandler(sock: any, options: CommandHandlerOptions): CommandHandler;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.createCommandHandler = void 0;
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const createCommandHandler = (sock, options) => {
|
|
7
|
+
const prefix = options.prefix ?? '!';
|
|
8
|
+
const logger = options.logger || sock.logger;
|
|
9
|
+
const commands = new Map();
|
|
10
|
+
const load = () => {
|
|
11
|
+
commands.clear();
|
|
12
|
+
const dir = options.commandsDir;
|
|
13
|
+
if (!fs.existsSync(dir)) {
|
|
14
|
+
logger?.warn?.({ dir }, 'commands directory does not exist');
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const files = fs.readdirSync(dir).filter(f => f.endsWith('.js'));
|
|
18
|
+
for (const file of files) {
|
|
19
|
+
const fullPath = path.join(dir, file);
|
|
20
|
+
try {
|
|
21
|
+
delete require.cache[require.resolve(fullPath)];
|
|
22
|
+
const mod = require(fullPath);
|
|
23
|
+
const cmd = mod?.default || mod;
|
|
24
|
+
if (!cmd?.name || typeof cmd.execute !== 'function') {
|
|
25
|
+
logger?.warn?.({ file }, 'skipping invalid command module (missing name/execute)');
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
commands.set(cmd.name.toLowerCase(), cmd);
|
|
29
|
+
for (const alias of cmd.aliases || []) {
|
|
30
|
+
commands.set(alias.toLowerCase(), cmd);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
logger?.error?.({ error, file }, 'failed to load command');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
logger?.debug?.({ count: commands.size }, 'commands loaded');
|
|
38
|
+
};
|
|
39
|
+
load();
|
|
40
|
+
const handler = async ({ messages, type }) => {
|
|
41
|
+
if (type !== 'notify')
|
|
42
|
+
return;
|
|
43
|
+
for (const msg of messages) {
|
|
44
|
+
try {
|
|
45
|
+
if (!msg.message || msg.key.fromMe)
|
|
46
|
+
continue;
|
|
47
|
+
const body = msg.message.conversation ||
|
|
48
|
+
msg.message.extendedTextMessage?.text ||
|
|
49
|
+
msg.message.imageMessage?.caption ||
|
|
50
|
+
msg.message.videoMessage?.caption ||
|
|
51
|
+
'';
|
|
52
|
+
if (!body.startsWith(prefix))
|
|
53
|
+
continue;
|
|
54
|
+
const [cmdName, ...args] = body.slice(prefix.length).trim().split(/\s+/);
|
|
55
|
+
if (!cmdName)
|
|
56
|
+
continue;
|
|
57
|
+
const cmd = commands.get(cmdName.toLowerCase());
|
|
58
|
+
if (!cmd)
|
|
59
|
+
continue;
|
|
60
|
+
const jid = msg.key.remoteJid;
|
|
61
|
+
const ctx = {
|
|
62
|
+
sock,
|
|
63
|
+
msg,
|
|
64
|
+
args,
|
|
65
|
+
text: args.join(' '),
|
|
66
|
+
jid,
|
|
67
|
+
sender: msg.key.participant || msg.key.remoteJid,
|
|
68
|
+
isGroup: !!jid && jid.endsWith('@g.us')
|
|
69
|
+
};
|
|
70
|
+
await cmd.execute(ctx);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (options.onError) {
|
|
74
|
+
options.onError(error, msg);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
logger?.error?.({ error }, 'command execution failed');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
sock.ev.on('messages.upsert', handler);
|
|
83
|
+
return {
|
|
84
|
+
commands,
|
|
85
|
+
reload: load,
|
|
86
|
+
stop: () => sock.ev.off('messages.upsert', handler)
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
exports.createCommandHandler = createCommandHandler;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.buildPairingQRData = exports.getCompanionPlatformId = exports.getCompanionWebClientType = exports.CompanionWebClientType = void 0;
|
|
4
|
+
var CompanionWebClientType;
|
|
5
|
+
(function (CompanionWebClientType) {
|
|
6
|
+
CompanionWebClientType[(CompanionWebClientType['UNKNOWN'] = 0)] = 'UNKNOWN';
|
|
7
|
+
CompanionWebClientType[(CompanionWebClientType['CHROME'] = 1)] = 'CHROME';
|
|
8
|
+
CompanionWebClientType[(CompanionWebClientType['EDGE'] = 2)] = 'EDGE';
|
|
9
|
+
CompanionWebClientType[(CompanionWebClientType['FIREFOX'] = 3)] = 'FIREFOX';
|
|
10
|
+
CompanionWebClientType[(CompanionWebClientType['IE'] = 4)] = 'IE';
|
|
11
|
+
CompanionWebClientType[(CompanionWebClientType['OPERA'] = 5)] = 'OPERA';
|
|
12
|
+
CompanionWebClientType[(CompanionWebClientType['SAFARI'] = 6)] = 'SAFARI';
|
|
13
|
+
CompanionWebClientType[(CompanionWebClientType['ELECTRON'] = 7)] = 'ELECTRON';
|
|
14
|
+
CompanionWebClientType[(CompanionWebClientType['UWP'] = 8)] = 'UWP';
|
|
15
|
+
CompanionWebClientType[(CompanionWebClientType['OTHER_WEB_CLIENT'] = 9)] = 'OTHER_WEB_CLIENT';
|
|
16
|
+
})((CompanionWebClientType = exports.CompanionWebClientType || (exports.CompanionWebClientType = {})));
|
|
17
|
+
const BROWSER_TO_COMPANION_WEB_CLIENT = {
|
|
18
|
+
Chrome: CompanionWebClientType.CHROME,
|
|
19
|
+
Edge: CompanionWebClientType.EDGE,
|
|
20
|
+
Firefox: CompanionWebClientType.FIREFOX,
|
|
21
|
+
IE: CompanionWebClientType.IE,
|
|
22
|
+
Opera: CompanionWebClientType.OPERA,
|
|
23
|
+
Safari: CompanionWebClientType.SAFARI
|
|
24
|
+
};
|
|
25
|
+
const getCompanionWebClientType = ([os, browserName]) => {
|
|
26
|
+
if (browserName === 'Desktop') {
|
|
27
|
+
return os === 'Windows' ? CompanionWebClientType.UWP : CompanionWebClientType.ELECTRON;
|
|
28
|
+
}
|
|
29
|
+
return BROWSER_TO_COMPANION_WEB_CLIENT[browserName] || CompanionWebClientType.OTHER_WEB_CLIENT;
|
|
30
|
+
};
|
|
31
|
+
exports.getCompanionWebClientType = getCompanionWebClientType;
|
|
32
|
+
const getCompanionPlatformId = (browser) => {
|
|
33
|
+
return getCompanionWebClientType(browser).toString();
|
|
34
|
+
};
|
|
35
|
+
exports.getCompanionPlatformId = getCompanionPlatformId;
|
|
36
|
+
const buildPairingQRData = (ref, noiseKeyB64, identityKeyB64, advB64, browser) => {
|
|
37
|
+
return ('https://wa.me/settings/linked_devices#' +
|
|
38
|
+
[ref, noiseKeyB64, identityKeyB64, advB64, getCompanionPlatformId(browser)].join(','));
|
|
39
|
+
};
|
|
40
|
+
exports.buildPairingQRData = buildPairingQRData;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.consumerApplicationToMessage = exports.decodeConsumerApplication = void 0;
|
|
4
|
+
const index_js_1 = require('../../WAProto/index.js');
|
|
5
|
+
const decodeConsumerApplication = buffer => {
|
|
6
|
+
if (!Buffer.isBuffer(buffer) && !(buffer instanceof Uint8Array)) {
|
|
7
|
+
throw new TypeError('decodeConsumerApplication: buffer must be Buffer or Uint8Array');
|
|
8
|
+
}
|
|
9
|
+
return index_js_1.proto.ConsumerApplication.decode(buffer);
|
|
10
|
+
};
|
|
11
|
+
exports.decodeConsumerApplication = decodeConsumerApplication;
|
|
12
|
+
const textOf = mt => (mt && typeof mt.text === 'string' ? mt.text : undefined);
|
|
13
|
+
const mapContent = content => {
|
|
14
|
+
if (!content)
|
|
15
|
+
return null;
|
|
16
|
+
switch (content.content) {
|
|
17
|
+
case 'messageText':
|
|
18
|
+
return { conversation: textOf(content.messageText) ?? '' };
|
|
19
|
+
case 'extendedTextMessage': {
|
|
20
|
+
const e = content.extendedTextMessage;
|
|
21
|
+
return {
|
|
22
|
+
extendedTextMessage: {
|
|
23
|
+
text: textOf(e.text),
|
|
24
|
+
matchedText: e.matchedText,
|
|
25
|
+
canonicalUrl: e.canonicalUrl,
|
|
26
|
+
description: e.description,
|
|
27
|
+
title: e.title
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
case 'imageMessage':
|
|
32
|
+
return {
|
|
33
|
+
imageMessage: { mediaPayload: content.imageMessage.image, caption: textOf(content.imageMessage.caption) }
|
|
34
|
+
};
|
|
35
|
+
case 'videoMessage':
|
|
36
|
+
return {
|
|
37
|
+
videoMessage: { mediaPayload: content.videoMessage.video, caption: textOf(content.videoMessage.caption) }
|
|
38
|
+
};
|
|
39
|
+
case 'audioMessage':
|
|
40
|
+
return { audioMessage: { mediaPayload: content.audioMessage.audio, ptt: content.audioMessage.ptt } };
|
|
41
|
+
case 'documentMessage':
|
|
42
|
+
return {
|
|
43
|
+
documentMessage: { mediaPayload: content.documentMessage.document, fileName: content.documentMessage.fileName }
|
|
44
|
+
};
|
|
45
|
+
case 'stickerMessage':
|
|
46
|
+
return { stickerMessage: { mediaPayload: content.stickerMessage.sticker } };
|
|
47
|
+
case 'contactMessage':
|
|
48
|
+
return { contactMessage: { mediaPayload: content.contactMessage.contact } };
|
|
49
|
+
case 'contactsArrayMessage':
|
|
50
|
+
return {
|
|
51
|
+
contactsArrayMessage: {
|
|
52
|
+
displayName: content.contactsArrayMessage.displayName,
|
|
53
|
+
contacts: content.contactsArrayMessage.contacts || []
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
case 'locationMessage': {
|
|
57
|
+
const loc = content.locationMessage.location || {};
|
|
58
|
+
return {
|
|
59
|
+
locationMessage: {
|
|
60
|
+
degreesLatitude: loc.degreesLatitude,
|
|
61
|
+
degreesLongitude: loc.degreesLongitude,
|
|
62
|
+
name: loc.name
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
case 'liveLocationMessage': {
|
|
67
|
+
const loc = content.liveLocationMessage.location || {};
|
|
68
|
+
return {
|
|
69
|
+
liveLocationMessage: {
|
|
70
|
+
degreesLatitude: loc.degreesLatitude,
|
|
71
|
+
degreesLongitude: loc.degreesLongitude,
|
|
72
|
+
caption: textOf(content.liveLocationMessage.caption)
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
case 'reactionMessage':
|
|
77
|
+
return { reactionMessage: content.reactionMessage };
|
|
78
|
+
default:
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
const consumerApplicationToMessage = app => {
|
|
83
|
+
const payload = app?.payload;
|
|
84
|
+
if (!payload)
|
|
85
|
+
return null;
|
|
86
|
+
switch (payload.payload) {
|
|
87
|
+
case 'content':
|
|
88
|
+
return mapContent(payload.content);
|
|
89
|
+
case 'applicationData': {
|
|
90
|
+
const ad = payload.applicationData;
|
|
91
|
+
if (ad?.applicationContent === 'revoke' && ad.revoke?.key) {
|
|
92
|
+
return {
|
|
93
|
+
protocolMessage: {
|
|
94
|
+
key: ad.revoke.key,
|
|
95
|
+
type: index_js_1.proto.Message.ProtocolMessage.Type.REVOKE
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
default:
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
exports.consumerApplicationToMessage = consumerApplicationToMessage;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.signedKeyPair = exports.Curve = exports.generateSignalPubKey = exports.hkdf = exports.md5 = void 0;
|
|
4
|
+
exports.aesEncryptGCM = aesEncryptGCM;
|
|
5
|
+
exports.aesDecryptGCM = aesDecryptGCM;
|
|
6
|
+
exports.aesEncryptCTR = aesEncryptCTR;
|
|
7
|
+
exports.aesDecryptCTR = aesDecryptCTR;
|
|
8
|
+
exports.aesDecrypt = aesDecrypt;
|
|
9
|
+
exports.aesDecryptWithIV = aesDecryptWithIV;
|
|
10
|
+
exports.aesEncrypt = aesEncrypt;
|
|
11
|
+
exports.aesEncrypWithIV = aesEncrypWithIV;
|
|
12
|
+
exports.hmacSign = hmacSign;
|
|
13
|
+
exports.sha256 = sha256;
|
|
14
|
+
exports.derivePairingCodeKey = derivePairingCodeKey;
|
|
15
|
+
const { subtle } = globalThis.crypto;
|
|
16
|
+
const Defaults_1 = require('../Defaults');
|
|
17
|
+
const nodeCrypto = require('crypto');
|
|
18
|
+
const md5 = buffer => nodeCrypto.createHash('md5').update(buffer).digest();
|
|
19
|
+
Object.defineProperty(exports, 'md5', { enumerable: true, get: () => md5 });
|
|
20
|
+
const hkdfExtractJS = (salt, ikm) => nodeCrypto.createHmac('sha256', salt).update(ikm).digest();
|
|
21
|
+
const hkdfExpandJS = (prk, length, info) => {
|
|
22
|
+
const hashLen = 32;
|
|
23
|
+
const n = Math.ceil(length / hashLen);
|
|
24
|
+
let t = Buffer.alloc(0);
|
|
25
|
+
let okm = Buffer.alloc(0);
|
|
26
|
+
for (let i = 1; i <= n; i++) {
|
|
27
|
+
t = nodeCrypto
|
|
28
|
+
.createHmac('sha256', prk)
|
|
29
|
+
.update(Buffer.concat([t, info, Buffer.from([i])]))
|
|
30
|
+
.digest();
|
|
31
|
+
okm = Buffer.concat([okm, t]);
|
|
32
|
+
}
|
|
33
|
+
return okm.subarray(0, length);
|
|
34
|
+
};
|
|
35
|
+
const hkdf = (ikm, length, options = {}) => {
|
|
36
|
+
const ikmBuf = Buffer.isBuffer(ikm) ? ikm : Buffer.from(ikm);
|
|
37
|
+
const saltBuf = options.salt
|
|
38
|
+
? Buffer.isBuffer(options.salt)
|
|
39
|
+
? options.salt
|
|
40
|
+
: Buffer.from(options.salt)
|
|
41
|
+
: Buffer.alloc(32);
|
|
42
|
+
const infoBuf = options.info
|
|
43
|
+
? Buffer.isBuffer(options.info)
|
|
44
|
+
? options.info
|
|
45
|
+
: Buffer.from(options.info)
|
|
46
|
+
: Buffer.alloc(0);
|
|
47
|
+
const prk = hkdfExtractJS(saltBuf, ikmBuf);
|
|
48
|
+
return hkdfExpandJS(prk, length, infoBuf);
|
|
49
|
+
};
|
|
50
|
+
Object.defineProperty(exports, 'hkdf', { enumerable: true, get: () => hkdf });
|
|
51
|
+
const generateSignalPubKey = pubKey => pubKey.length === 33 ? pubKey : Buffer.concat([Defaults_1.KEY_BUNDLE_TYPE, pubKey]);
|
|
52
|
+
exports.generateSignalPubKey = generateSignalPubKey;
|
|
53
|
+
const curve25519JS = require('./curve25519-js');
|
|
54
|
+
exports.Curve = {
|
|
55
|
+
generateKeyPair: () => curve25519JS.generateKeyPairJS(),
|
|
56
|
+
sharedKey: (privateKey, publicKey) => curve25519JS.sharedKeyJS(privateKey, publicKey),
|
|
57
|
+
sign: (privateKey, buf) => curve25519JS.signJS(privateKey, buf),
|
|
58
|
+
verify: (pubKey, message, signature) => curve25519JS.verifyJS(pubKey, message, signature)
|
|
59
|
+
};
|
|
60
|
+
const signedKeyPair = (identityKeyPair, keyId) => {
|
|
61
|
+
const preKey = exports.Curve.generateKeyPair();
|
|
62
|
+
const pubKey = generateSignalPubKey(preKey.public);
|
|
63
|
+
const signature = exports.Curve.sign(identityKeyPair.private, pubKey);
|
|
64
|
+
return { keyPair: preKey, signature, keyId };
|
|
65
|
+
};
|
|
66
|
+
exports.signedKeyPair = signedKeyPair;
|
|
67
|
+
function aesEncryptGCM(plaintext, key, iv, additionalData) {
|
|
68
|
+
const cipher = nodeCrypto.createCipheriv('aes-256-gcm', key, iv);
|
|
69
|
+
if (additionalData)
|
|
70
|
+
cipher.setAAD(additionalData);
|
|
71
|
+
const encrypted = Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
72
|
+
return Buffer.concat([encrypted, cipher.getAuthTag()]);
|
|
73
|
+
}
|
|
74
|
+
function aesDecryptGCM(ciphertext, key, iv, additionalData) {
|
|
75
|
+
const authTag = ciphertext.subarray(ciphertext.length - 16);
|
|
76
|
+
const encrypted = ciphertext.subarray(0, ciphertext.length - 16);
|
|
77
|
+
const decipher = nodeCrypto.createDecipheriv('aes-256-gcm', key, iv);
|
|
78
|
+
if (additionalData)
|
|
79
|
+
decipher.setAAD(additionalData);
|
|
80
|
+
decipher.setAuthTag(authTag);
|
|
81
|
+
return Buffer.concat([decipher.update(encrypted), decipher.final()]);
|
|
82
|
+
}
|
|
83
|
+
function aesEncryptCTR(plaintext, key, iv) {
|
|
84
|
+
const cipher = nodeCrypto.createCipheriv('aes-256-ctr', key, iv);
|
|
85
|
+
return Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
86
|
+
}
|
|
87
|
+
function aesDecryptCTR(ciphertext, key, iv) {
|
|
88
|
+
const decipher = nodeCrypto.createDecipheriv('aes-256-ctr', key, iv);
|
|
89
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
90
|
+
}
|
|
91
|
+
function aesDecrypt(buffer, key) {
|
|
92
|
+
return aesDecryptWithIV(buffer, key, Buffer.alloc(16));
|
|
93
|
+
}
|
|
94
|
+
function aesEncrypt(buffer, key) {
|
|
95
|
+
return aesEncrypWithIV(buffer, key, Buffer.alloc(16));
|
|
96
|
+
}
|
|
97
|
+
function aesDecryptWithIV(buffer, key, IV) {
|
|
98
|
+
const decipher = nodeCrypto.createDecipheriv('aes-256-cbc', key, IV);
|
|
99
|
+
return Buffer.concat([decipher.update(buffer), decipher.final()]);
|
|
100
|
+
}
|
|
101
|
+
function aesEncrypWithIV(buffer, key, IV) {
|
|
102
|
+
const cipher = nodeCrypto.createCipheriv('aes-256-cbc', key, IV);
|
|
103
|
+
return Buffer.concat([cipher.update(buffer), cipher.final()]);
|
|
104
|
+
}
|
|
105
|
+
function hmacSign(buffer, key, variant = 'sha256') {
|
|
106
|
+
return nodeCrypto.createHmac(variant, key).update(buffer).digest();
|
|
107
|
+
}
|
|
108
|
+
function sha256(buffer) {
|
|
109
|
+
return nodeCrypto.createHash('sha256').update(buffer).digest();
|
|
110
|
+
}
|
|
111
|
+
async function derivePairingCodeKey(pairingCode, salt) {
|
|
112
|
+
const encoder = new TextEncoder();
|
|
113
|
+
const pairingCodeBuffer = encoder.encode(pairingCode);
|
|
114
|
+
const saltBuffer = new Uint8Array(salt instanceof Uint8Array ? salt : new Uint8Array(salt));
|
|
115
|
+
const keyMaterial = await subtle.importKey('raw', pairingCodeBuffer, { name: 'PBKDF2' }, false, ['deriveBits']);
|
|
116
|
+
const derivedBits = await subtle.deriveBits({ name: 'PBKDF2', salt: saltBuffer, iterations: 2 << 16, hash: 'SHA-256' }, keyMaterial, 256);
|
|
117
|
+
return Buffer.from(derivedBits);
|
|
118
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.generateKeyPairJS = exports.sharedKeyJS = exports.signJS = exports.verifyJS = void 0;
|
|
4
|
+
const crypto = require('crypto');
|
|
5
|
+
const P = 2n ** 255n - 19n;
|
|
6
|
+
const L = 2n ** 252n + 27742317777372353535851937790883648493n;
|
|
7
|
+
function mod(a, m = P) {
|
|
8
|
+
const r = a % m;
|
|
9
|
+
return r >= 0n ? r : r + m;
|
|
10
|
+
}
|
|
11
|
+
function powmod(b, e, m) {
|
|
12
|
+
b = mod(b, m);
|
|
13
|
+
let r = 1n;
|
|
14
|
+
while (e > 0n) {
|
|
15
|
+
if (e & 1n)
|
|
16
|
+
r = mod(r * b, m);
|
|
17
|
+
b = mod(b * b, m);
|
|
18
|
+
e >>= 1n;
|
|
19
|
+
}
|
|
20
|
+
return r;
|
|
21
|
+
}
|
|
22
|
+
function invert(a, m = P) {
|
|
23
|
+
return powmod(a, m - 2n, m);
|
|
24
|
+
}
|
|
25
|
+
const D = mod(-121665n * invert(121666n));
|
|
26
|
+
const I = powmod(2n, (P - 1n) / 4n, P);
|
|
27
|
+
function pointAdd(p1, p2) {
|
|
28
|
+
const [X1, Y1, Z1, T1] = p1;
|
|
29
|
+
const [X2, Y2, Z2, T2] = p2;
|
|
30
|
+
const A = mod((Y1 - X1) * (Y2 - X2));
|
|
31
|
+
const Bb = mod((Y1 + X1) * (Y2 + X2));
|
|
32
|
+
const C = mod(T1 * 2n * D * T2);
|
|
33
|
+
const Dd = mod(Z1 * 2n * Z2);
|
|
34
|
+
const E = Bb - A;
|
|
35
|
+
const F = Dd - C;
|
|
36
|
+
const G = Dd + C;
|
|
37
|
+
const H = Bb + A;
|
|
38
|
+
return [mod(E * F), mod(G * H), mod(F * G), mod(E * H)];
|
|
39
|
+
}
|
|
40
|
+
function pointDouble(p) {
|
|
41
|
+
const [X1, Y1, Z1] = p;
|
|
42
|
+
const A = mod(X1 * X1);
|
|
43
|
+
const Bb = mod(Y1 * Y1);
|
|
44
|
+
const C = mod(2n * Z1 * Z1);
|
|
45
|
+
const H = A + Bb;
|
|
46
|
+
const E = H - mod((X1 + Y1) * (X1 + Y1));
|
|
47
|
+
const G = A - Bb;
|
|
48
|
+
const F = C + G;
|
|
49
|
+
return [mod(E * F), mod(G * H), mod(F * G), mod(E * H)];
|
|
50
|
+
}
|
|
51
|
+
function scalarMul(s, p) {
|
|
52
|
+
let result = [0n, 1n, 1n, 0n];
|
|
53
|
+
let addend = p;
|
|
54
|
+
s = mod(s, L * 8n);
|
|
55
|
+
if (s < 0n)
|
|
56
|
+
s += L * 8n;
|
|
57
|
+
while (s > 0n) {
|
|
58
|
+
if (s & 1n)
|
|
59
|
+
result = pointAdd(result, addend);
|
|
60
|
+
addend = pointDouble(addend);
|
|
61
|
+
s >>= 1n;
|
|
62
|
+
}
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
function negatePoint(p) {
|
|
66
|
+
const [X, Y, Z, T] = p;
|
|
67
|
+
return [mod(-X), Y, Z, mod(-T)];
|
|
68
|
+
}
|
|
69
|
+
function pointEqual(p1, p2) {
|
|
70
|
+
const [X1, Y1, Z1] = p1;
|
|
71
|
+
const [X2, Y2, Z2] = p2;
|
|
72
|
+
return mod(X1 * Z2 - X2 * Z1) === 0n && mod(Y1 * Z2 - Y2 * Z1) === 0n;
|
|
73
|
+
}
|
|
74
|
+
function recoverX(y, sign) {
|
|
75
|
+
const y2 = y * y;
|
|
76
|
+
const u = mod(y2 - 1n);
|
|
77
|
+
const v = mod(D * y2 + 1n);
|
|
78
|
+
let x = mod(u * invert(v));
|
|
79
|
+
x = powmod(x, (P + 3n) / 8n, P);
|
|
80
|
+
if (mod(x * x - u * invert(v)) !== 0n) {
|
|
81
|
+
x = mod(x * I);
|
|
82
|
+
}
|
|
83
|
+
if (mod(x * x - u * invert(v)) !== 0n) {
|
|
84
|
+
throw new Error('invalid Edwards point (not on curve)');
|
|
85
|
+
}
|
|
86
|
+
if ((x & 1n) !== BigInt(sign)) {
|
|
87
|
+
x = P - x;
|
|
88
|
+
}
|
|
89
|
+
return x;
|
|
90
|
+
}
|
|
91
|
+
const By = mod(4n * invert(5n));
|
|
92
|
+
const Bx = recoverX(By, 0n);
|
|
93
|
+
const BASE = [mod(Bx), mod(By), 1n, mod(mod(Bx) * mod(By))];
|
|
94
|
+
function bigIntToBytesLE(n, len) {
|
|
95
|
+
const out = Buffer.alloc(len);
|
|
96
|
+
for (let i = 0; i < len; i++) {
|
|
97
|
+
out[i] = Number(n & 0xffn);
|
|
98
|
+
n >>= 8n;
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
function bytesToBigIntLE(buf) {
|
|
103
|
+
let n = 0n;
|
|
104
|
+
for (let i = buf.length - 1; i >= 0; i--) {
|
|
105
|
+
n = (n << 8n) | BigInt(buf[i]);
|
|
106
|
+
}
|
|
107
|
+
return n;
|
|
108
|
+
}
|
|
109
|
+
function encodePoint(p) {
|
|
110
|
+
const [X, Y, Z] = p;
|
|
111
|
+
const zinv = invert(Z);
|
|
112
|
+
const x = mod(X * zinv);
|
|
113
|
+
const y = mod(Y * zinv);
|
|
114
|
+
const bytes = bigIntToBytesLE(y, 32);
|
|
115
|
+
if (x & 1n)
|
|
116
|
+
bytes[31] |= 0x80;
|
|
117
|
+
return bytes;
|
|
118
|
+
}
|
|
119
|
+
function decodePoint(buf) {
|
|
120
|
+
const sign = buf[31] & 0x80 ? 1 : 0;
|
|
121
|
+
const y = bytesToBigIntLE(buf) & ((1n << 255n) - 1n);
|
|
122
|
+
const x = recoverX(y, BigInt(sign));
|
|
123
|
+
return [x, y, 1n, mod(x * y)];
|
|
124
|
+
}
|
|
125
|
+
function sha512(...bufs) {
|
|
126
|
+
const h = crypto.createHash('sha512');
|
|
127
|
+
for (const b of bufs)
|
|
128
|
+
h.update(b);
|
|
129
|
+
return h.digest();
|
|
130
|
+
}
|
|
131
|
+
function hashI(i, ...bufs) {
|
|
132
|
+
const padding = Buffer.alloc(32, 0xff);
|
|
133
|
+
padding[0] = 0xff - i;
|
|
134
|
+
const h = crypto.createHash('sha512');
|
|
135
|
+
h.update(padding);
|
|
136
|
+
for (const b of bufs)
|
|
137
|
+
h.update(b);
|
|
138
|
+
return h.digest();
|
|
139
|
+
}
|
|
140
|
+
function signBit(pointBytes) {
|
|
141
|
+
return pointBytes[31] & 0x80 ? 1 : 0;
|
|
142
|
+
}
|
|
143
|
+
function montgomeryUToEdwardsY(uBuf) {
|
|
144
|
+
const u = mod(bytesToBigIntLE(uBuf), P);
|
|
145
|
+
return mod((u - 1n) * invert(mod(u + 1n, P)), P);
|
|
146
|
+
}
|
|
147
|
+
function encodeYWithSign(y, sign) {
|
|
148
|
+
const yBytes = bigIntToBytesLE(y, 32);
|
|
149
|
+
if (sign)
|
|
150
|
+
yBytes[31] |= 0x80;
|
|
151
|
+
return encodePoint(decodePoint(yBytes));
|
|
152
|
+
}
|
|
153
|
+
function stripKeyTypePrefix(buf) {
|
|
154
|
+
return buf.length === 33 ? buf.subarray(1) : buf;
|
|
155
|
+
}
|
|
156
|
+
function calculateKeyPair(kBuf) {
|
|
157
|
+
const k = bytesToBigIntLE(kBuf);
|
|
158
|
+
const rawPoint = scalarMul(k, BASE);
|
|
159
|
+
const rawEnc = encodePoint(rawPoint);
|
|
160
|
+
let a;
|
|
161
|
+
let A;
|
|
162
|
+
if (signBit(rawEnc) === 1) {
|
|
163
|
+
a = mod(-k, L);
|
|
164
|
+
A = encodePoint(negatePoint(rawPoint));
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
a = mod(k, L);
|
|
168
|
+
A = rawEnc;
|
|
169
|
+
}
|
|
170
|
+
return { a, A };
|
|
171
|
+
}
|
|
172
|
+
function signJS(privateKey, message) {
|
|
173
|
+
const Z = crypto.randomBytes(64);
|
|
174
|
+
const { a, A } = calculateKeyPair(privateKey);
|
|
175
|
+
const aBytes = bigIntToBytesLE(a, 32);
|
|
176
|
+
const r = mod(bytesToBigIntLE(hashI(1, aBytes, message, Z)), L);
|
|
177
|
+
const R = scalarMul(r, BASE);
|
|
178
|
+
const REnc = encodePoint(R);
|
|
179
|
+
const h = mod(bytesToBigIntLE(sha512(REnc, A, message)), L);
|
|
180
|
+
const s = mod(r + h * a, L);
|
|
181
|
+
return Buffer.concat([REnc, bigIntToBytesLE(s, 32)]);
|
|
182
|
+
}
|
|
183
|
+
exports.signJS = signJS;
|
|
184
|
+
function verifyJS(pubKey, message, signature) {
|
|
185
|
+
try {
|
|
186
|
+
const u = stripKeyTypePrefix(pubKey);
|
|
187
|
+
if (u.length !== 32 || signature.length !== 64)
|
|
188
|
+
return false;
|
|
189
|
+
const REnc = signature.subarray(0, 32);
|
|
190
|
+
const s = bytesToBigIntLE(signature.subarray(32, 64));
|
|
191
|
+
if (s >= L)
|
|
192
|
+
return false;
|
|
193
|
+
const A = encodeYWithSign(montgomeryUToEdwardsY(u), 0);
|
|
194
|
+
const APoint = decodePoint(A);
|
|
195
|
+
const h = mod(bytesToBigIntLE(sha512(REnc, A, message)), L);
|
|
196
|
+
const sB = scalarMul(s, BASE);
|
|
197
|
+
const hA = scalarMul(h, APoint);
|
|
198
|
+
const RCheck = pointAdd(sB, negatePoint(hA));
|
|
199
|
+
return encodePoint(RCheck).equals(REnc);
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
exports.verifyJS = verifyJS;
|
|
206
|
+
function b64url(buf) {
|
|
207
|
+
return buf.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
208
|
+
}
|
|
209
|
+
function fromB64url(str) {
|
|
210
|
+
str = str.replace(/-/g, '+').replace(/_/g, '/');
|
|
211
|
+
while (str.length % 4)
|
|
212
|
+
str += '=';
|
|
213
|
+
return Buffer.from(str, 'base64');
|
|
214
|
+
}
|
|
215
|
+
function generateKeyPairJS() {
|
|
216
|
+
const { publicKey, privateKey } = crypto.generateKeyPairSync('x25519');
|
|
217
|
+
const priv = fromB64url(privateKey.export({ format: 'jwk' }).d);
|
|
218
|
+
const pub = fromB64url(publicKey.export({ format: 'jwk' }).x);
|
|
219
|
+
return { private: priv, public: pub };
|
|
220
|
+
}
|
|
221
|
+
exports.generateKeyPairJS = generateKeyPairJS;
|
|
222
|
+
function sharedKeyJS(privateKey, publicKey) {
|
|
223
|
+
const pub = stripKeyTypePrefix(publicKey);
|
|
224
|
+
const privKey = crypto.createPrivateKey({
|
|
225
|
+
key: { kty: 'OKP', crv: 'X25519', d: b64url(privateKey), x: b64url(generateKeyPairJS_publicFrom(privateKey)) },
|
|
226
|
+
format: 'jwk'
|
|
227
|
+
});
|
|
228
|
+
const pubKey = crypto.createPublicKey({
|
|
229
|
+
key: { kty: 'OKP', crv: 'X25519', x: b64url(pub) },
|
|
230
|
+
format: 'jwk'
|
|
231
|
+
});
|
|
232
|
+
return crypto.diffieHellman({ privateKey: privKey, publicKey: pubKey });
|
|
233
|
+
}
|
|
234
|
+
exports.sharedKeyJS = sharedKeyJS;
|
|
235
|
+
function generateKeyPairJS_publicFrom(privateKeyRaw) {
|
|
236
|
+
const k = bytesToBigIntLE(privateKeyRaw);
|
|
237
|
+
const point = scalarMul(k, BASE);
|
|
238
|
+
const [, Y, Z] = point;
|
|
239
|
+
const y = mod(Y * invert(Z));
|
|
240
|
+
const u = mod((1n + y) * invert(mod(1n - y)));
|
|
241
|
+
return bigIntToBytesLE(u, 32);
|
|
242
|
+
}
|