Skip to content

[SDK] Optimize 4337 signature performance #7693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thirty-eels-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Optimize 4337 signature performance
16 changes: 8 additions & 8 deletions packages/thirdweb/src/pay/buyWithCrypto/getQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,14 @@ export async function getBuyWithCryptoQuote(
Number(
Value.format(quote.originAmount, firstStep.originToken.decimals),
) *
(firstStep.originToken.prices["USD"] || 0) *
(firstStep.originToken.prices.USD || 0) *
100,
amountWei: quote.originAmount.toString(),
token: {
chainId: firstStep.originToken.chainId,
decimals: firstStep.originToken.decimals,
name: firstStep.originToken.name,
priceUSDCents: (firstStep.originToken.prices["USD"] || 0) * 100,
priceUSDCents: (firstStep.originToken.prices.USD || 0) * 100,
symbol: firstStep.originToken.symbol,
tokenAddress: firstStep.originToken.address,
},
Expand All @@ -323,7 +323,7 @@ export async function getBuyWithCryptoQuote(
chainId: firstStep.originToken.chainId,
decimals: firstStep.originToken.decimals,
name: firstStep.originToken.name,
priceUSDCents: (firstStep.originToken.prices["USD"] || 0) * 100,
priceUSDCents: (firstStep.originToken.prices.USD || 0) * 100,
symbol: firstStep.originToken.symbol,
tokenAddress: firstStep.originToken.address,
},
Expand All @@ -337,7 +337,7 @@ export async function getBuyWithCryptoQuote(
Number(
Value.format(quote.originAmount, firstStep.originToken.decimals),
) *
(firstStep.originToken.prices["USD"] || 0) *
(firstStep.originToken.prices.USD || 0) *
100,
gasCostUSDCents: 0,
slippageBPS: 0,
Expand All @@ -348,7 +348,7 @@ export async function getBuyWithCryptoQuote(
firstStep.destinationToken.decimals,
),
) *
(firstStep.destinationToken.prices["USD"] || 0) *
(firstStep.destinationToken.prices.USD || 0) *
100,
toAmountUSDCents:
Number(
Expand All @@ -357,7 +357,7 @@ export async function getBuyWithCryptoQuote(
firstStep.destinationToken.decimals,
),
) *
(firstStep.destinationToken.prices["USD"] || 0) *
(firstStep.destinationToken.prices.USD || 0) *
100,
},
fromAddress: quote.intent.sender,
Expand All @@ -372,7 +372,7 @@ export async function getBuyWithCryptoQuote(
chainId: firstStep.originToken.chainId,
decimals: firstStep.originToken.decimals,
name: firstStep.originToken.name,
priceUSDCents: (firstStep.originToken.prices["USD"] || 0) * 100,
priceUSDCents: (firstStep.originToken.prices.USD || 0) * 100,
symbol: firstStep.originToken.symbol,
tokenAddress: firstStep.originToken.address,
},
Expand All @@ -395,7 +395,7 @@ export async function getBuyWithCryptoQuote(
chainId: firstStep.destinationToken.chainId,
decimals: firstStep.destinationToken.decimals,
name: firstStep.destinationToken.name,
priceUSDCents: (firstStep.destinationToken.prices["USD"] || 0) * 100,
priceUSDCents: (firstStep.destinationToken.prices.USD || 0) * 100,
symbol: firstStep.destinationToken.symbol,
tokenAddress: firstStep.destinationToken.address,
},
Expand Down
8 changes: 4 additions & 4 deletions packages/thirdweb/src/pay/buyWithCrypto/getTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,14 @@ export async function getBuyWithCryptoTransfer(
Number(
Value.format(quote.originAmount, firstStep.originToken.decimals),
) *
(firstStep.originToken.prices["USD"] || 0) *
(firstStep.originToken.prices.USD || 0) *
100,
amountWei: quote.originAmount.toString(),
token: {
chainId: firstStep.originToken.chainId,
decimals: firstStep.originToken.decimals,
name: firstStep.originToken.name,
priceUSDCents: (firstStep.originToken.prices["USD"] || 0) * 100,
priceUSDCents: (firstStep.originToken.prices.USD || 0) * 100,
symbol: firstStep.originToken.symbol,
tokenAddress: firstStep.originToken.address,
},
Expand All @@ -226,7 +226,7 @@ export async function getBuyWithCryptoTransfer(
firstStep.originToken.decimals,
),
) *
(firstStep.originToken.prices["USD"] || 0) *
(firstStep.originToken.prices.USD || 0) *
100
: 0,
amountWei:
Expand All @@ -237,7 +237,7 @@ export async function getBuyWithCryptoTransfer(
chainId: firstStep.originToken.chainId,
decimals: firstStep.originToken.decimals,
name: firstStep.originToken.name,
priceUSDCents: (firstStep.originToken.prices["USD"] || 0) * 100,
priceUSDCents: (firstStep.originToken.prices.USD || 0) * 100,
symbol: firstStep.originToken.symbol,
tokenAddress: firstStep.originToken.address,
},
Expand Down
6 changes: 3 additions & 3 deletions packages/thirdweb/src/pay/buyWithFiat/getQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ export async function getBuyWithFiatQuote(
chainId: token.chainId,
decimals: token.decimals,
name: token.name,
priceUSDCents: Math.round((token.prices["USD"] || 0) * 100),
priceUSDCents: Math.round((token.prices.USD || 0) * 100),
symbol: token.symbol,
tokenAddress: token.address,
});
Expand Down Expand Up @@ -408,7 +408,7 @@ export async function getBuyWithFiatQuote(
const onRampTokenObject = {
amount: onRampTokenAmount,
amountUSDCents: Math.round(
Number(onRampTokenAmount) * (onRampTokenRaw.prices["USD"] || 0) * 100,
Number(onRampTokenAmount) * (onRampTokenRaw.prices.USD || 0) * 100,
),
amountWei: onRampTokenAmountWei.toString(),
token: tokenToPayTokenInfo(onRampTokenRaw),
Expand All @@ -434,7 +434,7 @@ export async function getBuyWithFiatQuote(
routingTokenObject = {
amount: routingAmount,
amountUSDCents: Math.round(
Number(routingAmount) * (routingTokenRaw.prices["USD"] || 0) * 100,
Number(routingAmount) * (routingTokenRaw.prices.USD || 0) * 100,
),
amountWei: routingAmountWei.toString(),
token: tokenToPayTokenInfo(routingTokenRaw),
Expand Down
4 changes: 2 additions & 2 deletions packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ export function usePaymentMethods(options: {
validOwnedTokens.sort((a, b) => {
const aDollarBalance =
Number.parseFloat(toTokens(a.balance, a.originToken.decimals)) *
(a.originToken.prices["USD"] || 0);
(a.originToken.prices.USD || 0);
const bDollarBalance =
Number.parseFloat(toTokens(b.balance, b.originToken.decimals)) *
(b.originToken.prices["USD"] || 0);
(b.originToken.prices.USD || 0);
return bDollarBalance - aDollarBalance;
});

Expand Down
155 changes: 34 additions & 121 deletions packages/thirdweb/src/wallets/smart/lib/signing.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import type * as ox__TypedData from "ox/TypedData";
import { serializeErc6492Signature } from "../../../auth/serialize-erc6492-signature.js";
import {
verifyEip1271Signature,
verifyHash,
} from "../../../auth/verify-hash.js";
import { verifyEip1271Signature } from "../../../auth/verify-hash.js";
import type { Chain } from "../../../chains/types.js";
import type { ThirdwebClient } from "../../../client/client.js";
import {
getContract,
type ThirdwebContract,
} from "../../../contract/contract.js";
import type { ThirdwebContract } from "../../../contract/contract.js";
import { encode } from "../../../transaction/actions/encode.js";
import { readContract } from "../../../transaction/read-contract.js";
import { encodeAbiParameters } from "../../../utils/abi/encodeAbiParameters.js";
import { isContractDeployed } from "../../../utils/bytecode/is-contract-deployed.js";
import type { Hex } from "../../../utils/encoding/hex.js";
import { hashMessage } from "../../../utils/hashing/hashMessage.js";
import { hashTypedData } from "../../../utils/hashing/hashTypedData.js";
import type { SignableMessage } from "../../../utils/types.js";
Expand All @@ -40,33 +32,24 @@ export async function smartAccountSignMessage({
message: SignableMessage;
}) {
const originalMsgHash = hashMessage(message);
const is712Factory = await checkFor712Factory({
accountContract,
factoryContract,
originalMsgHash,
});

let sig: `0x${string}`;
if (is712Factory) {
const wrappedMessageHash = encodeAbiParameters(
[{ type: "bytes32" }],
[originalMsgHash],
);
const wrappedMessageHash = encodeAbiParameters(
[{ type: "bytes32" }],
[originalMsgHash],
);

sig = await options.personalAccount.signTypedData({
domain: {
chainId: options.chain.id,
name: "Account",
verifyingContract: accountContract.address,
version: "1",
},
message: { message: wrappedMessageHash },
primaryType: "AccountMessage",
types: { AccountMessage: [{ name: "message", type: "bytes" }] },
});
} else {
sig = await options.personalAccount.signMessage({ message });
}
sig = await options.personalAccount.signTypedData({
domain: {
chainId: options.chain.id,
name: "Account",
verifyingContract: accountContract.address,
version: "1",
},
message: { message: wrappedMessageHash },
primaryType: "AccountMessage",
types: { AccountMessage: [{ name: "message", type: "bytes" }] },
});

const isDeployed = await isContractDeployed(accountContract);
if (isDeployed) {
Expand Down Expand Up @@ -96,19 +79,7 @@ export async function smartAccountSignMessage({
signature: sig,
});

// check if the signature is valid
const isValid = await verifyHash({
address: accountContract.address,
chain: accountContract.chain,
client: accountContract.client,
hash: originalMsgHash,
signature: erc6492Sig,
});

if (isValid) {
return erc6492Sig;
}
throw new Error("Unable to verify ERC-6492 signature after signing.");
return erc6492Sig;
}
}

Expand Down Expand Up @@ -138,33 +109,23 @@ export async function smartAccountSignTypedData<
}

const originalMsgHash = hashTypedData(typedData);
// check if the account contract supports EIP721 domain separator based signing
const is712Factory = await checkFor712Factory({
accountContract,
factoryContract,
originalMsgHash,
});

let sig: `0x${string}`;
if (is712Factory) {
const wrappedMessageHash = encodeAbiParameters(
[{ type: "bytes32" }],
[originalMsgHash],
);
sig = await options.personalAccount.signTypedData({
domain: {
chainId: options.chain.id,
name: "Account",
verifyingContract: accountContract.address,
version: "1",
},
message: { message: wrappedMessageHash },
primaryType: "AccountMessage",
types: { AccountMessage: [{ name: "message", type: "bytes" }] },
});
} else {
sig = await options.personalAccount.signTypedData(typedData);
}
const wrappedMessageHash = encodeAbiParameters(
[{ type: "bytes32" }],
[originalMsgHash],
);
sig = await options.personalAccount.signTypedData({
domain: {
chainId: options.chain.id,
name: "Account",
verifyingContract: accountContract.address,
version: "1",
},
message: { message: wrappedMessageHash },
primaryType: "AccountMessage",
types: { AccountMessage: [{ name: "message", type: "bytes" }] },
});

const isDeployed = await isContractDeployed(accountContract);
if (isDeployed) {
Expand Down Expand Up @@ -194,21 +155,7 @@ export async function smartAccountSignTypedData<
signature: sig,
});

// check if the signature is valid
const isValid = await verifyHash({
address: accountContract.address,
chain: accountContract.chain,
client: accountContract.client,
hash: originalMsgHash,
signature: erc6492Sig,
});

if (isValid) {
return erc6492Sig;
}
throw new Error(
"Unable to verify signature on smart account, please make sure the admin wallet has permissions and the signature is valid.",
);
return erc6492Sig;
}
}

Expand All @@ -233,40 +180,6 @@ export async function confirmContractDeployment(args: {
}
}

async function checkFor712Factory({
factoryContract,
accountContract,
originalMsgHash,
}: {
factoryContract: ThirdwebContract;
accountContract: ThirdwebContract;
originalMsgHash: Hex;
}) {
try {
const implementationAccount = await readContract({
contract: factoryContract,
method: "function accountImplementation() public view returns (address)",
});
// check if the account contract supports EIP721 domain separator or modular based signing
const is712Factory = await readContract({
contract: getContract({
address: implementationAccount,
chain: accountContract.chain,
client: accountContract.client,
}),
method:
"function getMessageHash(bytes32 _hash) public view returns (bytes32)",
params: [originalMsgHash],
})
.then((res) => res !== "0x")
.catch(() => false);

return is712Factory;
} catch {
return false;
}
}

/**
* Deployes a smart account via a dummy transaction. If the account is already deployed, this will do nothing.
*
Expand Down
Loading
Loading