From 89f604f3243cdae39848827484967be00ff9a4d7 Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Wed, 13 Nov 2019 20:23:25 +0200 Subject: [PATCH] fix: Check if file exists --- .../src/encryption-key-manager.ts | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/daf-sodium-fs/src/encryption-key-manager.ts b/packages/daf-sodium-fs/src/encryption-key-manager.ts index 2ae8b3981..e79acfa4d 100644 --- a/packages/daf-sodium-fs/src/encryption-key-manager.ts +++ b/packages/daf-sodium-fs/src/encryption-key-manager.ts @@ -20,19 +20,21 @@ export class SodiumFsEncryptionKeyManager implements EncryptionKeyManager { private readFromFile(): void { try { - const raw = fs.readFileSync(this.fileName) - const parsed = JSON.parse(raw) - - Object.keys(parsed).forEach(did => { - const i = parsed[did] - this.didKeyPairMap[did] = { - keyType: i.keyType, - privateKeyHex: i.privateKeyHex, - publicKeyHex: i.publicKeyHex, - privateKey: Uint8Array.from(Buffer.from(i.privateKeyHex, 'hex')), - publicKey: Uint8Array.from(Buffer.from(i.publicKeyHex, 'hex')), - } - }) + if (fs.existsSync(this.fileName)) { + const raw = fs.readFileSync(this.fileName) + const parsed = JSON.parse(raw) + + Object.keys(parsed).forEach(did => { + const i = parsed[did] + this.didKeyPairMap[did] = { + keyType: i.keyType, + privateKeyHex: i.privateKeyHex, + publicKeyHex: i.publicKeyHex, + privateKey: Uint8Array.from(Buffer.from(i.privateKeyHex, 'hex')), + publicKey: Uint8Array.from(Buffer.from(i.publicKeyHex, 'hex')), + } + }) + } } catch (e) { debug(e) }