@@ -7,7 +7,7 @@ import { nonces } from "../extensions/erc20/__generated__/IERC20Permit/read/nonc
77import { type Address , getAddress } from "../utils/address.js" ;
88import { type Hex , toHex } from "../utils/encoding/hex.js" ;
99import type { Account } from "../wallets/interfaces/wallet.js" ;
10- import { detectSupportedAuthorizationMethods } from "./common.js" ;
10+ import { getSupportedSignatureType } from "./common.js" ;
1111import { encodePayment } from "./encode.js" ;
1212import {
1313 networkToChainId ,
@@ -72,68 +72,71 @@ async function signPaymentHeader(
7272) : Promise < RequestedPaymentPayload > {
7373 const from = getAddress ( account . address ) ;
7474 const chainId = networkToChainId ( paymentRequirements . network ) ;
75- const { usePermit, useTransferWithAuthorization } =
76- await detectSupportedAuthorizationMethods ( {
77- client,
78- asset : paymentRequirements . asset ,
79- chainId : chainId ,
80- eip712Extras : paymentRequirements . extra as
81- | ERC20TokenAmount [ "asset" ] [ "eip712" ]
82- | undefined ,
83- } ) ;
75+ const supportedSignatureType = await getSupportedSignatureType ( {
76+ client,
77+ asset : paymentRequirements . asset ,
78+ chainId : chainId ,
79+ eip712Extras : paymentRequirements . extra as
80+ | ERC20TokenAmount [ "asset" ] [ "eip712" ]
81+ | undefined ,
82+ } ) ;
8483
85- if ( usePermit ) {
86- const nonce = await nonces ( {
87- contract : getContract ( {
88- address : paymentRequirements . asset ,
89- chain : getCachedChain ( chainId ) ,
90- client : client ,
91- } ) ,
92- owner : from ,
93- } ) ;
94- const unsignedPaymentHeader = preparePaymentHeader (
95- from ,
96- x402Version ,
97- paymentRequirements ,
98- toHex ( nonce , { size : 32 } ) , // permit nonce
99- ) ;
100- const { signature } = await signERC2612Permit (
101- account ,
102- unsignedPaymentHeader . payload . authorization ,
103- paymentRequirements ,
104- ) ;
105- return {
106- ...unsignedPaymentHeader ,
107- payload : {
108- ...unsignedPaymentHeader . payload ,
109- signature,
110- } ,
111- } ;
112- } else if ( useTransferWithAuthorization ) {
113- // default to transfer with authorization
114- const nonce = await createNonce ( ) ;
115- const unsignedPaymentHeader = preparePaymentHeader (
116- from ,
117- x402Version ,
118- paymentRequirements ,
119- nonce , // random nonce
120- ) ;
121- const { signature } = await signERC3009Authorization (
122- account ,
123- unsignedPaymentHeader . payload . authorization ,
124- paymentRequirements ,
125- ) ;
126- return {
127- ...unsignedPaymentHeader ,
128- payload : {
129- ...unsignedPaymentHeader . payload ,
130- signature,
131- } ,
132- } ;
84+ switch ( supportedSignatureType ) {
85+ case "Permit" : {
86+ const nonce = await nonces ( {
87+ contract : getContract ( {
88+ address : paymentRequirements . asset ,
89+ chain : getCachedChain ( chainId ) ,
90+ client : client ,
91+ } ) ,
92+ owner : from ,
93+ } ) ;
94+ const unsignedPaymentHeader = preparePaymentHeader (
95+ from ,
96+ x402Version ,
97+ paymentRequirements ,
98+ toHex ( nonce , { size : 32 } ) , // permit nonce
99+ ) ;
100+ const { signature } = await signERC2612Permit (
101+ account ,
102+ unsignedPaymentHeader . payload . authorization ,
103+ paymentRequirements ,
104+ ) ;
105+ return {
106+ ...unsignedPaymentHeader ,
107+ payload : {
108+ ...unsignedPaymentHeader . payload ,
109+ signature,
110+ } ,
111+ } ;
112+ }
113+ case "TransferWithAuthorization" : {
114+ // default to transfer with authorization
115+ const nonce = await createNonce ( ) ;
116+ const unsignedPaymentHeader = preparePaymentHeader (
117+ from ,
118+ x402Version ,
119+ paymentRequirements ,
120+ nonce , // random nonce
121+ ) ;
122+ const { signature } = await signERC3009Authorization (
123+ account ,
124+ unsignedPaymentHeader . payload . authorization ,
125+ paymentRequirements ,
126+ ) ;
127+ return {
128+ ...unsignedPaymentHeader ,
129+ payload : {
130+ ...unsignedPaymentHeader . payload ,
131+ signature,
132+ } ,
133+ } ;
134+ }
135+ default :
136+ throw new Error (
137+ `No supported payment authorization methods found on ${ paymentRequirements . asset } on chain ${ paymentRequirements . network } ` ,
138+ ) ;
133139 }
134- throw new Error (
135- `No supported payment authorization methods found on ${ paymentRequirements . asset } on chain ${ paymentRequirements . network } ` ,
136- ) ;
137140}
138141
139142/**
0 commit comments