Skip to content

Commit

Permalink
test recover method
Browse files Browse the repository at this point in the history
  • Loading branch information
phipsae committed Jul 5, 2024
1 parent 6fcd724 commit 8da38d6
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 1,343 deletions.
12 changes: 6 additions & 6 deletions packages/foundry/contracts/AccountSimple.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ contract AccountSimple is IAccount {
}
}

contract AccountSimpleFactory {
function createAccount(address owner) external returns (address) {
AccountSimple acc = new AccountSimple(owner);
return address(acc);
}
}
// contract AccountSimpleFactory {
// function createAccount(address owner) external returns (address) {
// AccountSimple acc = new AccountSimple(owner);
// return address(acc);
// }
// }
11 changes: 11 additions & 0 deletions packages/foundry/contracts/AccountSimpleFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

import "./AccountSimple.sol";

contract AccountSimpleFactory {
function createAccount(address owner) external returns (address) {
AccountSimple acc = new AccountSimple(owner);
return address(acc);
}
}
2 changes: 2 additions & 0 deletions packages/foundry/contracts/Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
import "forge-std/console.sol";

/// to test if ECDSA.recover works
contract Test {
constructor(bytes memory sig) {
/// first we hash the message hello, then we use the EthSignedMessageHash to sign it
address recovered = ECDSA.recover(MessageHashUtils.toEthSignedMessageHash(keccak256("hello")), sig);
console.logAddress(recovered);
}
Expand Down
51 changes: 33 additions & 18 deletions packages/foundry/script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ pragma solidity ^0.8.9;
import "../contracts/Account123.sol";
import "../contracts/Account123Factory.sol";
import "../contracts/AccountSimple.sol";
import "../contracts/AccountSimpleFactory.sol";
import "../contracts/Paymaster.sol";
import "../contracts/Test.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
import "/Users/philip/Programming/Ethereum/AABuild/PWAA/node_modules/@account-abstraction/contracts/core/EntryPoint.sol";
import "./DeployHelpers.s.sol";

Expand All @@ -26,31 +29,43 @@ contract DeployScript is ScaffoldETHDeploy {
// Start broadcasting transactions
vm.startBroadcast(deployerPrivateKey);

// /// Deploy EntryPoint contract -- 0x5FbDB2315678afecb367f032d93F642f64180aa3
/// Deploy EntryPoint contract -- 0x5FbDB2315678afecb367f032d93F642f64180aa3
// EntryPoint entryPoint = new EntryPoint();


/// Deploy Account
Account123 yourAccount = new Account123(vm.addr(deployerPrivateKey));
// /// Deploy Account
// AccountSimple yourAccount = new AccountSimple(vm.addr(deployerPrivateKey));

/// Deploy Simple Account Factory --- 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
AccountSimpleFactory accountSimpleFactory= new AccountSimpleFactory();


Account123Factory accountFactory = new Account123Factory();
// /// Deploy Simple Account Factory --- 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
// AccountSimpleFactory accountSimpleFactory= new AccountSimpleFactory();
// // Account123Factory accountFactory = new Account123Factory();

// /// Deploy Paymaster
// Paymaster paymaster = new Paymaster();

// Log deployment addresses
console.logString(
string.concat(
// "EntryPoint deployed at: ", vm.toString(address(entryPoint))
// "YourAccount deployed at: ", vm.toString(address(yourAccount)),
// "AccountFactory deployed at: ", vm.toString(address(accountFactory)),
"AccountSimpleFactory deployed at: ", vm.toString(address(accountSimpleFactory))
// "Paymaster deployed at: ", vm.toString(address(paymaster))
)
);
// /// Log deployment addresses
// console.logString(
// string.concat(
// "EntryPoint deployed at: ", vm.toString(address(entryPoint)),
// // "YourAccount deployed at: ", vm.toString(address(yourAccount)),
// // "AccountFactory deployed at: ", vm.toString(address(accountFactory)),
// "AccountSimpleFactory deployed at: ", vm.toString(address(accountSimpleFactory)),
// "Paymaster deployed at: ", vm.toString(address(paymaster))
// )
// );

/// Test.sol contract test --> to verify signing works correctly
bytes32 messageHash = keccak256(abi.encodePacked("hello"));
bytes32 ethSignedMessageHash = MessageHashUtils.toEthSignedMessageHash(messageHash);

(uint8 v, bytes32 r, bytes32 s) = vm.sign(deployerPrivateKey, ethSignedMessageHash);
bytes memory signature = abi.encodePacked(r, s, v);

Test test = new Test(signature);

console.logString(string.concat("Signer Address: ", vm.toString(vm.addr(deployerPrivateKey))));



// Stop broadcasting transactions
vm.stopBroadcast();
Expand Down
157 changes: 119 additions & 38 deletions packages/nextjs/app/localChain/page.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
"use client";

import { ethers } from "ethers";
import type { NextPage } from "next";
import { createPublicClient, encodeFunctionData, getContract, getContractAddress, http, parseUnits } from "viem";
import { foundry } from "viem/chains";
import { useAccount, useWriteContract } from "wagmi";
import { useScaffoldContract, useScaffoldReadContract, useScaffoldWriteContract } from "~~/hooks/scaffold-eth";
import { getMetadata } from "~~/utils/scaffold-eth/getMetadata";

const EP_ADDRESS = "0x5FbDB2315678afecb367f032d93F642f64180aa3";
const FACTORY_ADDRESS = "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512";
const EP_ADDRESS = "0x4826533B4897376654Bb4d4AD88B7faFD0C98528";
const FACTORY_ADDRESS = "0x0E801D84Fa97b50751Dbf25036d067dCf18858bF";
const FACTORY_NONCE = 1;
const SMART_ACCOUNT = "0xD4eF5bFBe5925B905BD3EC0921bFe28b04ac61aE";
const PM_ADDRESS = "0x8f86403A4DE0BB5791fa46B8e795C547942fE4Cf";

const LocalChain: NextPage = () => {
const { data: entryPoint } = useScaffoldContract({
contractName: "EntryPoint",
});

const { data: yourContract } = useScaffoldContract({
contractName: "YourContract",
const { writeContractAsync: handleOpsAsync } = useScaffoldWriteContract("EntryPoint");
const { writeContractAsync: depositToAsync } = useScaffoldWriteContract("EntryPoint");

const { data: accountSimple } = useScaffoldContract({
contractName: "AccountSimple",
});

const { data: accountSimpleFactory } = useScaffoldContract({
contractName: "AccountSimpleFactory",
});

const signer = useAccount();
Expand All @@ -35,54 +46,124 @@ const LocalChain: NextPage = () => {
args: [sender, BigInt(0)],
});

// const createAccountEncoded = encodeFunctionData({
// abi: accountSimpleFactory?.abi,
// functionName: "createAccount",
// args: [signer.address],
// });
let createAccountEncoded = "";

if (accountSimpleFactory) {
createAccountEncoded = encodeFunctionData({
abi: accountSimpleFactory?.abi,
functionName: "createAccount",
args: [signer.address as `0x${string}`],
});
}

let executeEncoded = "";

if (accountSimple) {
executeEncoded = encodeFunctionData({
abi: accountSimple?.abi,
functionName: "execute",
});
}

let userOp = {} as any;

// const createUserOp = async () => {
// if (executeEncoded && createAccountEncoded) {
// // const initCode = "0x";
// const initCode = accountFactory?.address + createAccountEncoded.slice(2);

// console.log("InitCode", initCode);

// // const callData = "0x";
// const callData = executeEncoded;

// const userOp = {
// sender, // smart account address
// nonce: nonceFromEP, // nonce from the entrypoint nonce manager
// initCode,
// callData,
// callGasLimit: 500_000,
// verificationGasLimit: 500_000,
// preVerificationGas: 50_000,
// maxFeePerGas: ethers.parseUnits("10", "gwei"), //parseUnits("10", 9),
// maxPriorityFeePerGas: ethers.parseUnits("5", "gwei"),
// paymasterAndData: "0x",
// signature: "0x",
// };
// setUserOp(userOp);
// console.log(userOp);
// }
if (executeEncoded && createAccountEncoded) {
/// if already SA created then use 0x
const initCode = "0x";
// const initCode = accountSimpleFactory?.address + createAccountEncoded.slice(2);

// console.log("InitCode", initCode);

// const callData = "0x";
const callData = executeEncoded;

userOp = {
sender, // smart account address
nonce: nonceFromEP, // nonce from the entrypoint nonce manager
initCode,
callData,
callGasLimit: 500_000,
verificationGasLimit: 500_000,
preVerificationGas: 50_000,
maxFeePerGas: ethers.parseUnits("10", "gwei"), //parseUnits("10", 9),
maxPriorityFeePerGas: ethers.parseUnits("5", "gwei"),
paymasterAndData: PM_ADDRESS,
signature: "0x",
};
}
// };

/// read count from account
const publicClient = createPublicClient({
chain: foundry,
transport: http(),
});

const getCount = async () => {
if (accountSimple) {
const data = await publicClient.readContract({
address: SMART_ACCOUNT,
abi: accountSimple?.abi,
functionName: "count",
});
console.log(data);
}
};

return (
<>
<div className="text-center mt-8 bg-secondary p-10">
<h1 className="text-4xl my-0">Local Chain</h1>
</div>
<button
type="button"
className="btn btn-primary"
onClick={() => {
console.log("Entrypoint address", entryPoint?.abi);
console.log("Entrypoint address", accountSimple?.abi);
console.log("Signer", signer.address);
console.log({ sender });
console.log("Sender (Smart Account created)", sender);
console.log(userOp);
}}
>
Click Me for Info
</button>
<button
className="btn btn-secondary"
onClick={async () => {
try {
console.log("Deposit To Sender", sender);
console.log("Signer", signer.address);
await depositToAsync({
functionName: "depositTo",
args: [PM_ADDRESS],
value: parseUnits("100", 18),
});
} catch (e) {
console.error("Error setting greeting:", e);
}
}}
>
Click Me
1. Deposit to Account / Paymaster
</button>
<button
className="btn btn-primary"
onClick={async () => {
try {
console.log("User Op", userOp);
console.log("Signer", signer.address);
await handleOpsAsync({
functionName: "handleOps",
args: [[userOp], signer.address],
});
} catch (e) {
console.error("Error setting greeting:", e);
}
}}
>
2. Run handleOps in the EntryPoint contract
</button>
<button className="btn btn-secondary" onClick={() => getCount()}>
3. get count of smart contract {SMART_ACCOUNT}
</button>
</>
);
Expand Down
Loading

0 comments on commit 8da38d6

Please sign in to comment.