Skip to content

Commit 7afe2ef

Browse files
signature
1 parent 71b05f1 commit 7afe2ef

File tree

3 files changed

+60
-22
lines changed

3 files changed

+60
-22
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Signature, recoverAddress } from "viem";
1+
import { type SignableMessage, type Signature, recoverAddress } from "viem";
22
import type { Chain } from "../chains/types.js";
33
import type { ThirdwebClient } from "../client/client.js";
44
import { type Hex, isHex } from "../utils/encoding/hex.js";
@@ -10,7 +10,7 @@ import { verifyHash } from "./verify-hash.js";
1010
* @auth
1111
*/
1212
export type VerifyEOASignatureParams = {
13-
message: string;
13+
message: string | SignableMessage;
1414
signature: string | Uint8Array | Signature;
1515
address: string;
1616
};

packages/thirdweb/src/wallets/smart/index.ts

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import type { Chain } from "../../chains/types.js";
1010
import { getCachedChain } from "../../chains/utils.js";
1111
import type { ThirdwebClient } from "../../client/client.js";
12+
import { ZERO_ADDRESS } from "../../constants/addresses.js";
1213
import { type ThirdwebContract, getContract } from "../../contract/contract.js";
1314
import { allowance } from "../../extensions/erc20/__generated__/IERC20/read/allowance.js";
1415
import { approve } from "../../extensions/erc20/write/approve.js";
@@ -285,13 +286,13 @@ async function createSmartAccount(
285286
{ readContract },
286287
{ encodeAbiParameters },
287288
{ hashMessage },
288-
{ checkContractWalletSignature },
289+
{ verifyContractWalletSignature },
289290
] = await Promise.all([
290291
import("../../utils/bytecode/is-contract-deployed.js"),
291292
import("../../transaction/read-contract.js"),
292293
import("../../utils/abi/encodeAbiParameters.js"),
293294
import("../../utils/hashing/hashMessage.js"),
294-
import("../../extensions/erc1271/checkContractWalletSignature.js"),
295+
import("../../auth/verify-signature.js"),
295296
]);
296297
const isDeployed = await isContractDeployed(accountContract);
297298
if (!isDeployed) {
@@ -307,31 +308,56 @@ async function createSmartAccount(
307308
});
308309
}
309310

311+
console.log("contract deployed");
312+
310313
const originalMsgHash = hashMessage(message);
311-
// check if the account contract supports EIP721 domain separator based signing
312-
let factorySupports712 = false;
313-
try {
314-
// this will throw if the contract does not support it (old factories)
315-
await readContract({
314+
// check if the account contract supports EIP721 domain separator or modular based signing
315+
const [messageHash, isModularFactory] = await Promise.all([
316+
readContract({
316317
contract: accountContract,
317318
method:
318319
"function getMessageHash(bytes32 _hash) public view returns (bytes32)",
319320
params: [originalMsgHash],
320-
});
321-
factorySupports712 = true;
322-
} catch {
323-
// ignore
324-
}
321+
}).catch((err) => {
322+
console.error(err);
323+
return undefined;
324+
}),
325+
readContract({
326+
contract: accountContract,
327+
method: "function canInstall(address) public view returns (bool)",
328+
params: [ZERO_ADDRESS],
329+
})
330+
.catch(() => false)
331+
.then(() => true),
332+
]);
333+
334+
console.log("factoryType", messageHash, isModularFactory);
325335

326336
let sig: `0x${string}`;
327-
if (factorySupports712) {
337+
if (messageHash) {
328338
const wrappedMessageHash = encodeAbiParameters(
329339
[{ type: "bytes32" }],
330340
[originalMsgHash],
331341
);
342+
343+
const hasheTypedData = hashTypedData({
344+
domain: {
345+
name: isModularFactory ? "DefaultValidator" : "Account",
346+
version: "1",
347+
chainId: options.chain.id,
348+
verifyingContract: accountContract.address,
349+
},
350+
primaryType: "AccountMessage",
351+
types: { AccountMessage: [{ name: "message", type: "bytes" }] },
352+
message: { message: wrappedMessageHash },
353+
});
354+
355+
console.log("hasheTypedData", hasheTypedData);
356+
console.log("equal", messageHash === hasheTypedData);
357+
332358
sig = await options.personalAccount.signTypedData({
333359
domain: {
334-
name: "Account",
360+
name: isModularFactory ? "DefaultValidator" : "Account",
335361
version: "1",
336362
chainId: options.chain.id,
337363
verifyingContract: accountContract.address,
@@ -340,14 +366,25 @@ async function createSmartAccount(
340366
types: { AccountMessage: [{ name: "message", type: "bytes" }] },
341367
message: { message: wrappedMessageHash },
342368
});
369+
if (isModularFactory) {
370+
// add validator address
371+
sig = concatHex([ZERO_ADDRESS, sig]);
372+
}
343373
} else {
344374
sig = await options.personalAccount.signMessage({ message });
345375
}
346376

347-
const isValid = await checkContractWalletSignature({
348-
contract: accountContract,
377+
console.log(sig);
378+
379+
const isValid = await verifyContractWalletSignature({
380+
address: accountContract.address,
381+
chain: accountContract.chain,
382+
client: accountContract.client,
349383
message,
350384
signature: sig,
385+
}).catch((err) => {
386+
console.error("Error checking contract wallet signature", err);
387+
return false;
351388
});
352389

353390
if (isValid) {

packages/thirdweb/src/wallets/smart/smart-wallet-dev.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { beforeAll, describe, expect, it } from "vitest";
22
import { TEST_CLIENT } from "../../../test/src/test-clients.js";
3+
import { TEST_ACCOUNT_A } from "../../../test/src/test-wallets.js";
34
import { defineChain } from "../../chains/utils.js";
45
import { type ThirdwebContract, getContract } from "../../contract/contract.js";
56
import { balanceOf } from "../../extensions/erc1155/__generated__/IERC1155/read/balanceOf.js";
@@ -12,7 +13,6 @@ import type { Address } from "../../utils/address.js";
1213
import { isContractDeployed } from "../../utils/bytecode/is-contract-deployed.js";
1314
import { setThirdwebDomains } from "../../utils/domains.js";
1415
import type { Account, Wallet } from "../interfaces/wallet.js";
15-
import { generateAccount } from "../utils/generateAccount.js";
1616
import { smartWallet } from "./smart-wallet.js";
1717

1818
let wallet: Wallet;
@@ -41,9 +41,10 @@ describe.runIf(process.env.TW_SECRET_KEY).skip.sequential(
4141
storage: "storage.thirdweb-dev.com",
4242
bundler: "bundler.thirdweb-dev.com",
4343
});
44-
personalAccount = await generateAccount({
45-
client,
46-
});
44+
personalAccount = TEST_ACCOUNT_A;
45+
// personalAccount = await generateAccount({
46+
// client,
47+
// });
4748
wallet = smartWallet({
4849
chain,
4950
gasless: true,

0 commit comments

Comments
 (0)