@cordystackx/cordy_minikit 1.3.2 → 1.4.7

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.
@@ -0,0 +1,55 @@
1
+ export interface NetworkConfig {
2
+ chainId: number;
3
+ chainName: string;
4
+ nativeCurrency: {
5
+ name: string;
6
+ symbol: string;
7
+ decimals: number;
8
+ };
9
+ rpcUrls: string[];
10
+ blockExplorerUrls?: string[];
11
+ }
12
+ export declare const NETWORKS: Record<string, NetworkConfig>;
13
+ /**
14
+ * Detect which network the contract is deployed on
15
+ * by checking contract code existence across all supported networks
16
+ */
17
+ export declare function detectContractNetwork(contractAddress: string): Promise<NetworkConfig | null>;
18
+ /**
19
+ * Get network configuration by chain ID
20
+ */
21
+ export declare function getNetworkByChainId(chainId: number): NetworkConfig | null;
22
+ /**
23
+ * Check if a chain ID is supported
24
+ */
25
+ export declare function isNetworkSupported(chainId: number): boolean;
26
+ /**
27
+ * Add a custom network to the supported networks list
28
+ * Useful for adding networks that aren't included by default
29
+ */
30
+ export declare function addCustomNetwork(key: string, network: NetworkConfig): void;
31
+ /**
32
+ * Add multiple custom networks at once
33
+ */
34
+ export declare function addCustomNetworks(networks: Record<string, NetworkConfig>): void;
35
+ /**
36
+ * Helper to create a network configuration
37
+ */
38
+ export declare function createNetworkConfig(params: {
39
+ chainId: number;
40
+ chainName: string;
41
+ symbol: string;
42
+ symbolName?: string;
43
+ rpcUrl: string;
44
+ explorerUrl?: string;
45
+ }): NetworkConfig;
46
+ /**
47
+ * Force switch to the required network
48
+ * Throws error if user rejects
49
+ */
50
+ export declare function enforceNetwork(requiredChainId: number, networkConfig: NetworkConfig): Promise<boolean>;
51
+ /**
52
+ * Monitor network changes and enforce the correct network
53
+ */
54
+ export declare function setupNetworkGuard(requiredChainId: number, networkConfig: NetworkConfig, onWrongNetwork?: () => void): () => void;
55
+ //# sourceMappingURL=networkEnforcer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file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,EAAE,MAAM,EACvB,aAAa,EAAE,aAAa,GAC3B,OAAO,CAAC,OAAO,CAAC,CA6ClB;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,eAAe,EAAE,MAAM,EACvB,aAAa,EAAE,aAAa,EAC5B,cAAc,CAAC,EAAE,MAAM,IAAI,GAC1B,MAAM,IAAI,CAiCZ"}
@@ -0,0 +1,355 @@
1
+ import { ethers } from "ethers";
2
+ // Supported networks - Add more as needed
3
+ export const NETWORKS = {
4
+ // Ethereum Networks
5
+ ETHEREUM_MAINNET: {
6
+ chainId: 1,
7
+ chainName: "Ethereum Mainnet",
8
+ nativeCurrency: {
9
+ name: "Ethereum",
10
+ symbol: "ETH",
11
+ decimals: 18,
12
+ },
13
+ rpcUrls: ["https://eth.llamarpc.com"],
14
+ blockExplorerUrls: ["https://etherscan.io"],
15
+ },
16
+ ETHEREUM_SEPOLIA: {
17
+ chainId: 11155111,
18
+ chainName: "Sepolia Testnet",
19
+ nativeCurrency: {
20
+ name: "Ethereum",
21
+ symbol: "ETH",
22
+ decimals: 18,
23
+ },
24
+ rpcUrls: ["https://rpc.sepolia.org"],
25
+ blockExplorerUrls: ["https://sepolia.etherscan.io"],
26
+ },
27
+ // Base Networks
28
+ BASE_MAINNET: {
29
+ chainId: 8453,
30
+ chainName: "Base Mainnet",
31
+ nativeCurrency: {
32
+ name: "Ethereum",
33
+ symbol: "ETH",
34
+ decimals: 18,
35
+ },
36
+ rpcUrls: ["https://mainnet.base.org"],
37
+ blockExplorerUrls: ["https://basescan.org"],
38
+ },
39
+ BASE_SEPOLIA: {
40
+ chainId: 84532,
41
+ chainName: "Base Sepolia",
42
+ nativeCurrency: {
43
+ name: "Ethereum",
44
+ symbol: "ETH",
45
+ decimals: 18,
46
+ },
47
+ rpcUrls: ["https://sepolia.base.org"],
48
+ blockExplorerUrls: ["https://sepolia.basescan.org"],
49
+ },
50
+ // Core Networks
51
+ CORE_MAINNET: {
52
+ chainId: 1116,
53
+ chainName: "Core Blockchain Mainnet",
54
+ nativeCurrency: {
55
+ name: "Core",
56
+ symbol: "CORE",
57
+ decimals: 18,
58
+ },
59
+ rpcUrls: ["https://rpc.coredao.org"],
60
+ blockExplorerUrls: ["https://scan.coredao.org"],
61
+ },
62
+ CORE_TESTNET: {
63
+ chainId: 1115,
64
+ chainName: "Core Blockchain Testnet",
65
+ nativeCurrency: {
66
+ name: "tCore",
67
+ symbol: "tCORE",
68
+ decimals: 18,
69
+ },
70
+ rpcUrls: ["https://rpc.test.btcs.network"],
71
+ blockExplorerUrls: ["https://scan.test.btcs.network"],
72
+ },
73
+ CORE_TESTNET2: {
74
+ chainId: 1114,
75
+ chainName: "Core Blockchain Testnet2",
76
+ nativeCurrency: {
77
+ name: "tCore2",
78
+ symbol: "tCORE2",
79
+ decimals: 18,
80
+ },
81
+ rpcUrls: ["https://rpc.test2.btcs.network"],
82
+ blockExplorerUrls: [" https://scan.test2.btcs.network"],
83
+ },
84
+ // Polygon Networks
85
+ POLYGON_MAINNET: {
86
+ chainId: 137,
87
+ chainName: "Polygon Mainnet",
88
+ nativeCurrency: {
89
+ name: "MATIC",
90
+ symbol: "MATIC",
91
+ decimals: 18,
92
+ },
93
+ rpcUrls: ["https://polygon-rpc.com"],
94
+ blockExplorerUrls: ["https://polygonscan.com"],
95
+ },
96
+ POLYGON_AMOY: {
97
+ chainId: 80002,
98
+ chainName: "Polygon Amoy Testnet",
99
+ nativeCurrency: {
100
+ name: "MATIC",
101
+ symbol: "MATIC",
102
+ decimals: 18,
103
+ },
104
+ rpcUrls: ["https://rpc-amoy.polygon.technology"],
105
+ blockExplorerUrls: ["https://amoy.polygonscan.com"],
106
+ },
107
+ // Arbitrum Networks
108
+ ARBITRUM_ONE: {
109
+ chainId: 42161,
110
+ chainName: "Arbitrum One",
111
+ nativeCurrency: {
112
+ name: "Ethereum",
113
+ symbol: "ETH",
114
+ decimals: 18,
115
+ },
116
+ rpcUrls: ["https://arb1.arbitrum.io/rpc"],
117
+ blockExplorerUrls: ["https://arbiscan.io"],
118
+ },
119
+ ARBITRUM_SEPOLIA: {
120
+ chainId: 421614,
121
+ chainName: "Arbitrum Sepolia",
122
+ nativeCurrency: {
123
+ name: "Ethereum",
124
+ symbol: "ETH",
125
+ decimals: 18,
126
+ },
127
+ rpcUrls: ["https://sepolia-rollup.arbitrum.io/rpc"],
128
+ blockExplorerUrls: ["https://sepolia.arbiscan.io"],
129
+ },
130
+ // Optimism Networks
131
+ OPTIMISM_MAINNET: {
132
+ chainId: 10,
133
+ chainName: "Optimism Mainnet",
134
+ nativeCurrency: {
135
+ name: "Ethereum",
136
+ symbol: "ETH",
137
+ decimals: 18,
138
+ },
139
+ rpcUrls: ["https://mainnet.optimism.io"],
140
+ blockExplorerUrls: ["https://optimistic.etherscan.io"],
141
+ },
142
+ OPTIMISM_SEPOLIA: {
143
+ chainId: 11155420,
144
+ chainName: "Optimism Sepolia",
145
+ nativeCurrency: {
146
+ name: "Ethereum",
147
+ symbol: "ETH",
148
+ decimals: 18,
149
+ },
150
+ rpcUrls: ["https://sepolia.optimism.io"],
151
+ blockExplorerUrls: ["https://sepolia-optimism.etherscan.io"],
152
+ },
153
+ // Avalanche Networks
154
+ AVALANCHE_C_CHAIN: {
155
+ chainId: 43114,
156
+ chainName: "Avalanche C-Chain",
157
+ nativeCurrency: {
158
+ name: "Avalanche",
159
+ symbol: "AVAX",
160
+ decimals: 18,
161
+ },
162
+ rpcUrls: ["https://api.avax.network/ext/bc/C/rpc"],
163
+ blockExplorerUrls: ["https://snowtrace.io"],
164
+ },
165
+ AVALANCHE_FUJI: {
166
+ chainId: 43113,
167
+ chainName: "Avalanche Fuji Testnet",
168
+ nativeCurrency: {
169
+ name: "Avalanche",
170
+ symbol: "AVAX",
171
+ decimals: 18,
172
+ },
173
+ rpcUrls: ["https://api.avax-test.network/ext/bc/C/rpc"],
174
+ blockExplorerUrls: ["https://testnet.snowtrace.io"],
175
+ },
176
+ // BNB Chain Networks
177
+ BNB_SMART_CHAIN: {
178
+ chainId: 56,
179
+ chainName: "BNB Smart Chain",
180
+ nativeCurrency: {
181
+ name: "BNB",
182
+ symbol: "BNB",
183
+ decimals: 18,
184
+ },
185
+ rpcUrls: ["https://bsc-dataseed.binance.org"],
186
+ blockExplorerUrls: ["https://bscscan.com"],
187
+ },
188
+ BNB_TESTNET: {
189
+ chainId: 97,
190
+ chainName: "BNB Smart Chain Testnet",
191
+ nativeCurrency: {
192
+ name: "BNB",
193
+ symbol: "tBNB",
194
+ decimals: 18,
195
+ },
196
+ rpcUrls: ["https://data-seed-prebsc-1-s1.binance.org:8545"],
197
+ blockExplorerUrls: ["https://testnet.bscscan.com"],
198
+ },
199
+ };
200
+ /**
201
+ * Detect which network the contract is deployed on
202
+ * by checking contract code existence across all supported networks
203
+ */
204
+ export async function detectContractNetwork(contractAddress) {
205
+ console.log(`🔍 Scanning ${Object.keys(NETWORKS).length} networks for contract...`);
206
+ for (const [networkKey, network] of Object.entries(NETWORKS)) {
207
+ try {
208
+ const provider = new ethers.JsonRpcProvider(network.rpcUrls[0]);
209
+ const code = await provider.getCode(contractAddress);
210
+ if (code !== "0x") {
211
+ console.log(`✅ Contract found on ${network.chainName} (Chain ID: ${network.chainId})`);
212
+ return network;
213
+ }
214
+ }
215
+ catch (err) {
216
+ console.log(`⚠️ Could not check ${network.chainName}: ${err instanceof Error ? err.message : 'Unknown error'}`);
217
+ }
218
+ }
219
+ console.error(`❌ Contract not found on any supported network`);
220
+ console.log(`📋 Supported networks: ${Object.values(NETWORKS).map(n => n.chainName).join(', ')}`);
221
+ return null;
222
+ }
223
+ /**
224
+ * Get network configuration by chain ID
225
+ */
226
+ export function getNetworkByChainId(chainId) {
227
+ for (const network of Object.values(NETWORKS)) {
228
+ if (network.chainId === chainId) {
229
+ return network;
230
+ }
231
+ }
232
+ return null;
233
+ }
234
+ /**
235
+ * Check if a chain ID is supported
236
+ */
237
+ export function isNetworkSupported(chainId) {
238
+ return getNetworkByChainId(chainId) !== null;
239
+ }
240
+ /**
241
+ * Add a custom network to the supported networks list
242
+ * Useful for adding networks that aren't included by default
243
+ */
244
+ export function addCustomNetwork(key, network) {
245
+ if (NETWORKS[key]) {
246
+ console.warn(`⚠️ Network "${key}" already exists and will be overwritten`);
247
+ }
248
+ NETWORKS[key] = network;
249
+ console.log(`✅ Added custom network: ${network.chainName} (Chain ID: ${network.chainId})`);
250
+ }
251
+ /**
252
+ * Add multiple custom networks at once
253
+ */
254
+ export function addCustomNetworks(networks) {
255
+ Object.entries(networks).forEach(([key, network]) => {
256
+ addCustomNetwork(key, network);
257
+ });
258
+ }
259
+ /**
260
+ * Helper to create a network configuration
261
+ */
262
+ export function createNetworkConfig(params) {
263
+ return {
264
+ chainId: params.chainId,
265
+ chainName: params.chainName,
266
+ nativeCurrency: {
267
+ name: params.symbolName || params.symbol,
268
+ symbol: params.symbol,
269
+ decimals: 18,
270
+ },
271
+ rpcUrls: [params.rpcUrl],
272
+ blockExplorerUrls: params.explorerUrl ? [params.explorerUrl] : undefined,
273
+ };
274
+ }
275
+ /**
276
+ * Force switch to the required network
277
+ * Throws error if user rejects
278
+ */
279
+ export async function enforceNetwork(requiredChainId, networkConfig) {
280
+ if (!window.ethereum) {
281
+ throw new Error("No wallet detected");
282
+ }
283
+ try {
284
+ // Try to switch to the network
285
+ await window.ethereum.request({
286
+ method: "wallet_switchEthereumChain",
287
+ params: [{ chainId: ethers.toBeHex(requiredChainId) }],
288
+ });
289
+ console.log(`✅ Switched to ${networkConfig.chainName}`);
290
+ return true;
291
+ }
292
+ catch (switchError) {
293
+ // Network not added to wallet, try to add it
294
+ if (switchError.code === 4902) {
295
+ try {
296
+ await window.ethereum.request({
297
+ method: "wallet_addEthereumChain",
298
+ params: [
299
+ {
300
+ chainId: ethers.toBeHex(requiredChainId),
301
+ chainName: networkConfig.chainName,
302
+ nativeCurrency: networkConfig.nativeCurrency,
303
+ rpcUrls: networkConfig.rpcUrls,
304
+ blockExplorerUrls: networkConfig.blockExplorerUrls,
305
+ },
306
+ ],
307
+ });
308
+ console.log(`✅ Added and switched to ${networkConfig.chainName}`);
309
+ return true;
310
+ }
311
+ catch (addError) {
312
+ console.error("❌ Failed to add network:", addError);
313
+ throw new Error(`Failed to add ${networkConfig.chainName} to wallet`);
314
+ }
315
+ }
316
+ else if (switchError.code === 4001) {
317
+ // User rejected the request
318
+ throw new Error("Network switch rejected by user");
319
+ }
320
+ else {
321
+ console.error("❌ Network switch failed:", switchError);
322
+ throw new Error("Failed to switch network");
323
+ }
324
+ }
325
+ }
326
+ /**
327
+ * Monitor network changes and enforce the correct network
328
+ */
329
+ export function setupNetworkGuard(requiredChainId, networkConfig, onWrongNetwork) {
330
+ if (!window.ethereum) {
331
+ console.warn("No wallet detected for network guard");
332
+ return () => { };
333
+ }
334
+ const handleChainChanged = async (chainIdHex) => {
335
+ const currentChainId = parseInt(chainIdHex, 16);
336
+ if (currentChainId !== requiredChainId) {
337
+ console.warn(`⚠️ Wrong network detected (Chain ID: ${currentChainId}). Enforcing ${networkConfig.chainName}...`);
338
+ if (onWrongNetwork) {
339
+ onWrongNetwork();
340
+ }
341
+ try {
342
+ await enforceNetwork(requiredChainId, networkConfig);
343
+ }
344
+ catch (err) {
345
+ console.error("❌ Failed to enforce network:", err);
346
+ }
347
+ }
348
+ };
349
+ // Listen for network changes
350
+ window.ethereum.on("chainChanged", handleChainChanged);
351
+ // Cleanup function
352
+ return () => {
353
+ window.ethereum?.removeListener("chainChanged", handleChainChanged);
354
+ };
355
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cordystackx/cordy_minikit",
3
- "version": "1.3.2",
3
+ "version": "1.4.7",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org/"