@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.
- package/README.md +136 -0
- package/build/Base.d.ts +30 -0
- package/build/Base.js +109 -0
- package/build/Keyring.d.ts +49 -0
- package/build/Keyring.js +304 -0
- package/build/LICENSE +201 -0
- package/build/README.md +136 -0
- package/build/bundle-pezkuwi-ui-keyring.js +2751 -0
- package/build/bundle.d.ts +4 -0
- package/build/bundle.js +4 -0
- package/build/cjs/Base.d.ts +30 -0
- package/build/cjs/Base.js +113 -0
- package/build/cjs/Keyring.d.ts +49 -0
- package/build/cjs/Keyring.js +308 -0
- package/build/cjs/bundle.d.ts +4 -0
- package/build/cjs/bundle.js +8 -0
- package/build/cjs/defaults.d.ts +6 -0
- package/build/cjs/defaults.js +28 -0
- package/build/cjs/index.d.ts +4 -0
- package/build/cjs/index.js +7 -0
- package/build/cjs/observable/accounts.d.ts +1 -0
- package/build/cjs/observable/accounts.js +6 -0
- package/build/cjs/observable/addresses.d.ts +1 -0
- package/build/cjs/observable/addresses.js +6 -0
- package/build/cjs/observable/contracts.d.ts +1 -0
- package/build/cjs/observable/contracts.js +6 -0
- package/build/cjs/observable/env.d.ts +6 -0
- package/build/cjs/observable/env.js +12 -0
- package/build/cjs/observable/genericSubject.d.ts +2 -0
- package/build/cjs/observable/genericSubject.js +47 -0
- package/build/cjs/observable/index.d.ts +8 -0
- package/build/cjs/observable/index.js +16 -0
- package/build/cjs/observable/types.d.ts +15 -0
- package/build/cjs/observable/types.js +2 -0
- package/build/cjs/options/index.d.ts +15 -0
- package/build/cjs/options/index.js +116 -0
- package/build/cjs/options/item.d.ts +2 -0
- package/build/cjs/options/item.js +16 -0
- package/build/cjs/options/types.d.ts +15 -0
- package/build/cjs/options/types.js +2 -0
- package/build/cjs/package.json +3 -0
- package/build/cjs/packageDetect.d.ts +1 -0
- package/build/cjs/packageDetect.js +6 -0
- package/build/cjs/packageInfo.d.ts +6 -0
- package/build/cjs/packageInfo.js +4 -0
- package/build/cjs/stores/Browser.d.ts +7 -0
- package/build/cjs/stores/Browser.js +24 -0
- package/build/cjs/stores/File.d.ts +12 -0
- package/build/cjs/stores/File.js +72 -0
- package/build/cjs/stores/index.d.ts +2 -0
- package/build/cjs/stores/index.js +7 -0
- package/build/cjs/types.d.ts +78 -0
- package/build/cjs/types.js +2 -0
- package/build/defaults.d.ts +6 -0
- package/build/defaults.js +22 -0
- package/build/index.d.ts +4 -0
- package/build/index.js +4 -0
- package/build/observable/accounts.d.ts +1 -0
- package/build/observable/accounts.js +3 -0
- package/build/observable/addresses.d.ts +1 -0
- package/build/observable/addresses.js +3 -0
- package/build/observable/contracts.d.ts +1 -0
- package/build/observable/contracts.js +3 -0
- package/build/observable/env.d.ts +6 -0
- package/build/observable/env.js +9 -0
- package/build/observable/genericSubject.d.ts +2 -0
- package/build/observable/genericSubject.js +44 -0
- package/build/observable/index.d.ts +8 -0
- package/build/observable/index.js +13 -0
- package/build/observable/types.d.ts +15 -0
- package/build/observable/types.js +1 -0
- package/build/options/index.d.ts +15 -0
- package/build/options/index.js +112 -0
- package/build/options/item.d.ts +2 -0
- package/build/options/item.js +13 -0
- package/build/options/types.d.ts +15 -0
- package/build/options/types.js +1 -0
- package/build/package.json +355 -0
- package/build/packageDetect.d.ts +1 -0
- package/build/packageDetect.js +4 -0
- package/build/packageInfo.d.ts +6 -0
- package/build/packageInfo.js +1 -0
- package/build/stores/Browser.d.ts +7 -0
- package/build/stores/Browser.js +19 -0
- package/build/stores/File.d.ts +12 -0
- package/build/stores/File.js +67 -0
- package/build/stores/index.d.ts +2 -0
- package/build/stores/index.js +2 -0
- package/build/types.d.ts +78 -0
- package/build/types.js +1 -0
- package/build-tsc/Base.d.ts +30 -0
- package/build-tsc/Keyring.d.ts +49 -0
- package/build-tsc/bundle.d.ts +4 -0
- package/build-tsc/defaults.d.ts +6 -0
- package/build-tsc/index.d.ts +4 -0
- package/build-tsc/observable/accounts.d.ts +1 -0
- package/build-tsc/observable/addresses.d.ts +1 -0
- package/build-tsc/observable/contracts.d.ts +1 -0
- package/build-tsc/observable/env.d.ts +6 -0
- package/build-tsc/observable/genericSubject.d.ts +2 -0
- package/build-tsc/observable/index.d.ts +8 -0
- package/build-tsc/observable/types.d.ts +15 -0
- package/build-tsc/options/index.d.ts +15 -0
- package/build-tsc/options/item.d.ts +2 -0
- package/build-tsc/options/types.d.ts +15 -0
- package/build-tsc/packageDetect.d.ts +1 -0
- package/build-tsc/packageInfo.d.ts +6 -0
- package/build-tsc/stores/Browser.d.ts +7 -0
- package/build-tsc/stores/File.d.ts +12 -0
- package/build-tsc/stores/index.d.ts +2 -0
- package/build-tsc/types.d.ts +78 -0
- package/build-tsc-cjs/Base.js +113 -0
- package/build-tsc-cjs/Keyring.js +308 -0
- package/build-tsc-cjs/bundle.js +8 -0
- package/build-tsc-cjs/defaults.js +28 -0
- package/build-tsc-cjs/index.js +7 -0
- package/build-tsc-cjs/observable/accounts.js +6 -0
- package/build-tsc-cjs/observable/addresses.js +6 -0
- package/build-tsc-cjs/observable/contracts.js +6 -0
- package/build-tsc-cjs/observable/env.js +12 -0
- package/build-tsc-cjs/observable/genericSubject.js +47 -0
- package/build-tsc-cjs/observable/index.js +16 -0
- package/build-tsc-cjs/observable/types.js +2 -0
- package/build-tsc-cjs/options/index.js +116 -0
- package/build-tsc-cjs/options/item.js +16 -0
- package/build-tsc-cjs/options/types.js +2 -0
- package/build-tsc-cjs/packageDetect.js +6 -0
- package/build-tsc-cjs/packageInfo.js +4 -0
- package/build-tsc-cjs/stores/Browser.js +24 -0
- package/build-tsc-cjs/stores/File.js +72 -0
- package/build-tsc-cjs/stores/index.js +7 -0
- package/build-tsc-cjs/types.js +2 -0
- package/build-tsc-esm/Base.js +109 -0
- package/build-tsc-esm/Keyring.js +304 -0
- package/build-tsc-esm/bundle.js +4 -0
- package/build-tsc-esm/defaults.js +22 -0
- package/build-tsc-esm/index.js +4 -0
- package/build-tsc-esm/observable/accounts.js +3 -0
- package/build-tsc-esm/observable/addresses.js +3 -0
- package/build-tsc-esm/observable/contracts.js +3 -0
- package/build-tsc-esm/observable/env.js +9 -0
- package/build-tsc-esm/observable/genericSubject.js +44 -0
- package/build-tsc-esm/observable/index.js +13 -0
- package/build-tsc-esm/observable/types.js +1 -0
- package/build-tsc-esm/options/index.js +112 -0
- package/build-tsc-esm/options/item.js +13 -0
- package/build-tsc-esm/options/types.js +1 -0
- package/build-tsc-esm/packageDetect.js +4 -0
- package/build-tsc-esm/packageInfo.js +1 -0
- package/build-tsc-esm/stores/Browser.js +19 -0
- package/build-tsc-esm/stores/File.js +67 -0
- package/build-tsc-esm/stores/index.js +2 -0
- package/build-tsc-esm/types.js +1 -0
- package/package.json +41 -0
- package/src/Base.ts +156 -0
- package/src/Keyring.ts +420 -0
- package/src/bundle.ts +10 -0
- package/src/defaults.ts +34 -0
- package/src/index.ts +10 -0
- package/src/json.d.ts +11 -0
- package/src/observable/accounts.ts +7 -0
- package/src/observable/addresses.ts +7 -0
- package/src/observable/contracts.ts +7 -0
- package/src/observable/env.ts +15 -0
- package/src/observable/genericSubject.ts +66 -0
- package/src/observable/index.ts +28 -0
- package/src/observable/types.ts +21 -0
- package/src/options/index.ts +150 -0
- package/src/options/item.ts +22 -0
- package/src/options/types.ts +23 -0
- package/src/packageDetect.ts +12 -0
- package/src/packageInfo.ts +6 -0
- package/src/stores/Browser.ts +28 -0
- package/src/stores/File.ts +94 -0
- package/src/stores/index.ts +5 -0
- package/src/types.ts +91 -0
- package/tsconfig.build.json +14 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- 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,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,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,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 @@
|
|
|
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,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,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,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]*`, '');
|
package/build/index.d.ts
ADDED
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const accounts: import("./types.js").AddressSubject;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const addresses: import("./types.js").AddressSubject;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const contracts: import("./types.js").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
|
+
}
|