Files
ui/packages/ui-keyring/src/stores/File.ts
T
Jaco 86281dca3a Align tsconfig to allow moduleResolution: nodenext (#728)
* Align tsconfig to allow moduleResolution: nodenext

* Adjust

* Default target flag
2023-03-09 16:53:32 +02:00

69 lines
1.7 KiB
TypeScript

// Copyright 2017-2023 @polkadot/ui-keyring authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { KeyringJson, KeyringStore } from '../types.js';
import mkdirp from 'mkdirp';
import fs from 'node:fs';
import path from 'node:path';
import { assert } from '@polkadot/util';
// NOTE untested and unused by any known apps, probably broken in various mysterious ways
export class FileStore implements KeyringStore {
#path: string;
constructor (path: string) {
if (!fs.existsSync(path)) {
mkdirp.sync(path);
}
this.#path = path;
}
public all (fn: (key: string, value: KeyringJson) => void): void {
fs
.readdirSync(this.#path)
.filter((key): boolean => !['.', '..', '.DS_Store'].includes(key))
.forEach((key): void => {
const value = this._readKey(key);
value?.address && fn(key, value);
});
}
public get (key: string, fn: (value: KeyringJson) => void): void {
const value = this._readKey(key);
assert(value?.address, `Invalid JSON found for ${key}`);
fn(value);
}
public remove (key: string, fn?: () => void): void {
fs.unlinkSync(this._getPath(key));
fn && fn();
}
public set (key: string, value: KeyringJson, fn?: () => void): void {
fs.writeFileSync(this._getPath(key), Buffer.from(JSON.stringify(value), 'utf-8'));
fn && fn();
}
private _getPath (key: string): string {
return path.join(this.#path, key);
}
private _readKey (key: string): KeyringJson | undefined {
try {
return JSON.parse(
fs.readFileSync(this._getPath(key)).toString('utf-8')
) as KeyringJson;
} catch (error) {
console.error(error);
}
return undefined;
}
}