Assert of single key

This commit is contained in:
Jaco Greeff
2020-07-02 11:49:54 +02:00
parent a3658de7c2
commit 43da3b50de
2 changed files with 11 additions and 11 deletions
+2 -1
View File
@@ -2,7 +2,8 @@
## 0.55.0-beta.x ## 0.55.0-beta.x
- Adjust react-qr `Scan*` makign `onScan` callbacks required - Better error-handling for invalid FileStore accounts (Thanks to https://github.com/yuhui1208)
- Adjust react-qr `Scan*` making `onScan` callbacks required
- Rework the react-qr display to use functional components - Rework the react-qr display to use functional components
- Bump to `@polkadot/api` 1.21.1 - Bump to `@polkadot/api` 1.21.1
- Bump to `@polkadot/util` 2.15.1 - Bump to `@polkadot/util` 2.15.1
+9 -10
View File
@@ -7,6 +7,7 @@ import { KeyringStore, KeyringJson } from '../types';
import fs from 'fs'; import fs from 'fs';
import mkdirp from 'mkdirp'; import mkdirp from 'mkdirp';
import path from 'path'; import path from 'path';
import { assert } from '@polkadot/util';
// NOTE untested and unused by any known apps, probably broken in various mysterious ways // NOTE untested and unused by any known apps, probably broken in various mysterious ways
export default class FileStore implements KeyringStore { export default class FileStore implements KeyringStore {
@@ -27,18 +28,16 @@ export default class FileStore implements KeyringStore {
.forEach((key): void => { .forEach((key): void => {
const value = this._readKey(key); const value = this._readKey(key);
if (JSON.stringify(value) !== '{}') { value?.address && cb(key, value);
cb(key, value);
}
}); });
} }
public get (key: string, cb: (value: KeyringJson) => void): void { public get (key: string, cb: (value: KeyringJson) => void): void {
const value = this._readKey(key); const value = this._readKey(key);
if (JSON.stringify(value) !== '{}') { assert(value?.address, `Invalid JSON found for ${key}`);
cb(value);
} cb(value);
} }
public remove (key: string, cb?: () => void): void { public remove (key: string, cb?: () => void): void {
@@ -55,15 +54,15 @@ export default class FileStore implements KeyringStore {
return path.join(this.#path, key); return path.join(this.#path, key);
} }
private _readKey (key: string): KeyringJson { private _readKey (key: string): KeyringJson | undefined {
try { try {
return JSON.parse( return JSON.parse(
fs.readFileSync(this._getPath(key)).toString('utf-8') fs.readFileSync(this._getPath(key)).toString('utf-8')
) as KeyringJson; ) as KeyringJson;
} catch (error) { } catch (error) {
console.log('_readKey error:', error); console.error(error);
return {} as KeyringJson;
} }
return undefined;
} }
} }