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

Use overloads to detect account hoisting for viem wallets #125

Merged
merged 3 commits into from
Apr 7, 2024
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
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "userop",
"version": "0.4.0-beta.2",
"version": "0.4.0-beta.3",
"description": "A simple JS library for building ERC-4337 UserOperations.",
"types": "./dist/index.d.ts",
"main": "./dist/index.js",
Expand All @@ -23,17 +23,21 @@
"@types/jest": "^29.5.12",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"ethers": "^6.11.1",
"jest": "^29.7.0",
"nock": "^13.5.4",
"prettier": "3.2.5",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.2",
"typescript": "^5.4.2",
"typescript-eslint": "^7.3.1"
"typescript-eslint": "^7.3.1",
"viem": "^2.9.12"
},
"dependencies": {
"abitype": "^1.0.0",
"abitype": "^1.0.0"
},
"peerDependencies": {
"ethers": "^6.11.1",
"viem": "^2.7.19"
"viem": "^2.9.12"
}
}
40 changes: 16 additions & 24 deletions src/v06/account/commonConfigs/simpleAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,20 @@ import { RequestSignature } from "../hooks";

const FACTORY_ADDRESS = "0x9406Cc6185a346906296840746125a0E44976454";

type SimpleAccountArgsWithViemEOA<E extends WalletClient> = [
export function base(
ethClient: PublicClient | JsonRpcProvider,
eoa: E,
];
type SimpleAccountArgsWithViemEOANoHoist<E extends WalletClient> = [
ethClient: PublicClient | JsonRpcProvider,
eoa: E,
eoa: WalletClient<Transport, Chain | undefined, undefined>,
account: Account,
];
type SimpleAccountArgsWithEthersSigner<E extends Signer> = [
): RequiredAccountOpts<typeof AccountAbi, typeof FactoryAbi>;
export function base(
ethClient: PublicClient | JsonRpcProvider,
eoa: E,
];

export const base = <E extends WalletClient | Signer>(
...args: E extends WalletClient
? E["account"] extends Account
? SimpleAccountArgsWithViemEOA<E>
: SimpleAccountArgsWithViemEOANoHoist<E>
: E extends Signer
? SimpleAccountArgsWithEthersSigner<E>
: never
): RequiredAccountOpts<typeof AccountAbi, typeof FactoryAbi> => {
const [ethClient, eoa, account] = args;

eoa: WalletClient<Transport, Chain | undefined, Account> | Signer,
): RequiredAccountOpts<typeof AccountAbi, typeof FactoryAbi>;
export function base(
ethClient: PublicClient | JsonRpcProvider,
eoa: Signer | WalletClient,
account?: Account,
): RequiredAccountOpts<typeof AccountAbi, typeof FactoryAbi> {
return {
accountAbi: AccountAbi,
factoryAbi: FactoryAbi,
Expand All @@ -58,10 +47,13 @@ export const base = <E extends WalletClient | Signer>(
requestSignature:
"getAddresses" in eoa
? account !== undefined
? RequestSignature.withViemWalletClient(eoa, account)
? RequestSignature.withViemWalletClient(
eoa as WalletClient<Transport, Chain | undefined, undefined>,
account,
)
: RequestSignature.withViemWalletClient(
eoa as WalletClient<Transport, Chain | undefined, Account>,
)
: RequestSignature.withEthersSigner(eoa),
};
};
}
19 changes: 12 additions & 7 deletions src/v06/account/hooks/requestSignature/viem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import { WalletClient, Account, Transport, Chain } from "viem";
import { privateKeyToAccount, generatePrivateKey } from "viem/accounts";
import { RequestSignatureFunc } from "../types";

export const withViemWalletClient = <W extends WalletClient>(
...args: W["account"] extends Account
? [client: W]
: [client: W, account: Account]
): RequestSignatureFunc => {
export function withViemWalletClient(
client: WalletClient<Transport, Chain | undefined, undefined>,
account: Account,
): RequestSignatureFunc;
export function withViemWalletClient(
client: WalletClient<Transport, Chain | undefined, Account>,
): RequestSignatureFunc;
export function withViemWalletClient(
client: WalletClient,
account?: Account,
): RequestSignatureFunc {
const dummy = privateKeyToAccount(generatePrivateKey());
return async (type, message) => {
switch (type) {
Expand All @@ -15,7 +21,6 @@ export const withViemWalletClient = <W extends WalletClient>(
}

case "final": {
const [client, account] = args;
if (account) {
return client.signMessage({ account, message: { raw: message } });
}
Expand All @@ -27,4 +32,4 @@ export const withViemWalletClient = <W extends WalletClient>(
}
}
};
};
}
24 changes: 9 additions & 15 deletions src/v06/account/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import {
concat,
encodeFunctionData,
PublicClient,
BaseError,
ContractFunctionRevertedError,
RpcStateOverride,
Hex,
zeroAddress,
isAddress,
} from "viem";
import { JsonRpcProvider, Contract } from "ethers";
import {
Expand Down Expand Up @@ -213,20 +212,15 @@ export class Instance<A extends Abi, F extends Abi> {
functionName: "getSenderAddress",
args: [await this.getInitCode()],
});
} catch (error) {
if (error instanceof BaseError) {
const revertError = error.walk(
(err) => err instanceof ContractFunctionRevertedError,
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
const metaMessage = error?.metaMessages?.[1];
if (typeof metaMessage !== "string") throw error;

if (revertError instanceof ContractFunctionRevertedError) {
this.sender = revertError.data?.args?.[0] as Address;
} else {
throw error;
}
} else {
throw error;
}
const addr = metaMessage.trim().slice(1, -1);
if (!isAddress(addr)) throw error;

this.sender = addr;
}
}

Expand Down
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -899,9 +899,9 @@
integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==

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

"@scure/bip32@1.3.2":
version "1.3.2"
Expand Down Expand Up @@ -3335,10 +3335,10 @@ v8-to-istanbul@^9.0.1:
"@types/istanbul-lib-coverage" "^2.0.1"
convert-source-map "^1.6.0"

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