@sodax/wallet-sdk-core 0.0.1-rc.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/LICENSE +21 -0
- package/README.md +33 -0
- package/dist/index.cjs +734 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +291 -0
- package/dist/index.d.ts +291 -0
- package/dist/index.mjs +680 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +78 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,734 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var accounts = require('viem/accounts');
|
|
4
|
+
var viem = require('viem');
|
|
5
|
+
var chains = require('viem/chains');
|
|
6
|
+
var types = require('@sodax/types');
|
|
7
|
+
var client = require('@mysten/sui/client');
|
|
8
|
+
var ed25519 = require('@mysten/sui/keypairs/ed25519');
|
|
9
|
+
var walletStandard = require('@mysten/wallet-standard');
|
|
10
|
+
var IconSdkRaw = require('icon-sdk-js');
|
|
11
|
+
var sdkTs = require('@injectivelabs/sdk-ts');
|
|
12
|
+
var splToken = require('@solana/spl-token');
|
|
13
|
+
var web3_js = require('@solana/web3.js');
|
|
14
|
+
var stellarSdk = require('@stellar/stellar-sdk');
|
|
15
|
+
|
|
16
|
+
function _interopNamespace(e) {
|
|
17
|
+
if (e && e.__esModule) return e;
|
|
18
|
+
var n = Object.create(null);
|
|
19
|
+
if (e) {
|
|
20
|
+
Object.keys(e).forEach(function (k) {
|
|
21
|
+
if (k !== 'default') {
|
|
22
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
23
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get: function () { return e[k]; }
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
n.default = e;
|
|
31
|
+
return Object.freeze(n);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
var IconSdkRaw__namespace = /*#__PURE__*/_interopNamespace(IconSdkRaw);
|
|
35
|
+
|
|
36
|
+
// src/wallet-providers/EvmWalletProvider.ts
|
|
37
|
+
function getEvmViemChain(id) {
|
|
38
|
+
switch (id) {
|
|
39
|
+
case types.SONIC_MAINNET_CHAIN_ID:
|
|
40
|
+
return chains.sonic;
|
|
41
|
+
case types.AVALANCHE_MAINNET_CHAIN_ID:
|
|
42
|
+
return chains.avalanche;
|
|
43
|
+
case types.ARBITRUM_MAINNET_CHAIN_ID:
|
|
44
|
+
return chains.arbitrum;
|
|
45
|
+
case types.BASE_MAINNET_CHAIN_ID:
|
|
46
|
+
return chains.base;
|
|
47
|
+
case types.OPTIMISM_MAINNET_CHAIN_ID:
|
|
48
|
+
return chains.optimism;
|
|
49
|
+
case types.BSC_MAINNET_CHAIN_ID:
|
|
50
|
+
return chains.bsc;
|
|
51
|
+
case types.POLYGON_MAINNET_CHAIN_ID:
|
|
52
|
+
return chains.polygon;
|
|
53
|
+
case types.NIBIRU_MAINNET_CHAIN_ID:
|
|
54
|
+
return chains.nibiru;
|
|
55
|
+
default:
|
|
56
|
+
throw new Error(`Unsupported EVM chain ID: ${id}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
var EvmWalletProvider = class {
|
|
60
|
+
walletClient;
|
|
61
|
+
publicClient;
|
|
62
|
+
constructor(config) {
|
|
63
|
+
if (isPrivateKeyEvmWalletConfig(config)) {
|
|
64
|
+
const chain = getEvmViemChain(config.chainId);
|
|
65
|
+
this.walletClient = viem.createWalletClient({
|
|
66
|
+
chain,
|
|
67
|
+
transport: viem.http(config.rpcUrl ?? chain.rpcUrls.default.http[0]),
|
|
68
|
+
account: accounts.privateKeyToAccount(config.privateKey)
|
|
69
|
+
});
|
|
70
|
+
this.publicClient = viem.createPublicClient({
|
|
71
|
+
chain,
|
|
72
|
+
transport: viem.http(config.rpcUrl ?? chain.rpcUrls.default.http[0])
|
|
73
|
+
});
|
|
74
|
+
} else if (isBrowserExtensionEvmWalletConfig(config)) {
|
|
75
|
+
this.walletClient = config.walletClient;
|
|
76
|
+
this.publicClient = config.publicClient;
|
|
77
|
+
} else {
|
|
78
|
+
throw new Error("Invalid EVM wallet config");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async sendTransaction(evmRawTx) {
|
|
82
|
+
return this.walletClient.sendTransaction(evmRawTx);
|
|
83
|
+
}
|
|
84
|
+
async waitForTransactionReceipt(txHash) {
|
|
85
|
+
const receipt = await this.publicClient.waitForTransactionReceipt({ hash: txHash });
|
|
86
|
+
return {
|
|
87
|
+
...receipt,
|
|
88
|
+
transactionIndex: receipt.transactionIndex.toString(),
|
|
89
|
+
blockNumber: receipt.blockNumber.toString(),
|
|
90
|
+
cumulativeGasUsed: receipt.cumulativeGasUsed.toString(),
|
|
91
|
+
gasUsed: receipt.gasUsed.toString(),
|
|
92
|
+
contractAddress: receipt.contractAddress?.toString() ?? null,
|
|
93
|
+
logs: receipt.logs.map((log) => ({
|
|
94
|
+
...log,
|
|
95
|
+
blockNumber: log.blockNumber.toString(),
|
|
96
|
+
logIndex: log.logIndex.toString(),
|
|
97
|
+
transactionIndex: log.transactionIndex.toString()
|
|
98
|
+
})),
|
|
99
|
+
effectiveGasPrice: receipt.effectiveGasPrice.toString()
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
async getWalletAddress() {
|
|
103
|
+
return this.walletClient.account.address;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
function isPrivateKeyEvmWalletConfig(config) {
|
|
107
|
+
return "privateKey" in config && config.privateKey.startsWith("0x");
|
|
108
|
+
}
|
|
109
|
+
function isBrowserExtensionEvmWalletConfig(config) {
|
|
110
|
+
return "walletClient" in config && "publicClient" in config;
|
|
111
|
+
}
|
|
112
|
+
function isPrivateKeySuiWalletConfig(walletConfig) {
|
|
113
|
+
return "mnemonics" in walletConfig;
|
|
114
|
+
}
|
|
115
|
+
function isBrowserExtensionSuiWalletConfig(walletConfig) {
|
|
116
|
+
return "wallet" in walletConfig && "account" in walletConfig;
|
|
117
|
+
}
|
|
118
|
+
function isPkSuiWallet(wallet) {
|
|
119
|
+
return "keyPair" in wallet;
|
|
120
|
+
}
|
|
121
|
+
function isBrowserExtensionSuiWallet(wallet) {
|
|
122
|
+
return "wallet" in wallet && "account" in wallet;
|
|
123
|
+
}
|
|
124
|
+
function isSuiWallet(wallet) {
|
|
125
|
+
return isPkSuiWallet(wallet) || isBrowserExtensionSuiWallet(wallet);
|
|
126
|
+
}
|
|
127
|
+
var SuiWalletProvider = class {
|
|
128
|
+
client;
|
|
129
|
+
wallet;
|
|
130
|
+
constructor(walletConfig) {
|
|
131
|
+
if (isPrivateKeySuiWalletConfig(walletConfig)) {
|
|
132
|
+
this.client = new client.SuiClient({ url: walletConfig.rpcUrl });
|
|
133
|
+
this.wallet = {
|
|
134
|
+
keyPair: ed25519.Ed25519Keypair.deriveKeypair(walletConfig.mnemonics)
|
|
135
|
+
};
|
|
136
|
+
} else if (isBrowserExtensionSuiWalletConfig(walletConfig)) {
|
|
137
|
+
this.client = walletConfig.client;
|
|
138
|
+
this.wallet = {
|
|
139
|
+
wallet: walletConfig.wallet,
|
|
140
|
+
account: walletConfig.account
|
|
141
|
+
};
|
|
142
|
+
} else {
|
|
143
|
+
throw new Error("Invalid wallet configuration");
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async signAndExecuteTxn(txn) {
|
|
147
|
+
if (isPkSuiWallet(this.wallet)) {
|
|
148
|
+
const res = await this.client.signAndExecuteTransaction({
|
|
149
|
+
transaction: txn,
|
|
150
|
+
signer: this.wallet.keyPair
|
|
151
|
+
});
|
|
152
|
+
return res.digest;
|
|
153
|
+
}
|
|
154
|
+
if (isBrowserExtensionSuiWallet(this.wallet)) {
|
|
155
|
+
const browserWallet = this.wallet.wallet;
|
|
156
|
+
const browserAccount = this.wallet.account;
|
|
157
|
+
if (!browserAccount || browserAccount.chains.length === 0) {
|
|
158
|
+
throw new Error("No chains available for wallet account");
|
|
159
|
+
}
|
|
160
|
+
const chain = browserAccount.chains[0];
|
|
161
|
+
if (!chain) {
|
|
162
|
+
throw new Error("No chain available for wallet account");
|
|
163
|
+
}
|
|
164
|
+
const { bytes, signature } = await walletStandard.signTransaction(browserWallet, {
|
|
165
|
+
transaction: txn,
|
|
166
|
+
account: browserAccount,
|
|
167
|
+
chain
|
|
168
|
+
});
|
|
169
|
+
const res = await this.client.executeTransactionBlock({
|
|
170
|
+
transactionBlock: bytes,
|
|
171
|
+
signature,
|
|
172
|
+
options: {
|
|
173
|
+
showRawEffects: true
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
return res.digest;
|
|
177
|
+
}
|
|
178
|
+
throw new Error("Invalid wallet configuration");
|
|
179
|
+
}
|
|
180
|
+
async viewContract(tx, packageId, module, functionName, args, typeArgs = []) {
|
|
181
|
+
tx.moveCall({
|
|
182
|
+
target: `${packageId}::${module}::${functionName}`,
|
|
183
|
+
arguments: args,
|
|
184
|
+
typeArguments: typeArgs
|
|
185
|
+
});
|
|
186
|
+
const sender = this.getSuiAddress();
|
|
187
|
+
const txResults = await this.client.devInspectTransactionBlock({
|
|
188
|
+
transactionBlock: tx,
|
|
189
|
+
sender
|
|
190
|
+
});
|
|
191
|
+
if (txResults.results && txResults.results[0] !== void 0) {
|
|
192
|
+
return txResults.results[0];
|
|
193
|
+
}
|
|
194
|
+
throw Error(`transaction didn't return any values: ${JSON.stringify(txResults, null, 2)}`);
|
|
195
|
+
}
|
|
196
|
+
async getCoins(address, token) {
|
|
197
|
+
return this.client.getCoins({ owner: address, coinType: token, limit: 10 });
|
|
198
|
+
}
|
|
199
|
+
getSuiAddress() {
|
|
200
|
+
if (isPkSuiWallet(this.wallet)) {
|
|
201
|
+
return this.wallet.keyPair.toSuiAddress();
|
|
202
|
+
}
|
|
203
|
+
if (isBrowserExtensionSuiWallet(this.wallet)) {
|
|
204
|
+
return this.wallet.account.address;
|
|
205
|
+
}
|
|
206
|
+
throw new Error("Invalid wallet configuration");
|
|
207
|
+
}
|
|
208
|
+
async getWalletAddress() {
|
|
209
|
+
return this.getSuiAddress();
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
var IconSdk = "default" in IconSdkRaw__namespace.default ? IconSdkRaw__namespace.default : IconSdkRaw__namespace;
|
|
213
|
+
var { Converter, CallTransactionBuilder, Wallet } = IconSdk;
|
|
214
|
+
var IconWalletProvider = class {
|
|
215
|
+
wallet;
|
|
216
|
+
iconService;
|
|
217
|
+
constructor(wallet) {
|
|
218
|
+
if (isPrivateKeyIconWalletConfig(wallet)) {
|
|
219
|
+
this.wallet = {
|
|
220
|
+
type: "PRIVATE_KEY",
|
|
221
|
+
wallet: Wallet.loadPrivateKey(wallet.privateKey.slice(2))
|
|
222
|
+
};
|
|
223
|
+
this.iconService = new IconSdk.IconService(new IconSdk.IconService.HttpProvider(wallet.rpcUrl));
|
|
224
|
+
} else if (isBrowserExtensionIconWalletConfig(wallet)) {
|
|
225
|
+
this.wallet = {
|
|
226
|
+
type: "BROWSER_EXTENSION",
|
|
227
|
+
wallet: wallet.walletAddress
|
|
228
|
+
};
|
|
229
|
+
this.iconService = new IconSdk.IconService(new IconSdk.IconService.HttpProvider(wallet.rpcUrl));
|
|
230
|
+
} else {
|
|
231
|
+
throw new Error("Invalid Icon wallet config");
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
async sendTransaction(tx) {
|
|
235
|
+
const builtTx = new CallTransactionBuilder().from(tx.from).to(tx.to).stepLimit(Converter.toHex(2e6)).nid(tx.nid).version(tx.version ?? "0x3").timestamp(Converter.toHex(tx.timestamp ?? (/* @__PURE__ */ new Date()).getTime() * 1e3)).value(tx.value).method(tx.method).params(tx.params).build();
|
|
236
|
+
if (!isIconPkWallet(this.wallet)) {
|
|
237
|
+
const result2 = await requestJsonRpc(builtTx);
|
|
238
|
+
return result2.result;
|
|
239
|
+
}
|
|
240
|
+
const signedTx = new IconSdk.IconService.SignedTransaction(builtTx, this.wallet.wallet);
|
|
241
|
+
const result = await this.iconService.sendTransaction(signedTx).execute();
|
|
242
|
+
return result;
|
|
243
|
+
}
|
|
244
|
+
async waitForTransactionReceipt(txHash) {
|
|
245
|
+
const result = await this.iconService.waitTransactionResult(txHash).execute();
|
|
246
|
+
return {
|
|
247
|
+
...result,
|
|
248
|
+
status: +result.status,
|
|
249
|
+
cumulativeStepUsed: BigNumberToBigInt(result.cumulativeStepUsed),
|
|
250
|
+
stepUsed: BigNumberToBigInt(result.stepUsed),
|
|
251
|
+
stepPrice: BigNumberToBigInt(result.stepPrice)
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
async getWalletAddress() {
|
|
255
|
+
if (!this.wallet.wallet) {
|
|
256
|
+
throw new Error("Wallet not initialized");
|
|
257
|
+
}
|
|
258
|
+
return isIconPkWallet(this.wallet) ? this.wallet.wallet.getAddress() : this.wallet.wallet;
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
function isIconPkWallet(wallet) {
|
|
262
|
+
return wallet.type === "PRIVATE_KEY";
|
|
263
|
+
}
|
|
264
|
+
function isIconBrowserExtensionWallet(wallet) {
|
|
265
|
+
return wallet.type === "BROWSER_EXTENSION";
|
|
266
|
+
}
|
|
267
|
+
function isPrivateKeyIconWalletConfig(config) {
|
|
268
|
+
return "privateKey" in config && config.privateKey.startsWith("0x");
|
|
269
|
+
}
|
|
270
|
+
function isBrowserExtensionIconWalletConfig(config) {
|
|
271
|
+
return "walletAddress" in config && (isIconEoaAddress(config.walletAddress) || !config.walletAddress);
|
|
272
|
+
}
|
|
273
|
+
function isIconAddress(value) {
|
|
274
|
+
return typeof value === "string" && /^hx[a-f0-9]{40}$|^cx[a-f0-9]{40}$/.test(value);
|
|
275
|
+
}
|
|
276
|
+
function isIconEoaAddress(value) {
|
|
277
|
+
return typeof value === "string" && /^hx[a-f0-9]{40}$/.test(value);
|
|
278
|
+
}
|
|
279
|
+
function isResponseAddressType(value) {
|
|
280
|
+
return typeof value === "object" && value !== null && "type" in value && "payload" in value && value.type === "RESPONSE_ADDRESS" && isIconAddress(value.payload);
|
|
281
|
+
}
|
|
282
|
+
function isResponseSigningType(value) {
|
|
283
|
+
return typeof value === "object" && value !== null && "type" in value && "payload" in value && value.type === "RESPONSE_SIGNING" && typeof value.payload === "string";
|
|
284
|
+
}
|
|
285
|
+
function isJsonRpcPayloadResponse(value) {
|
|
286
|
+
return typeof value === "object" && value !== null && "id" in value && "result" in value && typeof value.result === "string";
|
|
287
|
+
}
|
|
288
|
+
function requestAddress() {
|
|
289
|
+
return new Promise((resolve) => {
|
|
290
|
+
const eventHandler = (event) => {
|
|
291
|
+
const customEvent = event;
|
|
292
|
+
const response = customEvent.detail;
|
|
293
|
+
if (isResponseAddressType(response)) {
|
|
294
|
+
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
295
|
+
resolve(response.payload);
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
299
|
+
window.addEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
300
|
+
window.dispatchEvent(
|
|
301
|
+
new CustomEvent("ICONEX_RELAY_REQUEST", {
|
|
302
|
+
detail: {
|
|
303
|
+
type: "REQUEST_ADDRESS"
|
|
304
|
+
}
|
|
305
|
+
})
|
|
306
|
+
);
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
function requestSigning(from, hash) {
|
|
310
|
+
return new Promise((resolve, reject) => {
|
|
311
|
+
const signRequest = new CustomEvent("ICONEX_RELAY_REQUEST", {
|
|
312
|
+
detail: {
|
|
313
|
+
type: "REQUEST_SIGNING",
|
|
314
|
+
payload: {
|
|
315
|
+
from,
|
|
316
|
+
hash
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
const eventHandler = (event) => {
|
|
321
|
+
const customEvent = event;
|
|
322
|
+
const response = customEvent.detail;
|
|
323
|
+
if (isResponseSigningType(response)) {
|
|
324
|
+
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
325
|
+
resolve(response.payload);
|
|
326
|
+
} else if (response.type === "CANCEL_SIGNING") {
|
|
327
|
+
reject(new Error("CANCEL_SIGNING"));
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
331
|
+
window.addEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
332
|
+
window.dispatchEvent(signRequest);
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
function requestJsonRpc(rawTransaction, id = 99999) {
|
|
336
|
+
return new Promise((resolve, reject) => {
|
|
337
|
+
const eventHandler = (event) => {
|
|
338
|
+
const customEvent = event;
|
|
339
|
+
const { type, payload } = customEvent.detail;
|
|
340
|
+
if (type === "RESPONSE_JSON-RPC") {
|
|
341
|
+
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
342
|
+
if (isJsonRpcPayloadResponse(payload)) {
|
|
343
|
+
resolve(payload);
|
|
344
|
+
} else {
|
|
345
|
+
reject(new Error("Invalid payload response type (expected JsonRpcPayloadResponse)"));
|
|
346
|
+
}
|
|
347
|
+
} else if (type === "CANCEL_JSON-RPC") {
|
|
348
|
+
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
349
|
+
reject(new Error("CANCEL_JSON-RPC"));
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
window.removeEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
353
|
+
window.addEventListener("ICONEX_RELAY_RESPONSE", eventHandler, false);
|
|
354
|
+
window.dispatchEvent(
|
|
355
|
+
new CustomEvent("ICONEX_RELAY_REQUEST", {
|
|
356
|
+
detail: {
|
|
357
|
+
type: "REQUEST_JSON-RPC",
|
|
358
|
+
payload: {
|
|
359
|
+
jsonrpc: "2.0",
|
|
360
|
+
method: "icx_sendTransaction",
|
|
361
|
+
params: rawTransaction,
|
|
362
|
+
id
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
})
|
|
366
|
+
);
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
function BigNumberToBigInt(bigNumber) {
|
|
370
|
+
if (!bigNumber.isInteger()) {
|
|
371
|
+
throw new Error("Cannot convert decimal number to BigInt");
|
|
372
|
+
}
|
|
373
|
+
return BigInt(bigNumber.toFixed(0));
|
|
374
|
+
}
|
|
375
|
+
function isBrowserExtensionInjectiveWalletConfig(config) {
|
|
376
|
+
return "msgBroadcaster" in config && "walletAddress" in config;
|
|
377
|
+
}
|
|
378
|
+
var InjectiveWalletProvider = class {
|
|
379
|
+
msgBroadcaster;
|
|
380
|
+
walletAddress;
|
|
381
|
+
constructor(config) {
|
|
382
|
+
if (isBrowserExtensionInjectiveWalletConfig(config)) {
|
|
383
|
+
this.msgBroadcaster = config.msgBroadcaster;
|
|
384
|
+
this.walletAddress = config.walletAddress;
|
|
385
|
+
} else {
|
|
386
|
+
throw new Error("Invalid Injective wallet config");
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
getRawTransaction(chainId, _, senderAddress, contractAddress, msg, memo) {
|
|
390
|
+
if (!this.walletAddress) {
|
|
391
|
+
throw new Error("Wallet address not found");
|
|
392
|
+
}
|
|
393
|
+
const msgExec = sdkTs.MsgExecuteContract.fromJSON({
|
|
394
|
+
contractAddress,
|
|
395
|
+
sender: senderAddress,
|
|
396
|
+
msg,
|
|
397
|
+
funds: []
|
|
398
|
+
});
|
|
399
|
+
const { txRaw } = sdkTs.createTransaction({
|
|
400
|
+
message: msgExec,
|
|
401
|
+
memo: "",
|
|
402
|
+
pubKey: Buffer.from(this.walletAddress).toString(),
|
|
403
|
+
sequence: 0,
|
|
404
|
+
accountNumber: 0,
|
|
405
|
+
chainId
|
|
406
|
+
});
|
|
407
|
+
const rawTx = {
|
|
408
|
+
from: senderAddress,
|
|
409
|
+
to: contractAddress,
|
|
410
|
+
signedDoc: {
|
|
411
|
+
bodyBytes: txRaw.bodyBytes,
|
|
412
|
+
chainId,
|
|
413
|
+
accountNumber: BigInt(0),
|
|
414
|
+
authInfoBytes: txRaw.authInfoBytes
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
return Promise.resolve(rawTx);
|
|
418
|
+
}
|
|
419
|
+
async getWalletAddress() {
|
|
420
|
+
if (!this.walletAddress) {
|
|
421
|
+
throw new Error("Wallet address not found");
|
|
422
|
+
}
|
|
423
|
+
return Promise.resolve(this.walletAddress);
|
|
424
|
+
}
|
|
425
|
+
async execute(senderAddress, contractAddress, msg, funds) {
|
|
426
|
+
if (!this.walletAddress) {
|
|
427
|
+
throw new Error("Wallet address not found");
|
|
428
|
+
}
|
|
429
|
+
const msgExec = sdkTs.MsgExecuteContractCompat.fromJSON({
|
|
430
|
+
contractAddress,
|
|
431
|
+
sender: senderAddress,
|
|
432
|
+
msg,
|
|
433
|
+
funds: funds || []
|
|
434
|
+
});
|
|
435
|
+
const txResult = await this.msgBroadcaster.broadcastWithFeeDelegation({
|
|
436
|
+
msgs: msgExec,
|
|
437
|
+
injectiveAddress: this.walletAddress
|
|
438
|
+
});
|
|
439
|
+
return types.InjectiveExecuteResponse.fromTxResponse(txResult);
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
function isPrivateKeySolanaWalletConfig(walletConfig) {
|
|
443
|
+
return "privateKey" in walletConfig;
|
|
444
|
+
}
|
|
445
|
+
function isBrowserExtensionSolanaWalletConfig(walletConfig) {
|
|
446
|
+
return "wallet" in walletConfig && !(walletConfig.wallet instanceof web3_js.Keypair);
|
|
447
|
+
}
|
|
448
|
+
var SolanaWalletProvider = class {
|
|
449
|
+
wallet;
|
|
450
|
+
connection;
|
|
451
|
+
isAdapterMode;
|
|
452
|
+
constructor(walletConfig) {
|
|
453
|
+
if (isPrivateKeySolanaWalletConfig(walletConfig)) {
|
|
454
|
+
this.wallet = web3_js.Keypair.fromSecretKey(walletConfig.privateKey);
|
|
455
|
+
this.connection = new web3_js.Connection(walletConfig.endpoint, "confirmed");
|
|
456
|
+
this.isAdapterMode = false;
|
|
457
|
+
} else if (isBrowserExtensionSolanaWalletConfig(walletConfig)) {
|
|
458
|
+
this.wallet = walletConfig.wallet;
|
|
459
|
+
this.connection = walletConfig.connection;
|
|
460
|
+
this.isAdapterMode = true;
|
|
461
|
+
} else {
|
|
462
|
+
throw new Error("Invalid wallet configuration");
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
async waitForConfirmation(signature, commitment = "finalized") {
|
|
466
|
+
const latestBlockhash = await this.connection.getLatestBlockhash();
|
|
467
|
+
const response = await this.connection.confirmTransaction(
|
|
468
|
+
{
|
|
469
|
+
signature,
|
|
470
|
+
blockhash: latestBlockhash.blockhash,
|
|
471
|
+
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight
|
|
472
|
+
},
|
|
473
|
+
commitment
|
|
474
|
+
);
|
|
475
|
+
return response;
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Send a raw transaction to the Solana network.
|
|
479
|
+
* @param rawTransaction - The raw transaction to send.
|
|
480
|
+
* @returns The transaction signature.
|
|
481
|
+
*/
|
|
482
|
+
async sendTransaction(rawTransaction) {
|
|
483
|
+
return this.connection.sendRawTransaction(rawTransaction);
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Send a raw transaction to the Solana network and wait for confirmation.
|
|
487
|
+
* @param rawTransaction - The raw transaction to send.
|
|
488
|
+
* @param commitment - The commitment level to use. Defaults to 'finalized'.
|
|
489
|
+
* @returns The transaction signature.
|
|
490
|
+
*/
|
|
491
|
+
async sendTransactionWithConfirmation(rawTransaction, commitment = "finalized") {
|
|
492
|
+
const txHash = await this.connection.sendRawTransaction(rawTransaction);
|
|
493
|
+
await this.waitForConfirmation(txHash, commitment);
|
|
494
|
+
return txHash;
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Build a v0 versioned transaction.
|
|
498
|
+
* @param instructions - The instructions to include in the transaction.
|
|
499
|
+
* @returns The v0 transaction.
|
|
500
|
+
*/
|
|
501
|
+
async buildV0Txn(rawInstructions) {
|
|
502
|
+
if (this.isAdapterMode) {
|
|
503
|
+
return this.buildV0TxnWithAdapter(rawInstructions);
|
|
504
|
+
}
|
|
505
|
+
return this.buildV0TxnWithKeypair(rawInstructions);
|
|
506
|
+
}
|
|
507
|
+
async buildV0TxnWithAdapter(rawInstructions) {
|
|
508
|
+
const adapterWallet = this.wallet;
|
|
509
|
+
if (!adapterWallet.publicKey) {
|
|
510
|
+
throw new Error("Wallet public key is not initialized");
|
|
511
|
+
}
|
|
512
|
+
if (!adapterWallet.signTransaction) {
|
|
513
|
+
throw new Error("Wallet signTransaction is not initialized");
|
|
514
|
+
}
|
|
515
|
+
const instructions = this.buildTransactionInstruction(rawInstructions);
|
|
516
|
+
const latestBlockhash = await this.connection.getLatestBlockhash();
|
|
517
|
+
const messageV0 = new web3_js.TransactionMessage({
|
|
518
|
+
payerKey: adapterWallet.publicKey,
|
|
519
|
+
recentBlockhash: latestBlockhash.blockhash,
|
|
520
|
+
instructions
|
|
521
|
+
}).compileToV0Message();
|
|
522
|
+
const tx = await adapterWallet.signTransaction(new web3_js.VersionedTransaction(messageV0));
|
|
523
|
+
return tx.serialize();
|
|
524
|
+
}
|
|
525
|
+
async buildV0TxnWithKeypair(rawInstructions) {
|
|
526
|
+
const keypairWallet = this.wallet;
|
|
527
|
+
const instructions = this.buildTransactionInstruction(rawInstructions);
|
|
528
|
+
const messageV0 = new web3_js.TransactionMessage({
|
|
529
|
+
payerKey: keypairWallet.publicKey,
|
|
530
|
+
recentBlockhash: (await this.connection.getLatestBlockhash()).blockhash,
|
|
531
|
+
instructions
|
|
532
|
+
}).compileToV0Message();
|
|
533
|
+
const tx = new web3_js.VersionedTransaction(messageV0);
|
|
534
|
+
tx.sign([keypairWallet]);
|
|
535
|
+
return tx.serialize();
|
|
536
|
+
}
|
|
537
|
+
getWalletBase58PublicKey() {
|
|
538
|
+
if (!this.wallet.publicKey) {
|
|
539
|
+
throw new Error("Wallet public key is not initialized");
|
|
540
|
+
}
|
|
541
|
+
return this.wallet.publicKey.toBase58();
|
|
542
|
+
}
|
|
543
|
+
async getWalletAddress() {
|
|
544
|
+
return this.getWalletBase58PublicKey();
|
|
545
|
+
}
|
|
546
|
+
async getAssociatedTokenAddress(mint) {
|
|
547
|
+
if (!this.wallet.publicKey) {
|
|
548
|
+
throw new Error("Wallet public key is not initialized");
|
|
549
|
+
}
|
|
550
|
+
return (await splToken.getAssociatedTokenAddress(new web3_js.PublicKey(mint), this.wallet.publicKey, true)).toBase58();
|
|
551
|
+
}
|
|
552
|
+
async getBalance(publicKey) {
|
|
553
|
+
return await this.connection.getBalance(new web3_js.PublicKey(publicKey));
|
|
554
|
+
}
|
|
555
|
+
async getTokenAccountBalance(publicKey) {
|
|
556
|
+
return this.connection.getTokenAccountBalance(new web3_js.PublicKey(publicKey));
|
|
557
|
+
}
|
|
558
|
+
buildTransactionInstruction(rawInstructions) {
|
|
559
|
+
return rawInstructions.map(
|
|
560
|
+
(rawInstruction) => new web3_js.TransactionInstruction({
|
|
561
|
+
keys: rawInstruction.keys.map((key) => ({
|
|
562
|
+
pubkey: new web3_js.PublicKey(key.pubkey),
|
|
563
|
+
isSigner: key.isSigner,
|
|
564
|
+
isWritable: key.isWritable
|
|
565
|
+
})),
|
|
566
|
+
programId: new web3_js.PublicKey(rawInstruction.programId),
|
|
567
|
+
data: Buffer.from(rawInstruction.data)
|
|
568
|
+
})
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
};
|
|
572
|
+
var STELLAR_HORIZON_URLS = {
|
|
573
|
+
TESTNET: "https://horizon-testnet.stellar.org",
|
|
574
|
+
PUBLIC: "https://horizon.stellar.org"
|
|
575
|
+
};
|
|
576
|
+
var STELLAR_NETWORK_PASSPHRASES = {
|
|
577
|
+
TESTNET: stellarSdk.Networks.TESTNET,
|
|
578
|
+
PUBLIC: stellarSdk.Networks.PUBLIC
|
|
579
|
+
};
|
|
580
|
+
var TX_POLL_INTERVAL = 2e3;
|
|
581
|
+
var TX_POLL_TIMEOUT = 6e4;
|
|
582
|
+
var StellarWalletError = class extends Error {
|
|
583
|
+
constructor(message, code) {
|
|
584
|
+
super(message);
|
|
585
|
+
this.code = code;
|
|
586
|
+
this.name = "StellarWalletError";
|
|
587
|
+
}
|
|
588
|
+
};
|
|
589
|
+
function isPrivateKeyStellarWalletConfig(config) {
|
|
590
|
+
return config.type === "PRIVATE_KEY";
|
|
591
|
+
}
|
|
592
|
+
function isBrowserExtensionStellarWalletConfig(config) {
|
|
593
|
+
return config.type === "BROWSER_EXTENSION";
|
|
594
|
+
}
|
|
595
|
+
function isStellarPkWallet(wallet) {
|
|
596
|
+
return wallet.type === "PRIVATE_KEY";
|
|
597
|
+
}
|
|
598
|
+
function isStellarBrowserExtensionWallet(wallet) {
|
|
599
|
+
return wallet.type === "BROWSER_EXTENSION";
|
|
600
|
+
}
|
|
601
|
+
function isValidStellarNetwork(network) {
|
|
602
|
+
return ["TESTNET", "PUBLIC"].includes(network);
|
|
603
|
+
}
|
|
604
|
+
function isValidStellarPrivateKey(privateKey) {
|
|
605
|
+
try {
|
|
606
|
+
const key = privateKey.startsWith("0x") ? privateKey.slice(2) : privateKey;
|
|
607
|
+
stellarSdk.Keypair.fromSecret(key);
|
|
608
|
+
return true;
|
|
609
|
+
} catch {
|
|
610
|
+
return false;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
var StellarWalletProvider = class {
|
|
614
|
+
wallet;
|
|
615
|
+
server;
|
|
616
|
+
networkPassphrase;
|
|
617
|
+
constructor(config) {
|
|
618
|
+
if (!isValidStellarNetwork(config.network)) {
|
|
619
|
+
throw new StellarWalletError(`Invalid network: ${config.network}`, "INVALID_NETWORK");
|
|
620
|
+
}
|
|
621
|
+
if (isPrivateKeyStellarWalletConfig(config)) {
|
|
622
|
+
if (!isValidStellarPrivateKey(config.privateKey)) {
|
|
623
|
+
throw new StellarWalletError("Invalid private key format", "INVALID_PRIVATE_KEY");
|
|
624
|
+
}
|
|
625
|
+
const privateKey = config.privateKey.startsWith("0x") ? config.privateKey.slice(2) : config.privateKey;
|
|
626
|
+
this.wallet = {
|
|
627
|
+
type: "PRIVATE_KEY",
|
|
628
|
+
keypair: stellarSdk.Keypair.fromSecret(privateKey)
|
|
629
|
+
};
|
|
630
|
+
this.server = new stellarSdk.Horizon.Server(config.rpcUrl ?? STELLAR_HORIZON_URLS[config.network]);
|
|
631
|
+
this.networkPassphrase = STELLAR_NETWORK_PASSPHRASES[config.network];
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
if (isBrowserExtensionStellarWalletConfig(config)) {
|
|
635
|
+
this.wallet = {
|
|
636
|
+
type: "BROWSER_EXTENSION",
|
|
637
|
+
walletsKit: config.walletsKit
|
|
638
|
+
};
|
|
639
|
+
this.server = new stellarSdk.Horizon.Server(config.rpcUrl ?? STELLAR_HORIZON_URLS[config.network]);
|
|
640
|
+
this.networkPassphrase = STELLAR_NETWORK_PASSPHRASES[config.network];
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
throw new StellarWalletError("Invalid wallet configuration", "INVALID_CONFIG");
|
|
644
|
+
}
|
|
645
|
+
async getWalletAddress() {
|
|
646
|
+
try {
|
|
647
|
+
if (isStellarPkWallet(this.wallet)) {
|
|
648
|
+
return this.wallet.keypair.publicKey();
|
|
649
|
+
}
|
|
650
|
+
const { address } = await this.wallet.walletsKit.getAddress();
|
|
651
|
+
return address;
|
|
652
|
+
} catch (error) {
|
|
653
|
+
throw new StellarWalletError(
|
|
654
|
+
error instanceof Error ? error.message : "Failed to get wallet address",
|
|
655
|
+
"INVALID_CONFIG"
|
|
656
|
+
);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
async signTransaction(tx) {
|
|
660
|
+
try {
|
|
661
|
+
if (isStellarPkWallet(this.wallet)) {
|
|
662
|
+
const transaction = new stellarSdk.Transaction(tx, this.networkPassphrase);
|
|
663
|
+
transaction.sign(this.wallet.keypair);
|
|
664
|
+
return transaction.toXDR();
|
|
665
|
+
}
|
|
666
|
+
const { signedTxXdr } = await this.wallet.walletsKit.signTransaction(tx, {
|
|
667
|
+
networkPassphrase: this.networkPassphrase
|
|
668
|
+
});
|
|
669
|
+
return signedTxXdr;
|
|
670
|
+
} catch (error) {
|
|
671
|
+
throw new StellarWalletError(
|
|
672
|
+
error instanceof Error ? error.message : "Failed to sign transaction",
|
|
673
|
+
"SIGN_TX_ERROR"
|
|
674
|
+
);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
async waitForTransactionReceipt(txHash) {
|
|
678
|
+
const startTime = Date.now();
|
|
679
|
+
while (Date.now() - startTime < TX_POLL_TIMEOUT) {
|
|
680
|
+
try {
|
|
681
|
+
const tx = await this.server.transactions().transaction(txHash).call();
|
|
682
|
+
return {
|
|
683
|
+
...tx,
|
|
684
|
+
_links: {
|
|
685
|
+
...tx._links,
|
|
686
|
+
transaction: tx._links.self
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
} catch (error) {
|
|
690
|
+
await new Promise((resolve) => setTimeout(resolve, TX_POLL_INTERVAL));
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
throw new StellarWalletError(
|
|
694
|
+
`Transaction receipt not found for hash ${txHash} after ${TX_POLL_TIMEOUT / 1e3} seconds.`,
|
|
695
|
+
"TX_RECEIPT_TIMEOUT"
|
|
696
|
+
);
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
|
|
700
|
+
exports.BigNumberToBigInt = BigNumberToBigInt;
|
|
701
|
+
exports.EvmWalletProvider = EvmWalletProvider;
|
|
702
|
+
exports.IconWalletProvider = IconWalletProvider;
|
|
703
|
+
exports.InjectiveWalletProvider = InjectiveWalletProvider;
|
|
704
|
+
exports.SolanaWalletProvider = SolanaWalletProvider;
|
|
705
|
+
exports.StellarWalletError = StellarWalletError;
|
|
706
|
+
exports.StellarWalletProvider = StellarWalletProvider;
|
|
707
|
+
exports.SuiWalletProvider = SuiWalletProvider;
|
|
708
|
+
exports.getEvmViemChain = getEvmViemChain;
|
|
709
|
+
exports.isBrowserExtensionEvmWalletConfig = isBrowserExtensionEvmWalletConfig;
|
|
710
|
+
exports.isBrowserExtensionIconWalletConfig = isBrowserExtensionIconWalletConfig;
|
|
711
|
+
exports.isBrowserExtensionInjectiveWalletConfig = isBrowserExtensionInjectiveWalletConfig;
|
|
712
|
+
exports.isBrowserExtensionStellarWalletConfig = isBrowserExtensionStellarWalletConfig;
|
|
713
|
+
exports.isBrowserExtensionSuiWallet = isBrowserExtensionSuiWallet;
|
|
714
|
+
exports.isIconAddress = isIconAddress;
|
|
715
|
+
exports.isIconBrowserExtensionWallet = isIconBrowserExtensionWallet;
|
|
716
|
+
exports.isIconEoaAddress = isIconEoaAddress;
|
|
717
|
+
exports.isIconPkWallet = isIconPkWallet;
|
|
718
|
+
exports.isJsonRpcPayloadResponse = isJsonRpcPayloadResponse;
|
|
719
|
+
exports.isPkSuiWallet = isPkSuiWallet;
|
|
720
|
+
exports.isPrivateKeyEvmWalletConfig = isPrivateKeyEvmWalletConfig;
|
|
721
|
+
exports.isPrivateKeyIconWalletConfig = isPrivateKeyIconWalletConfig;
|
|
722
|
+
exports.isPrivateKeyStellarWalletConfig = isPrivateKeyStellarWalletConfig;
|
|
723
|
+
exports.isResponseAddressType = isResponseAddressType;
|
|
724
|
+
exports.isResponseSigningType = isResponseSigningType;
|
|
725
|
+
exports.isStellarBrowserExtensionWallet = isStellarBrowserExtensionWallet;
|
|
726
|
+
exports.isStellarPkWallet = isStellarPkWallet;
|
|
727
|
+
exports.isSuiWallet = isSuiWallet;
|
|
728
|
+
exports.isValidStellarNetwork = isValidStellarNetwork;
|
|
729
|
+
exports.isValidStellarPrivateKey = isValidStellarPrivateKey;
|
|
730
|
+
exports.requestAddress = requestAddress;
|
|
731
|
+
exports.requestJsonRpc = requestJsonRpc;
|
|
732
|
+
exports.requestSigning = requestSigning;
|
|
733
|
+
//# sourceMappingURL=index.cjs.map
|
|
734
|
+
//# sourceMappingURL=index.cjs.map
|