Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
implement multidimensional nonces for smart wallets
  • Loading branch information
joaquim-verges committed Oct 20, 2023
commit 641b59a2a8b3051738ac083cdcab41f10d18ef01
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
30 changes: 29 additions & 1 deletion packages/wallets/src/evm/connectors/smart-wallet/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers, utils } from "ethers";
import { BigNumber, ethers, utils } from "ethers";
import { UserOperationStruct } from "@account-abstraction/contracts";

export function toJSON(op: Partial<UserOperationStruct>): Promise<any> {
Expand Down 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"]),
);
};