|
1 |
| -import { ethers, providers, utils } from "ethers"; |
| 1 | +import { Contract, ethers, providers, utils } from "ethers"; |
2 | 2 |
|
3 | 3 | import { Bytes, Signer } from "ethers";
|
4 | 4 | import { BaseAccountAPI } from "./base-api";
|
5 | 5 | import type { ERC4337EthersProvider } from "./erc4337-provider";
|
6 | 6 | import { HttpRpcClient } from "./http-rpc-client";
|
7 | 7 | import { hexlifyUserOp, randomNonce } from "./utils";
|
8 | 8 | import { ProviderConfig, UserOpOptions } from "../types";
|
| 9 | +import { signTypedDataInternal } from "@thirdweb-dev/sdk"; |
| 10 | +import { |
| 11 | + checkContractWalletSignature, |
| 12 | + chainIdToThirdwebRpc, |
| 13 | +} from "../../../wallets/abstract"; |
9 | 14 |
|
10 | 15 | export class ERC4337EthersSigner extends Signer {
|
11 | 16 | config: ProviderConfig;
|
@@ -138,20 +143,84 @@ Code: ${errorCode}`;
|
138 | 143 | return this.address as string;
|
139 | 144 | }
|
140 | 145 |
|
141 |
| - async signMessage(message: Bytes | string): Promise<string> { |
142 |
| - const isNotDeployed = await this.smartAccountAPI.checkAccountPhantom(); |
143 |
| - if (isNotDeployed && this.config.deployOnSign) { |
144 |
| - console.log( |
145 |
| - "Account contract not deployed yet. Deploying account before signing message", |
146 |
| - ); |
147 |
| - const tx = await this.sendTransaction({ |
148 |
| - to: await this.getAddress(), |
149 |
| - data: "0x", |
150 |
| - }); |
151 |
| - await tx.wait(); |
152 |
| - } |
153 |
| - |
154 |
| - return await this.originalSigner.signMessage(message); |
| 146 | + /** |
| 147 | + * Sign a message and return the signature |
| 148 | + */ |
| 149 | + public async signMessage(message: Bytes | string): Promise<string> { |
| 150 | + // Deploy smart wallet if needed |
| 151 | + const isNotDeployed = await this.smartAccountAPI.checkAccountPhantom(); |
| 152 | + if (isNotDeployed) { |
| 153 | + console.log( |
| 154 | + "Account contract not deployed yet. Deploying account before signing message", |
| 155 | + ); |
| 156 | + const tx = await this.sendTransaction({ |
| 157 | + to: await this.getAddress(), |
| 158 | + data: "0x", |
| 159 | + }); |
| 160 | + await tx.wait(); |
| 161 | + } |
| 162 | + |
| 163 | + const [chainId, address] = await Promise.all([ |
| 164 | + this.getChainId(), |
| 165 | + this.getAddress(), |
| 166 | + ]); |
| 167 | + const originalMsgHash = utils.hashMessage(message); |
| 168 | + |
| 169 | + let factorySupports712: boolean; |
| 170 | + let signature: string; |
| 171 | + |
| 172 | + try { |
| 173 | + const provider = new providers.JsonRpcProvider( |
| 174 | + chainIdToThirdwebRpc(chainId, this.config.clientId), |
| 175 | + chainId, |
| 176 | + ); |
| 177 | + const walletContract = new Contract( |
| 178 | + address, |
| 179 | + [ |
| 180 | + "function getMessageHash(bytes32 _hash) public view returns (bytes32)", |
| 181 | + ], |
| 182 | + provider, |
| 183 | + ); |
| 184 | + // if this fails it's a pre 712 factory |
| 185 | + await walletContract.getMessageHash(originalMsgHash); |
| 186 | + factorySupports712 = true; |
| 187 | + } catch { |
| 188 | + factorySupports712 = false; |
| 189 | + } |
| 190 | + |
| 191 | + if (factorySupports712) { |
| 192 | + const result = await signTypedDataInternal( |
| 193 | + this, |
| 194 | + { |
| 195 | + name: "Account", |
| 196 | + version: "1", |
| 197 | + chainId, |
| 198 | + verifyingContract: address, |
| 199 | + }, |
| 200 | + { AccountMessage: [{ name: "message", type: "bytes" }] }, |
| 201 | + { |
| 202 | + message: utils.defaultAbiCoder.encode(["bytes32"], [originalMsgHash]), |
| 203 | + }, |
| 204 | + ); |
| 205 | + signature = result.signature; |
| 206 | + } else { |
| 207 | + signature = await this.originalSigner.signMessage(message); |
| 208 | + } |
| 209 | + |
| 210 | + const isValid = await checkContractWalletSignature( |
| 211 | + message as string, |
| 212 | + signature, |
| 213 | + address, |
| 214 | + chainId, |
| 215 | + ); |
| 216 | + |
| 217 | + if (isValid) { |
| 218 | + return signature; |
| 219 | + } else { |
| 220 | + throw new Error( |
| 221 | + "Unable to verify signature on smart account, please make sure the smart account is deployed and the signature is valid.", |
| 222 | + ); |
| 223 | + } |
155 | 224 | }
|
156 | 225 |
|
157 | 226 | async signTransaction(
|
|
0 commit comments