Skip to content

Commit

Permalink
Adds EIP-1271 Signature Validation Fallback (#4156)
Browse files Browse the repository at this point in the history
This PR EIP-1271 fallback for smart contract validation since not all chains support the deployless call used in 6792 flow.

<!-- start pr-codex -->

---

## PR-Codex overview
The focus of this PR is to fix smart contract wallet signature validation on older chains.

### Detailed summary
- Updated the prompt text for signing in on the wallet UI
- Added a fallback to EIP1271 validation for chains not supporting eth_call simulation
- Introduced a new function `verifyEip1271Signature` for EIP1271 signature validation

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`

<!-- end pr-codex -->
  • Loading branch information
gregfromstl committed Aug 17, 2024
1 parent cf4443a commit f0d6e34
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/stale-jobs-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Fix smart contract wallet signature validation on older chains
33 changes: 33 additions & 0 deletions packages/thirdweb/src/auth/verify-hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
} from "viem";
import type { Chain } from "../chains/types.js";
import type { ThirdwebClient } from "../client/client.js";
import { type ThirdwebContract, getContract } from "../contract/contract.js";
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 { fromBytes } from "../utils/encoding/from-bytes.js";
Expand Down Expand Up @@ -110,8 +112,39 @@ export async function verifyHash({
const hexResult = isHex(result) ? toBytes(result) : result;
return equalBytes(hexResult, toBytes("0x1"));
} catch (error) {
// Some chains do not support the eth_call simulation and will fail, so we fall back to regular EIP1271 validation
const validEip1271 = await verifyEip1271Signature({
hash,
signature: signatureHex,
contract: getContract({
chain,
address,
client,
}),
}).catch(() => false);
if (validEip1271) {
return true;
}

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

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/auth/verify-hash.ts#L126-L127

Added lines #L126 - L127 were not covered by tests
// TODO: Improve overall RPC error handling so we can tell if this was an actual verification failure or some other error
// Verification failed somehow
return false;
}
}

const EIP_1271_MAGIC_VALUE = "0x1626ba7e";
async function verifyEip1271Signature({
hash,
signature,
contract,
}: {
hash: Hex;
signature: Hex;
contract: ThirdwebContract;
}): Promise<boolean> {
const result = await isValidSignature({
hash,
signature,
contract,
});
return result === EIP_1271_MAGIC_VALUE;
}

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

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/auth/verify-hash.ts#L149-L150

Added lines #L149 - L150 were not covered by tests
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const connectLocaleEn: ConnectLocale = {
},
signingScreen: {
title: "Signing In",
prompt: "Sign the signature request in your wallet",
prompt: "Signing the signature request in your wallet",
promptForSafe:
"Sign signature request in your wallet & approve transaction in Safe",
approveTransactionInSafe: "Approve transaction in Safe",
Expand Down

0 comments on commit f0d6e34

Please sign in to comment.