@keplr-wallet/provider 0.12.0-alpha.0 → 0.12.0-alpha.3

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/src/inject.ts CHANGED
@@ -7,29 +7,31 @@ import {
7
7
  KeplrMode,
8
8
  KeplrSignOptions,
9
9
  Key,
10
- } from "@keplr-wallet/types";
11
- import { Result, JSONUint8Array } from "@keplr-wallet/router";
12
- import {
13
10
  BroadcastMode,
14
11
  AminoSignResponse,
15
12
  StdSignDoc,
16
- StdTx,
17
- OfflineSigner,
13
+ OfflineAminoSigner,
18
14
  StdSignature,
19
- } from "@cosmjs/launchpad";
20
- import { SecretUtils } from "secretjs/types/enigmautils";
21
-
15
+ StdTx,
16
+ DirectSignResponse,
17
+ OfflineDirectSigner,
18
+ ICNSAdr36Signatures,
19
+ ChainInfoWithoutEndpoints,
20
+ SecretUtils,
21
+ SettledResponses,
22
+ } from "@keplr-wallet/types";
23
+ import { Result } from "@keplr-wallet/router";
24
+ import { JSONUint8Array } from "@keplr-wallet/common";
22
25
  import { KeplrEnigmaUtils } from "./enigma";
23
- import { DirectSignResponse, OfflineDirectSigner } from "@cosmjs/proto-signing";
24
-
25
26
  import { CosmJSOfflineSigner, CosmJSOfflineSignerOnlyAmino } from "./cosmjs";
26
27
  import deepmerge from "deepmerge";
27
28
  import Long from "long";
29
+ import { KeplrCoreTypes } from "./core-types";
28
30
 
29
31
  export interface ProxyRequest {
30
32
  type: "proxy-request";
31
33
  id: string;
32
- method: keyof Keplr;
34
+ method: keyof (Keplr & KeplrCoreTypes);
33
35
  args: any[];
34
36
  }
35
37
 
@@ -39,15 +41,57 @@ export interface ProxyRequestResponse {
39
41
  result: Result | undefined;
40
42
  }
41
43
 
44
+ function defineUnwritablePropertyIfPossible(o: any, p: string, value: any) {
45
+ const descriptor = Object.getOwnPropertyDescriptor(o, p);
46
+ if (!descriptor || descriptor.writable) {
47
+ if (!descriptor || descriptor.configurable) {
48
+ Object.defineProperty(o, p, {
49
+ value,
50
+ writable: false,
51
+ });
52
+ } else {
53
+ o[p] = value;
54
+ }
55
+ } else {
56
+ console.warn(
57
+ `Failed to inject ${p} from keplr. Probably, other wallet is trying to intercept Keplr`
58
+ );
59
+ }
60
+ }
61
+
62
+ export function injectKeplrToWindow(keplr: IKeplr): void {
63
+ defineUnwritablePropertyIfPossible(window, "keplr", keplr);
64
+ defineUnwritablePropertyIfPossible(
65
+ window,
66
+ "getOfflineSigner",
67
+ keplr.getOfflineSigner
68
+ );
69
+ defineUnwritablePropertyIfPossible(
70
+ window,
71
+ "getOfflineSignerOnlyAmino",
72
+ keplr.getOfflineSignerOnlyAmino
73
+ );
74
+ defineUnwritablePropertyIfPossible(
75
+ window,
76
+ "getOfflineSignerAuto",
77
+ keplr.getOfflineSignerAuto
78
+ );
79
+ defineUnwritablePropertyIfPossible(
80
+ window,
81
+ "getEnigmaUtils",
82
+ keplr.getEnigmaUtils
83
+ );
84
+ }
85
+
42
86
  /**
43
87
  * InjectedKeplr would be injected to the webpage.
44
88
  * In the webpage, it can't request any messages to the extension because it doesn't have any API related to the extension.
45
89
  * So, to request some methods of the extension, this will proxy the request to the content script that is injected to webpage on the extension level.
46
90
  * This will use `window.postMessage` to interact with the content script.
47
91
  */
48
- export class InjectedKeplr implements IKeplr {
92
+ export class InjectedKeplr implements IKeplr, KeplrCoreTypes {
49
93
  static startProxy(
50
- keplr: IKeplr,
94
+ keplr: IKeplr & KeplrCoreTypes,
51
95
  eventListener: {
52
96
  addMessageListener: (fn: (e: any) => void) => void;
53
97
  postMessage: (message: any) => void;
@@ -172,7 +216,10 @@ export class InjectedKeplr implements IKeplr {
172
216
  });
173
217
  }
174
218
 
175
- protected requestMethod(method: keyof IKeplr, args: any[]): Promise<any> {
219
+ protected requestMethod(
220
+ method: keyof (IKeplr & KeplrCoreTypes),
221
+ args: any[]
222
+ ): Promise<any> {
176
223
  const bytes = new Uint8Array(8);
177
224
  const id: string = Array.from(crypto.getRandomValues(bytes))
178
225
  .map((value) => {
@@ -244,12 +291,53 @@ export class InjectedKeplr implements IKeplr {
244
291
  window.postMessage(message, window.location.origin),
245
292
  },
246
293
  protected readonly parseMessage?: (message: any) => any
247
- ) {}
294
+ ) {
295
+ // Freeze fields/method except for "defaultOptions"
296
+ // Intentionally, "defaultOptions" can be mutated to allow a webpage to change the options with cosmjs usage.
297
+ // Freeze fields
298
+ const fieldNames = Object.keys(this);
299
+ for (const fieldName of fieldNames) {
300
+ if (fieldName !== "defaultOptions") {
301
+ Object.defineProperty(this, fieldName, {
302
+ value: (this as any)[fieldName],
303
+ writable: false,
304
+ });
305
+ }
306
+
307
+ // If field is "eventListener", try to iterate one-level deep.
308
+ if (fieldName === "eventListener") {
309
+ const fieldNames = Object.keys(this.eventListener);
310
+ for (const fieldName of fieldNames) {
311
+ Object.defineProperty(this.eventListener, fieldName, {
312
+ value: (this.eventListener as any)[fieldName],
313
+ writable: false,
314
+ });
315
+ }
316
+ }
317
+ }
318
+ // Freeze methods
319
+ const methodNames = Object.getOwnPropertyNames(InjectedKeplr.prototype);
320
+ for (const methodName of methodNames) {
321
+ if (
322
+ methodName !== "constructor" &&
323
+ typeof (this as any)[methodName] === "function"
324
+ ) {
325
+ Object.defineProperty(this, methodName, {
326
+ value: (this as any)[methodName].bind(this),
327
+ writable: false,
328
+ });
329
+ }
330
+ }
331
+ }
248
332
 
249
333
  async enable(chainIds: string | string[]): Promise<void> {
250
334
  await this.requestMethod("enable", [chainIds]);
251
335
  }
252
336
 
337
+ async disable(chainIds?: string | string[]): Promise<void> {
338
+ await this.requestMethod("disable", [chainIds]);
339
+ }
340
+
253
341
  async experimentalSuggestChain(chainInfo: ChainInfo): Promise<void> {
254
342
  if (
255
343
  chainInfo.features?.includes("stargate") ||
@@ -267,6 +355,10 @@ export class InjectedKeplr implements IKeplr {
267
355
  return await this.requestMethod("getKey", [chainId]);
268
356
  }
269
357
 
358
+ async getKeysSettled(chainIds: string[]): Promise<SettledResponses<Key>> {
359
+ return await this.requestMethod("getKeysSettled", [chainIds]);
360
+ }
361
+
270
362
  async sendTx(
271
363
  chainId: string,
272
364
  tx: StdTx | Uint8Array,
@@ -350,6 +442,22 @@ export class InjectedKeplr implements IKeplr {
350
442
  return await this.requestMethod("signArbitrary", [chainId, signer, data]);
351
443
  }
352
444
 
445
+ signICNSAdr36(
446
+ chainId: string,
447
+ contractAddress: string,
448
+ owner: string,
449
+ username: string,
450
+ addressChainIds: string[]
451
+ ): Promise<ICNSAdr36Signatures> {
452
+ return this.requestMethod("signICNSAdr36", [
453
+ chainId,
454
+ contractAddress,
455
+ owner,
456
+ username,
457
+ addressChainIds,
458
+ ]);
459
+ }
460
+
353
461
  async verifyArbitrary(
354
462
  chainId: string,
355
463
  signer: string,
@@ -378,17 +486,17 @@ export class InjectedKeplr implements IKeplr {
378
486
  ]);
379
487
  }
380
488
 
381
- getOfflineSigner(chainId: string): OfflineSigner & OfflineDirectSigner {
489
+ getOfflineSigner(chainId: string): OfflineAminoSigner & OfflineDirectSigner {
382
490
  return new CosmJSOfflineSigner(chainId, this);
383
491
  }
384
492
 
385
- getOfflineSignerOnlyAmino(chainId: string): OfflineSigner {
493
+ getOfflineSignerOnlyAmino(chainId: string): OfflineAminoSigner {
386
494
  return new CosmJSOfflineSignerOnlyAmino(chainId, this);
387
495
  }
388
496
 
389
497
  async getOfflineSignerAuto(
390
498
  chainId: string
391
- ): Promise<OfflineSigner | OfflineDirectSigner> {
499
+ ): Promise<OfflineAminoSigner | OfflineDirectSigner> {
392
500
  const key = await this.getKey(chainId);
393
501
  if (key.isNanoLedger) {
394
502
  return new CosmJSOfflineSignerOnlyAmino(chainId, this);
@@ -467,4 +575,44 @@ export class InjectedKeplr implements IKeplr {
467
575
  this.enigmaUtils.set(chainId, enigmaUtils);
468
576
  return enigmaUtils;
469
577
  }
578
+
579
+ async experimentalSignEIP712CosmosTx_v0(
580
+ chainId: string,
581
+ signer: string,
582
+ eip712: {
583
+ types: Record<string, { name: string; type: string }[] | undefined>;
584
+ domain: Record<string, any>;
585
+ primaryType: string;
586
+ },
587
+ signDoc: StdSignDoc,
588
+ signOptions: KeplrSignOptions = {}
589
+ ): Promise<AminoSignResponse> {
590
+ return await this.requestMethod("experimentalSignEIP712CosmosTx_v0", [
591
+ chainId,
592
+ signer,
593
+ eip712,
594
+ signDoc,
595
+ deepmerge(this.defaultOptions.sign ?? {}, signOptions),
596
+ ]);
597
+ }
598
+
599
+ async getChainInfosWithoutEndpoints(): Promise<ChainInfoWithoutEndpoints[]> {
600
+ return await this.requestMethod("getChainInfosWithoutEndpoints", []);
601
+ }
602
+
603
+ __core__getAnalyticsId(): Promise<string> {
604
+ return this.requestMethod("__core__getAnalyticsId", []);
605
+ }
606
+
607
+ async changeKeyRingName({
608
+ defaultName,
609
+ editable = true,
610
+ }: {
611
+ defaultName: string;
612
+ editable?: boolean;
613
+ }): Promise<string> {
614
+ return await this.requestMethod("changeKeyRingName", [
615
+ { defaultName, editable },
616
+ ]);
617
+ }
470
618
  }
@@ -1 +0,0 @@
1
- export * from "./msgs";
@@ -1,14 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
- };
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./msgs"), exports);
14
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yCAAuB"}
@@ -1,150 +0,0 @@
1
- import { Message } from "@keplr-wallet/router";
2
- import { ChainInfo, EthSignType, KeplrSignOptions, Key } from "@keplr-wallet/types";
3
- import { AminoSignResponse, StdSignature, StdSignDoc } from "@cosmjs/launchpad";
4
- export declare class EnableAccessMsg extends Message<void> {
5
- readonly chainIds: string[];
6
- static type(): string;
7
- constructor(chainIds: string[]);
8
- validateBasic(): void;
9
- route(): string;
10
- type(): string;
11
- }
12
- export declare class GetKeyMsg extends Message<Key> {
13
- readonly chainId: string;
14
- static type(): string;
15
- constructor(chainId: string);
16
- validateBasic(): void;
17
- route(): string;
18
- type(): string;
19
- }
20
- export declare class SuggestChainInfoMsg extends Message<void> {
21
- readonly chainInfo: ChainInfo;
22
- static type(): string;
23
- constructor(chainInfo: ChainInfo);
24
- validateBasic(): void;
25
- route(): string;
26
- type(): string;
27
- }
28
- export declare class SuggestTokenMsg extends Message<void> {
29
- readonly chainId: string;
30
- readonly contractAddress: string;
31
- readonly viewingKey?: string | undefined;
32
- static type(): string;
33
- constructor(chainId: string, contractAddress: string, viewingKey?: string | undefined);
34
- validateBasic(): void;
35
- route(): string;
36
- type(): string;
37
- }
38
- export declare class SendTxMsg extends Message<Uint8Array> {
39
- readonly chainId: string;
40
- readonly tx: unknown;
41
- readonly mode: "async" | "sync" | "block";
42
- static type(): string;
43
- constructor(chainId: string, tx: unknown, mode: "async" | "sync" | "block");
44
- validateBasic(): void;
45
- route(): string;
46
- type(): string;
47
- }
48
- export declare class GetSecret20ViewingKey extends Message<string> {
49
- readonly chainId: string;
50
- readonly contractAddress: string;
51
- static type(): string;
52
- constructor(chainId: string, contractAddress: string);
53
- validateBasic(): void;
54
- route(): string;
55
- type(): string;
56
- }
57
- export declare class RequestSignAminoMsg extends Message<AminoSignResponse> {
58
- readonly chainId: string;
59
- readonly signer: string;
60
- readonly signDoc: StdSignDoc;
61
- readonly signOptions: KeplrSignOptions & {
62
- isADR36WithString?: boolean;
63
- ethSignType?: EthSignType;
64
- };
65
- static type(): string;
66
- constructor(chainId: string, signer: string, signDoc: StdSignDoc, signOptions?: KeplrSignOptions & {
67
- isADR36WithString?: boolean;
68
- ethSignType?: EthSignType;
69
- });
70
- validateBasic(): void;
71
- route(): string;
72
- type(): string;
73
- }
74
- export declare class RequestVerifyADR36AminoSignDoc extends Message<boolean> {
75
- readonly chainId: string;
76
- readonly signer: string;
77
- readonly data: Uint8Array;
78
- readonly signature: StdSignature;
79
- static type(): string;
80
- constructor(chainId: string, signer: string, data: Uint8Array, signature: StdSignature);
81
- validateBasic(): void;
82
- route(): string;
83
- type(): string;
84
- }
85
- export declare class RequestSignDirectMsg extends Message<{
86
- readonly signed: {
87
- bodyBytes: Uint8Array;
88
- authInfoBytes: Uint8Array;
89
- chainId: string;
90
- accountNumber: string;
91
- };
92
- readonly signature: StdSignature;
93
- }> {
94
- readonly chainId: string;
95
- readonly signer: string;
96
- readonly signDoc: {
97
- bodyBytes?: Uint8Array | null;
98
- authInfoBytes?: Uint8Array | null;
99
- chainId?: string | null;
100
- accountNumber?: string | null;
101
- };
102
- readonly signOptions: KeplrSignOptions;
103
- static type(): string;
104
- constructor(chainId: string, signer: string, signDoc: {
105
- bodyBytes?: Uint8Array | null;
106
- authInfoBytes?: Uint8Array | null;
107
- chainId?: string | null;
108
- accountNumber?: string | null;
109
- }, signOptions?: KeplrSignOptions);
110
- validateBasic(): void;
111
- route(): string;
112
- type(): string;
113
- }
114
- export declare class GetPubkeyMsg extends Message<Uint8Array> {
115
- readonly chainId: string;
116
- static type(): string;
117
- constructor(chainId: string);
118
- validateBasic(): void;
119
- route(): string;
120
- type(): string;
121
- }
122
- export declare class ReqeustEncryptMsg extends Message<Uint8Array> {
123
- readonly chainId: string;
124
- readonly contractCodeHash: string;
125
- readonly msg: object;
126
- static type(): string;
127
- constructor(chainId: string, contractCodeHash: string, msg: object);
128
- validateBasic(): void;
129
- route(): string;
130
- type(): string;
131
- }
132
- export declare class RequestDecryptMsg extends Message<Uint8Array> {
133
- readonly chainId: string;
134
- readonly cipherText: Uint8Array;
135
- readonly nonce: Uint8Array;
136
- static type(): string;
137
- constructor(chainId: string, cipherText: Uint8Array, nonce: Uint8Array);
138
- validateBasic(): void;
139
- route(): string;
140
- type(): string;
141
- }
142
- export declare class GetTxEncryptionKeyMsg extends Message<Uint8Array> {
143
- readonly chainId: string;
144
- readonly nonce: Uint8Array;
145
- static type(): string;
146
- constructor(chainId: string, nonce: Uint8Array);
147
- validateBasic(): void;
148
- route(): string;
149
- type(): string;
150
- }