facinet 2.0.4 → 2.1.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/browser.js CHANGED
@@ -22567,8 +22567,9 @@ var CHAINS = {
22567
22567
  };
22568
22568
  var Facinet = class _Facinet {
22569
22569
  constructor(config = {}) {
22570
+ const apiUrl = (config.apiUrl || "https://x402-avalanche-chi.vercel.app").replace(/\/$/, "");
22570
22571
  this.config = {
22571
- apiUrl: config.apiUrl || "https://x402-avalanche-chi.vercel.app",
22572
+ apiUrl,
22572
22573
  privateKey: config.privateKey || "",
22573
22574
  network: config.network || "avalanche",
22574
22575
  rpcUrl: config.rpcUrl || ""
@@ -22657,28 +22658,45 @@ var Facinet = class _Facinet {
22657
22658
  if (this.wallet) {
22658
22659
  signature = await this.wallet.signTypedData(domain, types, value);
22659
22660
  } else if (typeof window !== "undefined" && window.ethereum) {
22660
- const messageForBrowser = {
22661
- from: payerAddress,
22662
- to: params.recipient,
22663
- value: "0x" + amount.toString(16),
22664
- // BigInt to hex string
22665
- validAfter,
22666
- validBefore,
22667
- nonce
22668
- };
22669
- const jsonMessage = JSON.stringify({
22661
+ const typedData = {
22670
22662
  domain,
22671
22663
  types,
22672
22664
  primaryType: "TransferWithAuthorization",
22673
- message: messageForBrowser
22674
- });
22665
+ message: {
22666
+ from: payerAddress,
22667
+ to: params.recipient,
22668
+ value: amount.toString(),
22669
+ // BigInt to decimal string
22670
+ validAfter: validAfter.toString(),
22671
+ validBefore: validBefore.toString(),
22672
+ nonce
22673
+ }
22674
+ };
22675
+ const jsonMessage = JSON.stringify(
22676
+ typedData,
22677
+ (_key, value2) => typeof value2 === "bigint" ? value2.toString() : value2
22678
+ );
22675
22679
  signature = await window.ethereum.request({
22676
22680
  method: "eth_signTypedData_v4",
22677
22681
  params: [payerAddress, jsonMessage]
22678
22682
  });
22683
+ console.log("\u{1F510} Browser signature created:", {
22684
+ signature,
22685
+ signatureLength: signature.length,
22686
+ payerAddress
22687
+ });
22679
22688
  } else {
22680
22689
  throw new Error("No signing method available");
22681
22690
  }
22691
+ console.log("\u{1F4DD} Final authorization:", {
22692
+ from: payerAddress,
22693
+ to: params.recipient,
22694
+ value: amount.toString(),
22695
+ validAfter: validAfter.toString(),
22696
+ validBefore: validBefore.toString(),
22697
+ nonce,
22698
+ signature: signature?.slice(0, 20) + "..."
22699
+ });
22682
22700
  const paymentPayload = {
22683
22701
  signature,
22684
22702
  authorization: {
@@ -22690,31 +22708,39 @@ var Facinet = class _Facinet {
22690
22708
  nonce
22691
22709
  }
22692
22710
  };
22693
- const response = await axios_default.post(
22694
- `${this.config.apiUrl}/api/x402/settle-custom`,
22695
- {
22696
- facilitatorId: facilitator.id,
22697
- paymentPayload
22711
+ try {
22712
+ const response = await axios_default.post(
22713
+ `${this.config.apiUrl}/api/x402/settle-custom`,
22714
+ {
22715
+ facilitatorId: facilitator.id,
22716
+ paymentPayload
22717
+ }
22718
+ );
22719
+ if (!response.data.success) {
22720
+ throw new Error(response.data.error || response.data.message || "Payment failed");
22698
22721
  }
22699
- );
22700
- if (!response.data.success) {
22701
- throw new Error("Payment failed: " + (response.data.error || "Unknown error"));
22702
- }
22703
- return {
22704
- success: true,
22705
- txHash: response.data.txHash,
22706
- facilitator: {
22707
- id: facilitator.id,
22708
- name: facilitator.name,
22709
- wallet: facilitator.facilitatorWallet
22710
- },
22711
- payment: {
22712
- from: payerAddress,
22713
- to: params.recipient,
22714
- amount: params.amount,
22715
- network: this.config.network
22722
+ return {
22723
+ success: true,
22724
+ txHash: response.data.txHash,
22725
+ facilitator: {
22726
+ id: facilitator.id,
22727
+ name: facilitator.name,
22728
+ wallet: facilitator.facilitatorWallet
22729
+ },
22730
+ payment: {
22731
+ from: payerAddress,
22732
+ to: params.recipient,
22733
+ amount: params.amount,
22734
+ network: this.config.network
22735
+ }
22736
+ };
22737
+ } catch (error) {
22738
+ if (error.response?.data) {
22739
+ const backendError = error.response.data.message || error.response.data.error;
22740
+ throw new Error(`Payment failed: ${backendError}`);
22716
22741
  }
22717
- };
22742
+ throw error;
22743
+ }
22718
22744
  }
22719
22745
  /**
22720
22746
  * Get all active facilitators