@pezkuwi/ui-keyring 3.16.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. package/README.md +136 -0
  2. package/build/Base.d.ts +30 -0
  3. package/build/Base.js +109 -0
  4. package/build/Keyring.d.ts +49 -0
  5. package/build/Keyring.js +304 -0
  6. package/build/LICENSE +201 -0
  7. package/build/README.md +136 -0
  8. package/build/bundle-pezkuwi-ui-keyring.js +2751 -0
  9. package/build/bundle.d.ts +4 -0
  10. package/build/bundle.js +4 -0
  11. package/build/cjs/Base.d.ts +30 -0
  12. package/build/cjs/Base.js +113 -0
  13. package/build/cjs/Keyring.d.ts +49 -0
  14. package/build/cjs/Keyring.js +308 -0
  15. package/build/cjs/bundle.d.ts +4 -0
  16. package/build/cjs/bundle.js +8 -0
  17. package/build/cjs/defaults.d.ts +6 -0
  18. package/build/cjs/defaults.js +28 -0
  19. package/build/cjs/index.d.ts +4 -0
  20. package/build/cjs/index.js +7 -0
  21. package/build/cjs/observable/accounts.d.ts +1 -0
  22. package/build/cjs/observable/accounts.js +6 -0
  23. package/build/cjs/observable/addresses.d.ts +1 -0
  24. package/build/cjs/observable/addresses.js +6 -0
  25. package/build/cjs/observable/contracts.d.ts +1 -0
  26. package/build/cjs/observable/contracts.js +6 -0
  27. package/build/cjs/observable/env.d.ts +6 -0
  28. package/build/cjs/observable/env.js +12 -0
  29. package/build/cjs/observable/genericSubject.d.ts +2 -0
  30. package/build/cjs/observable/genericSubject.js +47 -0
  31. package/build/cjs/observable/index.d.ts +8 -0
  32. package/build/cjs/observable/index.js +16 -0
  33. package/build/cjs/observable/types.d.ts +15 -0
  34. package/build/cjs/observable/types.js +2 -0
  35. package/build/cjs/options/index.d.ts +15 -0
  36. package/build/cjs/options/index.js +116 -0
  37. package/build/cjs/options/item.d.ts +2 -0
  38. package/build/cjs/options/item.js +16 -0
  39. package/build/cjs/options/types.d.ts +15 -0
  40. package/build/cjs/options/types.js +2 -0
  41. package/build/cjs/package.json +3 -0
  42. package/build/cjs/packageDetect.d.ts +1 -0
  43. package/build/cjs/packageDetect.js +6 -0
  44. package/build/cjs/packageInfo.d.ts +6 -0
  45. package/build/cjs/packageInfo.js +4 -0
  46. package/build/cjs/stores/Browser.d.ts +7 -0
  47. package/build/cjs/stores/Browser.js +24 -0
  48. package/build/cjs/stores/File.d.ts +12 -0
  49. package/build/cjs/stores/File.js +72 -0
  50. package/build/cjs/stores/index.d.ts +2 -0
  51. package/build/cjs/stores/index.js +7 -0
  52. package/build/cjs/types.d.ts +78 -0
  53. package/build/cjs/types.js +2 -0
  54. package/build/defaults.d.ts +6 -0
  55. package/build/defaults.js +22 -0
  56. package/build/index.d.ts +4 -0
  57. package/build/index.js +4 -0
  58. package/build/observable/accounts.d.ts +1 -0
  59. package/build/observable/accounts.js +3 -0
  60. package/build/observable/addresses.d.ts +1 -0
  61. package/build/observable/addresses.js +3 -0
  62. package/build/observable/contracts.d.ts +1 -0
  63. package/build/observable/contracts.js +3 -0
  64. package/build/observable/env.d.ts +6 -0
  65. package/build/observable/env.js +9 -0
  66. package/build/observable/genericSubject.d.ts +2 -0
  67. package/build/observable/genericSubject.js +44 -0
  68. package/build/observable/index.d.ts +8 -0
  69. package/build/observable/index.js +13 -0
  70. package/build/observable/types.d.ts +15 -0
  71. package/build/observable/types.js +1 -0
  72. package/build/options/index.d.ts +15 -0
  73. package/build/options/index.js +112 -0
  74. package/build/options/item.d.ts +2 -0
  75. package/build/options/item.js +13 -0
  76. package/build/options/types.d.ts +15 -0
  77. package/build/options/types.js +1 -0
  78. package/build/package.json +355 -0
  79. package/build/packageDetect.d.ts +1 -0
  80. package/build/packageDetect.js +4 -0
  81. package/build/packageInfo.d.ts +6 -0
  82. package/build/packageInfo.js +1 -0
  83. package/build/stores/Browser.d.ts +7 -0
  84. package/build/stores/Browser.js +19 -0
  85. package/build/stores/File.d.ts +12 -0
  86. package/build/stores/File.js +67 -0
  87. package/build/stores/index.d.ts +2 -0
  88. package/build/stores/index.js +2 -0
  89. package/build/types.d.ts +78 -0
  90. package/build/types.js +1 -0
  91. package/build-tsc/Base.d.ts +30 -0
  92. package/build-tsc/Keyring.d.ts +49 -0
  93. package/build-tsc/bundle.d.ts +4 -0
  94. package/build-tsc/defaults.d.ts +6 -0
  95. package/build-tsc/index.d.ts +4 -0
  96. package/build-tsc/observable/accounts.d.ts +1 -0
  97. package/build-tsc/observable/addresses.d.ts +1 -0
  98. package/build-tsc/observable/contracts.d.ts +1 -0
  99. package/build-tsc/observable/env.d.ts +6 -0
  100. package/build-tsc/observable/genericSubject.d.ts +2 -0
  101. package/build-tsc/observable/index.d.ts +8 -0
  102. package/build-tsc/observable/types.d.ts +15 -0
  103. package/build-tsc/options/index.d.ts +15 -0
  104. package/build-tsc/options/item.d.ts +2 -0
  105. package/build-tsc/options/types.d.ts +15 -0
  106. package/build-tsc/packageDetect.d.ts +1 -0
  107. package/build-tsc/packageInfo.d.ts +6 -0
  108. package/build-tsc/stores/Browser.d.ts +7 -0
  109. package/build-tsc/stores/File.d.ts +12 -0
  110. package/build-tsc/stores/index.d.ts +2 -0
  111. package/build-tsc/types.d.ts +78 -0
  112. package/build-tsc-cjs/Base.js +113 -0
  113. package/build-tsc-cjs/Keyring.js +308 -0
  114. package/build-tsc-cjs/bundle.js +8 -0
  115. package/build-tsc-cjs/defaults.js +28 -0
  116. package/build-tsc-cjs/index.js +7 -0
  117. package/build-tsc-cjs/observable/accounts.js +6 -0
  118. package/build-tsc-cjs/observable/addresses.js +6 -0
  119. package/build-tsc-cjs/observable/contracts.js +6 -0
  120. package/build-tsc-cjs/observable/env.js +12 -0
  121. package/build-tsc-cjs/observable/genericSubject.js +47 -0
  122. package/build-tsc-cjs/observable/index.js +16 -0
  123. package/build-tsc-cjs/observable/types.js +2 -0
  124. package/build-tsc-cjs/options/index.js +116 -0
  125. package/build-tsc-cjs/options/item.js +16 -0
  126. package/build-tsc-cjs/options/types.js +2 -0
  127. package/build-tsc-cjs/packageDetect.js +6 -0
  128. package/build-tsc-cjs/packageInfo.js +4 -0
  129. package/build-tsc-cjs/stores/Browser.js +24 -0
  130. package/build-tsc-cjs/stores/File.js +72 -0
  131. package/build-tsc-cjs/stores/index.js +7 -0
  132. package/build-tsc-cjs/types.js +2 -0
  133. package/build-tsc-esm/Base.js +109 -0
  134. package/build-tsc-esm/Keyring.js +304 -0
  135. package/build-tsc-esm/bundle.js +4 -0
  136. package/build-tsc-esm/defaults.js +22 -0
  137. package/build-tsc-esm/index.js +4 -0
  138. package/build-tsc-esm/observable/accounts.js +3 -0
  139. package/build-tsc-esm/observable/addresses.js +3 -0
  140. package/build-tsc-esm/observable/contracts.js +3 -0
  141. package/build-tsc-esm/observable/env.js +9 -0
  142. package/build-tsc-esm/observable/genericSubject.js +44 -0
  143. package/build-tsc-esm/observable/index.js +13 -0
  144. package/build-tsc-esm/observable/types.js +1 -0
  145. package/build-tsc-esm/options/index.js +112 -0
  146. package/build-tsc-esm/options/item.js +13 -0
  147. package/build-tsc-esm/options/types.js +1 -0
  148. package/build-tsc-esm/packageDetect.js +4 -0
  149. package/build-tsc-esm/packageInfo.js +1 -0
  150. package/build-tsc-esm/stores/Browser.js +19 -0
  151. package/build-tsc-esm/stores/File.js +67 -0
  152. package/build-tsc-esm/stores/index.js +2 -0
  153. package/build-tsc-esm/types.js +1 -0
  154. package/package.json +41 -0
  155. package/src/Base.ts +156 -0
  156. package/src/Keyring.ts +420 -0
  157. package/src/bundle.ts +10 -0
  158. package/src/defaults.ts +34 -0
  159. package/src/index.ts +10 -0
  160. package/src/json.d.ts +11 -0
  161. package/src/observable/accounts.ts +7 -0
  162. package/src/observable/addresses.ts +7 -0
  163. package/src/observable/contracts.ts +7 -0
  164. package/src/observable/env.ts +15 -0
  165. package/src/observable/genericSubject.ts +66 -0
  166. package/src/observable/index.ts +28 -0
  167. package/src/observable/types.ts +21 -0
  168. package/src/options/index.ts +150 -0
  169. package/src/options/item.ts +22 -0
  170. package/src/options/types.ts +23 -0
  171. package/src/packageDetect.ts +12 -0
  172. package/src/packageInfo.ts +6 -0
  173. package/src/stores/Browser.ts +28 -0
  174. package/src/stores/File.ts +94 -0
  175. package/src/stores/index.ts +5 -0
  176. package/src/types.ts +91 -0
  177. package/tsconfig.build.json +14 -0
  178. package/tsconfig.build.tsbuildinfo +1 -0
  179. package/tsconfig.spec.json +16 -0
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.genericSubject = genericSubject;
4
+ const rxjs_1 = require("rxjs");
5
+ const util_1 = require("@pezkuwi/util");
6
+ const item_js_1 = require("../options/item.js");
7
+ const env_js_1 = require("./env.js");
8
+ function callNext(current, subject, withTest) {
9
+ const isDevMode = env_js_1.env.isDevelopment();
10
+ const filtered = {};
11
+ Object.keys(current).forEach((key) => {
12
+ const { json: { meta: { isTesting = false } = {} } = {} } = current[key];
13
+ if (!withTest || isDevMode || isTesting !== true) {
14
+ filtered[key] = current[key];
15
+ }
16
+ });
17
+ subject.next(filtered);
18
+ }
19
+ function genericSubject(keyCreator, withTest = false) {
20
+ let current = {};
21
+ const subject = new rxjs_1.BehaviorSubject({});
22
+ const next = () => callNext(current, subject, withTest);
23
+ env_js_1.env.subject.subscribe(next);
24
+ return {
25
+ add: (store, address, json, type) => {
26
+ current = (0, util_1.objectCopy)(current);
27
+ current[address] = {
28
+ json: (0, util_1.objectSpread)({}, json, { address }),
29
+ option: (0, item_js_1.createOptionItem)(address, json.meta.name),
30
+ type
31
+ };
32
+ // we do not store dev or injected accounts (external/transient)
33
+ if (!json.meta.isInjected && (!json.meta.isTesting || env_js_1.env.isDevelopment())) {
34
+ store.set(keyCreator(address), json);
35
+ }
36
+ next();
37
+ return current[address];
38
+ },
39
+ remove: (store, address) => {
40
+ current = (0, util_1.objectCopy)(current);
41
+ delete current[address];
42
+ store.remove(keyCreator(address));
43
+ next();
44
+ },
45
+ subject
46
+ };
47
+ }
@@ -0,0 +1,8 @@
1
+ import type { SubjectInfo } from './types.js';
2
+ interface Result {
3
+ accounts: SubjectInfo;
4
+ addresses: SubjectInfo;
5
+ contracts: SubjectInfo;
6
+ }
7
+ export declare const obervableAll: import("rxjs").Observable<Result>;
8
+ export {};
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.obervableAll = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ const accounts_js_1 = require("./accounts.js");
6
+ const addresses_js_1 = require("./addresses.js");
7
+ const contracts_js_1 = require("./contracts.js");
8
+ exports.obervableAll = (0, rxjs_1.combineLatest)([
9
+ accounts_js_1.accounts.subject,
10
+ addresses_js_1.addresses.subject,
11
+ contracts_js_1.contracts.subject
12
+ ]).pipe((0, rxjs_1.map)(([accounts, addresses, contracts]) => ({
13
+ accounts,
14
+ addresses,
15
+ contracts
16
+ })));
@@ -0,0 +1,15 @@
1
+ import type { BehaviorSubject } from 'rxjs';
2
+ import type { KeypairType } from '@pezkuwi/util-crypto/types';
3
+ import type { KeyringSectionOption } from '../options/types.js';
4
+ import type { KeyringJson, KeyringStore } from '../types.js';
5
+ export interface SingleAddress {
6
+ json: KeyringJson;
7
+ option: KeyringSectionOption;
8
+ type?: KeypairType | undefined;
9
+ }
10
+ export type SubjectInfo = Record<string, SingleAddress>;
11
+ export interface AddressSubject {
12
+ add: (store: KeyringStore, address: string, json: KeyringJson, type?: KeypairType) => SingleAddress;
13
+ remove: (store: KeyringStore, address: string) => void;
14
+ subject: BehaviorSubject<SubjectInfo>;
15
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,15 @@
1
+ import type { KeyringStruct } from '../types.js';
2
+ import type { KeyringOptionInstance, KeyringOptions, KeyringSectionOption } from './types.js';
3
+ import { BehaviorSubject } from 'rxjs';
4
+ export declare class KeyringOption implements KeyringOptionInstance {
5
+ #private;
6
+ readonly optionsSubject: BehaviorSubject<KeyringOptions>;
7
+ createOptionHeader(name: string): KeyringSectionOption;
8
+ init(keyring: KeyringStruct): void;
9
+ clear(): void;
10
+ private linkItems;
11
+ private addAccounts;
12
+ private addAddresses;
13
+ private addContracts;
14
+ private emptyOptions;
15
+ }
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KeyringOption = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ const index_js_1 = require("../observable/index.js");
6
+ let hasCalledInitOptions = false;
7
+ const sortByName = (a, b) => {
8
+ const valueA = a.option.name;
9
+ const valueB = b.option.name;
10
+ return valueA.localeCompare(valueB);
11
+ };
12
+ const sortByCreated = (a, b) => {
13
+ const valueA = a.json.meta.whenCreated || 0;
14
+ const valueB = b.json.meta.whenCreated || 0;
15
+ if (valueA < valueB) {
16
+ return 1;
17
+ }
18
+ if (valueA > valueB) {
19
+ return -1;
20
+ }
21
+ return 0;
22
+ };
23
+ class KeyringOption {
24
+ #allSub = null;
25
+ optionsSubject = new rxjs_1.BehaviorSubject(this.emptyOptions());
26
+ createOptionHeader(name) {
27
+ return {
28
+ key: `header-${name.toLowerCase()}`,
29
+ name,
30
+ value: null
31
+ };
32
+ }
33
+ init(keyring) {
34
+ if (hasCalledInitOptions) {
35
+ throw new Error('Unable to initialise options more than once');
36
+ }
37
+ this.#allSub = index_js_1.obervableAll.subscribe(() => {
38
+ const opts = this.emptyOptions();
39
+ this.addAccounts(keyring, opts);
40
+ this.addAddresses(keyring, opts);
41
+ this.addContracts(keyring, opts);
42
+ opts.address = this.linkItems({ Addresses: opts.address, Recent: opts.recent });
43
+ opts.account = this.linkItems({ Accounts: opts.account, Development: opts.testing });
44
+ opts.contract = this.linkItems({ Contracts: opts.contract });
45
+ opts.all = [].concat(opts.account, opts.address);
46
+ opts.allPlus = [].concat(opts.account, opts.address, opts.contract);
47
+ this.optionsSubject.next(opts);
48
+ });
49
+ hasCalledInitOptions = true;
50
+ }
51
+ clear() {
52
+ if (this.#allSub) {
53
+ this.#allSub.unsubscribe();
54
+ }
55
+ }
56
+ linkItems(items) {
57
+ return Object.keys(items).reduce((result, header) => {
58
+ const options = items[header];
59
+ return result.concat(options.length
60
+ ? [this.createOptionHeader(header)]
61
+ : [], options);
62
+ }, []);
63
+ }
64
+ addAccounts(keyring, options) {
65
+ const available = keyring.accounts.subject.getValue();
66
+ Object
67
+ .values(available)
68
+ .sort(sortByName)
69
+ .forEach(({ json: { meta: { isTesting = false } }, option }) => {
70
+ if (!isTesting) {
71
+ options.account.push(option);
72
+ }
73
+ else {
74
+ options.testing.push(option);
75
+ }
76
+ });
77
+ }
78
+ addAddresses(keyring, options) {
79
+ const available = keyring.addresses.subject.getValue();
80
+ Object
81
+ .values(available)
82
+ .filter(({ json }) => !!json.meta.isRecent)
83
+ .sort(sortByCreated)
84
+ .forEach(({ option }) => {
85
+ options.recent.push(option);
86
+ });
87
+ Object
88
+ .values(available)
89
+ .filter(({ json }) => !json.meta.isRecent)
90
+ .sort(sortByName)
91
+ .forEach(({ option }) => {
92
+ options.address.push(option);
93
+ });
94
+ }
95
+ addContracts(keyring, options) {
96
+ const available = keyring.contracts.subject.getValue();
97
+ Object
98
+ .values(available)
99
+ .sort(sortByName)
100
+ .forEach(({ option }) => {
101
+ options.contract.push(option);
102
+ });
103
+ }
104
+ emptyOptions() {
105
+ return {
106
+ account: [],
107
+ address: [],
108
+ all: [],
109
+ allPlus: [],
110
+ contract: [],
111
+ recent: [],
112
+ testing: []
113
+ };
114
+ }
115
+ }
116
+ exports.KeyringOption = KeyringOption;
@@ -0,0 +1,2 @@
1
+ import type { KeyringSectionOption } from './types.js';
2
+ export declare function createOptionItem(address: string, _name?: string): KeyringSectionOption;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createOptionItem = createOptionItem;
4
+ const util_1 = require("@pezkuwi/util");
5
+ function createOptionItem(address, _name) {
6
+ const name = (0, util_1.isUndefined)(_name)
7
+ ? ((address.length > 15)
8
+ ? `${address.slice(0, 6)}…${address.slice(-6)}`
9
+ : address)
10
+ : _name;
11
+ return {
12
+ key: address,
13
+ name,
14
+ value: address
15
+ };
16
+ }
@@ -0,0 +1,15 @@
1
+ import type { KeyringItemType, KeyringStruct } from '../types.js';
2
+ export interface KeyringSectionOption {
3
+ key: string | null;
4
+ name: string;
5
+ value: string | null;
6
+ }
7
+ export type KeyringSectionOptions = KeyringSectionOption[];
8
+ export type KeyringOptions = {
9
+ [type in KeyringItemType | 'all' | 'allPlus' | 'recent' | 'testing']: KeyringSectionOptions;
10
+ };
11
+ export type KeyringOption$Type = keyof KeyringOptions;
12
+ export interface KeyringOptionInstance {
13
+ createOptionHeader: (name: string) => KeyringSectionOption;
14
+ init: (keyring: KeyringStruct) => void;
15
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const packageInfo_1 = require("@pezkuwi/ui-settings/cjs/packageInfo");
4
+ const util_1 = require("@pezkuwi/util");
5
+ const packageInfo_js_1 = require("./packageInfo.js");
6
+ (0, util_1.detectPackage)(packageInfo_js_1.packageInfo, null, [packageInfo_1.packageInfo]);
@@ -0,0 +1,6 @@
1
+ export declare const packageInfo: {
2
+ name: string;
3
+ path: string;
4
+ type: string;
5
+ version: string;
6
+ };
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.packageInfo = void 0;
4
+ exports.packageInfo = { name: '@pezkuwi/ui-keyring', path: typeof __dirname === 'string' ? __dirname : 'auto', type: 'cjs', version: '3.16.6' };
@@ -0,0 +1,7 @@
1
+ import type { KeyringJson, KeyringStore } from '../types.js';
2
+ export declare class BrowserStore implements KeyringStore {
3
+ all(fn: (key: string, value: KeyringJson) => void): void;
4
+ get(key: string, fn: (value: KeyringJson) => void): void;
5
+ remove(key: string, fn?: () => void): void;
6
+ set(key: string, value: KeyringJson, fn?: () => void): void;
7
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BrowserStore = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const store_1 = tslib_1.__importDefault(require("store"));
6
+ class BrowserStore {
7
+ all(fn) {
8
+ store_1.default.each((value, key) => {
9
+ fn(key, value);
10
+ });
11
+ }
12
+ get(key, fn) {
13
+ fn(store_1.default.get(key));
14
+ }
15
+ remove(key, fn) {
16
+ store_1.default.remove(key);
17
+ fn && fn();
18
+ }
19
+ set(key, value, fn) {
20
+ store_1.default.set(key, value);
21
+ fn && fn();
22
+ }
23
+ }
24
+ exports.BrowserStore = BrowserStore;
@@ -0,0 +1,12 @@
1
+ import type { KeyringJson, KeyringStore } from '../types.js';
2
+ export declare class FileStore implements KeyringStore {
3
+ #private;
4
+ constructor(path: string);
5
+ private validateKey;
6
+ all(fn: (key: string, value: KeyringJson) => void): void;
7
+ get(key: string, fn: (value: KeyringJson) => void): void;
8
+ remove(key: string, fn?: () => void): void;
9
+ set(key: string, value: KeyringJson, fn?: () => void): void;
10
+ private _getPath;
11
+ private _readKey;
12
+ }
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FileStore = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const mkdirp_1 = require("mkdirp");
6
+ const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
7
+ const node_path_1 = tslib_1.__importDefault(require("node:path"));
8
+ class FileStore {
9
+ #path;
10
+ constructor(path) {
11
+ if (!node_fs_1.default.existsSync(path)) {
12
+ (0, mkdirp_1.mkdirpSync)(path);
13
+ }
14
+ this.#path = path;
15
+ }
16
+ validateKey(key) {
17
+ // Make sure the key has a .json extension
18
+ if (!key.endsWith('.json')) {
19
+ console.error('Non-JSON file requested: ', key);
20
+ return false;
21
+ }
22
+ // Remove '.json'
23
+ const keyWithoutExtension = key.slice(0, -5);
24
+ // Only allow alphanumeric characters, hyphens, and underscores in the base filename
25
+ const safeKeyRegex = /^[a-zA-Z0-9_-]+$/;
26
+ if (!safeKeyRegex.test(keyWithoutExtension)) {
27
+ console.error('Invalid key format detected: ', key);
28
+ return false;
29
+ }
30
+ return true;
31
+ }
32
+ all(fn) {
33
+ node_fs_1.default
34
+ .readdirSync(this.#path)
35
+ .filter((key) => !['.', '..', '.DS_Store'].includes(key))
36
+ .forEach((key) => {
37
+ const value = this._readKey(key);
38
+ value?.address && fn(key, value);
39
+ });
40
+ }
41
+ get(key, fn) {
42
+ const value = this._readKey(key);
43
+ if (!value?.address) {
44
+ throw new Error(`Invalid JSON found for ${key}`);
45
+ }
46
+ fn(value);
47
+ }
48
+ remove(key, fn) {
49
+ node_fs_1.default.unlinkSync(this._getPath(key));
50
+ fn && fn();
51
+ }
52
+ set(key, value, fn) {
53
+ node_fs_1.default.writeFileSync(this._getPath(key), Buffer.from(JSON.stringify(value), 'utf-8'));
54
+ fn && fn();
55
+ }
56
+ _getPath(key) {
57
+ if (!this.validateKey(key)) {
58
+ throw new Error('Invalid key format');
59
+ }
60
+ return node_path_1.default.join(this.#path, key);
61
+ }
62
+ _readKey(key) {
63
+ try {
64
+ return JSON.parse(node_fs_1.default.readFileSync(this._getPath(key)).toString('utf-8'));
65
+ }
66
+ catch (error) {
67
+ console.error(error);
68
+ }
69
+ return undefined;
70
+ }
71
+ }
72
+ exports.FileStore = FileStore;
@@ -0,0 +1,2 @@
1
+ export { BrowserStore } from './Browser.js';
2
+ export { FileStore } from './File.js';
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FileStore = exports.BrowserStore = void 0;
4
+ var Browser_js_1 = require("./Browser.js");
5
+ Object.defineProperty(exports, "BrowserStore", { enumerable: true, get: function () { return Browser_js_1.BrowserStore; } });
6
+ var File_js_1 = require("./File.js");
7
+ Object.defineProperty(exports, "FileStore", { enumerable: true, get: function () { return File_js_1.FileStore; } });
@@ -0,0 +1,78 @@
1
+ import type { KeyringInstance as BaseKeyringInstance, KeyringOptions as KeyringOptionsBase, KeyringPair, KeyringPair$Json, KeyringPair$Meta } from '@pezkuwi/keyring/types';
2
+ import type { HexString } from '@pezkuwi/util/types';
3
+ import type { EncryptedJson } from '@pezkuwi/util-crypto/json/types';
4
+ import type { KeypairType } from '@pezkuwi/util-crypto/types';
5
+ import type { AddressSubject, SingleAddress } from './observable/types.js';
6
+ export type ContractMeta = NonNullable<KeyringPair$Meta['contract']>;
7
+ export type KeyringJson$Meta = KeyringPair$Meta;
8
+ export interface KeyringJson {
9
+ address: string;
10
+ meta: KeyringJson$Meta;
11
+ }
12
+ export interface KeyringPairs$Json extends EncryptedJson {
13
+ accounts: KeyringJson[];
14
+ }
15
+ export interface KeyringStore {
16
+ all: (cb: (key: string, value: KeyringJson) => void) => void;
17
+ get: (key: string, cb: (value: KeyringJson) => void) => void;
18
+ remove: (key: string, cb?: () => void) => void;
19
+ set: (key: string, value: KeyringJson, cb?: () => void) => void;
20
+ }
21
+ export interface KeyringOptions extends KeyringOptionsBase {
22
+ filter?: (json: KeyringJson) => boolean;
23
+ genesisHash?: HexString | {
24
+ toHex: () => HexString;
25
+ };
26
+ genesisHashAdd?: HexString[];
27
+ isDevelopment?: boolean;
28
+ store?: KeyringStore;
29
+ }
30
+ export interface KeyringAddress {
31
+ readonly address: string;
32
+ readonly meta: KeyringJson$Meta;
33
+ readonly publicKey: Uint8Array;
34
+ }
35
+ export type KeyringAddressType = 'address' | 'contract';
36
+ export type KeyringItemType = 'account' | KeyringAddressType;
37
+ export interface CreateResult {
38
+ json: KeyringPair$Json;
39
+ pair: KeyringPair;
40
+ }
41
+ export interface KeyringStruct {
42
+ readonly accounts: AddressSubject;
43
+ readonly addresses: AddressSubject;
44
+ readonly contracts: AddressSubject;
45
+ readonly keyring: BaseKeyringInstance | undefined;
46
+ readonly genesisHash?: string | undefined;
47
+ addExternal: (publicKey: Uint8Array, meta?: KeyringPair$Meta) => CreateResult;
48
+ addPair: (pair: KeyringPair, password: string) => CreateResult;
49
+ addUri: (suri: string, password?: string, meta?: KeyringPair$Meta, type?: KeypairType) => CreateResult;
50
+ backupAccount: (pair: KeyringPair, password: string) => KeyringPair$Json;
51
+ backupAccounts: (addresses: string[], password: string) => Promise<KeyringPairs$Json>;
52
+ createFromUri(suri: string, meta?: KeyringPair$Meta, type?: KeypairType): KeyringPair;
53
+ decodeAddress: (key: string | Uint8Array) => Uint8Array;
54
+ encodeAddress: (key: string | Uint8Array) => string;
55
+ encryptAccount: (pair: KeyringPair, password: string) => void;
56
+ forgetAccount: (address: string) => void;
57
+ forgetAddress: (address: string) => void;
58
+ forgetContract: (address: string) => void;
59
+ getAccount: (address: string | Uint8Array) => KeyringAddress | undefined;
60
+ getAccounts: () => KeyringAddress[];
61
+ getAddress: (address: string | Uint8Array, type: KeyringItemType | null) => KeyringAddress | undefined;
62
+ getAddresses: () => KeyringAddress[];
63
+ getContract: (address: string | Uint8Array) => KeyringAddress | undefined;
64
+ getContracts: (genesisHash?: string) => KeyringAddress[];
65
+ getPair: (address: string | Uint8Array) => KeyringPair;
66
+ getPairs: () => KeyringPair[];
67
+ isAvailable: (address: string | Uint8Array) => boolean;
68
+ isPassValid: (password: string) => boolean;
69
+ loadAll: (options: KeyringOptions) => void;
70
+ restoreAccount: (json: KeyringPair$Json, password: string) => KeyringPair;
71
+ restoreAccounts: (json: EncryptedJson, password: string) => void;
72
+ saveAccount: (pair: KeyringPair, password?: string) => KeyringPair$Json;
73
+ saveAccountMeta: (pair: KeyringPair, meta: KeyringPair$Meta) => void;
74
+ saveAddress: (address: string, meta: KeyringPair$Meta) => KeyringPair$Json;
75
+ saveContract: (address: string, meta: KeyringPair$Meta) => KeyringPair$Json;
76
+ saveRecent: (address: string) => SingleAddress;
77
+ setDevMode: (isDevelopment: boolean) => void;
78
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ export declare function accountKey(address: string): string;
2
+ export declare function addressKey(address: string): string;
3
+ export declare function contractKey(address: string): string;
4
+ export declare const accountRegex: RegExp;
5
+ export declare const addressRegex: RegExp;
6
+ export declare const contractRegex: RegExp;
@@ -0,0 +1,22 @@
1
+ import { decodeAddress } from '@pezkuwi/keyring';
2
+ import { u8aToHex } from '@pezkuwi/util';
3
+ const ACCOUNT_PREFIX = 'account:';
4
+ const ADDRESS_PREFIX = 'address:';
5
+ const CONTRACT_PREFIX = 'contract:';
6
+ function toHex(address) {
7
+ return u8aToHex(
8
+ // When saving pre-checksum changes, ensure that we can decode
9
+ decodeAddress(address, true));
10
+ }
11
+ export function accountKey(address) {
12
+ return `${ACCOUNT_PREFIX}${toHex(address)}`;
13
+ }
14
+ export function addressKey(address) {
15
+ return `${ADDRESS_PREFIX}${toHex(address)}`;
16
+ }
17
+ export function contractKey(address) {
18
+ return `${CONTRACT_PREFIX}${toHex(address)}`;
19
+ }
20
+ export const accountRegex = new RegExp(`^${ACCOUNT_PREFIX}0x[0-9a-f]*`, '');
21
+ export const addressRegex = new RegExp(`^${ADDRESS_PREFIX}0x[0-9a-f]*`, '');
22
+ export const contractRegex = new RegExp(`^${CONTRACT_PREFIX}0x[0-9a-f]*`, '');
@@ -0,0 +1,4 @@
1
+ import './packageDetect.js';
2
+ import { keyring } from './bundle.js';
3
+ export * from './bundle.js';
4
+ export default keyring;
package/build/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import './packageDetect.js';
2
+ import { keyring } from './bundle.js';
3
+ export * from './bundle.js';
4
+ export default keyring;
@@ -0,0 +1 @@
1
+ export declare const accounts: import("./types.js").AddressSubject;
@@ -0,0 +1,3 @@
1
+ import { accountKey } from '../defaults.js';
2
+ import { genericSubject } from './genericSubject.js';
3
+ export const accounts = /*#__PURE__*/ genericSubject(accountKey, true);
@@ -0,0 +1 @@
1
+ export declare const addresses: import("./types.js").AddressSubject;
@@ -0,0 +1,3 @@
1
+ import { addressKey } from '../defaults.js';
2
+ import { genericSubject } from './genericSubject.js';
3
+ export const addresses = /*#__PURE__*/ genericSubject(addressKey);
@@ -0,0 +1 @@
1
+ export declare const contracts: import("./types.js").AddressSubject;
@@ -0,0 +1,3 @@
1
+ import { contractKey } from '../defaults.js';
2
+ import { genericSubject } from './genericSubject.js';
3
+ export const contracts = /*#__PURE__*/ genericSubject(contractKey);
@@ -0,0 +1,6 @@
1
+ import { BehaviorSubject } from 'rxjs';
2
+ export declare const env: {
3
+ isDevelopment: () => boolean;
4
+ set: (isDevelopment: boolean) => void;
5
+ subject: BehaviorSubject<boolean>;
6
+ };
@@ -0,0 +1,9 @@
1
+ import { BehaviorSubject } from 'rxjs';
2
+ const subject = new BehaviorSubject(false);
3
+ export const env = {
4
+ isDevelopment: () => subject.getValue(),
5
+ set: (isDevelopment) => {
6
+ subject.next(isDevelopment);
7
+ },
8
+ subject
9
+ };
@@ -0,0 +1,2 @@
1
+ import type { AddressSubject } from './types.js';
2
+ export declare function genericSubject(keyCreator: (address: string) => string, withTest?: boolean): AddressSubject;
@@ -0,0 +1,44 @@
1
+ import { BehaviorSubject } from 'rxjs';
2
+ import { objectCopy, objectSpread } from '@pezkuwi/util';
3
+ import { createOptionItem } from '../options/item.js';
4
+ import { env } from './env.js';
5
+ function callNext(current, subject, withTest) {
6
+ const isDevMode = env.isDevelopment();
7
+ const filtered = {};
8
+ Object.keys(current).forEach((key) => {
9
+ const { json: { meta: { isTesting = false } = {} } = {} } = current[key];
10
+ if (!withTest || isDevMode || isTesting !== true) {
11
+ filtered[key] = current[key];
12
+ }
13
+ });
14
+ subject.next(filtered);
15
+ }
16
+ export function genericSubject(keyCreator, withTest = false) {
17
+ let current = {};
18
+ const subject = new BehaviorSubject({});
19
+ const next = () => callNext(current, subject, withTest);
20
+ env.subject.subscribe(next);
21
+ return {
22
+ add: (store, address, json, type) => {
23
+ current = objectCopy(current);
24
+ current[address] = {
25
+ json: objectSpread({}, json, { address }),
26
+ option: createOptionItem(address, json.meta.name),
27
+ type
28
+ };
29
+ // we do not store dev or injected accounts (external/transient)
30
+ if (!json.meta.isInjected && (!json.meta.isTesting || env.isDevelopment())) {
31
+ store.set(keyCreator(address), json);
32
+ }
33
+ next();
34
+ return current[address];
35
+ },
36
+ remove: (store, address) => {
37
+ current = objectCopy(current);
38
+ delete current[address];
39
+ store.remove(keyCreator(address));
40
+ next();
41
+ },
42
+ subject
43
+ };
44
+ }