diff --git a/.changeset/all-dolls-rest.md b/.changeset/all-dolls-rest.md new file mode 100644 index 00000000000..6e8551190f9 --- /dev/null +++ b/.changeset/all-dolls-rest.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Enhance compatiblity with third party x402 libs diff --git a/packages/thirdweb/src/x402/common.ts b/packages/thirdweb/src/x402/common.ts index bf4a4721f41..00b3adc3906 100644 --- a/packages/thirdweb/src/x402/common.ts +++ b/packages/thirdweb/src/x402/common.ts @@ -1,6 +1,6 @@ import type { Abi } from "abitype"; import { toFunctionSelector } from "viem/utils"; -import { type Money, moneySchema } from "x402/types"; +import { ChainIdToNetwork, type Money, moneySchema } from "x402/types"; import { getCachedChain } from "../chains/utils.js"; import type { ThirdwebClient } from "../client/client.js"; import { resolveContractAbi } from "../contract/actions/resolve-abi.js"; @@ -98,9 +98,10 @@ export async function decodePaymentRequest( const paymentRequirements: RequestedPaymentRequirements[] = []; + const mappedNetwork = ChainIdToNetwork[chainId]; paymentRequirements.push({ scheme: "exact", - network: `eip155:${chainId}`, + network: mappedNetwork ? mappedNetwork : `eip155:${chainId}`, maxAmountRequired, resource: resourceUrl, description: description ?? "", @@ -263,7 +264,7 @@ async function getDefaultAsset( ): Promise { const supportedAssets = await facilitator.supported(); const matchingAsset = supportedAssets.kinds.find( - (supported) => supported.network === `eip155:${chainId}`, + (supported) => networkToChainId(supported.network) === chainId, ); const assetConfig = matchingAsset?.extra?.defaultAsset as DefaultAsset; return assetConfig; diff --git a/packages/thirdweb/src/x402/fetchWithPayment.ts b/packages/thirdweb/src/x402/fetchWithPayment.ts index 1cb7abc4561..ffad27c61a2 100644 --- a/packages/thirdweb/src/x402/fetchWithPayment.ts +++ b/packages/thirdweb/src/x402/fetchWithPayment.ts @@ -1,4 +1,3 @@ -import { ChainIdToNetwork } from "x402/types"; import { getCachedChain } from "../chains/utils.js"; import type { ThirdwebClient } from "../client/client.js"; import type { Wallet } from "../wallets/interfaces/wallet.js"; @@ -149,10 +148,9 @@ function defaultPaymentRequirementsSelector( `No valid payment requirements found in server 402 response. ${error}`, ); } - const currentWalletNetwork = ChainIdToNetwork[chainId] || `eip155:${chainId}`; // find the payment requirements matching the connected wallet chain const matchingPaymentRequirements = paymentRequirements.find( - (x) => x.network === currentWalletNetwork && x.scheme === scheme, + (x) => networkToChainId(x.network) === chainId && x.scheme === scheme, ); if (matchingPaymentRequirements) {