zwave: network key base64 to hex data migration

This commit is contained in:
Koushik Dutta
2022-01-17 20:57:45 -08:00
parent 98ab9ab493
commit de39155605
2 changed files with 34 additions and 1 deletions

25
plugins/zwave/src/hex.ts Normal file
View File

@@ -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;
}

View File

@@ -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);