diff --git a/plugins/zwave/src/hex.ts b/plugins/zwave/src/hex.ts new file mode 100644 index 000000000..218302e75 --- /dev/null +++ b/plugins/zwave/src/hex.ts @@ -0,0 +1,25 @@ +export function isHex(s: string) { + for (const c of s) { + switch (c.toLowerCase()) { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + break; + default: return false; + } + } + return true; +} diff --git a/plugins/zwave/src/main.ts b/plugins/zwave/src/main.ts index 65657587a..8a1347158 100644 --- a/plugins/zwave/src/main.ts +++ b/plugins/zwave/src/main.ts @@ -8,6 +8,7 @@ import { Driver, Endpoint, ZWaveController, ZWaveNode, CommandClass } from "zwav import { ValueID, CommandClasses } from "@zwave-js/core" import { randomBytes } from "crypto"; import path from "path"; +import { isHex } from "./hex"; const { log, deviceManager } = sdk; @@ -53,7 +54,14 @@ export class ZwaveControllerProvider extends ScryptedDeviceBase implements Devic let s2AccessControlKey = this.storage.getItem('s2AccessControlKey'); let s2AuthenticatedKey = this.storage.getItem('s2AuthenticatedKey'); let s2UnauthenticatedKey = this.storage.getItem('s2UnauthenticatedKey'); - + + // 1/17/2022: the network key was stored as base64, but for consistency with HA + // and others, it was switched to hex. this is the data migration. + if (!isHex(networkKey)) { + networkKey = Buffer.from(networkKey, 'base64').toString('hex'); + this.storage.setItem('networkKey', networkKey); + } + if (!networkKey) { networkKey = randomBytes(16).toString('hex').toUpperCase(); this.storage.setItem('networkKey', networkKey);