UNPKG

nubli

Version:
127 lines (126 loc) 4.89 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const credentials_1 = require("./credentials"); const libsodium_wrappers_1 = __importDefault(require("libsodium-wrappers")); const fs_1 = __importDefault(require("fs")); const array_buffer_to_hex_1 = __importDefault(require("array-buffer-to-hex")); const hex_to_array_buffer_1 = __importDefault(require("hex-to-array-buffer")); class NukiConfig { constructor(uuid, paired = false, credentials, appId, authorizationId, slUUID) { this._authorizationId = undefined; this._slUUID = undefined; if (credentials == null) { credentials = new credentials_1.Credentials(); } if (appId == null) { appId = new Buffer(libsodium_wrappers_1.default.randombytes_buf(4)).readUInt32LE(0); } this._uuid = uuid; this._paired = paired; this._credentials = credentials; this._appId = appId; this._authorizationId = authorizationId; this._slUUID = slUUID; } serialize() { return { uuid: this._uuid, paired: this._paired, credentials: this.credentials.serialize(), appId: this._appId, authorizationId: this._authorizationId, slUUID: array_buffer_to_hex_1.default(this._slUUID) }; } get uuid() { return this._uuid; } get paired() { return this._paired; } set paired(paired) { this._paired = paired; } get credentials() { return this._credentials; } get appId() { return this._appId; } get authorizationId() { if (this._authorizationId == undefined) { throw new Error("Authorization ID not set"); } return this._authorizationId; } set authorizationId(authId) { if (this._authorizationId) { throw new Error("Authorization ID cannot be overwritten."); } this._authorizationId = authId; } get slUUID() { if (this._slUUID == undefined) { throw new Error("Authorization ID not set"); } return this._slUUID; } set slUUID(slUUID) { if (this._slUUID) { throw new Error("Smart Lock UUID cannot be overwritten."); } this._slUUID = slUUID; } static readConfig(uuid, path) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { fs_1.default.readFile(path + uuid + ".json", (err, buf) => { if (err) { reject(err); } else { let data = JSON.parse(buf.toString()); let credentials = new credentials_1.Credentials(new Uint8Array(hex_to_array_buffer_1.default(data.credentials.publicKey)), new Uint8Array(hex_to_array_buffer_1.default(data.credentials.privateKey)), Buffer.from(data.credentials.slPublicKey, 'hex'), Buffer.from(data.credentials.sharedSecret, 'hex')); let nukiConfig = new NukiConfig(data.uuid, data.paired, credentials, data.appId, data.authorizationId, new Uint8Array(hex_to_array_buffer_1.default(data.slUUID))); resolve(nukiConfig); } }); }); }); } static configExists(uuid, path) { try { fs_1.default.accessSync(path + uuid + ".json", fs_1.default.constants.R_OK); return true; } catch (e) { return false; } } save(path) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { fs_1.default.writeFile(path + this.uuid + ".json", JSON.stringify(this.serialize()), (err) => { if (err) { reject(err); } else { resolve(); } }); }); }); } } exports.NukiConfig = NukiConfig;