@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,4 @@
1
+ import { Keyring } from './Keyring.js';
2
+ export { packageInfo } from './packageInfo.js';
3
+ export declare const keyring: Keyring;
4
+ export { Keyring };
@@ -0,0 +1,4 @@
1
+ import { Keyring } from './Keyring.js';
2
+ export { packageInfo } from './packageInfo.js';
3
+ export const keyring = new Keyring();
4
+ export { Keyring };
@@ -0,0 +1,30 @@
1
+ import type { KeyringInstance, KeyringPair } from '@pezkuwi/keyring/types';
2
+ import type { HexString } from '@pezkuwi/util/types';
3
+ import type { Prefix } from '@pezkuwi/util-crypto/address/types';
4
+ import type { AddressSubject } from './observable/types.js';
5
+ import type { KeyringOptions, KeyringStore } from './types.js';
6
+ export declare class Base {
7
+ #private;
8
+ protected _store: KeyringStore;
9
+ protected _genesisHash?: HexString | undefined;
10
+ protected _genesisHashAdd: HexString[];
11
+ constructor();
12
+ get accounts(): AddressSubject;
13
+ get addresses(): AddressSubject;
14
+ get contracts(): AddressSubject;
15
+ get isEthereum(): boolean;
16
+ get keyring(): KeyringInstance;
17
+ get genesisHash(): HexString | undefined;
18
+ get genesisHashes(): HexString[];
19
+ decodeAddress: (key: string | Uint8Array, ignoreChecksum?: boolean, ss58Format?: Prefix) => Uint8Array;
20
+ encodeAddress: (key: string | Uint8Array, ss58Format?: Prefix) => string;
21
+ getPair(address: string | Uint8Array): KeyringPair;
22
+ getPairs(): KeyringPair[];
23
+ isAvailable(_address: Uint8Array | string): boolean;
24
+ isPassValid(password: string): boolean;
25
+ setSS58Format(ss58Format?: Prefix): void;
26
+ setDevMode(isDevelopment: boolean): void;
27
+ protected initKeyring(options: KeyringOptions): void;
28
+ protected addAccountPairs(): void;
29
+ protected addTimestamp(pair: KeyringPair): void;
30
+ }
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Base = void 0;
4
+ const keyring_1 = require("@pezkuwi/keyring");
5
+ const util_1 = require("@pezkuwi/util");
6
+ const accounts_js_1 = require("./observable/accounts.js");
7
+ const addresses_js_1 = require("./observable/addresses.js");
8
+ const contracts_js_1 = require("./observable/contracts.js");
9
+ const env_js_1 = require("./observable/env.js");
10
+ const Browser_js_1 = require("./stores/Browser.js"); // direct import (skip index with all)
11
+ class Base {
12
+ #accounts;
13
+ #addresses;
14
+ #contracts;
15
+ #isEthereum;
16
+ #keyring;
17
+ _store;
18
+ _genesisHash;
19
+ _genesisHashAdd = [];
20
+ constructor() {
21
+ this.#accounts = accounts_js_1.accounts;
22
+ this.#addresses = addresses_js_1.addresses;
23
+ this.#contracts = contracts_js_1.contracts;
24
+ this.#isEthereum = false;
25
+ this._store = new Browser_js_1.BrowserStore();
26
+ }
27
+ get accounts() {
28
+ return this.#accounts;
29
+ }
30
+ get addresses() {
31
+ return this.#addresses;
32
+ }
33
+ get contracts() {
34
+ return this.#contracts;
35
+ }
36
+ get isEthereum() {
37
+ return this.#isEthereum;
38
+ }
39
+ get keyring() {
40
+ if (this.#keyring) {
41
+ return this.#keyring;
42
+ }
43
+ throw new Error('Keyring should be initialised via \'loadAll\' before use');
44
+ }
45
+ get genesisHash() {
46
+ return this._genesisHash;
47
+ }
48
+ get genesisHashes() {
49
+ return this._genesisHash
50
+ ? [this._genesisHash, ...this._genesisHashAdd]
51
+ : [...this._genesisHashAdd];
52
+ }
53
+ decodeAddress = (key, ignoreChecksum, ss58Format) => {
54
+ return this.keyring.decodeAddress(key, ignoreChecksum, ss58Format);
55
+ };
56
+ encodeAddress = (key, ss58Format) => {
57
+ return this.keyring.encodeAddress(key, ss58Format);
58
+ };
59
+ getPair(address) {
60
+ return this.keyring.getPair(address);
61
+ }
62
+ getPairs() {
63
+ return this.keyring.getPairs().filter((pair) => env_js_1.env.isDevelopment() || pair.meta.isTesting !== true);
64
+ }
65
+ isAvailable(_address) {
66
+ const accountsValue = this.accounts.subject.getValue();
67
+ const addressesValue = this.addresses.subject.getValue();
68
+ const contractsValue = this.contracts.subject.getValue();
69
+ const address = (0, util_1.isString)(_address)
70
+ ? _address
71
+ : this.encodeAddress(_address);
72
+ return !accountsValue[address] && !addressesValue[address] && !contractsValue[address];
73
+ }
74
+ isPassValid(password) {
75
+ return password.length > 0;
76
+ }
77
+ setSS58Format(ss58Format) {
78
+ if (this.#keyring && (0, util_1.isNumber)(ss58Format)) {
79
+ this.#keyring.setSS58Format(ss58Format);
80
+ }
81
+ }
82
+ setDevMode(isDevelopment) {
83
+ env_js_1.env.set(isDevelopment);
84
+ }
85
+ initKeyring(options) {
86
+ const keyring = (0, keyring_1.createTestKeyring)(options, true);
87
+ if ((0, util_1.isBoolean)(options.isDevelopment)) {
88
+ this.setDevMode(options.isDevelopment);
89
+ }
90
+ // set Ethereum state
91
+ this.#isEthereum = keyring.type === 'ethereum';
92
+ this.#keyring = keyring;
93
+ this._genesisHash = options.genesisHash && ((0, util_1.isString)(options.genesisHash)
94
+ ? options.genesisHash.toString()
95
+ : options.genesisHash.toHex());
96
+ this._genesisHashAdd = options.genesisHashAdd || [];
97
+ this._store = options.store || this._store;
98
+ this.addAccountPairs();
99
+ }
100
+ addAccountPairs() {
101
+ this.keyring
102
+ .getPairs()
103
+ .forEach(({ address, meta }) => {
104
+ this.accounts.add(this._store, address, { address, meta });
105
+ });
106
+ }
107
+ addTimestamp(pair) {
108
+ if (!pair.meta.whenCreated) {
109
+ pair.setMeta({ whenCreated: Date.now() });
110
+ }
111
+ }
112
+ }
113
+ exports.Base = Base;
@@ -0,0 +1,49 @@
1
+ import type { KeyringPair, KeyringPair$Json, KeyringPair$Meta } from '@pezkuwi/keyring/types';
2
+ import type { BN } from '@pezkuwi/util';
3
+ import type { EncryptedJson } from '@pezkuwi/util-crypto/json/types';
4
+ import type { KeypairType } from '@pezkuwi/util-crypto/types';
5
+ import type { SingleAddress } from './observable/types.js';
6
+ import type { CreateResult, KeyringAddress, KeyringAddressType, KeyringItemType, KeyringJson$Meta, KeyringOptions, KeyringPairs$Json, KeyringStruct } from './types.js';
7
+ import { KeyringOption } from './options/index.js';
8
+ import { Base } from './Base.js';
9
+ export declare class Keyring extends Base implements KeyringStruct {
10
+ #private;
11
+ readonly keyringOption: KeyringOption;
12
+ addExternal(address: string | Uint8Array, meta?: KeyringPair$Meta): CreateResult;
13
+ addHardware(address: string | Uint8Array, hardwareType: string, meta?: KeyringPair$Meta): CreateResult;
14
+ addMultisig(addresses: (string | Uint8Array)[], threshold: bigint | BN | number, meta?: KeyringPair$Meta): CreateResult;
15
+ addPair(pair: KeyringPair, password: string): CreateResult;
16
+ addUri(suri: string, password?: string, meta?: KeyringPair$Meta, type?: KeypairType): CreateResult;
17
+ backupAccount(pair: KeyringPair, password: string): KeyringPair$Json;
18
+ backupAccounts(addresses: string[], password: string): Promise<KeyringPairs$Json>;
19
+ createFromJson(json: KeyringPair$Json, meta?: KeyringPair$Meta): KeyringPair;
20
+ createFromUri(suri: string, meta?: KeyringPair$Meta, type?: KeypairType): KeyringPair;
21
+ encryptAccount(pair: KeyringPair, password: string): void;
22
+ forgetAccount(address: string): void;
23
+ forgetAddress(address: string): void;
24
+ forgetContract(address: string): void;
25
+ getAccount(address: string | Uint8Array): KeyringAddress | undefined;
26
+ getAccounts(): KeyringAddress[];
27
+ getAddress(_address: string | Uint8Array, type?: KeyringItemType | null): KeyringAddress | undefined;
28
+ getAddresses(): KeyringAddress[];
29
+ getContract(address: string | Uint8Array): KeyringAddress | undefined;
30
+ getContracts(): KeyringAddress[];
31
+ private rewriteKey;
32
+ private loadAccount;
33
+ private loadAddress;
34
+ private loadContract;
35
+ private loadInjected;
36
+ private allowGenesis;
37
+ loadAll(options: KeyringOptions, injected?: {
38
+ address: string;
39
+ meta: KeyringJson$Meta;
40
+ type?: KeypairType;
41
+ }[]): void;
42
+ restoreAccount(json: KeyringPair$Json, password: string): KeyringPair;
43
+ restoreAccounts(json: EncryptedJson, password: string): void;
44
+ saveAccount(pair: KeyringPair, password?: string): KeyringPair$Json;
45
+ saveAccountMeta(pair: KeyringPair, meta: KeyringPair$Meta): void;
46
+ saveAddress(address: string, meta: KeyringPair$Meta, type?: KeyringAddressType): KeyringPair$Json;
47
+ saveContract(address: string, meta: KeyringPair$Meta): KeyringPair$Json;
48
+ saveRecent(address: string): SingleAddress;
49
+ }
@@ -0,0 +1,308 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Keyring = void 0;
4
+ const keyring_1 = require("@pezkuwi/keyring");
5
+ const ui_settings_1 = require("@pezkuwi/ui-settings");
6
+ const util_1 = require("@pezkuwi/util");
7
+ const util_crypto_1 = require("@pezkuwi/util-crypto");
8
+ const env_js_1 = require("./observable/env.js");
9
+ const index_js_1 = require("./options/index.js");
10
+ const Base_js_1 = require("./Base.js");
11
+ const defaults_js_1 = require("./defaults.js");
12
+ const RECENT_EXPIRY = 24 * 60 * 60;
13
+ class Keyring extends Base_js_1.Base {
14
+ keyringOption = new index_js_1.KeyringOption();
15
+ #stores = {
16
+ account: () => this.accounts,
17
+ address: () => this.addresses,
18
+ contract: () => this.contracts
19
+ };
20
+ addExternal(address, meta = {}) {
21
+ const pair = this.keyring.addFromAddress(address, (0, util_1.objectSpread)({}, meta, { isExternal: true }), null, meta?.type);
22
+ return {
23
+ json: this.saveAccount(pair),
24
+ pair
25
+ };
26
+ }
27
+ addHardware(address, hardwareType, meta = {}) {
28
+ return this.addExternal(address, (0, util_1.objectSpread)({}, meta, { hardwareType, isHardware: true }));
29
+ }
30
+ addMultisig(addresses, threshold, meta = {}) {
31
+ let address = (0, util_crypto_1.createKeyMulti)(addresses, threshold);
32
+ // For Ethereum chains, the first 20 bytes of the hash indicates the actual address
33
+ // Testcases via creation and on-chain events:
34
+ // - input: 0x7a1671a0224c8927b08f978027d586ab6868de0d31bb5bc956b625ced2ab18c4
35
+ // - output: 0x7a1671a0224c8927b08f978027d586ab6868de0d
36
+ if (this.isEthereum) {
37
+ address = address.slice(0, 20);
38
+ }
39
+ // we could use `sortAddresses`, but rather use internal encode/decode so we are 100%
40
+ const who = (0, util_1.u8aSorted)(addresses.map((who) => this.decodeAddress(who))).map((who) => this.encodeAddress(who));
41
+ return this.addExternal(address, (0, util_1.objectSpread)({}, meta, { isMultisig: true, threshold: (0, util_1.bnToBn)(threshold).toNumber(), who }));
42
+ }
43
+ addPair(pair, password) {
44
+ this.keyring.addPair(pair);
45
+ return {
46
+ json: this.saveAccount(pair, password),
47
+ pair
48
+ };
49
+ }
50
+ addUri(suri, password, meta = {}, type) {
51
+ const pair = this.keyring.addFromUri(suri, meta, type);
52
+ return {
53
+ json: this.saveAccount(pair, password),
54
+ pair
55
+ };
56
+ }
57
+ backupAccount(pair, password) {
58
+ if (!pair.isLocked) {
59
+ pair.lock();
60
+ }
61
+ pair.decodePkcs8(password);
62
+ return pair.toJson(password);
63
+ }
64
+ async backupAccounts(addresses, password) {
65
+ const accountPromises = addresses.map((address) => {
66
+ return new Promise((resolve) => {
67
+ this._store.get((0, defaults_js_1.accountKey)(address), resolve);
68
+ });
69
+ });
70
+ const accounts = await Promise.all(accountPromises);
71
+ return (0, util_1.objectSpread)({}, (0, util_crypto_1.jsonEncrypt)((0, util_1.stringToU8a)(JSON.stringify(accounts)), ['batch-pkcs8'], password), {
72
+ accounts: accounts.map((account) => ({
73
+ address: account.address,
74
+ meta: account.meta
75
+ }))
76
+ });
77
+ }
78
+ createFromJson(json, meta = {}) {
79
+ return this.keyring.createFromJson((0, util_1.objectSpread)({}, json, {
80
+ meta: (0, util_1.objectSpread)({}, json.meta, meta)
81
+ }));
82
+ }
83
+ createFromUri(suri, meta = {}, type) {
84
+ return this.keyring.createFromUri(suri, meta, type);
85
+ }
86
+ encryptAccount(pair, password) {
87
+ const json = pair.toJson(password);
88
+ json.meta.whenEdited = Date.now();
89
+ this.keyring.addFromJson(json);
90
+ this.accounts.add(this._store, pair.address, json, pair.type);
91
+ }
92
+ forgetAccount(address) {
93
+ this.keyring.removePair(address);
94
+ this.accounts.remove(this._store, address);
95
+ }
96
+ forgetAddress(address) {
97
+ this.addresses.remove(this._store, address);
98
+ }
99
+ forgetContract(address) {
100
+ this.contracts.remove(this._store, address);
101
+ }
102
+ getAccount(address) {
103
+ return this.getAddress(address, 'account');
104
+ }
105
+ getAccounts() {
106
+ const available = this.accounts.subject.getValue();
107
+ return Object
108
+ .keys(available)
109
+ .map((address) => this.getAddress(address, 'account'))
110
+ .filter((account) => !!account &&
111
+ (env_js_1.env.isDevelopment() ||
112
+ account.meta.isTesting !== true));
113
+ }
114
+ getAddress(_address, type = null) {
115
+ const address = (0, util_1.isString)(_address)
116
+ ? _address
117
+ : this.encodeAddress(_address);
118
+ const publicKey = this.decodeAddress(address);
119
+ const stores = type
120
+ ? [this.#stores[type]]
121
+ : Object.values(this.#stores);
122
+ const info = stores.reduce((lastInfo, store) => (store().subject.getValue()[address] || lastInfo), undefined);
123
+ return info && {
124
+ address,
125
+ meta: info.json.meta,
126
+ publicKey
127
+ };
128
+ }
129
+ getAddresses() {
130
+ const available = this.addresses.subject.getValue();
131
+ return Object
132
+ .keys(available)
133
+ .map((address) => this.getAddress(address))
134
+ .filter((account) => !!account);
135
+ }
136
+ getContract(address) {
137
+ return this.getAddress(address, 'contract');
138
+ }
139
+ getContracts() {
140
+ const available = this.contracts.subject.getValue();
141
+ return Object
142
+ .entries(available)
143
+ .filter(([, { json: { meta: { contract } } }]) => !!contract && contract.genesisHash === this.genesisHash)
144
+ .map(([address]) => this.getContract(address))
145
+ .filter((account) => !!account);
146
+ }
147
+ rewriteKey(json, key, hexAddr, creator) {
148
+ if (hexAddr.startsWith('0x')) {
149
+ return;
150
+ }
151
+ this._store.remove(key);
152
+ this._store.set(creator(hexAddr), json);
153
+ }
154
+ loadAccount(json, key) {
155
+ if (!json.meta.isTesting && json.encoded) {
156
+ const pair = this.keyring.addFromJson(json, true);
157
+ this.accounts.add(this._store, pair.address, json, pair.type);
158
+ }
159
+ const [, hexAddr] = key.split(':');
160
+ this.rewriteKey(json, key, hexAddr.trim(), defaults_js_1.accountKey);
161
+ }
162
+ loadAddress(json, key) {
163
+ const { isRecent, whenCreated = 0 } = json.meta;
164
+ if (isRecent && (Date.now() - whenCreated) > RECENT_EXPIRY) {
165
+ this._store.remove(key);
166
+ return;
167
+ }
168
+ // We assume anything hex that is not 32bytes (64 + 2 bytes hex) is an Ethereum-like address
169
+ // (this caters for both H160 addresses as well as full or compressed publicKeys) - in the case
170
+ // of both ecdsa and ethereum, we keep it as-is
171
+ const address = (0, util_1.isHex)(json.address) && json.address.length !== 66
172
+ ? json.address
173
+ : this.encodeAddress((0, util_1.isHex)(json.address)
174
+ ? (0, util_1.hexToU8a)(json.address)
175
+ // FIXME Just for the transition period (ignoreChecksum)
176
+ : this.decodeAddress(json.address, true));
177
+ const [, hexAddr] = key.split(':');
178
+ this.addresses.add(this._store, address, json);
179
+ this.rewriteKey(json, key, hexAddr, defaults_js_1.addressKey);
180
+ }
181
+ loadContract(json, key) {
182
+ const address = this.encodeAddress(this.decodeAddress(json.address));
183
+ const [, hexAddr] = key.split(':');
184
+ // move genesisHash to top-level (TODO Remove from contracts section?)
185
+ json.meta.genesisHash = json.meta.genesisHash || (json.meta.contract?.genesisHash);
186
+ this.contracts.add(this._store, address, json);
187
+ this.rewriteKey(json, key, hexAddr, defaults_js_1.contractKey);
188
+ }
189
+ loadInjected(address, meta, type) {
190
+ const json = {
191
+ address,
192
+ meta: (0, util_1.objectSpread)({}, meta, { isInjected: true })
193
+ };
194
+ const pair = this.keyring.addFromAddress(address, json.meta, null, type);
195
+ this.accounts.add(this._store, pair.address, json, pair.type);
196
+ }
197
+ allowGenesis(json) {
198
+ if (json?.meta && this.genesisHash) {
199
+ const hashes = Object.values(ui_settings_1.chains).find((hashes) => hashes.includes(this.genesisHash || '')) || [this.genesisHash];
200
+ if (json.meta.genesisHash) {
201
+ return hashes.includes(json.meta.genesisHash) || this.genesisHashes.includes(json.meta.genesisHash);
202
+ }
203
+ else if (json.meta.contract) {
204
+ return hashes.includes(json.meta.contract.genesisHash);
205
+ }
206
+ }
207
+ return true;
208
+ }
209
+ loadAll(options, injected = []) {
210
+ super.initKeyring(options);
211
+ this._store.all((key, json) => {
212
+ if (!(0, util_1.isFunction)(options.filter) || options.filter(json)) {
213
+ try {
214
+ if (this.allowGenesis(json)) {
215
+ if (defaults_js_1.accountRegex.test(key)) {
216
+ this.loadAccount(json, key);
217
+ }
218
+ else if (defaults_js_1.addressRegex.test(key)) {
219
+ this.loadAddress(json, key);
220
+ }
221
+ else if (defaults_js_1.contractRegex.test(key)) {
222
+ this.loadContract(json, key);
223
+ }
224
+ }
225
+ }
226
+ catch {
227
+ console.warn(`Keyring: Unable to load ${key}:${(0, util_1.stringify)(json)}`);
228
+ }
229
+ }
230
+ });
231
+ injected.forEach((account) => {
232
+ if (this.allowGenesis(account)) {
233
+ try {
234
+ this.loadInjected(account.address, account.meta, account.type);
235
+ }
236
+ catch {
237
+ console.warn(`Keyring: Unable to inject ${(0, util_1.stringify)(account)}`);
238
+ }
239
+ }
240
+ });
241
+ this.keyringOption.init(this);
242
+ }
243
+ restoreAccount(json, password) {
244
+ const cryptoType = Array.isArray(json.encoding.content) ? json.encoding.content[1] : 'ed25519';
245
+ const encType = Array.isArray(json.encoding.type) ? json.encoding.type : [json.encoding.type];
246
+ const pair = (0, keyring_1.createPair)({ toSS58: this.encodeAddress, type: cryptoType }, { publicKey: this.decodeAddress(json.address, true) }, json.meta, (0, util_1.isHex)(json.encoded) ? (0, util_1.hexToU8a)(json.encoded) : (0, util_crypto_1.base64Decode)(json.encoded), encType);
247
+ // unlock, save account and then lock (locking cleans secretKey, so needs to be last)
248
+ pair.decodePkcs8(password);
249
+ this.addPair(pair, password);
250
+ pair.lock();
251
+ return pair;
252
+ }
253
+ restoreAccounts(json, password) {
254
+ const accounts = JSON.parse((0, util_1.u8aToString)((0, util_crypto_1.jsonDecrypt)(json, password)));
255
+ accounts.forEach((account) => {
256
+ this.loadAccount(account, (0, defaults_js_1.accountKey)(account.address));
257
+ });
258
+ }
259
+ saveAccount(pair, password) {
260
+ this.addTimestamp(pair);
261
+ const json = pair.toJson(password);
262
+ this.keyring.addFromJson(json);
263
+ this.accounts.add(this._store, pair.address, json, pair.type);
264
+ return json;
265
+ }
266
+ saveAccountMeta(pair, meta) {
267
+ const address = pair.address;
268
+ this._store.get((0, defaults_js_1.accountKey)(address), (json) => {
269
+ pair.setMeta(meta);
270
+ json.meta = pair.meta;
271
+ this.accounts.add(this._store, address, json, pair.type);
272
+ });
273
+ }
274
+ saveAddress(address, meta, type = 'address') {
275
+ const available = this.addresses.subject.getValue();
276
+ const json = available[address]?.json || {
277
+ address,
278
+ meta: {
279
+ isRecent: undefined,
280
+ whenCreated: Date.now()
281
+ }
282
+ };
283
+ Object.keys(meta).forEach((key) => {
284
+ json.meta[key] = meta[key];
285
+ });
286
+ delete json.meta.isRecent;
287
+ this.#stores[type]().add(this._store, address, json);
288
+ return json;
289
+ }
290
+ saveContract(address, meta) {
291
+ return this.saveAddress(address, meta, 'contract');
292
+ }
293
+ saveRecent(address) {
294
+ const available = this.addresses.subject.getValue();
295
+ if (!available[address]) {
296
+ this.addresses.add(this._store, address, {
297
+ address,
298
+ meta: {
299
+ genesisHash: this.genesisHash,
300
+ isRecent: true,
301
+ whenCreated: Date.now()
302
+ }
303
+ });
304
+ }
305
+ return this.addresses.subject.getValue()[address];
306
+ }
307
+ }
308
+ exports.Keyring = Keyring;
@@ -0,0 +1,4 @@
1
+ import { Keyring } from './Keyring.js';
2
+ export { packageInfo } from './packageInfo.js';
3
+ export declare const keyring: Keyring;
4
+ export { Keyring };
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Keyring = exports.keyring = exports.packageInfo = void 0;
4
+ const Keyring_js_1 = require("./Keyring.js");
5
+ Object.defineProperty(exports, "Keyring", { enumerable: true, get: function () { return Keyring_js_1.Keyring; } });
6
+ var packageInfo_js_1 = require("./packageInfo.js");
7
+ Object.defineProperty(exports, "packageInfo", { enumerable: true, get: function () { return packageInfo_js_1.packageInfo; } });
8
+ exports.keyring = new Keyring_js_1.Keyring();
@@ -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,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.contractRegex = exports.addressRegex = exports.accountRegex = void 0;
4
+ exports.accountKey = accountKey;
5
+ exports.addressKey = addressKey;
6
+ exports.contractKey = contractKey;
7
+ const keyring_1 = require("@pezkuwi/keyring");
8
+ const util_1 = require("@pezkuwi/util");
9
+ const ACCOUNT_PREFIX = 'account:';
10
+ const ADDRESS_PREFIX = 'address:';
11
+ const CONTRACT_PREFIX = 'contract:';
12
+ function toHex(address) {
13
+ return (0, util_1.u8aToHex)(
14
+ // When saving pre-checksum changes, ensure that we can decode
15
+ (0, keyring_1.decodeAddress)(address, true));
16
+ }
17
+ function accountKey(address) {
18
+ return `${ACCOUNT_PREFIX}${toHex(address)}`;
19
+ }
20
+ function addressKey(address) {
21
+ return `${ADDRESS_PREFIX}${toHex(address)}`;
22
+ }
23
+ function contractKey(address) {
24
+ return `${CONTRACT_PREFIX}${toHex(address)}`;
25
+ }
26
+ exports.accountRegex = new RegExp(`^${ACCOUNT_PREFIX}0x[0-9a-f]*`, '');
27
+ exports.addressRegex = new RegExp(`^${ADDRESS_PREFIX}0x[0-9a-f]*`, '');
28
+ exports.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;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ require("./packageDetect.js");
5
+ const bundle_js_1 = require("./bundle.js");
6
+ tslib_1.__exportStar(require("./bundle.js"), exports);
7
+ exports.default = bundle_js_1.keyring;
@@ -0,0 +1 @@
1
+ export declare const accounts: import("./types.js").AddressSubject;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.accounts = void 0;
4
+ const defaults_js_1 = require("../defaults.js");
5
+ const genericSubject_js_1 = require("./genericSubject.js");
6
+ exports.accounts = (0, genericSubject_js_1.genericSubject)(defaults_js_1.accountKey, true);
@@ -0,0 +1 @@
1
+ export declare const addresses: import("./types.js").AddressSubject;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addresses = void 0;
4
+ const defaults_js_1 = require("../defaults.js");
5
+ const genericSubject_js_1 = require("./genericSubject.js");
6
+ exports.addresses = (0, genericSubject_js_1.genericSubject)(defaults_js_1.addressKey);
@@ -0,0 +1 @@
1
+ export declare const contracts: import("./types.js").AddressSubject;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.contracts = void 0;
4
+ const defaults_js_1 = require("../defaults.js");
5
+ const genericSubject_js_1 = require("./genericSubject.js");
6
+ exports.contracts = (0, genericSubject_js_1.genericSubject)(defaults_js_1.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,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.env = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ const subject = new rxjs_1.BehaviorSubject(false);
6
+ exports.env = {
7
+ isDevelopment: () => subject.getValue(),
8
+ set: (isDevelopment) => {
9
+ subject.next(isDevelopment);
10
+ },
11
+ subject
12
+ };
@@ -0,0 +1,2 @@
1
+ import type { AddressSubject } from './types.js';
2
+ export declare function genericSubject(keyCreator: (address: string) => string, withTest?: boolean): AddressSubject;