@injectivelabs/wallet-cosmos 1.16.38 → 1.16.39-alpha.1

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.
@@ -1,3 +1,139 @@
1
- export { CosmosWallet } from './wallet.js';
2
- export { CosmosWalletStrategy } from './strategy/strategy.js';
3
- export * from './utils/index.js';
1
+ import { BaseConcreteStrategy, ConcreteWalletStrategy, SendTransactionOptions, StdSignDoc, Wallet, WalletDeviceType } from "@injectivelabs/wallet-base";
2
+ import * as _cosmjs_stargate0 from "@cosmjs/stargate";
3
+ import { StdFee } from "@cosmjs/stargate";
4
+ import { CosmosTxV1Beta1TxPb } from "@injectivelabs/sdk-ts";
5
+ import { TxResponse } from "@injectivelabs/sdk-ts/core/tx";
6
+ import { AccountAddress, ChainId, CosmosChainId, EvmChainId, TestnetCosmosChainId } from "@injectivelabs/ts-types";
7
+ import * as _keplr_wallet_types0 from "@keplr-wallet/types";
8
+ import { AminoSignResponse, Keplr, OfflineAminoSigner, StdSignDoc as StdSignDoc$1 } from "@keplr-wallet/types";
9
+ import { EncodeObject, OfflineSigner } from "@cosmjs/proto-signing";
10
+ import { AminoSignResponse as AminoSignResponse$1, DirectSignResponse, TxRaw } from "@injectivelabs/sdk-ts/types";
11
+
12
+ //#region src/utils/index.d.ts
13
+ declare const isCosmosWalletInstalled: (wallet: Wallet) => boolean;
14
+ declare const confirmCosmosAddress: ({
15
+ wallet,
16
+ chainId,
17
+ injectiveAddress
18
+ }: {
19
+ wallet: Wallet;
20
+ chainId: ChainId;
21
+ injectiveAddress: string;
22
+ }) => Promise<void>;
23
+ //#endregion
24
+ //#region src/wallet.d.ts
25
+ declare class CosmosWallet {
26
+ wallet: Wallet;
27
+ private chainId;
28
+ constructor({
29
+ wallet,
30
+ chainId
31
+ }: {
32
+ wallet: Wallet;
33
+ chainId: CosmosChainId | TestnetCosmosChainId | ChainId;
34
+ });
35
+ isChainIdSupported(chainId: CosmosChainId): Promise<boolean>;
36
+ getCosmosWallet(): Promise<Keplr>;
37
+ chainNotSupported(): Promise<void>;
38
+ getAccounts(): Promise<readonly _keplr_wallet_types0.AccountData[]>;
39
+ getKey(): Promise<{
40
+ name: string;
41
+ algo: string;
42
+ isNanoLedger: boolean;
43
+ pubKey: Uint8Array;
44
+ address: Uint8Array;
45
+ bech32Address: string;
46
+ }>;
47
+ getOfflineSigner(chainId?: string): Promise<OfflineSigner>;
48
+ getOfflineAminoSigner(): Promise<OfflineAminoSigner>;
49
+ /**
50
+ * This method is used to broadcast a transaction to the network.
51
+ * Since it uses the `Sync` mode, it will not wait for the transaction to be included in a block,
52
+ * so we have to make sure the transaction is included in a block after its broadcasted
53
+ *
54
+ * @param txRaw - raw transaction to broadcast
55
+ * @returns tx hash
56
+ */
57
+ broadcastTx(txRaw: CosmosTxV1Beta1TxPb.TxRaw): Promise<string>;
58
+ /**
59
+ * This method is used to broadcast a transaction to the network.
60
+ * Since it uses the `Block` mode, and it will wait for the transaction to be included in a block,
61
+ *
62
+ * @param txRaw - raw transaction to broadcast
63
+ * @returns tx hash
64
+ */
65
+ broadcastTxBlock(txRaw: CosmosTxV1Beta1TxPb.TxRaw): Promise<string>;
66
+ signAndBroadcastAminoUsingCosmjs(messages: EncodeObject[], stdFee: StdFee, endpoints: {
67
+ rest: string;
68
+ rpc: string;
69
+ }): Promise<_cosmjs_stargate0.DeliverTxResponse>;
70
+ signArbitrary({
71
+ data,
72
+ signer
73
+ }: {
74
+ signer: string;
75
+ data: string | Uint8Array;
76
+ }): Promise<string>;
77
+ signEIP712CosmosTx({
78
+ eip712,
79
+ signDoc
80
+ }: {
81
+ eip712: any;
82
+ signDoc: StdSignDoc$1;
83
+ }): Promise<AminoSignResponse>;
84
+ checkChainIdSupport(): Promise<boolean>;
85
+ private getCosmos;
86
+ disableGasCheck(): Promise<void>;
87
+ enableGasCheck(): Promise<void>;
88
+ }
89
+ //#endregion
90
+ //#region src/strategy/strategy.d.ts
91
+ declare class CosmosWalletStrategy extends BaseConcreteStrategy implements ConcreteWalletStrategy {
92
+ wallet: Wallet;
93
+ private cosmosWallet;
94
+ constructor(args: {
95
+ chainId: ChainId | CosmosChainId;
96
+ endpoints?: {
97
+ rest: string;
98
+ rpc: string;
99
+ };
100
+ } & {
101
+ wallet: Wallet;
102
+ });
103
+ getWalletDeviceType(): Promise<WalletDeviceType>;
104
+ enable(): Promise<boolean>;
105
+ disconnect(): Promise<void>;
106
+ getAddresses(): Promise<string[]>;
107
+ getAddressesInfo(): Promise<{
108
+ address: string;
109
+ derivationPath: string;
110
+ baseDerivationPath: string;
111
+ }[]>;
112
+ getSessionOrConfirm(address: AccountAddress): Promise<string>;
113
+ sendEvmTransaction(_transaction: unknown, _options: {
114
+ address: AccountAddress;
115
+ evmChainId: EvmChainId;
116
+ }): Promise<string>;
117
+ sendTransaction(transaction: DirectSignResponse | TxRaw, options: SendTransactionOptions): Promise<TxResponse>;
118
+ signAminoCosmosTransaction(transaction: {
119
+ address: string;
120
+ signDoc: StdSignDoc;
121
+ }): Promise<AminoSignResponse$1>;
122
+ signCosmosTransaction(transaction: {
123
+ txRaw: TxRaw;
124
+ accountNumber: number;
125
+ chainId: string;
126
+ address: AccountAddress;
127
+ }): Promise<DirectSignResponse>;
128
+ signEip712TypedData(_eip712TypedData: string, _address: AccountAddress): Promise<string>;
129
+ signArbitrary(signer: string, data: string | Uint8Array): Promise<string>;
130
+ getEthereumChainId(): Promise<string>;
131
+ getEvmTransactionReceipt(_txHash: string): Promise<string>;
132
+ getPubKey(): Promise<string>;
133
+ onAccountChange(callback: (account: AccountAddress | string[]) => void): Promise<void>;
134
+ getCosmosWallet(chainId: ChainId): CosmosWallet;
135
+ getOfflineSigner(chainId?: string): Promise<OfflineSigner>;
136
+ private getCurrentCosmosWallet;
137
+ }
138
+ //#endregion
139
+ export { CosmosWallet, CosmosWalletStrategy, confirmCosmosAddress, isCosmosWalletInstalled };
package/dist/esm/index.js CHANGED
@@ -1,3 +1,465 @@
1
- export { CosmosWallet } from './wallet.js';
2
- export { CosmosWalletStrategy } from './strategy/strategy.js';
3
- export * from './utils/index.js';
1
+ import { capitalize } from "@injectivelabs/utils";
2
+ import { BaseConcreteStrategy, BroadcastMode, Wallet, WalletAction, WalletDeviceType, WalletEventListener, createCosmosSignDocFromSignDoc } from "@injectivelabs/wallet-base";
3
+ import { PublicKey } from "@injectivelabs/sdk-ts/core/accounts";
4
+ import { stringToUint8Array, uint8ArrayToBase64, uint8ArrayToHex } from "@injectivelabs/sdk-ts/utils";
5
+ import { CosmosWalletException, ErrorType, GeneralException, TransactionException, UnspecifiedErrorCode, WalletErrorActionModule } from "@injectivelabs/exceptions";
6
+ import { SigningStargateClient } from "@cosmjs/stargate";
7
+ import { CosmosTxV1Beta1TxPb } from "@injectivelabs/sdk-ts";
8
+ import { createSignDocFromTransaction, createTxRawFromSigResponse, waitTxBroadcasted } from "@injectivelabs/sdk-ts/core/tx";
9
+
10
+ //#region \0@oxc-project+runtime@0.98.0/helpers/typeof.js
11
+ function _typeof(o) {
12
+ "@babel/helpers - typeof";
13
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o$1) {
14
+ return typeof o$1;
15
+ } : function(o$1) {
16
+ return o$1 && "function" == typeof Symbol && o$1.constructor === Symbol && o$1 !== Symbol.prototype ? "symbol" : typeof o$1;
17
+ }, _typeof(o);
18
+ }
19
+
20
+ //#endregion
21
+ //#region \0@oxc-project+runtime@0.98.0/helpers/toPrimitive.js
22
+ function toPrimitive(t, r) {
23
+ if ("object" != _typeof(t) || !t) return t;
24
+ var e = t[Symbol.toPrimitive];
25
+ if (void 0 !== e) {
26
+ var i = e.call(t, r || "default");
27
+ if ("object" != _typeof(i)) return i;
28
+ throw new TypeError("@@toPrimitive must return a primitive value.");
29
+ }
30
+ return ("string" === r ? String : Number)(t);
31
+ }
32
+
33
+ //#endregion
34
+ //#region \0@oxc-project+runtime@0.98.0/helpers/toPropertyKey.js
35
+ function toPropertyKey(t) {
36
+ var i = toPrimitive(t, "string");
37
+ return "symbol" == _typeof(i) ? i : i + "";
38
+ }
39
+
40
+ //#endregion
41
+ //#region \0@oxc-project+runtime@0.98.0/helpers/defineProperty.js
42
+ function _defineProperty(e, r, t) {
43
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
44
+ value: t,
45
+ enumerable: !0,
46
+ configurable: !0,
47
+ writable: !0
48
+ }) : e[r] = t, e;
49
+ }
50
+
51
+ //#endregion
52
+ //#region src/wallet.ts
53
+ const $window = typeof window !== "undefined" ? window : {};
54
+ var CosmosWallet = class CosmosWallet {
55
+ constructor({ wallet, chainId }) {
56
+ _defineProperty(this, "wallet", void 0);
57
+ _defineProperty(this, "chainId", void 0);
58
+ this.wallet = wallet;
59
+ this.chainId = chainId;
60
+ }
61
+ async isChainIdSupported(chainId) {
62
+ const { wallet } = this;
63
+ return new CosmosWallet({
64
+ chainId,
65
+ wallet
66
+ }).checkChainIdSupport();
67
+ }
68
+ async getCosmosWallet() {
69
+ const { chainId } = this;
70
+ const cosmos = this.getCosmos();
71
+ try {
72
+ await cosmos.enable(chainId);
73
+ return cosmos;
74
+ } catch (e) {
75
+ throw new CosmosWalletException(new Error(e.message));
76
+ }
77
+ }
78
+ async chainNotSupported() {
79
+ const { chainId, wallet } = this;
80
+ const chainName = chainId.split("-");
81
+ const context = wallet === Wallet.Keplr ? "https://chains.keplr.app/" : wallet === Wallet.OWallet ? "https://owallet.io/" : void 0;
82
+ throw new CosmosWalletException(/* @__PURE__ */ new Error(`${capitalize(wallet)} may not support ${chainName[0] || chainId} network. Please check if the chain can be added.`), context ? { context } : {});
83
+ }
84
+ async getAccounts() {
85
+ const { chainId } = this;
86
+ const cosmos = this.getCosmos();
87
+ try {
88
+ return cosmos.getOfflineSigner(chainId).getAccounts();
89
+ } catch (e) {
90
+ throw new CosmosWalletException(new Error(e.message), { contextModule: WalletErrorActionModule.GetAccounts });
91
+ }
92
+ }
93
+ async getKey() {
94
+ const { wallet, chainId } = this;
95
+ const cosmosWallet = await this.getCosmosWallet();
96
+ try {
97
+ return cosmosWallet.getKey(chainId);
98
+ } catch (e) {
99
+ throw new CosmosWalletException(new Error(e.message), { contextModule: wallet });
100
+ }
101
+ }
102
+ async getOfflineSigner(chainId) {
103
+ const { wallet } = this;
104
+ try {
105
+ return this.getCosmos().getOfflineSigner(chainId || this.chainId);
106
+ } catch (e) {
107
+ throw new CosmosWalletException(new Error(e.message), { contextModule: wallet });
108
+ }
109
+ }
110
+ async getOfflineAminoSigner() {
111
+ const { chainId, wallet } = this;
112
+ if (![Wallet.Keplr, Wallet.OWallet].includes(wallet)) throw new CosmosWalletException(/* @__PURE__ */ new Error(`getOfflineAminoSigner is not support on ${capitalize(wallet)}`));
113
+ const cosmosWallet = await this.getCosmosWallet();
114
+ try {
115
+ return cosmosWallet.getOfflineSignerOnlyAmino(chainId);
116
+ } catch (e) {
117
+ throw new CosmosWalletException(new Error(e.message), { context: wallet });
118
+ }
119
+ }
120
+ /**
121
+ * This method is used to broadcast a transaction to the network.
122
+ * Since it uses the `Sync` mode, it will not wait for the transaction to be included in a block,
123
+ * so we have to make sure the transaction is included in a block after its broadcasted
124
+ *
125
+ * @param txRaw - raw transaction to broadcast
126
+ * @returns tx hash
127
+ */
128
+ async broadcastTx(txRaw) {
129
+ const { chainId, wallet } = this;
130
+ const cosmosWallet = await this.getCosmosWallet();
131
+ try {
132
+ const result = await cosmosWallet.sendTx(chainId, CosmosTxV1Beta1TxPb.TxRaw.toBinary(txRaw), BroadcastMode.Sync);
133
+ if (!result || result.length === 0) throw new TransactionException(/* @__PURE__ */ new Error("Transaction failed to be broadcasted"), { contextModule: wallet });
134
+ return uint8ArrayToHex(result);
135
+ } catch (e) {
136
+ if (e instanceof TransactionException) throw e;
137
+ throw new CosmosWalletException(new Error(e.message), {
138
+ context: "broadcast-tx",
139
+ contextModule: wallet
140
+ });
141
+ }
142
+ }
143
+ /**
144
+ * This method is used to broadcast a transaction to the network.
145
+ * Since it uses the `Block` mode, and it will wait for the transaction to be included in a block,
146
+ *
147
+ * @param txRaw - raw transaction to broadcast
148
+ * @returns tx hash
149
+ */
150
+ async broadcastTxBlock(txRaw) {
151
+ const { chainId, wallet } = this;
152
+ const cosmosWallet = await this.getCosmosWallet();
153
+ try {
154
+ const result = await cosmosWallet.sendTx(chainId, CosmosTxV1Beta1TxPb.TxRaw.toBinary(txRaw), BroadcastMode.Block);
155
+ if (!result || result.length === 0) throw new TransactionException(/* @__PURE__ */ new Error("Transaction failed to be broadcasted"), { contextModule: wallet });
156
+ return uint8ArrayToHex(result);
157
+ } catch (e) {
158
+ if (e instanceof TransactionException) throw e;
159
+ throw new CosmosWalletException(new Error(e.message), {
160
+ context: "broadcast-tx",
161
+ contextModule: wallet
162
+ });
163
+ }
164
+ }
165
+ async signAndBroadcastAminoUsingCosmjs(messages, stdFee, endpoints) {
166
+ const { chainId, wallet } = this;
167
+ const cosmosWallet = await this.getCosmosWallet();
168
+ if (![Wallet.Keplr, Wallet.OWallet].includes(wallet)) throw new CosmosWalletException(/* @__PURE__ */ new Error(`signAndBroadcastAminoUsingCosmjs is not support on ${capitalize(wallet)}`));
169
+ if (!endpoints.rpc) throw new GeneralException(/* @__PURE__ */ new Error(`Please provide rpc endpoint`));
170
+ const offlineSigner = cosmosWallet.getOfflineSignerOnlyAmino(chainId);
171
+ const [account] = await offlineSigner.getAccounts();
172
+ return await (await SigningStargateClient.connectWithSigner(endpoints.rpc, offlineSigner)).signAndBroadcast(account.address, messages, stdFee);
173
+ }
174
+ async signArbitrary({ data, signer }) {
175
+ const { chainId, wallet } = this;
176
+ const cosmosWallet = await this.getCosmosWallet();
177
+ if (wallet !== Wallet.Keplr) throw new CosmosWalletException(/* @__PURE__ */ new Error(`signArbitrary is not supported on ${capitalize(wallet)}`));
178
+ try {
179
+ return (await cosmosWallet.signArbitrary(chainId, signer, data)).signature;
180
+ } catch (e) {
181
+ throw new CosmosWalletException(new Error(e.message), {
182
+ context: wallet,
183
+ contextModule: "sign-arbitrary"
184
+ });
185
+ }
186
+ }
187
+ async signEIP712CosmosTx({ eip712, signDoc }) {
188
+ const { chainId, wallet } = this;
189
+ const cosmosWallet = await this.getCosmosWallet();
190
+ const key = await this.getKey();
191
+ try {
192
+ return cosmosWallet.experimentalSignEIP712CosmosTx_v0(chainId, key.bech32Address, eip712, signDoc);
193
+ } catch (e) {
194
+ throw new CosmosWalletException(new Error(e.message), {
195
+ context: wallet,
196
+ contextModule: "sign-eip712-cosmos-tx"
197
+ });
198
+ }
199
+ }
200
+ async checkChainIdSupport() {
201
+ const { chainId, wallet } = this;
202
+ const cosmos = this.getCosmos();
203
+ const chainName = chainId.split("-");
204
+ try {
205
+ return !!await cosmos.getKey(chainId);
206
+ } catch (_unused) {
207
+ throw new CosmosWalletException(/* @__PURE__ */ new Error(`${capitalize(wallet)} doesn't support ${chainName[0] || chainId} network. Please use another Cosmos wallet`));
208
+ }
209
+ }
210
+ getCosmos() {
211
+ const { wallet } = this;
212
+ if (!$window) throw new CosmosWalletException(/* @__PURE__ */ new Error(`Please install ${capitalize(wallet)} extension`), {
213
+ code: UnspecifiedErrorCode,
214
+ type: ErrorType.WalletNotInstalledError,
215
+ contextModule: wallet
216
+ });
217
+ let cosmos = void 0;
218
+ if (wallet === Wallet.OWallet) cosmos = $window.owallet;
219
+ if (wallet === Wallet.Keplr) cosmos = $window.keplr;
220
+ if (wallet === Wallet.Ninji) cosmos = $window.ninji;
221
+ if (wallet === Wallet.Leap) cosmos = $window.leap;
222
+ if (!cosmos) throw new CosmosWalletException(/* @__PURE__ */ new Error(`Please install ${capitalize(wallet)} extension`), {
223
+ code: UnspecifiedErrorCode,
224
+ type: ErrorType.WalletNotInstalledError,
225
+ contextModule: wallet
226
+ });
227
+ return cosmos;
228
+ }
229
+ async disableGasCheck() {
230
+ const { wallet } = this;
231
+ const cosmosWallet = await this.getCosmosWallet();
232
+ if (![Wallet.Keplr, Wallet.OWallet].includes(wallet)) throw new CosmosWalletException(/* @__PURE__ */ new Error(`disableGasCheck is not support on ${capitalize(wallet)}`));
233
+ cosmosWallet.defaultOptions = {
234
+ ...cosmosWallet.defaultOptions,
235
+ sign: {
236
+ ...cosmosWallet.defaultOptions.sign,
237
+ disableBalanceCheck: true
238
+ }
239
+ };
240
+ }
241
+ async enableGasCheck() {
242
+ const { wallet } = this;
243
+ const cosmosWallet = await this.getCosmosWallet();
244
+ if (![Wallet.Keplr, Wallet.OWallet].includes(wallet)) throw new CosmosWalletException(/* @__PURE__ */ new Error(`EnableGasCheck is not support on ${capitalize(wallet)}`));
245
+ cosmosWallet.defaultOptions = {
246
+ ...cosmosWallet.defaultOptions,
247
+ sign: {
248
+ ...cosmosWallet.defaultOptions.sign,
249
+ disableBalanceCheck: false
250
+ }
251
+ };
252
+ }
253
+ };
254
+
255
+ //#endregion
256
+ //#region src/data/index.ts
257
+ const cosmosWallets$1 = [
258
+ Wallet.Leap,
259
+ Wallet.Ninji,
260
+ Wallet.Keplr,
261
+ Wallet.OWallet
262
+ ];
263
+
264
+ //#endregion
265
+ //#region src/utils/index.ts
266
+ const isCosmosWalletInstalled = (wallet) => {
267
+ const $window$1 = typeof window !== "undefined" ? window : {};
268
+ switch (wallet) {
269
+ case Wallet.Keplr: return $window$1.keplr !== void 0;
270
+ case Wallet.Ninji: return $window$1.ninji !== void 0;
271
+ case Wallet.Leap: return $window$1.leap !== void 0;
272
+ case Wallet.OWallet: return $window$1.oWallet !== void 0;
273
+ default: return false;
274
+ }
275
+ };
276
+ const confirmCosmosAddress = async ({ wallet, chainId, injectiveAddress }) => {
277
+ if (!cosmosWallets$1.includes(wallet)) throw new CosmosWalletException(/* @__PURE__ */ new Error(`Cosmos Wallet for ${capitalize(wallet)} is not supported.`));
278
+ const key = await new CosmosWallet({
279
+ chainId,
280
+ wallet
281
+ }).getKey();
282
+ const { address: derivedAddress } = PublicKey.fromBase64(uint8ArrayToBase64(key.pubKey)).toAddress();
283
+ if (derivedAddress !== injectiveAddress) throw new CosmosWalletException(/* @__PURE__ */ new Error(`Connected ${capitalize(wallet)} address is wrong. Please update Injective on ${capitalize(wallet)}.`));
284
+ };
285
+
286
+ //#endregion
287
+ //#region src/strategy/strategy.ts
288
+ const cosmosWallets = [
289
+ Wallet.Leap,
290
+ Wallet.Ninji,
291
+ Wallet.Keplr,
292
+ Wallet.OWallet
293
+ ];
294
+ var CosmosWalletStrategy = class extends BaseConcreteStrategy {
295
+ constructor(args) {
296
+ super({
297
+ ...args,
298
+ chainId: args.chainId
299
+ });
300
+ _defineProperty(this, "wallet", void 0);
301
+ _defineProperty(this, "cosmosWallet", void 0);
302
+ if (!cosmosWallets.includes(args.wallet)) throw new CosmosWalletException(/* @__PURE__ */ new Error(`Cosmos Wallet for ${capitalize(args.wallet)} is not supported.`));
303
+ this.wallet = args.wallet;
304
+ this.chainId = args.chainId;
305
+ this.cosmosWallet = new CosmosWallet({
306
+ wallet: args.wallet,
307
+ chainId: args.chainId
308
+ });
309
+ }
310
+ async getWalletDeviceType() {
311
+ return (await this.getCurrentCosmosWallet().getKey()).isNanoLedger ? Promise.resolve(WalletDeviceType.Hardware) : Promise.resolve(WalletDeviceType.Browser);
312
+ }
313
+ async enable() {
314
+ return await this.getCurrentCosmosWallet().checkChainIdSupport();
315
+ }
316
+ async disconnect() {
317
+ const { wallet } = this;
318
+ if (this.listeners[WalletEventListener.AccountChange]) {
319
+ if (wallet === Wallet.Ninji) window.ninji.off("accountsChanged", this.listeners[WalletEventListener.AccountChange]);
320
+ if ([Wallet.Keplr, Wallet.OWallet].includes(wallet)) window.removeEventListener("keplr_keystorechange", this.listeners[WalletEventListener.AccountChange]);
321
+ if (wallet === Wallet.Leap) window.removeEventListener("leap_keystorechange", this.listeners[WalletEventListener.AccountChange]);
322
+ }
323
+ this.listeners = {};
324
+ }
325
+ async getAddresses() {
326
+ const cosmosWallet = this.getCurrentCosmosWallet();
327
+ try {
328
+ return (await cosmosWallet.getAccounts()).map((account) => account.address);
329
+ } catch (e) {
330
+ throw new CosmosWalletException(new Error(e.message), {
331
+ code: UnspecifiedErrorCode,
332
+ context: WalletAction.GetAccounts
333
+ });
334
+ }
335
+ }
336
+ async getAddressesInfo() {
337
+ throw new CosmosWalletException(/* @__PURE__ */ new Error("getAddressesInfo is not implemented"), {
338
+ code: UnspecifiedErrorCode,
339
+ context: WalletAction.GetAccounts
340
+ });
341
+ }
342
+ async getSessionOrConfirm(address) {
343
+ return Promise.resolve(`0x${uint8ArrayToHex(stringToUint8Array(`Confirmation for ${address} at time: ${Date.now()}`))}`);
344
+ }
345
+ async sendEvmTransaction(_transaction, _options) {
346
+ const { wallet } = this;
347
+ throw new CosmosWalletException(/* @__PURE__ */ new Error(`sendEvmTransaction is not supported. ${capitalize(wallet)} only supports sending cosmos transactions`), {
348
+ code: UnspecifiedErrorCode,
349
+ context: WalletAction.SendEvmTransaction
350
+ });
351
+ }
352
+ async sendTransaction(transaction, options) {
353
+ const cosmosWallet = this.getCurrentCosmosWallet();
354
+ const txRaw = createTxRawFromSigResponse(transaction);
355
+ if (!options.endpoints) throw new CosmosWalletException(/* @__PURE__ */ new Error("You have to pass endpoints within the options to broadcast transaction"));
356
+ try {
357
+ return await waitTxBroadcasted(await cosmosWallet.broadcastTx(txRaw), options);
358
+ } catch (e) {
359
+ if (e instanceof TransactionException) throw e;
360
+ throw new TransactionException(new Error(e.message), {
361
+ code: UnspecifiedErrorCode,
362
+ context: WalletAction.SendTransaction
363
+ });
364
+ }
365
+ }
366
+ async signAminoCosmosTransaction(transaction) {
367
+ const signer = await this.getCurrentCosmosWallet().getOfflineAminoSigner();
368
+ try {
369
+ return await signer.signAmino(transaction.address, transaction.signDoc);
370
+ } catch (e) {
371
+ throw new CosmosWalletException(new Error(e.message), {
372
+ code: UnspecifiedErrorCode,
373
+ context: WalletAction.SignTransaction
374
+ });
375
+ }
376
+ }
377
+ async signCosmosTransaction(transaction) {
378
+ const signer = await this.getCurrentCosmosWallet().getOfflineSigner(this.chainId);
379
+ const signDoc = createSignDocFromTransaction(transaction);
380
+ try {
381
+ if (!("signDirect" in signer)) throw new CosmosWalletException(/* @__PURE__ */ new Error("signDirect not available"), {
382
+ code: UnspecifiedErrorCode,
383
+ context: WalletAction.SendTransaction
384
+ });
385
+ return await signer.signDirect(transaction.address, createCosmosSignDocFromSignDoc(signDoc));
386
+ } catch (e) {
387
+ throw new CosmosWalletException(new Error(e.message), {
388
+ code: UnspecifiedErrorCode,
389
+ context: WalletAction.SendTransaction
390
+ });
391
+ }
392
+ }
393
+ async signEip712TypedData(_eip712TypedData, _address) {
394
+ throw new CosmosWalletException(/* @__PURE__ */ new Error("This wallet does not support signing Evm transactions"), {
395
+ code: UnspecifiedErrorCode,
396
+ context: WalletAction.SendTransaction
397
+ });
398
+ }
399
+ async signArbitrary(signer, data) {
400
+ const cosmosWallet = this.getCurrentCosmosWallet();
401
+ try {
402
+ return await cosmosWallet.signArbitrary({
403
+ data,
404
+ signer
405
+ });
406
+ } catch (e) {
407
+ throw new CosmosWalletException(new Error(e.message), {
408
+ code: UnspecifiedErrorCode,
409
+ context: WalletAction.SignArbitrary
410
+ });
411
+ }
412
+ }
413
+ async getEthereumChainId() {
414
+ const { wallet } = this;
415
+ throw new CosmosWalletException(/* @__PURE__ */ new Error(`getEthereumChainId is not supported on ${capitalize(wallet)}`), {
416
+ code: UnspecifiedErrorCode,
417
+ context: WalletAction.GetChainId
418
+ });
419
+ }
420
+ async getEvmTransactionReceipt(_txHash) {
421
+ const { wallet } = this;
422
+ throw new CosmosWalletException(/* @__PURE__ */ new Error(`getEvmTransactionReceipt is not supported on ${capitalize(wallet)}`), {
423
+ code: UnspecifiedErrorCode,
424
+ context: WalletAction.GetEvmTransactionReceipt
425
+ });
426
+ }
427
+ async getPubKey() {
428
+ return uint8ArrayToBase64((await this.getCurrentCosmosWallet().getKey()).pubKey);
429
+ }
430
+ async onAccountChange(callback) {
431
+ const { wallet } = this;
432
+ const listener = async () => {
433
+ const [account] = await this.getAddresses();
434
+ callback(account);
435
+ };
436
+ this.listeners = { [WalletEventListener.AccountChange]: listener };
437
+ if (wallet === Wallet.Ninji) window.ninji.on("accountsChanged", listener);
438
+ if ([Wallet.Keplr, Wallet.OWallet].includes(wallet)) window.addEventListener("keplr_keystorechange", listener);
439
+ if (wallet === Wallet.Leap) window.addEventListener("leap_keystorechange", listener);
440
+ }
441
+ getCosmosWallet(chainId) {
442
+ const { wallet, cosmosWallet } = this;
443
+ return !cosmosWallet ? new CosmosWallet({
444
+ chainId,
445
+ wallet
446
+ }) : cosmosWallet;
447
+ }
448
+ async getOfflineSigner(chainId) {
449
+ const cosmosWallet = await this.getCosmosWallet(chainId || this.chainId);
450
+ if (!cosmosWallet) throw new Error("no cosmos wallet");
451
+ return await cosmosWallet.getOfflineSigner(chainId || this.chainId);
452
+ }
453
+ getCurrentCosmosWallet() {
454
+ const { wallet, cosmosWallet } = this;
455
+ if (!cosmosWallet) throw new CosmosWalletException(/* @__PURE__ */ new Error(`Please install the ${capitalize(wallet)} wallet extension`), {
456
+ code: UnspecifiedErrorCode,
457
+ type: ErrorType.WalletNotInstalledError,
458
+ context: WalletAction.SignTransaction
459
+ });
460
+ return cosmosWallet;
461
+ }
462
+ };
463
+
464
+ //#endregion
465
+ export { CosmosWallet, CosmosWalletStrategy, confirmCosmosAddress, isCosmosWalletInstalled };
@@ -1,3 +1,3 @@
1
1
  {
2
- "type": "module"
3
- }
2
+ "type": "module"
3
+ }