Comparing version 1.4.1 to 2.0.0
@@ -283,3 +283,3 @@ "use strict"; | ||
const patchChat = async (jid, modification) => { | ||
const patch = chat_utils_1.encodeSyncdPatch(modification, { remoteJid: jid }, authState); | ||
const patch = await chat_utils_1.encodeSyncdPatch(modification, { remoteJid: jid }, authState); | ||
const type = 'regular_high'; | ||
@@ -345,3 +345,4 @@ const ver = authState.creds.appStateVersion[type] || 0; | ||
const patches = WABinary_1.getBinaryNodeChildren(patchesNode, 'patch'); | ||
const successfulMutations = patches.flatMap(({ content }) => { | ||
const successfulMutations = []; | ||
for (const { content } of patches) { | ||
if (content) { | ||
@@ -353,10 +354,9 @@ const syncd = WAProto_1.proto.SyncdPatch.decode(content); | ||
} | ||
const { mutations, failures } = chat_utils_1.decodeSyncdPatch(syncd, authState); | ||
const { mutations, failures } = await chat_utils_1.decodeSyncdPatch(syncd, authState); | ||
if (failures.length) { | ||
logger.info({ failures: failures.map(f => ({ trace: f.stack, data: f.data })) }, 'failed to decode'); | ||
} | ||
return mutations; | ||
successfulMutations.push(...mutations); | ||
} | ||
return []; | ||
}); | ||
} | ||
processSyncActions(successfulMutations); | ||
@@ -363,0 +363,0 @@ ev.emit('auth-state.update', authState); |
@@ -89,3 +89,3 @@ "use strict"; | ||
}; | ||
const processMessage = (message, chatUpdate) => { | ||
const processMessage = async (message, chatUpdate) => { | ||
var _a; | ||
@@ -96,7 +96,6 @@ const protocolMsg = (_a = message.message) === null || _a === void 0 ? void 0 : _a.protocolMessage; | ||
case WAProto_1.proto.ProtocolMessage.ProtocolMessageType.APP_STATE_SYNC_KEY_SHARE: | ||
const newKeys = JSON.parse(JSON.stringify(protocolMsg.appStateSyncKeyShare.keys)); | ||
authState.creds.appStateSyncKeys = [ | ||
...(authState.creds.appStateSyncKeys || []), | ||
...newKeys | ||
]; | ||
for (const { keyData, keyId } of protocolMsg.appStateSyncKeyShare.keys || []) { | ||
const str = Buffer.from(keyId.keyId).toString('base64'); | ||
await authState.keys.setAppStateSyncKey(str, keyData); | ||
} | ||
ev.emit('auth-state.update', authState); | ||
@@ -382,6 +381,6 @@ break; | ||
}); | ||
ev.on('messages.upsert', ({ messages }) => { | ||
ev.on('messages.upsert', async ({ messages }) => { | ||
const chat = { id: messages[0].key.remoteJid }; | ||
for (const msg of messages) { | ||
processMessage(msg, chat); | ||
await processMessage(msg, chat); | ||
if (!!msg.message && !msg.message.protocolMessage) { | ||
@@ -388,0 +387,0 @@ chat.conversationTimestamp = Utils_1.toNumber(msg.messageTimestamp); |
@@ -11,3 +11,3 @@ /// <reference types="node" /> | ||
*/ | ||
export declare const makeSocket: ({ waWebSocketUrl, connectTimeoutMs, logger, agent, keepAliveIntervalMs, version, browser, auth: initialAuthState }: SocketConfig) => { | ||
export declare const makeSocket: ({ waWebSocketUrl, connectTimeoutMs, logger, agent, keepAliveIntervalMs, version, browser, auth: initialAuthState, printQRInTerminal, }: SocketConfig) => { | ||
ws: WebSocket; | ||
@@ -14,0 +14,0 @@ ev: BaileysEventEmitter; |
@@ -44,3 +44,3 @@ "use strict"; | ||
*/ | ||
const makeSocket = ({ waWebSocketUrl, connectTimeoutMs, logger, agent, keepAliveIntervalMs, version, browser, auth: initialAuthState }) => { | ||
const makeSocket = ({ waWebSocketUrl, connectTimeoutMs, logger, agent, keepAliveIntervalMs, version, browser, auth: initialAuthState, printQRInTerminal, }) => { | ||
const ws = new ws_1.default(waWebSocketUrl, undefined, { | ||
@@ -379,6 +379,8 @@ origin: Defaults_1.DEFAULT_ORIGIN, | ||
const postQR = async () => { | ||
const QR = await Promise.resolve().then(() => __importStar(require('qrcode-terminal'))).catch(err => { | ||
logger.error('add `qrcode-terminal` as a dependency to auto-print QR'); | ||
}); | ||
QR === null || QR === void 0 ? void 0 : QR.generate(qr, { small: true }); | ||
if (printQRInTerminal) { | ||
const QR = await Promise.resolve().then(() => __importStar(require('qrcode-terminal'))).catch(err => { | ||
logger.error('add `qrcode-terminal` as a dependency to auto-print QR'); | ||
}); | ||
QR === null || QR === void 0 ? void 0 : QR.generate(qr, { small: true }); | ||
} | ||
}; | ||
@@ -385,0 +387,0 @@ const refs = stanza.content[0].content.map(n => n.content); |
@@ -30,6 +30,6 @@ import type { Contact } from "./Contact"; | ||
signalIdentities?: SignalIdentity[]; | ||
appStateSyncKeys?: proto.IAppStateSyncKey[]; | ||
appStateVersion?: { | ||
[T in CollectionType]: number; | ||
}; | ||
myAppStateKeyId?: string; | ||
firstUnuploadedPreKeyId: number; | ||
@@ -47,2 +47,4 @@ serverHasPreKeys: boolean; | ||
setSenderKey: (id: string, item: any | null) => Awaitable<void>; | ||
getAppStateSyncKey: (id: string) => Awaitable<proto.IAppStateSyncKeyData>; | ||
setAppStateSyncKey: (id: string, item: proto.IAppStateSyncKeyData | null) => Awaitable<void>; | ||
}; | ||
@@ -49,0 +51,0 @@ export declare type AuthenticationState = { |
@@ -8,2 +8,6 @@ import type { proto } from "../../WAProto"; | ||
} | ||
export declare type ChatMutation = { | ||
action: proto.ISyncActionValue; | ||
index: [string, string]; | ||
}; | ||
export declare type Chat = Omit<proto.IConversation, 'messages'> & { | ||
@@ -10,0 +14,0 @@ /** unix timestamp of date when mute ends, if applicable */ |
import { Boom } from '@hapi/boom'; | ||
import { AuthenticationState, ChatModification } from "../Types"; | ||
import { AuthenticationState, ChatModification, ChatMutation } from "../Types"; | ||
import { proto } from '../../WAProto'; | ||
export declare const encodeSyncdPatch: (action: ChatModification, lastMessageKey: proto.IMessageKey, { creds: { appStateSyncKeys: [key], appStateVersion } }: AuthenticationState) => proto.ISyncdPatch; | ||
export declare const decodeSyncdPatch: (msg: proto.ISyncdPatch, { creds }: AuthenticationState) => { | ||
mutations: { | ||
action: proto.ISyncActionValue; | ||
index: [string, string]; | ||
}[]; | ||
export declare const encodeSyncdPatch: (action: ChatModification, lastMessageKey: proto.IMessageKey, { creds: { appStateVersion, myAppStateKeyId }, keys }: AuthenticationState) => Promise<proto.ISyncdPatch>; | ||
export declare const decodeSyncdPatch: (msg: proto.ISyncdPatch, { keys }: AuthenticationState) => Promise<{ | ||
mutations: ChatMutation[]; | ||
failures: Boom<any>[]; | ||
}; | ||
}>; |
@@ -9,3 +9,3 @@ "use strict"; | ||
const mutationKeys = (keydata) => { | ||
const expanded = crypto_1.hkdf(Buffer.from(keydata, 'base64'), 160, { info: 'WhatsApp Mutation Keys' }); | ||
const expanded = crypto_1.hkdf(keydata, 160, { info: 'WhatsApp Mutation Keys' }); | ||
return { | ||
@@ -68,3 +68,3 @@ indexKey: expanded.slice(0, 32), | ||
}; | ||
const encodeSyncdPatch = (action, lastMessageKey, { creds: { appStateSyncKeys: [key], appStateVersion } }) => { | ||
const encodeSyncdPatch = async (action, lastMessageKey, { creds: { appStateVersion, myAppStateKeyId }, keys }) => { | ||
let syncAction = {}; | ||
@@ -100,6 +100,11 @@ if ('archive' in action) { | ||
const encoded = WAProto_1.proto.SyncActionValue.encode(syncAction).finish(); | ||
const key = !!myAppStateKeyId ? await keys.getAppStateSyncKey(myAppStateKeyId) : undefined; | ||
if (!key) { | ||
throw new boom_1.Boom(`myAppStateKey not present`, { statusCode: 404 }); | ||
} | ||
const encKeyId = Buffer.from(myAppStateKeyId, 'base64'); | ||
const index = JSON.stringify([Object.keys(action)[0], lastMessageKey.remoteJid]); | ||
const keyValue = mutationKeys(key.keyData.keyData); | ||
const keyValue = mutationKeys(key.keyData); | ||
const encValue = crypto_1.aesEncrypt(encoded, keyValue.valueEncryptionKey); | ||
const macValue = generateMac(1, encValue, key.keyId.keyId, keyValue.valueMacKey); | ||
const macValue = generateMac(1, encValue, encKeyId, keyValue.valueMacKey); | ||
const indexMacValue = crypto_1.hmacSign(Buffer.from(index), keyValue.indexKey); | ||
@@ -112,3 +117,3 @@ const type = 'regular_high'; | ||
snapshotMac: snapshotMac, | ||
keyId: { id: key.keyId.keyId }, | ||
keyId: { id: encKeyId }, | ||
mutations: [ | ||
@@ -124,3 +129,3 @@ { | ||
}, | ||
keyId: { id: key.keyId.keyId } | ||
keyId: { id: encKeyId } | ||
} | ||
@@ -133,14 +138,13 @@ } | ||
exports.encodeSyncdPatch = encodeSyncdPatch; | ||
const decodeSyncdPatch = (msg, { creds }) => { | ||
const decodeSyncdPatch = async (msg, { keys }) => { | ||
const keyCache = {}; | ||
const getKey = (keyId) => { | ||
var _a; | ||
const getKey = async (keyId) => { | ||
const base64Key = Buffer.from(keyId).toString('base64'); | ||
let key = keyCache[base64Key]; | ||
if (!key) { | ||
const keyEnc = (_a = creds.appStateSyncKeys) === null || _a === void 0 ? void 0 : _a.find(k => (k.keyId.keyId === base64Key)); | ||
const keyEnc = await keys.getAppStateSyncKey(base64Key); | ||
if (!keyEnc) { | ||
throw new boom_1.Boom(`failed to find key "${base64Key}" to decode mutation`, { statusCode: 500, data: msg }); | ||
} | ||
const result = mutationKeys(keyEnc.keyData.keyData); | ||
const result = mutationKeys(keyEnc.keyData); | ||
keyCache[base64Key] = result; | ||
@@ -164,3 +168,3 @@ key = result; | ||
try { | ||
const key = getKey(record.keyId.id); | ||
const key = await getKey(record.keyId.id); | ||
const content = Buffer.from(record.value.blob); | ||
@@ -167,0 +171,0 @@ const encContent = content.slice(0, -32); |
@@ -22,5 +22,5 @@ /// <reference types="node" /> | ||
export declare function sha256(buffer: Buffer): Buffer; | ||
export declare function hkdf(buffer: Buffer, expandedLength: number, { info, salt }: { | ||
export declare function hkdf(buffer: Uint8Array, expandedLength: number, { info, salt }: { | ||
salt?: Buffer; | ||
info?: string; | ||
}): Buffer; |
@@ -0,1 +1,2 @@ | ||
import { proto } from '../../WAProto'; | ||
import type { AuthenticationState, SocketConfig, SignalKeyStore, AuthenticationCreds, KeyPair } from "../Types"; | ||
@@ -5,3 +6,3 @@ import { BinaryNode } from '../WABinary'; | ||
export declare const generateRegistrationNode: ({ registrationId, signedPreKey, signedIdentityKey }: Pick<AuthenticationCreds, 'registrationId' | 'signedPreKey' | 'signedIdentityKey'>, config: Pick<SocketConfig, 'version' | 'browser'>) => Uint8Array; | ||
export declare const initInMemoryKeyStore: ({ preKeys, sessions, senderKeys }?: { | ||
export declare const initInMemoryKeyStore: ({ preKeys, sessions, senderKeys, appStateSyncKeys }?: { | ||
preKeys?: { | ||
@@ -16,2 +17,5 @@ [k: number]: KeyPair; | ||
}; | ||
appStateSyncKeys?: { | ||
[k: string]: proto.IAppStateSyncKeyData; | ||
}; | ||
}) => SignalKeyStore; | ||
@@ -18,0 +22,0 @@ export declare const initAuthState: () => AuthenticationState; |
@@ -78,6 +78,7 @@ "use strict"; | ||
exports.generateRegistrationNode = generateRegistrationNode; | ||
const initInMemoryKeyStore = ({ preKeys, sessions, senderKeys } = {}) => { | ||
const initInMemoryKeyStore = ({ preKeys, sessions, senderKeys, appStateSyncKeys } = {}) => { | ||
preKeys = preKeys || {}; | ||
sessions = sessions || {}; | ||
senderKeys = senderKeys || {}; | ||
appStateSyncKeys = appStateSyncKeys || {}; | ||
return { | ||
@@ -87,2 +88,3 @@ preKeys, | ||
senderKeys, | ||
appStateSyncKeys, | ||
getPreKey: keyId => preKeys[keyId], | ||
@@ -110,2 +112,14 @@ setPreKey: (keyId, pair) => { | ||
delete senderKeys[id]; | ||
}, | ||
getAppStateSyncKey: id => { | ||
const obj = appStateSyncKeys[id]; | ||
if (obj) { | ||
return WAProto_1.proto.AppStateSyncKeyData.fromObject(obj); | ||
} | ||
}, | ||
setAppStateSyncKey: (id, item) => { | ||
if (item) | ||
appStateSyncKeys[id] = item; | ||
else | ||
delete appStateSyncKeys[id]; | ||
} | ||
@@ -112,0 +126,0 @@ }; |
{ | ||
"name": "md-wa", | ||
"version": "1.4.1", | ||
"version": "2.0.0", | ||
"description": "WhatsApp Multi-Device API", | ||
@@ -33,3 +33,3 @@ "homepage": "https://github.com/adiwajshing/Baileys", | ||
"repository": { | ||
"url": "git+ssh://git@github.com/adiwajshing/baileys.git" | ||
"url": "git@github.com:adiwajshing/baileys.git" | ||
}, | ||
@@ -47,3 +47,2 @@ "dependencies": { | ||
"optionalDependencies": { | ||
"@adiwajshing/keyed-db": "^0.2.4", | ||
"qrcode-terminal": "^0.12.0" | ||
@@ -58,3 +57,2 @@ }, | ||
"devDependencies": { | ||
"@adiwajshing/keyed-db": "^0.2.4", | ||
"@types/got": "^9.6.11", | ||
@@ -71,9 +69,3 @@ "@types/jest": "^26.0.24", | ||
"typescript": "^4.0.0" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/adiwajshing/baileys/issues" | ||
}, | ||
"directories": { | ||
"lib": "lib" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
7048309
9
11
175
141661