@keplr-wallet/stores-core 0.12.32-rc.0

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 (54) hide show
  1. package/.eslintignore +2 -0
  2. package/.prettierignore +2 -0
  3. package/LICENSE +209 -0
  4. package/build/core/index.d.ts +4 -0
  5. package/build/core/index.js +21 -0
  6. package/build/core/index.js.map +1 -0
  7. package/build/core/interaction/chain-suggest.d.ts +35 -0
  8. package/build/core/interaction/chain-suggest.js +106 -0
  9. package/build/core/interaction/chain-suggest.js.map +1 -0
  10. package/build/core/interaction/eth-sign.d.ts +25 -0
  11. package/build/core/interaction/eth-sign.js +60 -0
  12. package/build/core/interaction/eth-sign.js.map +1 -0
  13. package/build/core/interaction/icns.d.ts +32 -0
  14. package/build/core/interaction/icns.js +58 -0
  15. package/build/core/interaction/icns.js.map +1 -0
  16. package/build/core/interaction/index.d.ts +6 -0
  17. package/build/core/interaction/index.js +23 -0
  18. package/build/core/interaction/index.js.map +1 -0
  19. package/build/core/interaction/interaction.d.ts +64 -0
  20. package/build/core/interaction/interaction.js +232 -0
  21. package/build/core/interaction/interaction.js.map +1 -0
  22. package/build/core/interaction/permission.d.ts +22 -0
  23. package/build/core/interaction/permission.js +108 -0
  24. package/build/core/interaction/permission.js.map +1 -0
  25. package/build/core/interaction/sign.d.ts +49 -0
  26. package/build/core/interaction/sign.js +78 -0
  27. package/build/core/interaction/sign.js.map +1 -0
  28. package/build/core/keyring.d.ts +132 -0
  29. package/build/core/keyring.js +278 -0
  30. package/build/core/keyring.js.map +1 -0
  31. package/build/core/permission-manager.d.ts +15 -0
  32. package/build/core/permission-manager.js +122 -0
  33. package/build/core/permission-manager.js.map +1 -0
  34. package/build/core/tokens.d.ts +39 -0
  35. package/build/core/tokens.js +225 -0
  36. package/build/core/tokens.js.map +1 -0
  37. package/build/index.d.ts +1 -0
  38. package/build/index.js +18 -0
  39. package/build/index.js.map +1 -0
  40. package/jest.config.js +5 -0
  41. package/package.json +31 -0
  42. package/src/core/index.ts +4 -0
  43. package/src/core/interaction/chain-suggest.ts +136 -0
  44. package/src/core/interaction/eth-sign.ts +71 -0
  45. package/src/core/interaction/icns.ts +68 -0
  46. package/src/core/interaction/index.ts +6 -0
  47. package/src/core/interaction/interaction.ts +290 -0
  48. package/src/core/interaction/permission.ts +121 -0
  49. package/src/core/interaction/sign.ts +124 -0
  50. package/src/core/keyring.ts +354 -0
  51. package/src/core/permission-manager.ts +114 -0
  52. package/src/core/tokens.ts +275 -0
  53. package/src/index.ts +1 -0
  54. package/tsconfig.json +12 -0
@@ -0,0 +1,354 @@
1
+ import { BACKGROUND_PORT, MessageRequester } from "@keplr-wallet/router";
2
+ import {
3
+ autorun,
4
+ computed,
5
+ flow,
6
+ makeObservable,
7
+ observable,
8
+ runInAction,
9
+ } from "mobx";
10
+ import { toGenerator } from "@keplr-wallet/common";
11
+ import {
12
+ AppendLedgerKeyAppMsg,
13
+ BIP44HDPath,
14
+ ChangeKeyRingNameMsg,
15
+ ChangeUserPasswordMsg,
16
+ CheckLegacyKeyRingPasswordMsg,
17
+ ComputeNotFinalizedKeyAddressesMsg,
18
+ DeleteKeyRingMsg,
19
+ FinalizeKeyCoinTypeMsg,
20
+ GetKeyRingStatusMsg,
21
+ GetKeyRingStatusOnlyMsg,
22
+ KeyInfo,
23
+ KeyRingStatus,
24
+ LockKeyRingMsg,
25
+ NewKeystoneKeyMsg,
26
+ NewLedgerKeyMsg,
27
+ NewMnemonicKeyMsg,
28
+ NewPrivateKeyKeyMsg,
29
+ PlainObject,
30
+ SelectKeyRingMsg,
31
+ ShowSensitiveKeyRingDataMsg,
32
+ UnlockKeyRingMsg,
33
+ } from "@keplr-wallet/background";
34
+ import type { MultiAccounts } from "@keplr-wallet/background";
35
+ import { ChainInfo } from "@keplr-wallet/types";
36
+ import { ChainIdHelper } from "@keplr-wallet/cosmos";
37
+
38
+ export class KeyRingStore {
39
+ @observable
40
+ protected _isInitialized: boolean = false;
41
+
42
+ @observable
43
+ protected _status: KeyRingStatus | "not-loaded" = "not-loaded";
44
+
45
+ @observable
46
+ protected _needMigration: boolean = false;
47
+
48
+ @observable
49
+ protected _isMigrating: boolean = false;
50
+
51
+ @observable.ref
52
+ protected _keyInfos: KeyInfo[] = [];
53
+
54
+ constructor(
55
+ protected readonly eventDispatcher: {
56
+ dispatchEvent: (type: string) => void;
57
+ },
58
+ protected readonly requester: MessageRequester
59
+ ) {
60
+ makeObservable(this);
61
+
62
+ this.init();
63
+ }
64
+
65
+ async init(): Promise<void> {
66
+ await this.refreshKeyRingStatus();
67
+
68
+ runInAction(() => {
69
+ this._isInitialized = true;
70
+ });
71
+ }
72
+
73
+ get needMigration(): boolean {
74
+ return this._needMigration;
75
+ }
76
+
77
+ get isMigrating(): boolean {
78
+ return this._isMigrating;
79
+ }
80
+
81
+ get isInitialized(): boolean {
82
+ return this._isInitialized;
83
+ }
84
+
85
+ async waitUntilInitialized(): Promise<void> {
86
+ if (this.isInitialized) {
87
+ return;
88
+ }
89
+
90
+ return new Promise((resolve) => {
91
+ const disposal = autorun(() => {
92
+ if (this.isInitialized) {
93
+ resolve();
94
+
95
+ if (disposal) {
96
+ disposal();
97
+ }
98
+ }
99
+ });
100
+ });
101
+ }
102
+
103
+ get status(): KeyRingStatus | "not-loaded" {
104
+ return this._status;
105
+ }
106
+
107
+ get keyInfos(): KeyInfo[] {
108
+ return this._keyInfos;
109
+ }
110
+
111
+ async fetchKeyRingStatus(): Promise<KeyRingStatus> {
112
+ const msg = new GetKeyRingStatusOnlyMsg();
113
+ const result = await this.requester.sendMessage(BACKGROUND_PORT, msg);
114
+ return result.status;
115
+ }
116
+
117
+ @computed
118
+ get selectedKeyInfo(): KeyInfo | undefined {
119
+ return this._keyInfos.find((keyInfo) => keyInfo.isSelected);
120
+ }
121
+
122
+ get isEmpty(): boolean {
123
+ return this._status === "empty";
124
+ }
125
+
126
+ @flow
127
+ *refreshKeyRingStatus() {
128
+ const msg = new GetKeyRingStatusMsg();
129
+ const result = yield* toGenerator(
130
+ this.requester.sendMessage(BACKGROUND_PORT, msg)
131
+ );
132
+ this._status = result.status;
133
+ this._keyInfos = result.keyInfos;
134
+ this._needMigration = result.needMigration;
135
+ this._isMigrating = result.isMigrating;
136
+ }
137
+
138
+ @flow
139
+ *selectKeyRing(vaultId: string) {
140
+ const msg = new SelectKeyRingMsg(vaultId);
141
+ const result = yield* toGenerator(
142
+ this.requester.sendMessage(BACKGROUND_PORT, msg)
143
+ );
144
+ this._status = result.status;
145
+ this._keyInfos = result.keyInfos;
146
+
147
+ this.eventDispatcher.dispatchEvent("keplr_keystorechange");
148
+ }
149
+
150
+ needKeyCoinTypeFinalize(vaultId: string, chainInfo: ChainInfo): boolean {
151
+ const keyInfo = this.keyInfos.find((keyInfo) => keyInfo.id === vaultId);
152
+ if (!keyInfo) {
153
+ return false;
154
+ }
155
+
156
+ if (keyInfo.type !== "mnemonic" && keyInfo.type !== "keystone") {
157
+ return false;
158
+ }
159
+
160
+ const coinTypeTag = `keyRing-${
161
+ ChainIdHelper.parse(chainInfo.chainId).identifier
162
+ }-coinType`;
163
+
164
+ return keyInfo.insensitive[coinTypeTag] == null;
165
+ }
166
+
167
+ async computeNotFinalizedKeyAddresses(
168
+ vaultId: string,
169
+ chainId: string
170
+ ): Promise<
171
+ {
172
+ coinType: number;
173
+ bech32Address: string;
174
+ }[]
175
+ > {
176
+ const msg = new ComputeNotFinalizedKeyAddressesMsg(vaultId, chainId);
177
+
178
+ return await this.requester.sendMessage(BACKGROUND_PORT, msg);
179
+ }
180
+
181
+ async showKeyRing(vaultId: string, password: string) {
182
+ const msg = new ShowSensitiveKeyRingDataMsg(vaultId, password);
183
+ return await this.requester.sendMessage(BACKGROUND_PORT, msg);
184
+ }
185
+
186
+ @flow
187
+ *finalizeKeyCoinType(vaultId: string, chainId: string, coinType: number) {
188
+ const msg = new FinalizeKeyCoinTypeMsg(vaultId, chainId, coinType);
189
+ const result = yield* toGenerator(
190
+ this.requester.sendMessage(BACKGROUND_PORT, msg)
191
+ );
192
+ this._status = result.status;
193
+ this._keyInfos = result.keyInfos;
194
+
195
+ this.eventDispatcher.dispatchEvent("keplr_keystorechange");
196
+ }
197
+
198
+ @flow
199
+ *newMnemonicKey(
200
+ mnemonic: string,
201
+ bip44HDPath: BIP44HDPath,
202
+ name: string,
203
+ password: string | undefined
204
+ ) {
205
+ const msg = new NewMnemonicKeyMsg(mnemonic, bip44HDPath, name, password);
206
+ const result = yield* toGenerator(
207
+ this.requester.sendMessage(BACKGROUND_PORT, msg)
208
+ );
209
+ this._status = result.status;
210
+ this._keyInfos = result.keyInfos;
211
+
212
+ this.eventDispatcher.dispatchEvent("keplr_keystorechange");
213
+
214
+ return result.vaultId;
215
+ }
216
+
217
+ @flow
218
+ *newLedgerKey(
219
+ pubKey: Uint8Array,
220
+ app: string,
221
+ bip44HDPath: BIP44HDPath,
222
+ name: string,
223
+ password: string | undefined
224
+ ) {
225
+ const msg = new NewLedgerKeyMsg(pubKey, app, bip44HDPath, name, password);
226
+ const result = yield* toGenerator(
227
+ this.requester.sendMessage(BACKGROUND_PORT, msg)
228
+ );
229
+ this._status = result.status;
230
+ this._keyInfos = result.keyInfos;
231
+
232
+ this.eventDispatcher.dispatchEvent("keplr_keystorechange");
233
+
234
+ return result.vaultId;
235
+ }
236
+
237
+ @flow
238
+ *appendLedgerKeyApp(vaultId: string, pubKey: Uint8Array, app: string) {
239
+ const msg = new AppendLedgerKeyAppMsg(vaultId, pubKey, app);
240
+ const result = yield* toGenerator(
241
+ this.requester.sendMessage(BACKGROUND_PORT, msg)
242
+ );
243
+ this._status = result.status;
244
+ this._keyInfos = result.keyInfos;
245
+
246
+ this.eventDispatcher.dispatchEvent("keplr_keystorechange");
247
+ }
248
+
249
+ @flow
250
+ *newKeystoneKey(
251
+ multiAccounts: MultiAccounts,
252
+ name: string,
253
+ password: string | undefined
254
+ ) {
255
+ const msg = new NewKeystoneKeyMsg(multiAccounts, name, password);
256
+ const result = yield* toGenerator(
257
+ this.requester.sendMessage(BACKGROUND_PORT, msg)
258
+ );
259
+ this._status = result.status;
260
+ this._keyInfos = result.keyInfos;
261
+
262
+ this.eventDispatcher.dispatchEvent("keplr_keystorechange");
263
+
264
+ return result.vaultId;
265
+ }
266
+
267
+ @flow
268
+ *newPrivateKeyKey(
269
+ privateKey: Uint8Array,
270
+ meta: PlainObject,
271
+ name: string,
272
+ password: string | undefined
273
+ ) {
274
+ const msg = new NewPrivateKeyKeyMsg(privateKey, meta, name, password);
275
+ const result = yield* toGenerator(
276
+ this.requester.sendMessage(BACKGROUND_PORT, msg)
277
+ );
278
+ this._status = result.status;
279
+ this._keyInfos = result.keyInfos;
280
+
281
+ this.eventDispatcher.dispatchEvent("keplr_keystorechange");
282
+
283
+ return result.vaultId;
284
+ }
285
+
286
+ @flow
287
+ *lock() {
288
+ const msg = new LockKeyRingMsg();
289
+ const result = yield* toGenerator(
290
+ this.requester.sendMessage(BACKGROUND_PORT, msg)
291
+ );
292
+ this._status = result.status;
293
+ }
294
+
295
+ @flow
296
+ *unlock(password: string) {
297
+ if (this._needMigration) {
298
+ this._isMigrating = true;
299
+ }
300
+
301
+ try {
302
+ const msg = new UnlockKeyRingMsg(password);
303
+ const result = yield* toGenerator(
304
+ this.requester.sendMessage(BACKGROUND_PORT, msg)
305
+ );
306
+ this._status = result.status;
307
+ this._keyInfos = result.keyInfos;
308
+
309
+ this._needMigration = false;
310
+ } finally {
311
+ // Set the flag to false even if the migration is failed.
312
+ this._isMigrating = false;
313
+ }
314
+ }
315
+
316
+ @flow
317
+ *changeKeyRingName(vaultId: string, name: string) {
318
+ const msg = new ChangeKeyRingNameMsg(vaultId, name);
319
+ const result = yield* toGenerator(
320
+ this.requester.sendMessage(BACKGROUND_PORT, msg)
321
+ );
322
+ this._status = result.status;
323
+ this._keyInfos = result.keyInfos;
324
+
325
+ this.eventDispatcher.dispatchEvent("keplr_keystorechange");
326
+ }
327
+
328
+ @flow
329
+ *deleteKeyRing(vaultId: string, password: string) {
330
+ const msg = new DeleteKeyRingMsg(vaultId, password);
331
+ const result = yield* toGenerator(
332
+ this.requester.sendMessage(BACKGROUND_PORT, msg)
333
+ );
334
+ this._status = result.status;
335
+ this._keyInfos = result.keyInfos;
336
+
337
+ if (result.wasSelected && result.status === "unlocked") {
338
+ this.eventDispatcher.dispatchEvent("keplr_keystorechange");
339
+ }
340
+ }
341
+
342
+ async changeUserPassword(
343
+ prevUserPassword: string,
344
+ newUserPassword: string
345
+ ): Promise<void> {
346
+ const msg = new ChangeUserPasswordMsg(prevUserPassword, newUserPassword);
347
+ return await this.requester.sendMessage(BACKGROUND_PORT, msg);
348
+ }
349
+
350
+ async checkLegacyKeyRingPassword(password: string): Promise<void> {
351
+ const msg = new CheckLegacyKeyRingPasswordMsg(password);
352
+ return await this.requester.sendMessage(BACKGROUND_PORT, msg);
353
+ }
354
+ }
@@ -0,0 +1,114 @@
1
+ import { BACKGROUND_PORT, MessageRequester } from "@keplr-wallet/router";
2
+ import { computed, makeObservable, observable, runInAction } from "mobx";
3
+ import {
4
+ AllPermissionDataPerOrigin,
5
+ ClearAllPermissionsMsg,
6
+ ClearOriginPermissionMsg,
7
+ GetAllPermissionDataPerOriginMsg,
8
+ RemoveGlobalPermissionOriginMsg,
9
+ RemovePermissionOrigin,
10
+ } from "@keplr-wallet/background";
11
+ import { ChainIdHelper } from "@keplr-wallet/cosmos";
12
+
13
+ export class PermissionManagerStore {
14
+ @observable
15
+ protected _isInitialized: boolean = false;
16
+
17
+ @observable
18
+ protected _permissionData: AllPermissionDataPerOrigin = {};
19
+
20
+ constructor(protected readonly requester: MessageRequester) {
21
+ makeObservable(this);
22
+
23
+ this.init();
24
+ }
25
+
26
+ protected async init() {
27
+ const msg = new GetAllPermissionDataPerOriginMsg();
28
+ const result = await this.requester.sendMessage(BACKGROUND_PORT, msg);
29
+ runInAction(() => {
30
+ this._permissionData = result;
31
+ });
32
+
33
+ runInAction(() => {
34
+ this._isInitialized = true;
35
+ });
36
+ }
37
+
38
+ get isInitialized(): boolean {
39
+ return this._isInitialized;
40
+ }
41
+
42
+ @computed
43
+ get permissionData(): AllPermissionDataPerOrigin {
44
+ // Background has origin with empty permissions.
45
+ // However, it is not needed to show in the UI.
46
+ // So, trim the empty origins.
47
+ const trimmed: AllPermissionDataPerOrigin = {};
48
+ for (const [origin, perms] of Object.entries(this._permissionData)) {
49
+ if (
50
+ perms &&
51
+ (perms.permissions.length > 0 || perms.globalPermissions.length > 0)
52
+ ) {
53
+ trimmed[origin] = perms;
54
+ }
55
+ }
56
+
57
+ return trimmed;
58
+ }
59
+
60
+ async clearAllPermissions() {
61
+ runInAction(() => {
62
+ this._permissionData = {};
63
+ });
64
+
65
+ const msg = new ClearAllPermissionsMsg();
66
+ await this.requester.sendMessage(BACKGROUND_PORT, msg);
67
+ }
68
+
69
+ async clearOrigin(origin: string) {
70
+ const perms = this._permissionData[origin];
71
+ if (perms) {
72
+ runInAction(() => {
73
+ delete this._permissionData[origin];
74
+ });
75
+
76
+ const msg = new ClearOriginPermissionMsg(origin);
77
+ await this.requester.sendMessage(BACKGROUND_PORT, msg);
78
+ }
79
+ }
80
+
81
+ async removePermission(origin: string, chainId: string, type: string) {
82
+ const chainIdentifier = ChainIdHelper.parse(chainId).identifier;
83
+ const perms = this._permissionData[origin];
84
+ const i = perms
85
+ ? perms.permissions.findIndex(
86
+ (perm) =>
87
+ perm.chainIdentifier === chainIdentifier && perm.type === type
88
+ )
89
+ : -1;
90
+ if (i >= 0 && perms) {
91
+ runInAction(() => {
92
+ perms.permissions.splice(i, 1);
93
+ });
94
+
95
+ const msg = new RemovePermissionOrigin(chainId, type, origin);
96
+ await this.requester.sendMessage(BACKGROUND_PORT, msg);
97
+ }
98
+ }
99
+
100
+ async removeGlobalPermission(origin: string, type: string) {
101
+ const perms = this._permissionData[origin];
102
+ const i = perms
103
+ ? perms.globalPermissions.findIndex((perm) => perm.type === type)
104
+ : -1;
105
+ if (i >= 0 && perms) {
106
+ runInAction(() => {
107
+ perms.globalPermissions.splice(i, 1);
108
+ });
109
+
110
+ const msg = new RemoveGlobalPermissionOriginMsg(type, origin);
111
+ await this.requester.sendMessage(BACKGROUND_PORT, msg);
112
+ }
113
+ }
114
+ }