feat: implement identity key change detection for Signal protocol
This implements the functionality from WhiskeySockets/Baileys PR #2307 with enhanced improvements: ## Core Features - Extract identity key from PreKeyWhisperMessage before decryption - Detect when a contact reinstalls WhatsApp (identity key changes) - Automatically delete old session and recreate on identity change - Trust On First Use (TOFU) for new contacts ## Improvements over original PR - LRU cache for identity keys (1000 keys, 30min TTL) - Integration with existing Circuit Breaker (reset on identity change) - New Prometheus metrics for observability: - signal_identity_changes_total (new/changed) - signal_mac_errors_total - signal_session_recreations_total - signal_identity_key_cache_hits/misses - signal_identity_key_operations_ms (histogram) - New 'identity.changed' event for applications to notify users - RetryReason enum with MAC_ERROR_CODES and SESSION_ERROR_CODES - Robust protobuf parsing with validation - Structured logging with fingerprints ## Technical Details - Manual protobuf parsing (compatible with any Signal implementation) - SHA-256 fingerprints for key identification - Atomic session deletion + identity key save - Best-effort identity tracking (doesn't fail decryption on errors) Resolves permanent MAC errors when contacts reinstall WhatsApp. https://claude.ai/code/session_01SWAcNuGZQmEKyBPYkBhVHg
This commit is contained in:
@@ -81,6 +81,8 @@ export type SignalDataTypeMap = {
|
||||
'lid-mapping': string
|
||||
'device-list': string[]
|
||||
tctoken: { token: Buffer; timestamp?: string }
|
||||
/** Identity key for Signal Protocol - used for detecting contact reinstalls */
|
||||
'identity-key': Uint8Array
|
||||
}
|
||||
|
||||
export type SignalDataSet = { [T in keyof SignalDataTypeMap]?: { [id: string]: SignalDataTypeMap[T] | null } }
|
||||
|
||||
@@ -134,6 +134,25 @@ export type BaileysEventMap = {
|
||||
setting: 'channelsPersonalisedRecommendation'
|
||||
value: proto.SyncActionValue.IPrivacySettingChannelsPersonalisedRecommendationAction
|
||||
}
|
||||
|
||||
/**
|
||||
* Emitted when a contact's Signal identity key changes.
|
||||
* This typically indicates the contact reinstalled WhatsApp or switched devices.
|
||||
* Applications can use this to notify users about the security code change,
|
||||
* similar to the "Security code changed" notification in the official WhatsApp client.
|
||||
*/
|
||||
'identity.changed': {
|
||||
/** JID of the contact whose identity key changed */
|
||||
jid: string
|
||||
/** SHA-256 fingerprint of the previous identity key (hex string) */
|
||||
previousKeyFingerprint: string | null
|
||||
/** SHA-256 fingerprint of the new identity key (hex string) */
|
||||
newKeyFingerprint: string
|
||||
/** Timestamp when the change was detected */
|
||||
timestamp: number
|
||||
/** Whether this is a new contact (true) or an existing contact with changed key (false) */
|
||||
isNewContact: boolean
|
||||
}
|
||||
}
|
||||
|
||||
export type BufferedEventData = {
|
||||
|
||||
Reference in New Issue
Block a user