1- import {
2- type ERC20TokenAmount ,
3- type Money ,
4- moneySchema ,
5- type Network ,
6- SupportedEVMNetworks ,
7- } from "x402/types" ;
1+ import { type ERC20TokenAmount , type Money , moneySchema } from "x402/types" ;
82import { getAddress } from "../utils/address.js" ;
93import { decodePayment } from "./encode.js" ;
104import type { facilitator as facilitatorType } from "./facilitator.js" ;
115import {
12- type FacilitatorNetwork ,
136 networkToChainId ,
147 type RequestedPaymentPayload ,
158 type RequestedPaymentRequirements ,
@@ -54,9 +47,28 @@ export async function decodePaymentRequest(
5447 errorMessages,
5548 discoverable,
5649 } = routeConfig ;
50+
51+ let chainId : number ;
52+ try {
53+ chainId = networkToChainId ( network ) ;
54+ } catch ( error ) {
55+ return {
56+ status : 402 ,
57+ responseHeaders : { "Content-Type" : "application/json" } ,
58+ responseBody : {
59+ x402Version,
60+ error :
61+ error instanceof Error
62+ ? error . message
63+ : `Invalid network: ${ network } ` ,
64+ accepts : [ ] ,
65+ } ,
66+ } ;
67+ }
68+
5769 const atomicAmountForAsset = await processPriceToAtomicAmount (
5870 price ,
59- network ,
71+ chainId ,
6072 facilitator ,
6173 ) ;
6274 if ( "error" in atomicAmountForAsset ) {
@@ -74,45 +86,28 @@ export async function decodePaymentRequest(
7486
7587 const paymentRequirements : RequestedPaymentRequirements [ ] = [ ] ;
7688
77- if (
78- SupportedEVMNetworks . includes ( network as Network ) ||
79- network . startsWith ( "eip155:" )
80- ) {
81- paymentRequirements . push ( {
82- scheme : "exact" ,
83- network,
84- maxAmountRequired,
85- resource : resourceUrl ,
86- description : description ?? "" ,
87- mimeType : mimeType ?? "application/json" ,
88- payTo : getAddress ( payTo ) ,
89- maxTimeoutSeconds : maxTimeoutSeconds ?? 300 ,
90- asset : getAddress ( asset . address ) ,
91- // TODO: Rename outputSchema to requestStructure
92- outputSchema : {
93- input : {
94- type : "http" ,
95- method,
96- discoverable : discoverable ?? true ,
97- ...inputSchema ,
98- } ,
99- output : outputSchema ,
100- } ,
101- extra : ( asset as ERC20TokenAmount [ "asset" ] ) . eip712 ,
102- } ) ;
103- } else {
104- return {
105- status : 402 ,
106- responseHeaders : {
107- "Content-Type" : "application/json" ,
108- } ,
109- responseBody : {
110- x402Version,
111- error : `Unsupported network: ${ network } ` ,
112- accepts : paymentRequirements ,
89+ paymentRequirements . push ( {
90+ scheme : "exact" ,
91+ network : `eip155:${ chainId } ` ,
92+ maxAmountRequired,
93+ resource : resourceUrl ,
94+ description : description ?? "" ,
95+ mimeType : mimeType ?? "application/json" ,
96+ payTo : getAddress ( payTo ) ,
97+ maxTimeoutSeconds : maxTimeoutSeconds ?? 300 ,
98+ asset : getAddress ( asset . address ) ,
99+ // TODO: Rename outputSchema to requestStructure
100+ outputSchema : {
101+ input : {
102+ type : "http" ,
103+ method,
104+ discoverable : discoverable ?? true ,
105+ ...inputSchema ,
113106 } ,
114- } ;
115- }
107+ output : outputSchema ,
108+ } ,
109+ extra : ( asset as ERC20TokenAmount [ "asset" ] ) . eip712 ,
110+ } ) ;
116111
117112 // Check for payment header
118113 if ( ! paymentData ) {
@@ -188,7 +183,7 @@ export async function decodePaymentRequest(
188183 */
189184async function processPriceToAtomicAmount (
190185 price : Money | ERC20TokenAmount ,
191- network : FacilitatorNetwork ,
186+ chainId : number ,
192187 facilitator : ReturnType < typeof facilitatorType > ,
193188) : Promise <
194189 | { maxAmountRequired : string ; asset : ERC20TokenAmount [ "asset" ] }
@@ -207,10 +202,10 @@ async function processPriceToAtomicAmount(
207202 } ;
208203 }
209204 const parsedUsdAmount = parsedAmount . data ;
210- const defaultAsset = await getDefaultAsset ( network , facilitator ) ;
205+ const defaultAsset = await getDefaultAsset ( chainId , facilitator ) ;
211206 if ( ! defaultAsset ) {
212207 return {
213- error : `Unable to get default asset on ${ network } . Please specify an asset in the payment requirements.` ,
208+ error : `Unable to get default asset on chain ${ chainId } . Please specify an asset in the payment requirements.` ,
214209 } ;
215210 }
216211 asset = defaultAsset ;
@@ -228,11 +223,10 @@ async function processPriceToAtomicAmount(
228223}
229224
230225async function getDefaultAsset (
231- network : FacilitatorNetwork ,
226+ chainId : number ,
232227 facilitator : ReturnType < typeof facilitatorType > ,
233228) : Promise < ERC20TokenAmount [ "asset" ] | undefined > {
234229 const supportedAssets = await facilitator . supported ( ) ;
235- const chainId = networkToChainId ( network ) ;
236230 const matchingAsset = supportedAssets . kinds . find (
237231 ( supported ) => supported . network === `eip155:${ chainId } ` ,
238232 ) ;
0 commit comments