UNPKG

matterbridge-roborock-vacuum-plugin

Version:
38 lines (37 loc) 1.14 kB
import { randomBytes, randomInt } from 'node:crypto'; import * as CryptoUtils from '../../helper/cryptoHelper.js'; export class MessageContext { endpoint; devices = new Map(); nonce; serializeNonce; constructor(userdata) { this.endpoint = CryptoUtils.md5bin(userdata.rriot.k).subarray(8, 14).toString('base64'); this.nonce = randomInt(1000, 1000000); this.serializeNonce = randomBytes(16); } registerDevice(duid, localKey, pv, nonce) { this.devices.set(duid, { localKey: localKey, protocolVersion: pv, nonce }); } updateNonce(duid, nonce) { const device = this.devices.get(duid); if (device) { device.nonce = nonce; } } getSerializeNonceAsHex() { return this.serializeNonce.toString('hex').toUpperCase(); } getLocalKey(duid) { return this.devices.get(duid)?.localKey; } getProtocolVersion(duid) { return this.devices.get(duid)?.protocolVersion; } getDeviceNonce(duid) { return this.devices.get(duid)?.nonce; } getEndpoint() { return this.endpoint; } }