@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/esm/wallet.js
DELETED
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
import { capitalize } from '@injectivelabs/utils';
|
|
2
|
-
import { SigningStargateClient } from '@cosmjs/stargate';
|
|
3
|
-
import { CosmosTxV1Beta1Tx } from '@injectivelabs/sdk-ts';
|
|
4
|
-
import { Wallet, BroadcastMode } from '@injectivelabs/wallet-base';
|
|
5
|
-
import { ErrorType, GeneralException, UnspecifiedErrorCode, TransactionException, CosmosWalletException, WalletErrorActionModule, } from '@injectivelabs/exceptions';
|
|
6
|
-
const $window = (typeof window !== 'undefined' ? window : {});
|
|
7
|
-
export class CosmosWallet {
|
|
8
|
-
wallet;
|
|
9
|
-
chainId;
|
|
10
|
-
constructor({ wallet, chainId, }) {
|
|
11
|
-
this.wallet = wallet;
|
|
12
|
-
this.chainId = chainId;
|
|
13
|
-
}
|
|
14
|
-
async isChainIdSupported(chainId) {
|
|
15
|
-
const { wallet } = this;
|
|
16
|
-
return new CosmosWallet({ chainId, wallet }).checkChainIdSupport();
|
|
17
|
-
}
|
|
18
|
-
async getCosmosWallet() {
|
|
19
|
-
const { chainId } = this;
|
|
20
|
-
const cosmos = this.getCosmos();
|
|
21
|
-
try {
|
|
22
|
-
await cosmos.enable(chainId);
|
|
23
|
-
return cosmos;
|
|
24
|
-
}
|
|
25
|
-
catch (e) {
|
|
26
|
-
throw new CosmosWalletException(new Error(e.message));
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
async chainNotSupported() {
|
|
30
|
-
const { chainId, wallet } = this;
|
|
31
|
-
const chainName = chainId.split('-');
|
|
32
|
-
const context = wallet === Wallet.Keplr
|
|
33
|
-
? 'https://chains.keplr.app/'
|
|
34
|
-
: wallet === Wallet.OWallet
|
|
35
|
-
? 'https://owallet.io/'
|
|
36
|
-
: undefined;
|
|
37
|
-
throw new CosmosWalletException(new Error(`${capitalize(wallet)} may not support ${chainName[0] || chainId} network. Please check if the chain can be added.`), context ? { context } : {});
|
|
38
|
-
}
|
|
39
|
-
async getAccounts() {
|
|
40
|
-
const { chainId } = this;
|
|
41
|
-
const cosmos = this.getCosmos();
|
|
42
|
-
try {
|
|
43
|
-
return cosmos.getOfflineSigner(chainId).getAccounts();
|
|
44
|
-
}
|
|
45
|
-
catch (e) {
|
|
46
|
-
throw new CosmosWalletException(new Error(e.message), {
|
|
47
|
-
contextModule: WalletErrorActionModule.GetAccounts,
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
async getKey() {
|
|
52
|
-
const { wallet, chainId } = this;
|
|
53
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
54
|
-
try {
|
|
55
|
-
return cosmosWallet.getKey(chainId);
|
|
56
|
-
}
|
|
57
|
-
catch (e) {
|
|
58
|
-
throw new CosmosWalletException(new Error(e.message), {
|
|
59
|
-
contextModule: wallet,
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
async getOfflineSigner(chainId) {
|
|
64
|
-
const { wallet } = this;
|
|
65
|
-
try {
|
|
66
|
-
return this.getCosmos().getOfflineSigner(chainId || this.chainId);
|
|
67
|
-
}
|
|
68
|
-
catch (e) {
|
|
69
|
-
throw new CosmosWalletException(new Error(e.message), {
|
|
70
|
-
contextModule: wallet,
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
async getOfflineAminoSigner() {
|
|
75
|
-
const { chainId, wallet } = this;
|
|
76
|
-
if (![Wallet.Keplr, Wallet.OWallet].includes(wallet)) {
|
|
77
|
-
throw new CosmosWalletException(new Error(`getOfflineAminoSigner is not support on ${capitalize(wallet)}`));
|
|
78
|
-
}
|
|
79
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
80
|
-
try {
|
|
81
|
-
return cosmosWallet.getOfflineSignerOnlyAmino(chainId);
|
|
82
|
-
}
|
|
83
|
-
catch (e) {
|
|
84
|
-
throw new CosmosWalletException(new Error(e.message), {
|
|
85
|
-
context: wallet,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* This method is used to broadcast a transaction to the network.
|
|
91
|
-
* Since it uses the `Sync` mode, it will not wait for the transaction to be included in a block,
|
|
92
|
-
* so we have to make sure the transaction is included in a block after its broadcasted
|
|
93
|
-
*
|
|
94
|
-
* @param txRaw - raw transaction to broadcast
|
|
95
|
-
* @returns tx hash
|
|
96
|
-
*/
|
|
97
|
-
async broadcastTx(txRaw) {
|
|
98
|
-
const { chainId, wallet } = this;
|
|
99
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
100
|
-
try {
|
|
101
|
-
const result = await cosmosWallet.sendTx(chainId, CosmosTxV1Beta1Tx.TxRaw.encode(txRaw).finish(), BroadcastMode.Sync);
|
|
102
|
-
if (!result || result.length === 0) {
|
|
103
|
-
throw new TransactionException(new Error('Transaction failed to be broadcasted'), { contextModule: wallet });
|
|
104
|
-
}
|
|
105
|
-
return Buffer.from(result).toString('hex');
|
|
106
|
-
}
|
|
107
|
-
catch (e) {
|
|
108
|
-
if (e instanceof TransactionException) {
|
|
109
|
-
throw e;
|
|
110
|
-
}
|
|
111
|
-
throw new CosmosWalletException(new Error(e.message), {
|
|
112
|
-
context: 'broadcast-tx',
|
|
113
|
-
contextModule: wallet,
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* This method is used to broadcast a transaction to the network.
|
|
119
|
-
* Since it uses the `Block` mode, and it will wait for the transaction to be included in a block,
|
|
120
|
-
*
|
|
121
|
-
* @param txRaw - raw transaction to broadcast
|
|
122
|
-
* @returns tx hash
|
|
123
|
-
*/
|
|
124
|
-
async broadcastTxBlock(txRaw) {
|
|
125
|
-
const { chainId, wallet } = this;
|
|
126
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
127
|
-
try {
|
|
128
|
-
const result = await cosmosWallet.sendTx(chainId, CosmosTxV1Beta1Tx.TxRaw.encode(txRaw).finish(), BroadcastMode.Block);
|
|
129
|
-
if (!result || result.length === 0) {
|
|
130
|
-
throw new TransactionException(new Error('Transaction failed to be broadcasted'), { contextModule: wallet });
|
|
131
|
-
}
|
|
132
|
-
return Buffer.from(result).toString('hex');
|
|
133
|
-
}
|
|
134
|
-
catch (e) {
|
|
135
|
-
if (e instanceof TransactionException) {
|
|
136
|
-
throw e;
|
|
137
|
-
}
|
|
138
|
-
throw new CosmosWalletException(new Error(e.message), {
|
|
139
|
-
context: 'broadcast-tx',
|
|
140
|
-
contextModule: wallet,
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
async signAndBroadcastAminoUsingCosmjs(messages, stdFee, endpoints) {
|
|
145
|
-
const { chainId, wallet } = this;
|
|
146
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
147
|
-
if (![Wallet.Keplr, Wallet.OWallet].includes(wallet)) {
|
|
148
|
-
throw new CosmosWalletException(new Error(`signAndBroadcastAminoUsingCosmjs is not support on ${capitalize(wallet)}`));
|
|
149
|
-
}
|
|
150
|
-
if (!endpoints.rpc) {
|
|
151
|
-
throw new GeneralException(new Error(`Please provide rpc endpoint`));
|
|
152
|
-
}
|
|
153
|
-
const offlineSigner = cosmosWallet.getOfflineSignerOnlyAmino(chainId);
|
|
154
|
-
const [account] = await offlineSigner.getAccounts();
|
|
155
|
-
const client = await SigningStargateClient.connectWithSigner(endpoints.rpc, offlineSigner);
|
|
156
|
-
const txResponse = await client.signAndBroadcast(account.address, messages, stdFee);
|
|
157
|
-
return txResponse;
|
|
158
|
-
}
|
|
159
|
-
async signArbitrary({ data, signer, }) {
|
|
160
|
-
const { chainId, wallet } = this;
|
|
161
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
162
|
-
if (wallet !== Wallet.Keplr) {
|
|
163
|
-
throw new CosmosWalletException(new Error(`signArbitrary is not supported on ${capitalize(wallet)}`));
|
|
164
|
-
}
|
|
165
|
-
try {
|
|
166
|
-
const signature = await cosmosWallet.signArbitrary(chainId, signer, data);
|
|
167
|
-
return signature.signature;
|
|
168
|
-
}
|
|
169
|
-
catch (e) {
|
|
170
|
-
throw new CosmosWalletException(new Error(e.message), {
|
|
171
|
-
context: wallet,
|
|
172
|
-
contextModule: 'sign-arbitrary',
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
async signEIP712CosmosTx({ eip712, signDoc, }) {
|
|
177
|
-
const { chainId, wallet } = this;
|
|
178
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
179
|
-
const key = await this.getKey();
|
|
180
|
-
try {
|
|
181
|
-
return cosmosWallet.experimentalSignEIP712CosmosTx_v0(chainId, key.bech32Address, eip712, signDoc);
|
|
182
|
-
}
|
|
183
|
-
catch (e) {
|
|
184
|
-
throw new CosmosWalletException(new Error(e.message), {
|
|
185
|
-
context: wallet,
|
|
186
|
-
contextModule: 'sign-eip712-cosmos-tx',
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
async checkChainIdSupport() {
|
|
191
|
-
const { chainId, wallet } = this;
|
|
192
|
-
const cosmos = this.getCosmos();
|
|
193
|
-
const chainName = chainId.split('-');
|
|
194
|
-
try {
|
|
195
|
-
return !!(await cosmos.getKey(chainId));
|
|
196
|
-
}
|
|
197
|
-
catch {
|
|
198
|
-
throw new CosmosWalletException(new Error(`${capitalize(wallet)} doesn't support ${chainName[0] || chainId} network. Please use another Cosmos wallet`));
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
getCosmos() {
|
|
202
|
-
const { wallet } = this;
|
|
203
|
-
if (!$window) {
|
|
204
|
-
throw new CosmosWalletException(new Error(`Please install ${capitalize(wallet)} extension`), {
|
|
205
|
-
code: UnspecifiedErrorCode,
|
|
206
|
-
type: ErrorType.WalletNotInstalledError,
|
|
207
|
-
contextModule: wallet,
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
let cosmos = undefined;
|
|
211
|
-
if (wallet === Wallet.OWallet) {
|
|
212
|
-
cosmos = $window.owallet;
|
|
213
|
-
}
|
|
214
|
-
if (wallet === Wallet.Keplr) {
|
|
215
|
-
cosmos = $window.keplr;
|
|
216
|
-
}
|
|
217
|
-
if (wallet === Wallet.Ninji) {
|
|
218
|
-
cosmos = $window.ninji;
|
|
219
|
-
}
|
|
220
|
-
if (wallet === Wallet.Leap) {
|
|
221
|
-
cosmos = $window.leap;
|
|
222
|
-
}
|
|
223
|
-
if (!cosmos) {
|
|
224
|
-
throw new CosmosWalletException(new Error(`Please install ${capitalize(wallet)} extension`), {
|
|
225
|
-
code: UnspecifiedErrorCode,
|
|
226
|
-
type: ErrorType.WalletNotInstalledError,
|
|
227
|
-
contextModule: wallet,
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
return cosmos;
|
|
231
|
-
}
|
|
232
|
-
async disableGasCheck() {
|
|
233
|
-
const { wallet } = this;
|
|
234
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
235
|
-
if (![Wallet.Keplr, Wallet.OWallet].includes(wallet)) {
|
|
236
|
-
throw new CosmosWalletException(new Error(`disableGasCheck is not support on ${capitalize(wallet)}`));
|
|
237
|
-
}
|
|
238
|
-
// Temporary disable tx gas check for fee delegation purposes
|
|
239
|
-
cosmosWallet.defaultOptions = {
|
|
240
|
-
...cosmosWallet.defaultOptions,
|
|
241
|
-
sign: {
|
|
242
|
-
...cosmosWallet.defaultOptions.sign,
|
|
243
|
-
disableBalanceCheck: true,
|
|
244
|
-
},
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
async enableGasCheck() {
|
|
248
|
-
const { wallet } = this;
|
|
249
|
-
const cosmosWallet = await this.getCosmosWallet();
|
|
250
|
-
if (![Wallet.Keplr, Wallet.OWallet].includes(wallet)) {
|
|
251
|
-
throw new CosmosWalletException(new Error(`EnableGasCheck is not support on ${capitalize(wallet)}`));
|
|
252
|
-
}
|
|
253
|
-
// Temporary disable tx gas check for fee delegation purposes
|
|
254
|
-
cosmosWallet.defaultOptions = {
|
|
255
|
-
...cosmosWallet.defaultOptions,
|
|
256
|
-
sign: {
|
|
257
|
-
...cosmosWallet.defaultOptions.sign,
|
|
258
|
-
disableBalanceCheck: false,
|
|
259
|
-
},
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
}
|