Add ExtensionStore (from @polkadot/extension) (#142)

* Add ExtensionStore (from @polkadot/extension)

* Consistent naming, untested/unused File store

* Update File.ts
This commit is contained in:
Jaco Greeff
2019-06-06 18:13:28 +02:00
committed by GitHub
parent abb94d254c
commit da84d5a8ae
9 changed files with 163 additions and 8 deletions
@@ -6,7 +6,7 @@ import { KeyringStore } from '../types';
import store from 'store';
export default class Store implements KeyringStore {
export default class BrowserStore implements KeyringStore {
all (cb: (key: string, value: any) => void): void {
store.each((value: any, key: string) =>
cb(key, value)
@@ -0,0 +1,58 @@
// Copyright 2019 @polkadot/ui-keyring authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringStore } from '../types';
import extension from 'extensionizer';
const lastError = (type: string): void => {
const error = extension.runtime.lastError;
if (error) {
console.error(`ExtensionStore.${type}:: runtime.lastError:`, error);
}
};
export default class ExtensionStore implements KeyringStore {
all (cb: (key: string, value: any) => void): void {
extension.storage.local.get(null, (result: { [index: string]: any }) => {
lastError('all');
Object.entries(result).forEach(([key, value]) =>
cb(key, value)
);
});
}
get (key: string, cb: (value: any) => void): void {
extension.storage.local.get([key], (result: { [index: string]: any }) => {
lastError('get');
cb(result[key]);
});
}
remove (key: string, cb?: () => void): void {
extension.storage.local.remove(key, () => {
lastError('remove');
cb && cb();
});
}
set (key: string, value: any, cb?: () => void): void {
// shortcut, don't save testing accounts in extension storage
if (key.indexOf('account:') === 0 && value.meta && value.meta.isTesting) {
cb && cb();
return;
}
extension.storage.local.set({ [key]: value }, () => {
lastError('set');
cb && cb();
});
}
}
+53
View File
@@ -0,0 +1,53 @@
// Copyright 2017-2019 @polkadot/ui-keyring authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringStore } from '../types';
import fs from 'fs';
import mkdirp from 'mkdirp';
import path from 'path';
// NOTE untested and unused by any known apps, probably broken in various mysterious ways
export default class FileStore implements KeyringStore {
private _path: string;
constructor (path: string) {
if (!fs.existsSync(path)) {
mkdirp.sync(path);
}
this._path = path;
}
all (cb: (key: string, value: any) => void): void {
fs
.readdirSync(this._path)
.filter((key) => !['.', '..'].includes(key))
.forEach((key) => cb(key, this._readKey(key)));
}
get (key: string, cb: (value: any) => void): void {
cb(this._readKey(key));
}
remove (key: string, cb?: () => void): void {
fs.unlinkSync(this._getPath(key));
cb && cb();
}
set (key: string, value: any, cb?: () => void): void {
fs.writeFileSync(this._getPath(key), Buffer.from(JSON.stringify(value), 'utf-8'));
cb && cb();
}
private _getPath (key: string): string {
return path.join(this._path, key);
}
private _readKey (key: string): any {
return JSON.parse(
fs.readFileSync(this._getPath(key)).toString('utf-8')
);
}
}
+7
View File
@@ -0,0 +1,7 @@
// Copyright 2017-2019 @polkadot/ui-keyring authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export { default as BrowserStore } from './Browser';
export { default as ExtensionStore } from './Extension';
export { default as FileStore } from './File';