Skip to content
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/gentle-kids-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Support erc6492 signature verification on zksync
55 changes: 38 additions & 17 deletions packages/thirdweb/src/auth/verify-hash.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { equalBytes } from "@noble/curves/abstract/utils";
import {
type Signature,
encodeDeployData,
encodeFunctionData,
isErc6492Signature,
serializeSignature,
universalSignatureValidatorAbi,
universalSignatureValidatorByteCode,
Expand All @@ -12,10 +13,9 @@
import { isValidSignature } from "../extensions/erc1271/__generated__/isValidSignature/read/isValidSignature.js";
import { eth_call } from "../rpc/actions/eth_call.js";
import { getRpcClient } from "../rpc/rpc.js";
import { isZkSyncChain } from "../utils/any-evm/zksync/isZkSyncChain.js";
import { fromBytes } from "../utils/encoding/from-bytes.js";
import { type Hex, isHex } from "../utils/encoding/hex.js";
import { toBytes } from "../utils/encoding/to-bytes.js";
import { isErc6492Signature } from "./is-erc6492-signature.js";
import { type Hex, hexToBool, isHex } from "../utils/encoding/hex.js";
import { serializeErc6492Signature } from "./serialize-erc6492-signature.js";

export type VerifyHashParams = {
Expand All @@ -30,6 +30,8 @@
};
};

const ZKSYNC_VALIDATOR_ADDRESS = "0xfB688330379976DA81eB64Fe4BF50d7401763B9C";

/**
* @description Verify that an address created the provided signature for a given hash using [ERC-6492](https://eips.ethereum.org/EIPS/eip-6492). This function is interoperable with all wallet types, including EOAs.
* This function should rarely be used directly, instead use @see {import("./verify-signature.js")} and @see {import("./verify-typed-data.js")}}
Expand Down Expand Up @@ -77,7 +79,7 @@
);
})();

const wrappedSignature = await (async () => {
const wrappedSignature: Hex = await (async () => {
// If no factory is provided, we have to assume its already deployed or is an EOA
// TODO: Figure out how to automatically tell if our default factory was used
if (!accountFactory) return signatureHex;
Expand All @@ -93,24 +95,40 @@
});
})();

const verificationData = encodeDeployData({
abi: universalSignatureValidatorAbi,
args: [address, hash, wrappedSignature],
bytecode: universalSignatureValidatorByteCode,
});
let verificationData: {
to?: string;
data: Hex;
};
const zkSyncChain = await isZkSyncChain(chain);
if (zkSyncChain) {
// zksync chains dont support deploying code with eth_call
// need to call a deployed contract instead
verificationData = {
to: ZKSYNC_VALIDATOR_ADDRESS,
data: encodeFunctionData({
abi: universalSignatureValidatorAbi,
functionName: "isValidSig",
args: [address, hash, wrappedSignature],
}),
};

Check warning on line 113 in packages/thirdweb/src/auth/verify-hash.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/auth/verify-hash.ts#L106-L113

Added lines #L106 - L113 were not covered by tests
} else {
verificationData = {
data: encodeDeployData({
abi: universalSignatureValidatorAbi,
args: [address, hash, wrappedSignature],
bytecode: universalSignatureValidatorByteCode,
}),
};
}

const rpcRequest = getRpcClient({
chain,
client,
});

try {
const result = await eth_call(rpcRequest, {
data: verificationData,
});

const hexResult = isHex(result) ? toBytes(result) : result;
return equalBytes(hexResult, toBytes(true));
const result = await eth_call(rpcRequest, verificationData);
return hexToBool(result);
} catch {
// Some chains do not support the eth_call simulation and will fail, so we fall back to regular EIP1271 validation
const validEip1271 = await verifyEip1271Signature({
Expand All @@ -121,7 +139,10 @@
address,
client,
}),
}).catch(() => false);
}).catch((err) => {
console.error("Error verifying EIP-1271 signature", err);
return false;
});
if (validEip1271) {
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/thirdweb/src/auth/verify-signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@
if (isVerifyContractWalletSignatureParams(options)) {
try {
return await verifyContractWalletSignature(options);
} catch {
} catch (err) {
console.error("Error verifying smart contract wallet signature", err);

Check warning on line 156 in packages/thirdweb/src/auth/verify-signature.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/auth/verify-signature.ts#L156

Added line #L156 was not covered by tests
// no-op we skip to return false
}
} else if (!warningTriggered) {
Expand Down