@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.
- package/dist/cjs/index.cjs +468 -0
- package/dist/cjs/index.d.cts +139 -0
- package/dist/cjs/package.json +2 -2
- package/dist/esm/index.d.ts +139 -3
- package/dist/esm/index.js +465 -3
- package/dist/esm/package.json +2 -2
- package/package.json +45 -51
- package/dist/cjs/data/index.d.ts +0 -2
- package/dist/cjs/data/index.js +0 -10
- package/dist/cjs/index.d.ts +0 -3
- package/dist/cjs/index.js +0 -22
- package/dist/cjs/strategy/strategy.d.ts +0 -48
- package/dist/cjs/strategy/strategy.js +0 -212
- package/dist/cjs/utils/index.d.ts +0 -8
- package/dist/cjs/utils/index.js +0 -38
- package/dist/cjs/wallet.d.ts +0 -62
- package/dist/cjs/wallet.js +0 -266
- package/dist/esm/data/index.d.ts +0 -2
- package/dist/esm/data/index.js +0 -7
- package/dist/esm/strategy/strategy.d.ts +0 -48
- package/dist/esm/strategy/strategy.js +0 -208
- package/dist/esm/utils/index.d.ts +0 -8
- package/dist/esm/utils/index.js +0 -33
- package/dist/esm/wallet.d.ts +0 -62
- package/dist/esm/wallet.js +0 -262
package/dist/cjs/wallet.js
DELETED
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CosmosWallet = void 0;
|
|
4
|
-
const utils_1 = require("@injectivelabs/utils");
|
|
5
|
-
const stargate_1 = require("@cosmjs/stargate");
|
|
6
|
-
const sdk_ts_1 = require("@injectivelabs/sdk-ts");
|
|
7
|
-
const wallet_base_1 = require("@injectivelabs/wallet-base");
|
|
8
|
-
const exceptions_1 = require("@injectivelabs/exceptions");
|
|
9
|
-
const $window = (typeof window !== 'undefined' ? window : {});
|
|
10
|
-
class CosmosWallet {
|
|
11
|
-
wallet;
|
|
12
|
-
chainId;
|
|
13
|
-
constructor({ wallet, chainId, }) {
|
|
14
|
-
this.wallet = wallet;
|
|
15
|
-
this.chainId = chainId;
|
|
16
|
-
}
|
|
17
|
-
async isChainIdSupported(chainId) {
|
|
18
|
-
const { wallet } = this;
|
|
19
|
-
return new CosmosWallet({ chainId, wallet }).checkChainIdSupport();
|
|
20
|
-
}
|
|
21
|
-
async getCosmosWallet() {
|
|
22
|
-
const { chainId } = this;
|
|
23
|
-
const cosmos = this.getCosmos();
|
|
24
|
-
try {
|
|
25
|
-
await cosmos.enable(chainId);
|
|
26
|
-
return cosmos;
|
|
27
|
-
}
|
|
28
|
-
catch (e) {
|
|
29
|
-
throw new exceptions_1.CosmosWalletException(new Error(e.message));
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
async chainNotSupported() {
|
|
33
|
-
const { chainId, wallet } = this;
|
|
34
|
-
const chainName = chainId.split('-');
|
|
35
|
-
const context = wallet === wallet_base_1.Wallet.Keplr
|
|
36
|
-
? 'https://chains.keplr.app/'
|
|
37
|
-
: wallet === wallet_base_1.Wallet.OWallet
|
|
38
|
-
? 'https://owallet.io/'
|
|
39
|
-
: undefined;
|
|
40
|
-
throw new exceptions_1.CosmosWalletException(new Error(`${(0, utils_1.capitalize)(wallet)} may not support ${chainName[0] || chainId} network. Please check if the chain can be added.`), context ? { context } : {});
|
|
41
|
-
}
|
|
42
|
-
async getAccounts() {
|
|
43
|
-
const { chainId } = this;
|
|
44
|
-
const cosmos = this.getCosmos();
|
|
45
|
-
try {
|
|
46
|
-
return cosmos.getOfflineSigner(chainId).getAccounts();
|
|
47
|
-
}
|
|
48
|
-
catch (e) {
|
|
49
|
-
throw new exceptions_1.CosmosWalletException(new Error(e.message), {
|
|
50
|
-
contextModule: exceptions_1.WalletErrorActionModule.GetAccounts,
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
async getKey() {
|
|
55
|
-
const { wallet, chainId } = this;
|
|
56
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
57
|
-
try {
|
|
58
|
-
return cosmosWallet.getKey(chainId);
|
|
59
|
-
}
|
|
60
|
-
catch (e) {
|
|
61
|
-
throw new exceptions_1.CosmosWalletException(new Error(e.message), {
|
|
62
|
-
contextModule: wallet,
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
async getOfflineSigner(chainId) {
|
|
67
|
-
const { wallet } = this;
|
|
68
|
-
try {
|
|
69
|
-
return this.getCosmos().getOfflineSigner(chainId || this.chainId);
|
|
70
|
-
}
|
|
71
|
-
catch (e) {
|
|
72
|
-
throw new exceptions_1.CosmosWalletException(new Error(e.message), {
|
|
73
|
-
contextModule: wallet,
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
async getOfflineAminoSigner() {
|
|
78
|
-
const { chainId, wallet } = this;
|
|
79
|
-
if (![wallet_base_1.Wallet.Keplr, wallet_base_1.Wallet.OWallet].includes(wallet)) {
|
|
80
|
-
throw new exceptions_1.CosmosWalletException(new Error(`getOfflineAminoSigner is not support on ${(0, utils_1.capitalize)(wallet)}`));
|
|
81
|
-
}
|
|
82
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
83
|
-
try {
|
|
84
|
-
return cosmosWallet.getOfflineSignerOnlyAmino(chainId);
|
|
85
|
-
}
|
|
86
|
-
catch (e) {
|
|
87
|
-
throw new exceptions_1.CosmosWalletException(new Error(e.message), {
|
|
88
|
-
context: wallet,
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* This method is used to broadcast a transaction to the network.
|
|
94
|
-
* Since it uses the `Sync` mode, it will not wait for the transaction to be included in a block,
|
|
95
|
-
* so we have to make sure the transaction is included in a block after its broadcasted
|
|
96
|
-
*
|
|
97
|
-
* @param txRaw - raw transaction to broadcast
|
|
98
|
-
* @returns tx hash
|
|
99
|
-
*/
|
|
100
|
-
async broadcastTx(txRaw) {
|
|
101
|
-
const { chainId, wallet } = this;
|
|
102
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
103
|
-
try {
|
|
104
|
-
const result = await cosmosWallet.sendTx(chainId, sdk_ts_1.CosmosTxV1Beta1Tx.TxRaw.encode(txRaw).finish(), wallet_base_1.BroadcastMode.Sync);
|
|
105
|
-
if (!result || result.length === 0) {
|
|
106
|
-
throw new exceptions_1.TransactionException(new Error('Transaction failed to be broadcasted'), { contextModule: wallet });
|
|
107
|
-
}
|
|
108
|
-
return Buffer.from(result).toString('hex');
|
|
109
|
-
}
|
|
110
|
-
catch (e) {
|
|
111
|
-
if (e instanceof exceptions_1.TransactionException) {
|
|
112
|
-
throw e;
|
|
113
|
-
}
|
|
114
|
-
throw new exceptions_1.CosmosWalletException(new Error(e.message), {
|
|
115
|
-
context: 'broadcast-tx',
|
|
116
|
-
contextModule: wallet,
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* This method is used to broadcast a transaction to the network.
|
|
122
|
-
* Since it uses the `Block` mode, and it will wait for the transaction to be included in a block,
|
|
123
|
-
*
|
|
124
|
-
* @param txRaw - raw transaction to broadcast
|
|
125
|
-
* @returns tx hash
|
|
126
|
-
*/
|
|
127
|
-
async broadcastTxBlock(txRaw) {
|
|
128
|
-
const { chainId, wallet } = this;
|
|
129
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
130
|
-
try {
|
|
131
|
-
const result = await cosmosWallet.sendTx(chainId, sdk_ts_1.CosmosTxV1Beta1Tx.TxRaw.encode(txRaw).finish(), wallet_base_1.BroadcastMode.Block);
|
|
132
|
-
if (!result || result.length === 0) {
|
|
133
|
-
throw new exceptions_1.TransactionException(new Error('Transaction failed to be broadcasted'), { contextModule: wallet });
|
|
134
|
-
}
|
|
135
|
-
return Buffer.from(result).toString('hex');
|
|
136
|
-
}
|
|
137
|
-
catch (e) {
|
|
138
|
-
if (e instanceof exceptions_1.TransactionException) {
|
|
139
|
-
throw e;
|
|
140
|
-
}
|
|
141
|
-
throw new exceptions_1.CosmosWalletException(new Error(e.message), {
|
|
142
|
-
context: 'broadcast-tx',
|
|
143
|
-
contextModule: wallet,
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
async signAndBroadcastAminoUsingCosmjs(messages, stdFee, endpoints) {
|
|
148
|
-
const { chainId, wallet } = this;
|
|
149
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
150
|
-
if (![wallet_base_1.Wallet.Keplr, wallet_base_1.Wallet.OWallet].includes(wallet)) {
|
|
151
|
-
throw new exceptions_1.CosmosWalletException(new Error(`signAndBroadcastAminoUsingCosmjs is not support on ${(0, utils_1.capitalize)(wallet)}`));
|
|
152
|
-
}
|
|
153
|
-
if (!endpoints.rpc) {
|
|
154
|
-
throw new exceptions_1.GeneralException(new Error(`Please provide rpc endpoint`));
|
|
155
|
-
}
|
|
156
|
-
const offlineSigner = cosmosWallet.getOfflineSignerOnlyAmino(chainId);
|
|
157
|
-
const [account] = await offlineSigner.getAccounts();
|
|
158
|
-
const client = await stargate_1.SigningStargateClient.connectWithSigner(endpoints.rpc, offlineSigner);
|
|
159
|
-
const txResponse = await client.signAndBroadcast(account.address, messages, stdFee);
|
|
160
|
-
return txResponse;
|
|
161
|
-
}
|
|
162
|
-
async signArbitrary({ data, signer, }) {
|
|
163
|
-
const { chainId, wallet } = this;
|
|
164
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
165
|
-
if (wallet !== wallet_base_1.Wallet.Keplr) {
|
|
166
|
-
throw new exceptions_1.CosmosWalletException(new Error(`signArbitrary is not supported on ${(0, utils_1.capitalize)(wallet)}`));
|
|
167
|
-
}
|
|
168
|
-
try {
|
|
169
|
-
const signature = await cosmosWallet.signArbitrary(chainId, signer, data);
|
|
170
|
-
return signature.signature;
|
|
171
|
-
}
|
|
172
|
-
catch (e) {
|
|
173
|
-
throw new exceptions_1.CosmosWalletException(new Error(e.message), {
|
|
174
|
-
context: wallet,
|
|
175
|
-
contextModule: 'sign-arbitrary',
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
async signEIP712CosmosTx({ eip712, signDoc, }) {
|
|
180
|
-
const { chainId, wallet } = this;
|
|
181
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
182
|
-
const key = await this.getKey();
|
|
183
|
-
try {
|
|
184
|
-
return cosmosWallet.experimentalSignEIP712CosmosTx_v0(chainId, key.bech32Address, eip712, signDoc);
|
|
185
|
-
}
|
|
186
|
-
catch (e) {
|
|
187
|
-
throw new exceptions_1.CosmosWalletException(new Error(e.message), {
|
|
188
|
-
context: wallet,
|
|
189
|
-
contextModule: 'sign-eip712-cosmos-tx',
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
async checkChainIdSupport() {
|
|
194
|
-
const { chainId, wallet } = this;
|
|
195
|
-
const cosmos = this.getCosmos();
|
|
196
|
-
const chainName = chainId.split('-');
|
|
197
|
-
try {
|
|
198
|
-
return !!(await cosmos.getKey(chainId));
|
|
199
|
-
}
|
|
200
|
-
catch {
|
|
201
|
-
throw new exceptions_1.CosmosWalletException(new Error(`${(0, utils_1.capitalize)(wallet)} doesn't support ${chainName[0] || chainId} network. Please use another Cosmos wallet`));
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
getCosmos() {
|
|
205
|
-
const { wallet } = this;
|
|
206
|
-
if (!$window) {
|
|
207
|
-
throw new exceptions_1.CosmosWalletException(new Error(`Please install ${(0, utils_1.capitalize)(wallet)} extension`), {
|
|
208
|
-
code: exceptions_1.UnspecifiedErrorCode,
|
|
209
|
-
type: exceptions_1.ErrorType.WalletNotInstalledError,
|
|
210
|
-
contextModule: wallet,
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
let cosmos = undefined;
|
|
214
|
-
if (wallet === wallet_base_1.Wallet.OWallet) {
|
|
215
|
-
cosmos = $window.owallet;
|
|
216
|
-
}
|
|
217
|
-
if (wallet === wallet_base_1.Wallet.Keplr) {
|
|
218
|
-
cosmos = $window.keplr;
|
|
219
|
-
}
|
|
220
|
-
if (wallet === wallet_base_1.Wallet.Ninji) {
|
|
221
|
-
cosmos = $window.ninji;
|
|
222
|
-
}
|
|
223
|
-
if (wallet === wallet_base_1.Wallet.Leap) {
|
|
224
|
-
cosmos = $window.leap;
|
|
225
|
-
}
|
|
226
|
-
if (!cosmos) {
|
|
227
|
-
throw new exceptions_1.CosmosWalletException(new Error(`Please install ${(0, utils_1.capitalize)(wallet)} extension`), {
|
|
228
|
-
code: exceptions_1.UnspecifiedErrorCode,
|
|
229
|
-
type: exceptions_1.ErrorType.WalletNotInstalledError,
|
|
230
|
-
contextModule: wallet,
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
return cosmos;
|
|
234
|
-
}
|
|
235
|
-
async disableGasCheck() {
|
|
236
|
-
const { wallet } = this;
|
|
237
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
238
|
-
if (![wallet_base_1.Wallet.Keplr, wallet_base_1.Wallet.OWallet].includes(wallet)) {
|
|
239
|
-
throw new exceptions_1.CosmosWalletException(new Error(`disableGasCheck is not support on ${(0, utils_1.capitalize)(wallet)}`));
|
|
240
|
-
}
|
|
241
|
-
// Temporary disable tx gas check for fee delegation purposes
|
|
242
|
-
cosmosWallet.defaultOptions = {
|
|
243
|
-
...cosmosWallet.defaultOptions,
|
|
244
|
-
sign: {
|
|
245
|
-
...cosmosWallet.defaultOptions.sign,
|
|
246
|
-
disableBalanceCheck: true,
|
|
247
|
-
},
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
async enableGasCheck() {
|
|
251
|
-
const { wallet } = this;
|
|
252
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
253
|
-
if (![wallet_base_1.Wallet.Keplr, wallet_base_1.Wallet.OWallet].includes(wallet)) {
|
|
254
|
-
throw new exceptions_1.CosmosWalletException(new Error(`EnableGasCheck is not support on ${(0, utils_1.capitalize)(wallet)}`));
|
|
255
|
-
}
|
|
256
|
-
// Temporary disable tx gas check for fee delegation purposes
|
|
257
|
-
cosmosWallet.defaultOptions = {
|
|
258
|
-
...cosmosWallet.defaultOptions,
|
|
259
|
-
sign: {
|
|
260
|
-
...cosmosWallet.defaultOptions.sign,
|
|
261
|
-
disableBalanceCheck: false,
|
|
262
|
-
},
|
|
263
|
-
};
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
exports.CosmosWallet = CosmosWallet;
|
package/dist/esm/data/index.d.ts
DELETED
package/dist/esm/data/index.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { Wallet, WalletDeviceType, BaseConcreteStrategy } from '@injectivelabs/wallet-base';
|
|
2
|
-
import { CosmosWallet } from './../wallet.js';
|
|
3
|
-
import type { OfflineSigner } from '@cosmjs/proto-signing';
|
|
4
|
-
import type { ChainId, EvmChainId, CosmosChainId, AccountAddress } from '@injectivelabs/ts-types';
|
|
5
|
-
import type { TxRaw, TxResponse, AminoSignResponse, DirectSignResponse } from '@injectivelabs/sdk-ts';
|
|
6
|
-
import type { StdSignDoc, ConcreteWalletStrategy, SendTransactionOptions } from '@injectivelabs/wallet-base';
|
|
7
|
-
export declare class CosmosWalletStrategy extends BaseConcreteStrategy implements ConcreteWalletStrategy {
|
|
8
|
-
wallet: Wallet;
|
|
9
|
-
private cosmosWallet;
|
|
10
|
-
constructor(args: {
|
|
11
|
-
chainId: ChainId | CosmosChainId;
|
|
12
|
-
endpoints?: {
|
|
13
|
-
rest: string;
|
|
14
|
-
rpc: string;
|
|
15
|
-
};
|
|
16
|
-
} & {
|
|
17
|
-
wallet: Wallet;
|
|
18
|
-
});
|
|
19
|
-
getWalletDeviceType(): Promise<WalletDeviceType>;
|
|
20
|
-
enable(): Promise<boolean>;
|
|
21
|
-
disconnect(): Promise<void>;
|
|
22
|
-
getAddresses(): Promise<string[]>;
|
|
23
|
-
getSessionOrConfirm(address: AccountAddress): Promise<string>;
|
|
24
|
-
sendEvmTransaction(_transaction: unknown, _options: {
|
|
25
|
-
address: AccountAddress;
|
|
26
|
-
evmChainId: EvmChainId;
|
|
27
|
-
}): Promise<string>;
|
|
28
|
-
sendTransaction(transaction: DirectSignResponse | TxRaw, options: SendTransactionOptions): Promise<TxResponse>;
|
|
29
|
-
signAminoCosmosTransaction(transaction: {
|
|
30
|
-
address: string;
|
|
31
|
-
signDoc: StdSignDoc;
|
|
32
|
-
}): Promise<AminoSignResponse>;
|
|
33
|
-
signCosmosTransaction(transaction: {
|
|
34
|
-
txRaw: TxRaw;
|
|
35
|
-
accountNumber: number;
|
|
36
|
-
chainId: string;
|
|
37
|
-
address: AccountAddress;
|
|
38
|
-
}): Promise<DirectSignResponse>;
|
|
39
|
-
signEip712TypedData(_eip712TypedData: string, _address: AccountAddress): Promise<string>;
|
|
40
|
-
signArbitrary(signer: string, data: string | Uint8Array): Promise<string>;
|
|
41
|
-
getEthereumChainId(): Promise<string>;
|
|
42
|
-
getEvmTransactionReceipt(_txHash: string): Promise<string>;
|
|
43
|
-
getPubKey(): Promise<string>;
|
|
44
|
-
onAccountChange(callback: (account: AccountAddress | string[]) => void): Promise<void>;
|
|
45
|
-
getCosmosWallet(chainId: ChainId): CosmosWallet;
|
|
46
|
-
getOfflineSigner(chainId?: string): Promise<OfflineSigner>;
|
|
47
|
-
private getCurrentCosmosWallet;
|
|
48
|
-
}
|
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import { capitalize } from '@injectivelabs/utils';
|
|
2
|
-
import { waitTxBroadcasted, createTxRawFromSigResponse, createSignDocFromTransaction, } from '@injectivelabs/sdk-ts';
|
|
3
|
-
import { ErrorType, UnspecifiedErrorCode, CosmosWalletException, TransactionException, } from '@injectivelabs/exceptions';
|
|
4
|
-
import { Wallet, WalletAction, WalletDeviceType, WalletEventListener, BaseConcreteStrategy, createCosmosSignDocFromSignDoc, } from '@injectivelabs/wallet-base';
|
|
5
|
-
import { CosmosWallet } from './../wallet.js';
|
|
6
|
-
const cosmosWallets = [
|
|
7
|
-
Wallet.Leap,
|
|
8
|
-
Wallet.Ninji,
|
|
9
|
-
Wallet.Keplr,
|
|
10
|
-
Wallet.OWallet,
|
|
11
|
-
];
|
|
12
|
-
export class CosmosWalletStrategy extends BaseConcreteStrategy {
|
|
13
|
-
wallet;
|
|
14
|
-
cosmosWallet;
|
|
15
|
-
constructor(args) {
|
|
16
|
-
super({ ...args, chainId: args.chainId });
|
|
17
|
-
if (!cosmosWallets.includes(args.wallet)) {
|
|
18
|
-
throw new CosmosWalletException(new Error(`Cosmos Wallet for ${capitalize(args.wallet)} is not supported.`));
|
|
19
|
-
}
|
|
20
|
-
this.wallet = args.wallet;
|
|
21
|
-
this.chainId = args.chainId;
|
|
22
|
-
this.cosmosWallet = new CosmosWallet({
|
|
23
|
-
wallet: args.wallet,
|
|
24
|
-
chainId: args.chainId,
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
async getWalletDeviceType() {
|
|
28
|
-
const cosmosWallet = this.getCurrentCosmosWallet();
|
|
29
|
-
const key = await cosmosWallet.getKey();
|
|
30
|
-
return key.isNanoLedger
|
|
31
|
-
? Promise.resolve(WalletDeviceType.Hardware)
|
|
32
|
-
: Promise.resolve(WalletDeviceType.Browser);
|
|
33
|
-
}
|
|
34
|
-
async enable() {
|
|
35
|
-
const cosmosWallet = this.getCurrentCosmosWallet();
|
|
36
|
-
return await cosmosWallet.checkChainIdSupport();
|
|
37
|
-
}
|
|
38
|
-
async disconnect() {
|
|
39
|
-
const { wallet } = this;
|
|
40
|
-
if (this.listeners[WalletEventListener.AccountChange]) {
|
|
41
|
-
if (wallet === Wallet.Ninji) {
|
|
42
|
-
window.ninji.off('accountsChanged', this.listeners[WalletEventListener.AccountChange]);
|
|
43
|
-
}
|
|
44
|
-
if ([Wallet.Keplr, Wallet.OWallet].includes(wallet)) {
|
|
45
|
-
window.removeEventListener('keplr_keystorechange', this.listeners[WalletEventListener.AccountChange]);
|
|
46
|
-
}
|
|
47
|
-
if (wallet === Wallet.Leap) {
|
|
48
|
-
window.removeEventListener('leap_keystorechange', this.listeners[WalletEventListener.AccountChange]);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
this.listeners = {};
|
|
52
|
-
}
|
|
53
|
-
async getAddresses() {
|
|
54
|
-
const cosmosWallet = this.getCurrentCosmosWallet();
|
|
55
|
-
try {
|
|
56
|
-
const accounts = await cosmosWallet.getAccounts();
|
|
57
|
-
return accounts.map((account) => account.address);
|
|
58
|
-
}
|
|
59
|
-
catch (e) {
|
|
60
|
-
throw new CosmosWalletException(new Error(e.message), {
|
|
61
|
-
code: UnspecifiedErrorCode,
|
|
62
|
-
context: WalletAction.GetAccounts,
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
async getSessionOrConfirm(address) {
|
|
67
|
-
return Promise.resolve(`0x${Buffer.from(`Confirmation for ${address} at time: ${Date.now()}`).toString('hex')}`);
|
|
68
|
-
}
|
|
69
|
-
async sendEvmTransaction(_transaction, _options) {
|
|
70
|
-
const { wallet } = this;
|
|
71
|
-
throw new CosmosWalletException(new Error(`sendEvmTransaction is not supported. ${capitalize(wallet)} only supports sending cosmos transactions`), {
|
|
72
|
-
code: UnspecifiedErrorCode,
|
|
73
|
-
context: WalletAction.SendEvmTransaction,
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
async sendTransaction(transaction, options) {
|
|
77
|
-
const cosmosWallet = this.getCurrentCosmosWallet();
|
|
78
|
-
const txRaw = createTxRawFromSigResponse(transaction);
|
|
79
|
-
if (!options.endpoints) {
|
|
80
|
-
throw new CosmosWalletException(new Error('You have to pass endpoints within the options to broadcast transaction'));
|
|
81
|
-
}
|
|
82
|
-
try {
|
|
83
|
-
const txHash = await cosmosWallet.broadcastTx(txRaw);
|
|
84
|
-
return await waitTxBroadcasted(txHash, options);
|
|
85
|
-
}
|
|
86
|
-
catch (e) {
|
|
87
|
-
if (e instanceof TransactionException) {
|
|
88
|
-
throw e;
|
|
89
|
-
}
|
|
90
|
-
throw new TransactionException(new Error(e.message), {
|
|
91
|
-
code: UnspecifiedErrorCode,
|
|
92
|
-
context: WalletAction.SendTransaction,
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
async signAminoCosmosTransaction(transaction) {
|
|
97
|
-
const cosmosWallet = this.getCurrentCosmosWallet();
|
|
98
|
-
const signer = await cosmosWallet.getOfflineAminoSigner();
|
|
99
|
-
try {
|
|
100
|
-
return await signer.signAmino(transaction.address, transaction.signDoc);
|
|
101
|
-
}
|
|
102
|
-
catch (e) {
|
|
103
|
-
throw new CosmosWalletException(new Error(e.message), {
|
|
104
|
-
code: UnspecifiedErrorCode,
|
|
105
|
-
context: WalletAction.SignTransaction,
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
async signCosmosTransaction(transaction) {
|
|
110
|
-
const cosmosWallet = this.getCurrentCosmosWallet();
|
|
111
|
-
const signer = await cosmosWallet.getOfflineSigner(this.chainId);
|
|
112
|
-
const signDoc = createSignDocFromTransaction(transaction);
|
|
113
|
-
try {
|
|
114
|
-
if (!('signDirect' in signer)) {
|
|
115
|
-
throw new CosmosWalletException(new Error('signDirect not available'), {
|
|
116
|
-
code: UnspecifiedErrorCode,
|
|
117
|
-
context: WalletAction.SendTransaction,
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
return await signer.signDirect(transaction.address, createCosmosSignDocFromSignDoc(signDoc));
|
|
121
|
-
}
|
|
122
|
-
catch (e) {
|
|
123
|
-
throw new CosmosWalletException(new Error(e.message), {
|
|
124
|
-
code: UnspecifiedErrorCode,
|
|
125
|
-
context: WalletAction.SendTransaction,
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
async signEip712TypedData(_eip712TypedData, _address) {
|
|
130
|
-
throw new CosmosWalletException(new Error('This wallet does not support signing Evm transactions'), {
|
|
131
|
-
code: UnspecifiedErrorCode,
|
|
132
|
-
context: WalletAction.SendTransaction,
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
async signArbitrary(signer, data) {
|
|
136
|
-
const cosmosWallet = this.getCurrentCosmosWallet();
|
|
137
|
-
try {
|
|
138
|
-
const signature = await cosmosWallet.signArbitrary({ data, signer });
|
|
139
|
-
return signature;
|
|
140
|
-
}
|
|
141
|
-
catch (e) {
|
|
142
|
-
throw new CosmosWalletException(new Error(e.message), {
|
|
143
|
-
code: UnspecifiedErrorCode,
|
|
144
|
-
context: WalletAction.SignArbitrary,
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
async getEthereumChainId() {
|
|
149
|
-
const { wallet } = this;
|
|
150
|
-
throw new CosmosWalletException(new Error(`getEthereumChainId is not supported on ${capitalize(wallet)}`), {
|
|
151
|
-
code: UnspecifiedErrorCode,
|
|
152
|
-
context: WalletAction.GetChainId,
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
async getEvmTransactionReceipt(_txHash) {
|
|
156
|
-
const { wallet } = this;
|
|
157
|
-
throw new CosmosWalletException(new Error(`getEvmTransactionReceipt is not supported on ${capitalize(wallet)}`), {
|
|
158
|
-
code: UnspecifiedErrorCode,
|
|
159
|
-
context: WalletAction.GetEvmTransactionReceipt,
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
async getPubKey() {
|
|
163
|
-
const cosmosWallet = this.getCurrentCosmosWallet();
|
|
164
|
-
const key = await cosmosWallet.getKey();
|
|
165
|
-
return Buffer.from(key.pubKey).toString('base64');
|
|
166
|
-
}
|
|
167
|
-
async onAccountChange(callback) {
|
|
168
|
-
const { wallet } = this;
|
|
169
|
-
const listener = async () => {
|
|
170
|
-
const [account] = await this.getAddresses();
|
|
171
|
-
callback(account);
|
|
172
|
-
};
|
|
173
|
-
this.listeners = {
|
|
174
|
-
[WalletEventListener.AccountChange]: listener,
|
|
175
|
-
};
|
|
176
|
-
if (wallet === Wallet.Ninji) {
|
|
177
|
-
window.ninji.on('accountsChanged', listener);
|
|
178
|
-
}
|
|
179
|
-
if ([Wallet.Keplr, Wallet.OWallet].includes(wallet)) {
|
|
180
|
-
window.addEventListener('keplr_keystorechange', listener);
|
|
181
|
-
}
|
|
182
|
-
if (wallet === Wallet.Leap) {
|
|
183
|
-
window.addEventListener('leap_keystorechange', listener);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
getCosmosWallet(chainId) {
|
|
187
|
-
const { wallet, cosmosWallet } = this;
|
|
188
|
-
return !cosmosWallet ? new CosmosWallet({ chainId, wallet }) : cosmosWallet;
|
|
189
|
-
}
|
|
190
|
-
async getOfflineSigner(chainId) {
|
|
191
|
-
const cosmosWallet = await this.getCosmosWallet(chainId || this.chainId);
|
|
192
|
-
if (!cosmosWallet) {
|
|
193
|
-
throw new Error('no cosmos wallet');
|
|
194
|
-
}
|
|
195
|
-
return await cosmosWallet.getOfflineSigner(chainId || this.chainId);
|
|
196
|
-
}
|
|
197
|
-
getCurrentCosmosWallet() {
|
|
198
|
-
const { wallet, cosmosWallet } = this;
|
|
199
|
-
if (!cosmosWallet) {
|
|
200
|
-
throw new CosmosWalletException(new Error(`Please install the ${capitalize(wallet)} wallet extension`), {
|
|
201
|
-
code: UnspecifiedErrorCode,
|
|
202
|
-
type: ErrorType.WalletNotInstalledError,
|
|
203
|
-
context: WalletAction.SignTransaction,
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
return cosmosWallet;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Wallet } from '@injectivelabs/wallet-base';
|
|
2
|
-
import type { ChainId } from '@injectivelabs/ts-types';
|
|
3
|
-
export declare const isCosmosWalletInstalled: (wallet: Wallet) => boolean;
|
|
4
|
-
export declare const confirmCosmosAddress: ({ wallet, chainId, injectiveAddress, }: {
|
|
5
|
-
wallet: Wallet;
|
|
6
|
-
chainId: ChainId;
|
|
7
|
-
injectiveAddress: string;
|
|
8
|
-
}) => Promise<void>;
|
package/dist/esm/utils/index.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { PublicKey } from '@injectivelabs/sdk-ts';
|
|
2
|
-
import { capitalize } from '@injectivelabs/utils';
|
|
3
|
-
import { Wallet } from '@injectivelabs/wallet-base';
|
|
4
|
-
import { CosmosWalletException } from '@injectivelabs/exceptions';
|
|
5
|
-
import { CosmosWallet } from './../wallet.js';
|
|
6
|
-
import { cosmosWallets } from './../data/index.js';
|
|
7
|
-
export const isCosmosWalletInstalled = (wallet) => {
|
|
8
|
-
const $window = (typeof window !== 'undefined' ? window : {});
|
|
9
|
-
switch (wallet) {
|
|
10
|
-
case Wallet.Keplr:
|
|
11
|
-
return $window.keplr !== undefined;
|
|
12
|
-
case Wallet.Ninji:
|
|
13
|
-
return $window.ninji !== undefined;
|
|
14
|
-
case Wallet.Leap:
|
|
15
|
-
return $window.leap !== undefined;
|
|
16
|
-
case Wallet.OWallet:
|
|
17
|
-
return $window.oWallet !== undefined;
|
|
18
|
-
default:
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
export const confirmCosmosAddress = async ({ wallet, chainId, injectiveAddress, }) => {
|
|
23
|
-
if (!cosmosWallets.includes(wallet)) {
|
|
24
|
-
throw new CosmosWalletException(new Error(`Cosmos Wallet for ${capitalize(wallet)} is not supported.`));
|
|
25
|
-
}
|
|
26
|
-
const cosmosWallet = new CosmosWallet({ chainId, wallet });
|
|
27
|
-
const key = await cosmosWallet.getKey();
|
|
28
|
-
const publicKey = PublicKey.fromBase64(Buffer.from(key.pubKey).toString('base64'));
|
|
29
|
-
const { address: derivedAddress } = publicKey.toAddress();
|
|
30
|
-
if (derivedAddress !== injectiveAddress) {
|
|
31
|
-
throw new CosmosWalletException(new Error(`Connected ${capitalize(wallet)} address is wrong. Please update Injective on ${capitalize(wallet)}.`));
|
|
32
|
-
}
|
|
33
|
-
};
|
package/dist/esm/wallet.d.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { CosmosTxV1Beta1Tx } from '@injectivelabs/sdk-ts';
|
|
2
|
-
import { Wallet } from '@injectivelabs/wallet-base';
|
|
3
|
-
import type { StdFee } from '@cosmjs/stargate';
|
|
4
|
-
import type { EncodeObject } from '@cosmjs/proto-signing';
|
|
5
|
-
import type { OfflineSigner } from '@cosmjs/proto-signing';
|
|
6
|
-
import type { ChainId, CosmosChainId, TestnetCosmosChainId } from '@injectivelabs/ts-types';
|
|
7
|
-
import type { Keplr, StdSignDoc, AminoSignResponse, OfflineAminoSigner } from '@keplr-wallet/types';
|
|
8
|
-
export declare class CosmosWallet {
|
|
9
|
-
wallet: Wallet;
|
|
10
|
-
private chainId;
|
|
11
|
-
constructor({ wallet, chainId, }: {
|
|
12
|
-
wallet: Wallet;
|
|
13
|
-
chainId: CosmosChainId | TestnetCosmosChainId | ChainId;
|
|
14
|
-
});
|
|
15
|
-
isChainIdSupported(chainId: CosmosChainId): Promise<boolean>;
|
|
16
|
-
getCosmosWallet(): Promise<Keplr>;
|
|
17
|
-
chainNotSupported(): Promise<void>;
|
|
18
|
-
getAccounts(): Promise<readonly import("@keplr-wallet/types").AccountData[]>;
|
|
19
|
-
getKey(): Promise<{
|
|
20
|
-
name: string;
|
|
21
|
-
algo: string;
|
|
22
|
-
isNanoLedger: boolean;
|
|
23
|
-
pubKey: Uint8Array;
|
|
24
|
-
address: Uint8Array;
|
|
25
|
-
bech32Address: string;
|
|
26
|
-
}>;
|
|
27
|
-
getOfflineSigner(chainId?: string): Promise<OfflineSigner>;
|
|
28
|
-
getOfflineAminoSigner(): Promise<OfflineAminoSigner>;
|
|
29
|
-
/**
|
|
30
|
-
* This method is used to broadcast a transaction to the network.
|
|
31
|
-
* Since it uses the `Sync` mode, it will not wait for the transaction to be included in a block,
|
|
32
|
-
* so we have to make sure the transaction is included in a block after its broadcasted
|
|
33
|
-
*
|
|
34
|
-
* @param txRaw - raw transaction to broadcast
|
|
35
|
-
* @returns tx hash
|
|
36
|
-
*/
|
|
37
|
-
broadcastTx(txRaw: CosmosTxV1Beta1Tx.TxRaw): Promise<string>;
|
|
38
|
-
/**
|
|
39
|
-
* This method is used to broadcast a transaction to the network.
|
|
40
|
-
* Since it uses the `Block` mode, and it will wait for the transaction to be included in a block,
|
|
41
|
-
*
|
|
42
|
-
* @param txRaw - raw transaction to broadcast
|
|
43
|
-
* @returns tx hash
|
|
44
|
-
*/
|
|
45
|
-
broadcastTxBlock(txRaw: CosmosTxV1Beta1Tx.TxRaw): Promise<string>;
|
|
46
|
-
signAndBroadcastAminoUsingCosmjs(messages: EncodeObject[], stdFee: StdFee, endpoints: {
|
|
47
|
-
rest: string;
|
|
48
|
-
rpc: string;
|
|
49
|
-
}): Promise<import("@cosmjs/stargate").DeliverTxResponse>;
|
|
50
|
-
signArbitrary({ data, signer, }: {
|
|
51
|
-
signer: string;
|
|
52
|
-
data: string | Uint8Array;
|
|
53
|
-
}): Promise<string>;
|
|
54
|
-
signEIP712CosmosTx({ eip712, signDoc, }: {
|
|
55
|
-
eip712: any;
|
|
56
|
-
signDoc: StdSignDoc;
|
|
57
|
-
}): Promise<AminoSignResponse>;
|
|
58
|
-
checkChainIdSupport(): Promise<boolean>;
|
|
59
|
-
private getCosmos;
|
|
60
|
-
disableGasCheck(): Promise<void>;
|
|
61
|
-
enableGasCheck(): Promise<void>;
|
|
62
|
-
}
|