Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit 7ca66cc

Browse files
authored
Use overloads to detect account hoisting for viem wallets (#125)
1 parent 35d66c0 commit 7ca66cc

File tree

5 files changed

+52
-57
lines changed

5 files changed

+52
-57
lines changed

package.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "userop",
3-
"version": "0.4.0-beta.2",
3+
"version": "0.4.0-beta.3",
44
"description": "A simple JS library for building ERC-4337 UserOperations.",
55
"types": "./dist/index.d.ts",
66
"main": "./dist/index.js",
@@ -23,17 +23,21 @@
2323
"@types/jest": "^29.5.12",
2424
"eslint": "^8.57.0",
2525
"eslint-config-prettier": "^9.1.0",
26+
"ethers": "^6.11.1",
2627
"jest": "^29.7.0",
2728
"nock": "^13.5.4",
2829
"prettier": "3.2.5",
2930
"rimraf": "^5.0.5",
3031
"ts-jest": "^29.1.2",
3132
"typescript": "^5.4.2",
32-
"typescript-eslint": "^7.3.1"
33+
"typescript-eslint": "^7.3.1",
34+
"viem": "^2.9.12"
3335
},
3436
"dependencies": {
35-
"abitype": "^1.0.0",
37+
"abitype": "^1.0.0"
38+
},
39+
"peerDependencies": {
3640
"ethers": "^6.11.1",
37-
"viem": "^2.7.19"
41+
"viem": "^2.9.12"
3842
}
3943
}

src/v06/account/commonConfigs/simpleAccount.ts

+16-24
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,20 @@ import { RequestSignature } from "../hooks";
1313

1414
const FACTORY_ADDRESS = "0x9406Cc6185a346906296840746125a0E44976454";
1515

16-
type SimpleAccountArgsWithViemEOA<E extends WalletClient> = [
16+
export function base(
1717
ethClient: PublicClient | JsonRpcProvider,
18-
eoa: E,
19-
];
20-
type SimpleAccountArgsWithViemEOANoHoist<E extends WalletClient> = [
21-
ethClient: PublicClient | JsonRpcProvider,
22-
eoa: E,
18+
eoa: WalletClient<Transport, Chain | undefined, undefined>,
2319
account: Account,
24-
];
25-
type SimpleAccountArgsWithEthersSigner<E extends Signer> = [
20+
): RequiredAccountOpts<typeof AccountAbi, typeof FactoryAbi>;
21+
export function base(
2622
ethClient: PublicClient | JsonRpcProvider,
27-
eoa: E,
28-
];
29-
30-
export const base = <E extends WalletClient | Signer>(
31-
...args: E extends WalletClient
32-
? E["account"] extends Account
33-
? SimpleAccountArgsWithViemEOA<E>
34-
: SimpleAccountArgsWithViemEOANoHoist<E>
35-
: E extends Signer
36-
? SimpleAccountArgsWithEthersSigner<E>
37-
: never
38-
): RequiredAccountOpts<typeof AccountAbi, typeof FactoryAbi> => {
39-
const [ethClient, eoa, account] = args;
40-
23+
eoa: WalletClient<Transport, Chain | undefined, Account> | Signer,
24+
): RequiredAccountOpts<typeof AccountAbi, typeof FactoryAbi>;
25+
export function base(
26+
ethClient: PublicClient | JsonRpcProvider,
27+
eoa: Signer | WalletClient,
28+
account?: Account,
29+
): RequiredAccountOpts<typeof AccountAbi, typeof FactoryAbi> {
4130
return {
4231
accountAbi: AccountAbi,
4332
factoryAbi: FactoryAbi,
@@ -58,10 +47,13 @@ export const base = <E extends WalletClient | Signer>(
5847
requestSignature:
5948
"getAddresses" in eoa
6049
? account !== undefined
61-
? RequestSignature.withViemWalletClient(eoa, account)
50+
? RequestSignature.withViemWalletClient(
51+
eoa as WalletClient<Transport, Chain | undefined, undefined>,
52+
account,
53+
)
6254
: RequestSignature.withViemWalletClient(
6355
eoa as WalletClient<Transport, Chain | undefined, Account>,
6456
)
6557
: RequestSignature.withEthersSigner(eoa),
6658
};
67-
};
59+
}

src/v06/account/hooks/requestSignature/viem.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ import { WalletClient, Account, Transport, Chain } from "viem";
22
import { privateKeyToAccount, generatePrivateKey } from "viem/accounts";
33
import { RequestSignatureFunc } from "../types";
44

5-
export const withViemWalletClient = <W extends WalletClient>(
6-
...args: W["account"] extends Account
7-
? [client: W]
8-
: [client: W, account: Account]
9-
): RequestSignatureFunc => {
5+
export function withViemWalletClient(
6+
client: WalletClient<Transport, Chain | undefined, undefined>,
7+
account: Account,
8+
): RequestSignatureFunc;
9+
export function withViemWalletClient(
10+
client: WalletClient<Transport, Chain | undefined, Account>,
11+
): RequestSignatureFunc;
12+
export function withViemWalletClient(
13+
client: WalletClient,
14+
account?: Account,
15+
): RequestSignatureFunc {
1016
const dummy = privateKeyToAccount(generatePrivateKey());
1117
return async (type, message) => {
1218
switch (type) {
@@ -15,7 +21,6 @@ export const withViemWalletClient = <W extends WalletClient>(
1521
}
1622

1723
case "final": {
18-
const [client, account] = args;
1924
if (account) {
2025
return client.signMessage({ account, message: { raw: message } });
2126
}
@@ -27,4 +32,4 @@ export const withViemWalletClient = <W extends WalletClient>(
2732
}
2833
}
2934
};
30-
};
35+
}

src/v06/account/instance.ts

+9-15
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ import {
99
concat,
1010
encodeFunctionData,
1111
PublicClient,
12-
BaseError,
13-
ContractFunctionRevertedError,
1412
RpcStateOverride,
1513
Hex,
1614
zeroAddress,
15+
isAddress,
1716
} from "viem";
1817
import { JsonRpcProvider, Contract } from "ethers";
1918
import {
@@ -213,20 +212,15 @@ export class Instance<A extends Abi, F extends Abi> {
213212
functionName: "getSenderAddress",
214213
args: [await this.getInitCode()],
215214
});
216-
} catch (error) {
217-
if (error instanceof BaseError) {
218-
const revertError = error.walk(
219-
(err) => err instanceof ContractFunctionRevertedError,
220-
);
215+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
216+
} catch (error: any) {
217+
const metaMessage = error?.metaMessages?.[1];
218+
if (typeof metaMessage !== "string") throw error;
221219

222-
if (revertError instanceof ContractFunctionRevertedError) {
223-
this.sender = revertError.data?.args?.[0] as Address;
224-
} else {
225-
throw error;
226-
}
227-
} else {
228-
throw error;
229-
}
220+
const addr = metaMessage.trim().slice(1, -1);
221+
if (!isAddress(addr)) throw error;
222+
223+
this.sender = addr;
230224
}
231225
}
232226

yarn.lock

+7-7
Original file line numberDiff line numberDiff line change
@@ -899,9 +899,9 @@
899899
integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==
900900

901901
"@scure/base@~1.1.0", "@scure/base@~1.1.2":
902-
version "1.1.5"
903-
resolved "https://registry.yarnpkg.com/@scure/base/-/base-1.1.5.tgz#1d85d17269fe97694b9c592552dd9e5e33552157"
904-
integrity sha512-Brj9FiG2W1MRQSTB212YVPRrcbjkv48FoZi/u4l/zds/ieRrqsh7aUf6CLwkAq61oKXr/ZlTzlY66gLIj3TFTQ==
902+
version "1.1.6"
903+
resolved "https://registry.yarnpkg.com/@scure/base/-/base-1.1.6.tgz#8ce5d304b436e4c84f896e0550c83e4d88cb917d"
904+
integrity sha512-ok9AWwhcgYuGG3Zfhyqg+zwl+Wn5uE+dwC0NV/2qQkx4dABbb/bx96vWu8NSj+BNjjSjno+JRYRjle1jV08k3g==
905905

906906
907907
version "1.3.2"
@@ -3335,10 +3335,10 @@ v8-to-istanbul@^9.0.1:
33353335
"@types/istanbul-lib-coverage" "^2.0.1"
33363336
convert-source-map "^1.6.0"
33373337

3338-
viem@^2.7.19:
3339-
version "2.7.19"
3340-
resolved "https://registry.yarnpkg.com/viem/-/viem-2.7.19.tgz#fa6bd8f46df2f0332e5ca6d116772dff6f161a72"
3341-
integrity sha512-UOMeqy+8p2709ra2j9HEOL1NfjsXZzlJ8gwR6YO/zXH8KIZvyzW07t4iQARF5+ShVZ/7+/1ec8oPjVi1M//33A==
3338+
viem@^2.9.12:
3339+
version "2.9.12"
3340+
resolved "https://registry.yarnpkg.com/viem/-/viem-2.9.12.tgz#27a865f8bd95dc1de7931bbcad485bf561dc1027"
3341+
integrity sha512-wxeFgcdEbf9+CYGGS2NrxlbhZQc3kNDArEjFtI7jAMttcQSHle9KFps+kRa35poARi1LKS2MtAa9ODPloNRlcw==
33423342
dependencies:
33433343
"@adraffy/ens-normalize" "1.10.0"
33443344
"@noble/curves" "1.2.0"

0 commit comments

Comments
 (0)