Files
Baileys/src/Signal/Group/sender-chain-key.ts
João Lucas de Oliveira Lopes 482db6edc5 refactor: migrate WASignalGroup to TypeScript and remove deprecated files (#1320)
* refactor: migrate WASignalGroup to TypeScript and remove deprecated files

* chore: remove WASignalGroup JavaScript files from package.json

* refactor: update SenderKeyStore and SenderKeyStateStructure interfaces to export and add deserialize method. Fix types

* chore: lint issue

* refactor: improve constructor type checking and getSeed method in SenderChainKey

* refactor: update key handling to use Buffer for improved consistency

* signal: consistent naming and dir structure + add some missing types

* fix: lint issues

---------

Co-authored-by: Rajeh Taher <rajeh@reforward.dev>
2025-06-21 18:02:15 +03:00

39 lines
1.1 KiB
TypeScript

import { calculateMAC } from 'libsignal/src/crypto'
import { SenderMessageKey } from './sender-message-key'
export class SenderChainKey {
private readonly MESSAGE_KEY_SEED: Uint8Array = Buffer.from([0x01])
private readonly CHAIN_KEY_SEED: Uint8Array = Buffer.from([0x02])
private readonly iteration: number
private readonly chainKey: Buffer
constructor(iteration: number, chainKey: any) {
this.iteration = iteration
if (chainKey instanceof Buffer) {
this.chainKey = chainKey
} else {
this.chainKey = Buffer.from(chainKey || [])
}
}
public getIteration(): number {
return this.iteration
}
public getSenderMessageKey(): SenderMessageKey {
return new SenderMessageKey(this.iteration, this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey))
}
public getNext(): SenderChainKey {
return new SenderChainKey(this.iteration + 1, this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey))
}
public getSeed(): Uint8Array {
return this.chainKey
}
private getDerivative(seed: Uint8Array, key: Buffer): Uint8Array {
return calculateMAC(key, seed)
}
}