coinley-checkout 0.3.6 → 0.3.8

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.
@@ -95,7 +95,6 @@ const TOKEN_CONFIG = {
95
95
  },
96
96
  [NETWORK_TYPES.ALGORAND]: {
97
97
  address: "312769",
98
- // USDT ASA ID on Algorand
99
98
  decimals: 6
100
99
  }
101
100
  },
@@ -114,7 +113,6 @@ const TOKEN_CONFIG = {
114
113
  },
115
114
  [NETWORK_TYPES.ALGORAND]: {
116
115
  address: "31566704",
117
- // USDC ASA ID on Algorand
118
116
  decimals: 6
119
117
  }
120
118
  },
@@ -165,34 +163,14 @@ const TOKEN_CONFIG = {
165
163
  }
166
164
  }
167
165
  };
168
- const getWeb3Instance = () => __async(void 0, null, function* () {
169
- try {
170
- if (typeof window === "undefined" || !window.ethereum) {
171
- throw new Error("No ethereum provider found");
172
- }
173
- let Web3Class = null;
174
- try {
175
- const Web3Module = yield import("web3");
176
- Web3Class = Web3Module.default || Web3Module.Web3 || Web3Module;
177
- console.log("Web3 imported via dynamic import:", Web3Class);
178
- } catch (importError) {
179
- console.log("Dynamic import failed, trying global Web3:", importError);
180
- if (typeof window !== "undefined" && window.Web3) {
181
- Web3Class = window.Web3;
182
- console.log("Using global Web3:", Web3Class);
183
- }
184
- }
185
- if (!Web3Class) {
186
- throw new Error("Web3 not available. Please ensure web3 is installed or MetaMask is running.");
187
- }
188
- const web3Instance = new Web3Class(window.ethereum);
189
- console.log("Web3 instance created successfully:", web3Instance);
190
- return web3Instance;
191
- } catch (error) {
192
- console.error("Failed to load Web3:", error);
193
- throw new Error(`Web3 initialization failed: ${error.message}. Please ensure MetaMask is installed and running.`);
194
- }
195
- });
166
+ const toHex = (num) => {
167
+ return "0x" + Math.floor(parseFloat(num) * Math.pow(10, 18)).toString(16);
168
+ };
169
+ const calculateTokenAmount = (amount, decimals) => {
170
+ const multiplier = Math.pow(10, decimals || 18);
171
+ const amountInSmallestUnit = Math.floor(parseFloat(amount) * multiplier);
172
+ return "0x" + amountInSmallestUnit.toString(16);
173
+ };
196
174
  const detectWallets = () => {
197
175
  const wallets = {
198
176
  [WALLET_TYPES.METAMASK]: false,
@@ -204,10 +182,62 @@ const detectWallets = () => {
204
182
  return wallets;
205
183
  }
206
184
  try {
207
- wallets[WALLET_TYPES.METAMASK] = !!(window.ethereum && window.ethereum.isMetaMask);
208
- wallets[WALLET_TYPES.TRONLINK] = !!window.tronWeb;
209
- wallets[WALLET_TYPES.TRUST_WALLET] = !!(window.ethereum && window.ethereum.isTrust);
210
- wallets[WALLET_TYPES.LUTE] = !!(window.algorand && window.algorand.isLute);
185
+ console.log("=== WALLET DETECTION DEBUG ===");
186
+ console.log("window.ethereum:", window.ethereum);
187
+ console.log("window.tronWeb:", window.tronWeb);
188
+ console.log("window.tronLink:", window.tronLink);
189
+ console.log("window.algorand:", window.algorand);
190
+ if (window.ethereum) {
191
+ if (window.ethereum.isMetaMask) {
192
+ wallets[WALLET_TYPES.METAMASK] = true;
193
+ console.log("✅ MetaMask detected via isMetaMask");
194
+ } else if (window.ethereum.providers) {
195
+ const metamaskProvider = window.ethereum.providers.find((p2) => p2.isMetaMask);
196
+ if (metamaskProvider) {
197
+ wallets[WALLET_TYPES.METAMASK] = true;
198
+ console.log("✅ MetaMask detected via providers array");
199
+ }
200
+ }
201
+ }
202
+ if (window.ethereum) {
203
+ if (window.ethereum.isTrust) {
204
+ wallets[WALLET_TYPES.TRUST_WALLET] = true;
205
+ console.log("✅ Trust Wallet detected via isTrust");
206
+ } else if (window.ethereum.isTrustWallet) {
207
+ wallets[WALLET_TYPES.TRUST_WALLET] = true;
208
+ console.log("✅ Trust Wallet detected via isTrustWallet");
209
+ } else if (window.trustwallet) {
210
+ wallets[WALLET_TYPES.TRUST_WALLET] = true;
211
+ console.log("✅ Trust Wallet detected via window.trustwallet");
212
+ } else if (window.ethereum.providers) {
213
+ const trustProvider = window.ethereum.providers.find((p2) => p2.isTrust || p2.isTrustWallet);
214
+ if (trustProvider) {
215
+ wallets[WALLET_TYPES.TRUST_WALLET] = true;
216
+ console.log("✅ Trust Wallet detected via providers array");
217
+ }
218
+ }
219
+ }
220
+ if (window.tronWeb) {
221
+ wallets[WALLET_TYPES.TRONLINK] = true;
222
+ console.log("✅ TronLink detected via tronWeb");
223
+ } else if (window.tronLink) {
224
+ wallets[WALLET_TYPES.TRONLINK] = true;
225
+ console.log("✅ TronLink detected via tronLink");
226
+ } else if (window.tron) {
227
+ wallets[WALLET_TYPES.TRONLINK] = true;
228
+ console.log("✅ TronLink detected via tron");
229
+ }
230
+ if (window.algorand) {
231
+ if (window.algorand.isLute) {
232
+ wallets[WALLET_TYPES.LUTE] = true;
233
+ console.log("✅ Lute Wallet detected via algorand.isLute");
234
+ } else {
235
+ wallets[WALLET_TYPES.LUTE] = true;
236
+ console.log("✅ Algorand wallet detected (assuming Lute)");
237
+ }
238
+ }
239
+ console.log("Final wallet detection results:", wallets);
240
+ console.log("=== END WALLET DETECTION DEBUG ===");
211
241
  } catch (error) {
212
242
  console.warn("Error detecting wallets:", error);
213
243
  }
@@ -240,9 +270,6 @@ const connectMetaMask = (network) => __async(void 0, null, function* () {
240
270
  if (typeof window === "undefined" || !window.ethereum) {
241
271
  throw new Error("MetaMask is not installed. Please install MetaMask extension.");
242
272
  }
243
- if (!window.ethereum.isMetaMask) {
244
- console.warn("MetaMask not detected as primary provider");
245
- }
246
273
  try {
247
274
  console.log("Requesting accounts from MetaMask...");
248
275
  const accounts = yield window.ethereum.request({
@@ -250,7 +277,7 @@ const connectMetaMask = (network) => __async(void 0, null, function* () {
250
277
  });
251
278
  console.log("Accounts received:", accounts);
252
279
  if (!accounts || accounts.length === 0) {
253
- throw new Error("No accounts found. Please unlock MetaMask and ensure you have at least one account.");
280
+ throw new Error("No accounts found. Please unlock MetaMask.");
254
281
  }
255
282
  const networkConfig = NETWORK_CONFIG[network];
256
283
  if (networkConfig && networkConfig.chainId) {
@@ -267,10 +294,10 @@ const connectMetaMask = (network) => __async(void 0, null, function* () {
267
294
  } catch (error) {
268
295
  console.error("MetaMask connection error:", error);
269
296
  if (error.code === 4001) {
270
- throw new Error("Connection rejected by user. Please accept the connection request in MetaMask.");
297
+ throw new Error("Connection rejected by user.");
271
298
  }
272
299
  if (error.code === -32002) {
273
- throw new Error("Connection request already pending. Please check MetaMask.");
300
+ throw new Error("Connection request pending. Please check MetaMask.");
274
301
  }
275
302
  throw new Error(`Failed to connect MetaMask: ${error.message}`);
276
303
  }
@@ -278,7 +305,7 @@ const connectMetaMask = (network) => __async(void 0, null, function* () {
278
305
  const connectTronLink = () => __async(void 0, null, function* () {
279
306
  var _a;
280
307
  if (typeof window === "undefined" || !window.tronWeb) {
281
- throw new Error("TronLink is not installed. Please install TronLink extension.");
308
+ throw new Error("TronLink is not installed.");
282
309
  }
283
310
  let attempts = 0;
284
311
  const maxAttempts = 10;
@@ -287,11 +314,11 @@ const connectTronLink = () => __async(void 0, null, function* () {
287
314
  attempts++;
288
315
  }
289
316
  if (!window.tronWeb.ready) {
290
- throw new Error("TronLink is not ready. Please unlock your TronLink wallet and try again.");
317
+ throw new Error("TronLink is not ready. Please unlock your wallet.");
291
318
  }
292
319
  const address = (_a = window.tronWeb.defaultAddress) == null ? void 0 : _a.base58;
293
320
  if (!address) {
294
- throw new Error("No account found in TronLink. Please make sure you have an account set up.");
321
+ throw new Error("No account found in TronLink.");
295
322
  }
296
323
  return {
297
324
  address,
@@ -301,7 +328,7 @@ const connectTronLink = () => __async(void 0, null, function* () {
301
328
  });
302
329
  const connectTrustWallet = (network) => __async(void 0, null, function* () {
303
330
  if (typeof window === "undefined" || !window.ethereum || !window.ethereum.isTrust) {
304
- throw new Error("Trust Wallet is not installed. Please install Trust Wallet extension.");
331
+ throw new Error("Trust Wallet is not installed.");
305
332
  }
306
333
  try {
307
334
  const accounts = yield window.ethereum.request({
@@ -328,7 +355,7 @@ const connectTrustWallet = (network) => __async(void 0, null, function* () {
328
355
  });
329
356
  const connectLute = () => __async(void 0, null, function* () {
330
357
  if (typeof window === "undefined" || !window.algorand || !window.algorand.isLute) {
331
- throw new Error("Lute wallet is not installed. Please install Lute wallet extension.");
358
+ throw new Error("Lute wallet is not installed.");
332
359
  }
333
360
  try {
334
361
  const accounts = yield window.algorand.connect();
@@ -358,7 +385,6 @@ const switchEVMNetwork = (networkConfig) => __async(void 0, null, function* () {
358
385
  } catch (switchError) {
359
386
  console.error("Network switch error:", switchError);
360
387
  if (switchError.code === 4902) {
361
- console.log("Network not found, attempting to add:", networkConfig.chainName);
362
388
  try {
363
389
  yield window.ethereum.request({
364
390
  method: "wallet_addEthereumChain",
@@ -366,13 +392,12 @@ const switchEVMNetwork = (networkConfig) => __async(void 0, null, function* () {
366
392
  });
367
393
  console.log("Network added successfully");
368
394
  } catch (addError) {
369
- console.error("Failed to add network:", addError);
370
- throw new Error(`Failed to add ${networkConfig.chainName} to wallet. Please add it manually.`);
395
+ throw new Error(`Failed to add ${networkConfig.chainName} to wallet.`);
371
396
  }
372
397
  } else if (switchError.code === 4001) {
373
- throw new Error("User rejected network switch request. Please switch network manually in MetaMask.");
398
+ throw new Error("User rejected network switch request.");
374
399
  } else {
375
- throw new Error(`Failed to switch to ${networkConfig.chainName}. Please switch manually in MetaMask.`);
400
+ throw new Error(`Failed to switch to ${networkConfig.chainName}.`);
376
401
  }
377
402
  }
378
403
  });
@@ -383,7 +408,7 @@ const sendTransaction = (walletConnection, transactionData) => __async(void 0, n
383
408
  switch (walletType) {
384
409
  case WALLET_TYPES.METAMASK:
385
410
  case WALLET_TYPES.TRUST_WALLET:
386
- return yield sendEVMTransaction(address, to, amount, tokenAddress, tokenDecimals);
411
+ return yield sendEVMTransactionNative(address, to, amount, tokenAddress, tokenDecimals);
387
412
  case WALLET_TYPES.TRONLINK:
388
413
  return yield sendTronTransaction(to, amount, tokenAddress, tokenDecimals);
389
414
  case WALLET_TYPES.LUTE:
@@ -392,53 +417,49 @@ const sendTransaction = (walletConnection, transactionData) => __async(void 0, n
392
417
  throw new Error(`Unsupported wallet type: ${walletType}`);
393
418
  }
394
419
  });
395
- const sendEVMTransaction = (from, to, amount, tokenAddress, tokenDecimals) => __async(void 0, null, function* () {
396
- console.log("sendEVMTransaction called with:", { from, to, amount, tokenAddress, tokenDecimals });
420
+ const sendEVMTransactionNative = (from, to, amount, tokenAddress, tokenDecimals) => __async(void 0, null, function* () {
421
+ console.log("sendEVMTransactionNative called with:", { from, to, amount, tokenAddress, tokenDecimals });
397
422
  if (typeof window === "undefined" || !window.ethereum) {
398
423
  throw new Error("Ethereum provider not found");
399
424
  }
400
425
  try {
401
- const web3 = yield getWeb3Instance();
402
- console.log("Web3 instance obtained for transaction");
403
- const decimals = tokenDecimals || 18;
404
- const amountBN = web3.utils.toBN(
405
- Math.floor(parseFloat(amount) * Math.pow(10, decimals)).toString()
406
- );
407
- console.log("Amount calculated:", {
408
- originalAmount: amount,
409
- decimals,
410
- calculatedAmount: amountBN.toString()
411
- });
412
426
  if (tokenAddress && tokenAddress !== "native") {
413
427
  console.log("Preparing ERC20 token transfer...");
414
- const tokenABI = [
415
- {
416
- constant: false,
417
- inputs: [
418
- { name: "_to", type: "address" },
419
- { name: "_value", type: "uint256" }
420
- ],
421
- name: "transfer",
422
- outputs: [{ name: "", type: "bool" }],
423
- payable: false,
424
- stateMutability: "nonpayable",
425
- type: "function"
426
- }
427
- ];
428
- const contract = new web3.eth.Contract(tokenABI, tokenAddress);
429
- console.log("Contract created, sending transaction...");
430
- const receipt = yield contract.methods.transfer(to, amountBN.toString()).send({ from });
431
- console.log("ERC20 transaction successful:", receipt.transactionHash);
432
- return receipt.transactionHash;
428
+ const decimals = tokenDecimals || 18;
429
+ const amountHex = calculateTokenAmount(amount, decimals);
430
+ const transferMethodId = "0xa9059cbb";
431
+ const paddedToAddress = to.replace("0x", "").padStart(64, "0");
432
+ const paddedAmount = amountHex.replace("0x", "").padStart(64, "0");
433
+ const data = transferMethodId + paddedToAddress + paddedAmount;
434
+ console.log("ERC20 transaction data:", { amountHex, data });
435
+ const txHash = yield window.ethereum.request({
436
+ method: "eth_sendTransaction",
437
+ params: [{
438
+ from,
439
+ to: tokenAddress,
440
+ data,
441
+ gas: "0x15F90"
442
+ // 90000 gas limit
443
+ }]
444
+ });
445
+ console.log("ERC20 transaction successful:", txHash);
446
+ return txHash;
433
447
  } else {
434
448
  console.log("Preparing native token transfer...");
435
- const receipt = yield web3.eth.sendTransaction({
436
- from,
437
- to,
438
- value: amountBN.toString()
449
+ const amountHex = toHex(amount);
450
+ console.log("Native transaction amount:", { amount, amountHex });
451
+ const txHash = yield window.ethereum.request({
452
+ method: "eth_sendTransaction",
453
+ params: [{
454
+ from,
455
+ to,
456
+ value: amountHex,
457
+ gas: "0x5208"
458
+ // 21000 gas limit for simple transfer
459
+ }]
439
460
  });
440
- console.log("Native transaction successful:", receipt.transactionHash);
441
- return receipt.transactionHash;
461
+ console.log("Native transaction successful:", txHash);
462
+ return txHash;
442
463
  }
443
464
  } catch (error) {
444
465
  console.error("EVM transaction error:", error);
@@ -448,9 +469,6 @@ const sendEVMTransaction = (from, to, amount, tokenAddress, tokenDecimals) => __
448
469
  if (error.message && error.message.includes("insufficient funds")) {
449
470
  throw new Error("Insufficient funds to complete the transaction");
450
471
  }
451
- if (error.message && error.message.includes("gas")) {
452
- throw new Error("Transaction failed due to gas estimation issues. Please try again.");
453
- }
454
472
  throw new Error(`Transaction failed: ${error.message || "Unknown error"}`);
455
473
  }
456
474
  });