Skip to content

[SmartWallet] implement multidimensional nonces for smart wallets #1821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 20, 2023
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
5 changes: 5 additions & 0 deletions .changeset/famous-penguins-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/wallets": patch
---

Multidimensional nonces for smart wallets
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class ERC4337EthersProvider extends providers.BaseProvider {
hash: userOpHash,
confirmations: 0,
from: userOp.sender,
nonce: BigNumber.from(userOp.nonce).toNumber(),
nonce: 0, // not the real nonce, but good enough for this purpose
gasLimit: BigNumber.from(userOp.callGasLimit), // ??
value: BigNumber.from(0),
data: utils.hexValue(userOp.callData), // should extract the actual called method from this "execFromEntryPoint()" call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ClientConfig } from "@account-abstraction/sdk";
import { BaseAccountAPI } from "./base-api";
import type { ERC4337EthersProvider } from "./erc4337-provider";
import { HttpRpcClient } from "./http-rpc-client";
import { randomNonce } from "./utils";

export class ERC4337EthersSigner extends Signer {
config: ClientConfig;
Expand Down Expand Up @@ -40,12 +41,14 @@ export class ERC4337EthersSigner extends Signer {
const tx = await ethers.utils.resolveProperties(transaction);
await this.verifyAllNecessaryFields(tx);

const multidimensionalNonce = randomNonce();
const userOperation = await this.smartAccountAPI.createSignedUserOp(
{
target: tx.to || "",
data: tx.data?.toString() || "0x",
value: tx.value,
gasLimit: tx.gasLimit,
nonce: multidimensionalNonce,
},
batched,
);
Expand Down
28 changes: 28 additions & 0 deletions packages/wallets/src/evm/connectors/smart-wallet/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,31 @@ export async function getUserOpHashV06(
);
return utils.keccak256(enc);
}

const generateRandomUint192 = (): bigint => {
const rand1 = BigInt(Math.floor(Math.random() * 0x100000000));
const rand2 = BigInt(Math.floor(Math.random() * 0x100000000));
const rand3 = BigInt(Math.floor(Math.random() * 0x100000000));
const rand4 = BigInt(Math.floor(Math.random() * 0x100000000));
const rand5 = BigInt(Math.floor(Math.random() * 0x100000000));
const rand6 = BigInt(Math.floor(Math.random() * 0x100000000));
return (
(rand1 << 160n) |
(rand2 << 128n) |
(rand3 << 96n) |
(rand4 << 64n) |
(rand5 << 32n) |
rand6
);
};

export const randomNonce = () => {
let hexString = generateRandomUint192().toString(16);
if (hexString.length % 2 !== 0) {
hexString = "0" + hexString;
}
hexString = "0x" + hexString;
return ethers.BigNumber.from(
ethers.utils.concat([hexString, "0x0000000000000000"]),
);
};