Skip to content

Commit da2b065

Browse files
cleanup
1 parent a1e06b2 commit da2b065

File tree

3 files changed

+80
-85
lines changed

3 files changed

+80
-85
lines changed

packages/thirdweb/src/x402/common.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type ERC20TokenAmount,
2020
type PaymentArgs,
2121
type PaymentRequiredResult,
22+
type SupportedSignatureType,
2223
x402Version,
2324
} from "./types.js";
2425

@@ -247,31 +248,16 @@ async function getDefaultAsset(
247248
return assetConfig;
248249
}
249250

250-
export type SupportedAuthorizationMethods = {
251-
usePermit: boolean;
252-
useTransferWithAuthorization: boolean;
253-
};
254-
255-
export async function detectSupportedAuthorizationMethods(args: {
251+
export async function getSupportedSignatureType(args: {
256252
client: ThirdwebClient;
257253
asset: string;
258254
chainId: number;
259255
eip712Extras: ERC20TokenAmount["asset"]["eip712"] | undefined;
260-
}): Promise<SupportedAuthorizationMethods> {
256+
}): Promise<SupportedSignatureType | undefined> {
261257
const primaryType = args.eip712Extras?.primaryType;
262258

263-
if (primaryType === "Permit") {
264-
return {
265-
usePermit: true,
266-
useTransferWithAuthorization: false,
267-
};
268-
}
269-
270-
if (primaryType === "TransferWithAuthorization") {
271-
return {
272-
usePermit: false,
273-
useTransferWithAuthorization: true,
274-
};
259+
if (primaryType === "Permit" || primaryType === "TransferWithAuthorization") {
260+
return primaryType;
275261
}
276262

277263
// not specified, so we need to detect it
@@ -292,8 +278,12 @@ export async function detectSupportedAuthorizationMethods(args: {
292278
const hasTransferWithAuthorization =
293279
isTransferWithAuthorizationSupported(selectors);
294280

295-
return {
296-
usePermit: hasPermit && !hasTransferWithAuthorization, // only use permit if transfer with authorization is unsupported
297-
useTransferWithAuthorization: hasTransferWithAuthorization,
298-
};
281+
// prefer transferWithAuthorization over permit
282+
if (hasTransferWithAuthorization) {
283+
return "TransferWithAuthorization";
284+
}
285+
if (hasPermit) {
286+
return "Permit";
287+
}
288+
return undefined;
299289
}

packages/thirdweb/src/x402/sign.ts

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { nonces } from "../extensions/erc20/__generated__/IERC20Permit/read/nonc
77
import { type Address, getAddress } from "../utils/address.js";
88
import { type Hex, toHex } from "../utils/encoding/hex.js";
99
import type { Account } from "../wallets/interfaces/wallet.js";
10-
import { detectSupportedAuthorizationMethods } from "./common.js";
10+
import { getSupportedSignatureType } from "./common.js";
1111
import { encodePayment } from "./encode.js";
1212
import {
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
/**

packages/thirdweb/src/x402/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ export type VerifyPaymentResult = Prettify<
8686
| PaymentRequiredResult
8787
>;
8888

89+
export type SupportedSignatureType = "TransferWithAuthorization" | "Permit";
90+
8991
export type ERC20TokenAmount = {
9092
amount: string;
9193
asset: {
@@ -94,7 +96,7 @@ export type ERC20TokenAmount = {
9496
eip712: {
9597
name: string;
9698
version: string;
97-
primaryType: "TransferWithAuthorization" | "Permit";
99+
primaryType: SupportedSignatureType;
98100
};
99101
};
100102
};

0 commit comments

Comments
 (0)