@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
package/src/Keyring.ts ADDED
@@ -0,0 +1,420 @@
1
+ // Copyright 2017-2025 @pezkuwi/ui-keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { KeyringPair, KeyringPair$Json, KeyringPair$Meta } from '@pezkuwi/keyring/types';
5
+ import type { BN } from '@pezkuwi/util';
6
+ import type { EncryptedJson } from '@pezkuwi/util-crypto/json/types';
7
+ import type { KeypairType } from '@pezkuwi/util-crypto/types';
8
+ import type { AddressSubject, SingleAddress } from './observable/types.js';
9
+ import type { CreateResult, KeyringAddress, KeyringAddressType, KeyringItemType, KeyringJson, KeyringJson$Meta, KeyringOptions, KeyringPairs$Json, KeyringStruct } from './types.js';
10
+
11
+ import { createPair } from '@pezkuwi/keyring';
12
+ import { chains } from '@pezkuwi/ui-settings';
13
+ import { bnToBn, hexToU8a, isFunction, isHex, isString, objectSpread, stringify, stringToU8a, u8aSorted, u8aToString } from '@pezkuwi/util';
14
+ import { base64Decode, createKeyMulti, jsonDecrypt, jsonEncrypt } from '@pezkuwi/util-crypto';
15
+
16
+ import { env } from './observable/env.js';
17
+ import { KeyringOption } from './options/index.js';
18
+ import { Base } from './Base.js';
19
+ import { accountKey, accountRegex, addressKey, addressRegex, contractKey, contractRegex } from './defaults.js';
20
+
21
+ const RECENT_EXPIRY = 24 * 60 * 60;
22
+
23
+ // No accounts (or test accounts) should be loaded until after the chain determination.
24
+ // Chain determination occurs outside of Keyring. Loading `keyring.loadAll({ type: 'ed25519' | 'sr25519' })` is triggered
25
+ // from the API after the chain is received
26
+ export class Keyring extends Base implements KeyringStruct {
27
+ public readonly keyringOption = new KeyringOption();
28
+
29
+ #stores = {
30
+ account: (): AddressSubject => this.accounts,
31
+ address: (): AddressSubject => this.addresses,
32
+ contract: (): AddressSubject => this.contracts
33
+ };
34
+
35
+ public addExternal (address: string | Uint8Array, meta: KeyringPair$Meta = {}): CreateResult {
36
+ const pair = this.keyring.addFromAddress(address, objectSpread<KeyringJson$Meta>({}, meta, { isExternal: true }), null, meta?.type);
37
+
38
+ return {
39
+ json: this.saveAccount(pair),
40
+ pair
41
+ };
42
+ }
43
+
44
+ public addHardware (address: string | Uint8Array, hardwareType: string, meta: KeyringPair$Meta = {}): CreateResult {
45
+ return this.addExternal(address, objectSpread<KeyringPair$Meta>({}, meta, { hardwareType, isHardware: true }));
46
+ }
47
+
48
+ public addMultisig (addresses: (string | Uint8Array)[], threshold: bigint | BN | number, meta: KeyringPair$Meta = {}): CreateResult {
49
+ let address = createKeyMulti(addresses, threshold);
50
+
51
+ // For Ethereum chains, the first 20 bytes of the hash indicates the actual address
52
+ // Testcases via creation and on-chain events:
53
+ // - input: 0x7a1671a0224c8927b08f978027d586ab6868de0d31bb5bc956b625ced2ab18c4
54
+ // - output: 0x7a1671a0224c8927b08f978027d586ab6868de0d
55
+ if (this.isEthereum) {
56
+ address = address.slice(0, 20);
57
+ }
58
+
59
+ // we could use `sortAddresses`, but rather use internal encode/decode so we are 100%
60
+ const who = u8aSorted(
61
+ addresses.map((who) => this.decodeAddress(who))
62
+ ).map((who) => this.encodeAddress(who));
63
+
64
+ return this.addExternal(address, objectSpread<KeyringPair$Meta>({}, meta, { isMultisig: true, threshold: bnToBn(threshold).toNumber(), who }));
65
+ }
66
+
67
+ public addPair (pair: KeyringPair, password: string): CreateResult {
68
+ this.keyring.addPair(pair);
69
+
70
+ return {
71
+ json: this.saveAccount(pair, password),
72
+ pair
73
+ };
74
+ }
75
+
76
+ public addUri (suri: string, password?: string, meta: KeyringPair$Meta = {}, type?: KeypairType): CreateResult {
77
+ const pair = this.keyring.addFromUri(suri, meta, type);
78
+
79
+ return {
80
+ json: this.saveAccount(pair, password),
81
+ pair
82
+ };
83
+ }
84
+
85
+ public backupAccount (pair: KeyringPair, password: string): KeyringPair$Json {
86
+ if (!pair.isLocked) {
87
+ pair.lock();
88
+ }
89
+
90
+ pair.decodePkcs8(password);
91
+
92
+ return pair.toJson(password);
93
+ }
94
+
95
+ public async backupAccounts (addresses: string[], password: string): Promise<KeyringPairs$Json> {
96
+ const accountPromises = addresses.map((address) => {
97
+ return new Promise<KeyringJson>((resolve) => {
98
+ this._store.get(accountKey(address), resolve);
99
+ });
100
+ });
101
+
102
+ const accounts = await Promise.all(accountPromises);
103
+
104
+ return objectSpread({}, jsonEncrypt(stringToU8a(JSON.stringify(accounts)), ['batch-pkcs8'], password), {
105
+ accounts: accounts.map((account) => ({
106
+ address: account.address,
107
+ meta: account.meta
108
+ }))
109
+ });
110
+ }
111
+
112
+ public createFromJson (json: KeyringPair$Json, meta: KeyringPair$Meta = {}): KeyringPair {
113
+ return this.keyring.createFromJson(
114
+ objectSpread({}, json, {
115
+ meta: objectSpread({}, json.meta, meta)
116
+ })
117
+ );
118
+ }
119
+
120
+ public createFromUri (suri: string, meta: KeyringPair$Meta = {}, type?: KeypairType): KeyringPair {
121
+ return this.keyring.createFromUri(suri, meta, type);
122
+ }
123
+
124
+ public encryptAccount (pair: KeyringPair, password: string): void {
125
+ const json = pair.toJson(password);
126
+
127
+ json.meta.whenEdited = Date.now();
128
+
129
+ this.keyring.addFromJson(json);
130
+ this.accounts.add(this._store, pair.address, json, pair.type);
131
+ }
132
+
133
+ public forgetAccount (address: string): void {
134
+ this.keyring.removePair(address);
135
+ this.accounts.remove(this._store, address);
136
+ }
137
+
138
+ public forgetAddress (address: string): void {
139
+ this.addresses.remove(this._store, address);
140
+ }
141
+
142
+ public forgetContract (address: string): void {
143
+ this.contracts.remove(this._store, address);
144
+ }
145
+
146
+ public getAccount (address: string | Uint8Array): KeyringAddress | undefined {
147
+ return this.getAddress(address, 'account');
148
+ }
149
+
150
+ public getAccounts (): KeyringAddress[] {
151
+ const available = this.accounts.subject.getValue();
152
+
153
+ return Object
154
+ .keys(available)
155
+ .map((address) => this.getAddress(address, 'account'))
156
+ .filter((account): account is KeyringAddress =>
157
+ !!account &&
158
+ (
159
+ env.isDevelopment() ||
160
+ account.meta.isTesting !== true
161
+ )
162
+ );
163
+ }
164
+
165
+ public getAddress (_address: string | Uint8Array, type: KeyringItemType | null = null): KeyringAddress | undefined {
166
+ const address = isString(_address)
167
+ ? _address
168
+ : this.encodeAddress(_address);
169
+ const publicKey = this.decodeAddress(address);
170
+ const stores = type
171
+ ? [this.#stores[type]]
172
+ : Object.values(this.#stores);
173
+
174
+ const info = stores.reduce<SingleAddress | undefined>((lastInfo, store): SingleAddress | undefined =>
175
+ (store().subject.getValue()[address] || lastInfo), undefined);
176
+
177
+ return info && {
178
+ address,
179
+ meta: info.json.meta,
180
+ publicKey
181
+ };
182
+ }
183
+
184
+ public getAddresses (): KeyringAddress[] {
185
+ const available = this.addresses.subject.getValue();
186
+
187
+ return Object
188
+ .keys(available)
189
+ .map((address) => this.getAddress(address))
190
+ .filter((account): account is KeyringAddress => !!account);
191
+ }
192
+
193
+ public getContract (address: string | Uint8Array): KeyringAddress | undefined {
194
+ return this.getAddress(address, 'contract');
195
+ }
196
+
197
+ public getContracts (): KeyringAddress[] {
198
+ const available = this.contracts.subject.getValue();
199
+
200
+ return Object
201
+ .entries(available)
202
+ .filter(([, { json: { meta: { contract } } }]): boolean =>
203
+ !!contract && contract.genesisHash === this.genesisHash
204
+ )
205
+ .map(([address]) => this.getContract(address))
206
+ .filter((account): account is KeyringAddress => !!account);
207
+ }
208
+
209
+ private rewriteKey (json: KeyringJson, key: string, hexAddr: string, creator: (addr: string) => string): void {
210
+ if (hexAddr.startsWith('0x')) {
211
+ return;
212
+ }
213
+
214
+ this._store.remove(key);
215
+ this._store.set(creator(hexAddr), json);
216
+ }
217
+
218
+ private loadAccount (json: KeyringJson, key: string): void {
219
+ if (!json.meta.isTesting && (json as KeyringPair$Json).encoded) {
220
+ const pair = this.keyring.addFromJson(json as KeyringPair$Json, true);
221
+
222
+ this.accounts.add(this._store, pair.address, json, pair.type);
223
+ }
224
+
225
+ const [, hexAddr] = key.split(':');
226
+
227
+ this.rewriteKey(json, key, hexAddr.trim(), accountKey);
228
+ }
229
+
230
+ private loadAddress (json: KeyringJson, key: string): void {
231
+ const { isRecent, whenCreated = 0 } = json.meta;
232
+
233
+ if (isRecent && (Date.now() - whenCreated) > RECENT_EXPIRY) {
234
+ this._store.remove(key);
235
+
236
+ return;
237
+ }
238
+
239
+ // We assume anything hex that is not 32bytes (64 + 2 bytes hex) is an Ethereum-like address
240
+ // (this caters for both H160 addresses as well as full or compressed publicKeys) - in the case
241
+ // of both ecdsa and ethereum, we keep it as-is
242
+ const address = isHex(json.address) && json.address.length !== 66
243
+ ? json.address
244
+ : this.encodeAddress(
245
+ isHex(json.address)
246
+ ? hexToU8a(json.address)
247
+ // FIXME Just for the transition period (ignoreChecksum)
248
+ : this.decodeAddress(json.address, true)
249
+ );
250
+ const [, hexAddr] = key.split(':');
251
+
252
+ this.addresses.add(this._store, address, json);
253
+ this.rewriteKey(json, key, hexAddr, addressKey);
254
+ }
255
+
256
+ private loadContract (json: KeyringJson, key: string): void {
257
+ const address = this.encodeAddress(
258
+ this.decodeAddress(json.address)
259
+ );
260
+ const [, hexAddr] = key.split(':');
261
+
262
+ // move genesisHash to top-level (TODO Remove from contracts section?)
263
+ json.meta.genesisHash = json.meta.genesisHash || (json.meta.contract?.genesisHash);
264
+
265
+ this.contracts.add(this._store, address, json);
266
+ this.rewriteKey(json, key, hexAddr, contractKey);
267
+ }
268
+
269
+ private loadInjected (address: string, meta: KeyringJson$Meta, type?: KeypairType): void {
270
+ const json = {
271
+ address,
272
+ meta: objectSpread<KeyringJson$Meta>({}, meta, { isInjected: true })
273
+ };
274
+ const pair = this.keyring.addFromAddress(address, json.meta, null, type);
275
+
276
+ this.accounts.add(this._store, pair.address, json, pair.type);
277
+ }
278
+
279
+ private allowGenesis (json?: KeyringJson | { meta: KeyringJson$Meta } | null): boolean {
280
+ if (json?.meta && this.genesisHash) {
281
+ const hashes: (string | null | undefined)[] = Object.values(chains).find((hashes): boolean =>
282
+ hashes.includes(this.genesisHash || '')
283
+ ) || [this.genesisHash];
284
+
285
+ if (json.meta.genesisHash) {
286
+ return hashes.includes(json.meta.genesisHash) || this.genesisHashes.includes(json.meta.genesisHash);
287
+ } else if (json.meta.contract) {
288
+ return hashes.includes(json.meta.contract.genesisHash);
289
+ }
290
+ }
291
+
292
+ return true;
293
+ }
294
+
295
+ public loadAll (options: KeyringOptions, injected: { address: string; meta: KeyringJson$Meta, type?: KeypairType }[] = []): void {
296
+ super.initKeyring(options);
297
+
298
+ this._store.all((key: string, json: KeyringJson): void => {
299
+ if (!isFunction(options.filter) || options.filter(json)) {
300
+ try {
301
+ if (this.allowGenesis(json)) {
302
+ if (accountRegex.test(key)) {
303
+ this.loadAccount(json, key);
304
+ } else if (addressRegex.test(key)) {
305
+ this.loadAddress(json, key);
306
+ } else if (contractRegex.test(key)) {
307
+ this.loadContract(json, key);
308
+ }
309
+ }
310
+ } catch {
311
+ console.warn(`Keyring: Unable to load ${key}:${stringify(json)}`);
312
+ }
313
+ }
314
+ });
315
+
316
+ injected.forEach((account): void => {
317
+ if (this.allowGenesis(account)) {
318
+ try {
319
+ this.loadInjected(account.address, account.meta, account.type);
320
+ } catch {
321
+ console.warn(`Keyring: Unable to inject ${stringify(account)}`);
322
+ }
323
+ }
324
+ });
325
+
326
+ this.keyringOption.init(this);
327
+ }
328
+
329
+ public restoreAccount (json: KeyringPair$Json, password: string): KeyringPair {
330
+ const cryptoType = Array.isArray(json.encoding.content) ? json.encoding.content[1] : 'ed25519';
331
+ const encType = Array.isArray(json.encoding.type) ? json.encoding.type : [json.encoding.type];
332
+ const pair = createPair(
333
+ { toSS58: this.encodeAddress, type: cryptoType as KeypairType },
334
+ { publicKey: this.decodeAddress(json.address, true) },
335
+ json.meta,
336
+ isHex(json.encoded) ? hexToU8a(json.encoded) : base64Decode(json.encoded),
337
+ encType
338
+ );
339
+
340
+ // unlock, save account and then lock (locking cleans secretKey, so needs to be last)
341
+ pair.decodePkcs8(password);
342
+ this.addPair(pair, password);
343
+ pair.lock();
344
+
345
+ return pair;
346
+ }
347
+
348
+ public restoreAccounts (json: EncryptedJson, password: string): void {
349
+ const accounts: KeyringJson[] = JSON.parse(u8aToString(jsonDecrypt(json, password))) as KeyringJson[];
350
+
351
+ accounts.forEach((account) => {
352
+ this.loadAccount(account, accountKey(account.address));
353
+ });
354
+ }
355
+
356
+ public saveAccount (pair: KeyringPair, password?: string): KeyringPair$Json {
357
+ this.addTimestamp(pair);
358
+
359
+ const json = pair.toJson(password);
360
+
361
+ this.keyring.addFromJson(json);
362
+ this.accounts.add(this._store, pair.address, json, pair.type);
363
+
364
+ return json;
365
+ }
366
+
367
+ public saveAccountMeta (pair: KeyringPair, meta: KeyringPair$Meta): void {
368
+ const address = pair.address;
369
+
370
+ this._store.get(accountKey(address), (json: KeyringJson): void => {
371
+ pair.setMeta(meta);
372
+ json.meta = pair.meta;
373
+
374
+ this.accounts.add(this._store, address, json, pair.type);
375
+ });
376
+ }
377
+
378
+ public saveAddress (address: string, meta: KeyringPair$Meta, type: KeyringAddressType = 'address'): KeyringPair$Json {
379
+ const available = this.addresses.subject.getValue();
380
+
381
+ const json = available[address]?.json || {
382
+ address,
383
+ meta: {
384
+ isRecent: undefined,
385
+ whenCreated: Date.now()
386
+ }
387
+ };
388
+
389
+ Object.keys(meta).forEach((key): void => {
390
+ json.meta[key] = meta[key];
391
+ });
392
+
393
+ delete json.meta.isRecent;
394
+
395
+ this.#stores[type]().add(this._store, address, json);
396
+
397
+ return json as KeyringPair$Json;
398
+ }
399
+
400
+ public saveContract (address: string, meta: KeyringPair$Meta): KeyringPair$Json {
401
+ return this.saveAddress(address, meta, 'contract');
402
+ }
403
+
404
+ public saveRecent (address: string): SingleAddress {
405
+ const available = this.addresses.subject.getValue();
406
+
407
+ if (!available[address]) {
408
+ this.addresses.add(this._store, address, {
409
+ address,
410
+ meta: {
411
+ genesisHash: this.genesisHash,
412
+ isRecent: true,
413
+ whenCreated: Date.now()
414
+ }
415
+ });
416
+ }
417
+
418
+ return this.addresses.subject.getValue()[address];
419
+ }
420
+ }
package/src/bundle.ts ADDED
@@ -0,0 +1,10 @@
1
+ // Copyright 2017-2025 @pezkuwi/ui-keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Keyring } from './Keyring.js';
5
+
6
+ export { packageInfo } from './packageInfo.js';
7
+
8
+ export const keyring = new Keyring();
9
+
10
+ export { Keyring };
@@ -0,0 +1,34 @@
1
+ // Copyright 2017-2025 @pezkuwi/ui-keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { decodeAddress } from '@pezkuwi/keyring';
5
+ import { u8aToHex } from '@pezkuwi/util';
6
+
7
+ const ACCOUNT_PREFIX = 'account:';
8
+ const ADDRESS_PREFIX = 'address:';
9
+ const CONTRACT_PREFIX = 'contract:';
10
+
11
+ function toHex (address: string): string {
12
+ return u8aToHex(
13
+ // When saving pre-checksum changes, ensure that we can decode
14
+ decodeAddress(address, true)
15
+ );
16
+ }
17
+
18
+ export function accountKey (address: string): string {
19
+ return `${ACCOUNT_PREFIX}${toHex(address)}`;
20
+ }
21
+
22
+ export function addressKey (address: string): string {
23
+ return `${ADDRESS_PREFIX}${toHex(address)}`;
24
+ }
25
+
26
+ export function contractKey (address: string): string {
27
+ return `${CONTRACT_PREFIX}${toHex(address)}`;
28
+ }
29
+
30
+ export const accountRegex = new RegExp(`^${ACCOUNT_PREFIX}0x[0-9a-f]*`, '');
31
+
32
+ export const addressRegex = new RegExp(`^${ADDRESS_PREFIX}0x[0-9a-f]*`, '');
33
+
34
+ export const contractRegex = new RegExp(`^${CONTRACT_PREFIX}0x[0-9a-f]*`, '');
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ // Copyright 2017-2025 @pezkuwi/ui-keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import './packageDetect.js';
5
+
6
+ import { keyring } from './bundle.js';
7
+
8
+ export * from './bundle.js';
9
+
10
+ export default keyring;
package/src/json.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ // Copyright 2017-2025 @pezkuwi/ui-keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // Be able to import json in TS
5
+ // https://stackoverflow.com/questions/49996456/importing-json-file-in-typescript
6
+ declare module '*.json' {
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ const value: any;
9
+
10
+ export default value;
11
+ }
@@ -0,0 +1,7 @@
1
+ // Copyright 2017-2025 @pezkuwi/ui-keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { accountKey } from '../defaults.js';
5
+ import { genericSubject } from './genericSubject.js';
6
+
7
+ export const accounts = /*#__PURE__*/ genericSubject(accountKey, true);
@@ -0,0 +1,7 @@
1
+ // Copyright 2017-2025 @pezkuwi/ui-keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { addressKey } from '../defaults.js';
5
+ import { genericSubject } from './genericSubject.js';
6
+
7
+ export const addresses = /*#__PURE__*/ genericSubject(addressKey);
@@ -0,0 +1,7 @@
1
+ // Copyright 2017-2025 @pezkuwi/ui-keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { contractKey } from '../defaults.js';
5
+ import { genericSubject } from './genericSubject.js';
6
+
7
+ export const contracts = /*#__PURE__*/ genericSubject(contractKey);
@@ -0,0 +1,15 @@
1
+ // Copyright 2017-2025 @pezkuwi/ui-keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { BehaviorSubject } from 'rxjs';
5
+
6
+ const subject = new BehaviorSubject(false);
7
+
8
+ export const env = {
9
+ isDevelopment: (): boolean =>
10
+ subject.getValue(),
11
+ set: (isDevelopment: boolean): void => {
12
+ subject.next(isDevelopment);
13
+ },
14
+ subject
15
+ };
@@ -0,0 +1,66 @@
1
+ // Copyright 2017-2025 @pezkuwi/ui-keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { KeypairType } from '@pezkuwi/util-crypto/types';
5
+ import type { KeyringJson, KeyringStore } from '../types.js';
6
+ import type { AddressSubject, SingleAddress, SubjectInfo } from './types.js';
7
+
8
+ import { BehaviorSubject } from 'rxjs';
9
+
10
+ import { objectCopy, objectSpread } from '@pezkuwi/util';
11
+
12
+ import { createOptionItem } from '../options/item.js';
13
+ import { env } from './env.js';
14
+
15
+ function callNext (current: SubjectInfo, subject: BehaviorSubject<SubjectInfo>, withTest: boolean): void {
16
+ const isDevMode = env.isDevelopment();
17
+ const filtered: SubjectInfo = {};
18
+
19
+ Object.keys(current).forEach((key): void => {
20
+ const { json: { meta: { isTesting = false } = {} } = {} } = current[key];
21
+
22
+ if (!withTest || isDevMode || isTesting !== true) {
23
+ filtered[key] = current[key];
24
+ }
25
+ });
26
+
27
+ subject.next(filtered);
28
+ }
29
+
30
+ export function genericSubject (keyCreator: (address: string) => string, withTest = false): AddressSubject {
31
+ let current: SubjectInfo = {};
32
+ const subject = new BehaviorSubject({});
33
+ const next = (): void => callNext(current, subject, withTest);
34
+
35
+ env.subject.subscribe(next);
36
+
37
+ return {
38
+ add: (store: KeyringStore, address: string, json: KeyringJson, type?: KeypairType): SingleAddress => {
39
+ current = objectCopy(current);
40
+
41
+ current[address] = {
42
+ json: objectSpread({}, json, { address }),
43
+ option: createOptionItem(address, json.meta.name),
44
+ type
45
+ };
46
+
47
+ // we do not store dev or injected accounts (external/transient)
48
+ if (!json.meta.isInjected && (!json.meta.isTesting || env.isDevelopment())) {
49
+ store.set(keyCreator(address), json);
50
+ }
51
+
52
+ next();
53
+
54
+ return current[address];
55
+ },
56
+ remove: (store: KeyringStore, address: string): void => {
57
+ current = objectCopy(current);
58
+
59
+ delete current[address];
60
+
61
+ store.remove(keyCreator(address));
62
+ next();
63
+ },
64
+ subject
65
+ };
66
+ }
@@ -0,0 +1,28 @@
1
+ // Copyright 2017-2025 @pezkuwi/ui-keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { SubjectInfo } from './types.js';
5
+
6
+ import { combineLatest, map } from 'rxjs';
7
+
8
+ import { accounts } from './accounts.js';
9
+ import { addresses } from './addresses.js';
10
+ import { contracts } from './contracts.js';
11
+
12
+ interface Result {
13
+ accounts: SubjectInfo;
14
+ addresses: SubjectInfo;
15
+ contracts: SubjectInfo;
16
+ }
17
+
18
+ export const obervableAll = /*#__PURE__*/ combineLatest([
19
+ accounts.subject,
20
+ addresses.subject,
21
+ contracts.subject
22
+ ]).pipe(
23
+ map(([accounts, addresses, contracts]): Result => ({
24
+ accounts,
25
+ addresses,
26
+ contracts
27
+ }))
28
+ );
@@ -0,0 +1,21 @@
1
+ // Copyright 2017-2025 @pezkuwi/ui-keyring authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { BehaviorSubject } from 'rxjs';
5
+ import type { KeypairType } from '@pezkuwi/util-crypto/types';
6
+ import type { KeyringSectionOption } from '../options/types.js';
7
+ import type { KeyringJson, KeyringStore } from '../types.js';
8
+
9
+ export interface SingleAddress {
10
+ json: KeyringJson;
11
+ option: KeyringSectionOption;
12
+ type?: KeypairType | undefined;
13
+ }
14
+
15
+ export type SubjectInfo = Record<string, SingleAddress>;
16
+
17
+ export interface AddressSubject {
18
+ add: (store: KeyringStore, address: string, json: KeyringJson, type?: KeypairType) => SingleAddress;
19
+ remove: (store: KeyringStore, address: string) => void;
20
+ subject: BehaviorSubject<SubjectInfo>;
21
+ }