Skip to content
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

Fixed UserOp Identifier #75

Merged
merged 4 commits into from
Oct 11, 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
2 changes: 1 addition & 1 deletion examples/send-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function main(): Promise<void> {
safeSaltNonce: DEFAULT_SAFE_SALT_NONCE,
});
const deployed = await txManager.safeDeployed(chainId);
console.log("Safe Deployed:", deployed);
console.log(`Safe Deployed (on chainId ${chainId}): ${deployed}`);
const transactions = [
// TODO: Replace dummy transaction with real user transaction.
{
Expand Down
11 changes: 6 additions & 5 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { saltNonceFromMessage } from "./util";
import { toHex } from "viem";

// 44996514629493770112085868524049986283670269803674596648610276180743582360860
export const DEFAULT_SAFE_SALT_NONCE = saltNonceFromMessage(
"bitteprotocol/near-safe"
);
const DOMAIN_SEPARATOR = "bitte/near-safe";
// 0x62697474652f6e6561722d7361666500
export const USER_OP_IDENTIFIER = toHex(DOMAIN_SEPARATOR, { size: 16 });
// 130811896738364114529934864114944206080
export const DEFAULT_SAFE_SALT_NONCE = BigInt(USER_OP_IDENTIFIER).toString();
26 changes: 16 additions & 10 deletions src/lib/safe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Address,
concat,
encodeFunctionData,
encodePacked,
getCreate2Address,
Expand All @@ -12,6 +13,7 @@ import {
} from "viem";

import { SAFE_DEPLOYMENTS } from "../_gen/deployments";
import { USER_OP_IDENTIFIER } from "../constants";
import {
Deployment,
GasPrice,
Expand Down Expand Up @@ -171,16 +173,20 @@ export class SafeContractSuite {
nonce: toHex(nonce),
...this.factoryDataForSetup(safeNotDeployed, setup, safeSaltNonce),
// <https://github.com/safe-global/safe-modules/blob/9a18245f546bf2a8ed9bdc2b04aae44f949ec7a0/modules/4337/contracts/Safe4337Module.sol#L172>
callData: encodeFunctionData({
abi: this.m4337.abi,
functionName: "executeUserOp",
args: [
txData.to,
BigInt(txData.value),
txData.data,
txData.operation || 0,
],
}),
callData: concat([
encodeFunctionData({
abi: this.m4337.abi,
functionName: "executeUserOp",
args: [
txData.to,
BigInt(txData.value),
txData.data,
txData.operation || 0,
],
}),
// Append On-Chain Identifier:
USER_OP_IDENTIFIER,
]),
...feeData,
};
}
Expand Down
3 changes: 3 additions & 0 deletions src/near-safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ export class NearSafe {
if (transactions.length === 0) {
throw new Error("Empty transaction set!");
}
console.log(
`Building UserOp on chainId ${chainId} with ${transactions.length} transaction(s)`
);
const bundler = this.bundlerForChainId(chainId);
const [gasFees, nonce, safeDeployed] = await Promise.all([
bundler.getGasPrice(),
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/constants.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { DEFAULT_SAFE_SALT_NONCE, USER_OP_IDENTIFIER } from "../../src";

// DO NOT MODIFY
describe("Protocol Domain Separator", () => {
it("USER_OP_IDENTIFIER", async () => {
expect(USER_OP_IDENTIFIER).toBe("0x62697474652f6e6561722d7361666500");
});
it("DEFAULT_SAFE_SALT_NONCE", async () => {
expect(DEFAULT_SAFE_SALT_NONCE).toBe(
"130811896738364114529934864114944206080"
);
});
});
7 changes: 3 additions & 4 deletions tests/unit/utils.spec.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are some test files called .spec.ts and others .test.ts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh whoops. They are both common. I just forgot to only use one.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers } from "ethers";
import { zeroAddress } from "viem";

import { DEFAULT_SAFE_SALT_NONCE, PaymasterData } from "../../src";
import { PaymasterData } from "../../src";
import {
PLACEHOLDER_SIG,
containsValue,
Expand Down Expand Up @@ -77,9 +77,8 @@ describe("Utility Functions (mostly byte packing)", () => {
).toBe(true);
});
it("saltNonceFromMessage", async () => {
expect(saltNonceFromMessage("bitteprotocol/near-safe")).toBe(
DEFAULT_SAFE_SALT_NONCE
expect(saltNonceFromMessage("bitte/near-safe")).toBe(
"26371153660914144112327059280066269158753782528888197421682303285265580464377"
);
// console.log("DEFAULT_SAFE_SALT_NONCE", DEFAULT_SAFE_SALT_NONCE);
});
});