account-store.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2017-2020 @polkadot/apps authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { app } from 'electron';
  5. import { FileStore } from '@polkadot/ui-keyring/stores';
  6. import { KeyringJson } from '@polkadot/ui-keyring/types';
  7. import path from 'path';
  8. import { IpcMainHandler } from './ipc-main-handler';
  9. import { registerIpcHandler } from './register-ipc-handler';
  10. const ACCOUNTS_SUBFOLDER = 'polkadot-accounts';
  11. export const accountStoreIpcHandler = (fileStore: FileStore): IpcMainHandler => ({
  12. 'account-store-all': () => {
  13. let result: { key: string, value: KeyringJson }[] = [];
  14. const collect = (key: string, value: KeyringJson) => {
  15. result = [...result, { key, value }];
  16. };
  17. fileStore.all(collect);
  18. return result;
  19. },
  20. 'account-store-get': async (key: string) => new Promise((resolve) => {
  21. try {
  22. fileStore.get(key, resolve);
  23. } catch (err) {
  24. resolve(null);
  25. }
  26. }),
  27. 'account-store-remove': async (key: string) => new Promise((resolve) =>
  28. fileStore.remove(key, resolve)
  29. ),
  30. 'account-store-set': async (key: string, value: KeyringJson) => new Promise((resolve) =>
  31. fileStore.set(key, value, resolve)
  32. )
  33. });
  34. export const registerAccountStoreHandlers = (): void => {
  35. const defaultStorePath = path.join(app.getPath('userData'), ACCOUNTS_SUBFOLDER);
  36. const fileStore = new FileStore(defaultStorePath);
  37. registerIpcHandler(accountStoreIpcHandler(fileStore));
  38. };