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

🔨 make BaseScript more generic/powerful #8

Merged
merged 1 commit into from
Jan 10, 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
22 changes: 17 additions & 5 deletions script/AccountFactory/CreateAccount.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ import { AccountFactory } from "src/AccountFactory.sol";
import { BaseScript } from "../Base.s.sol";

/// @title Create an Account using an already deployed AccountFactory
/**
* @notice
* forge script CreateAccount --sig "run(address,bytes32)" <...args> <...flags>
*/
/// @dev If you need to deploy an AccountFactory, use the Deploy script in this directory
contract CreateAccount is BaseScript {
function run(address factoryAddress, bytes32 loginHash) public broadcaster returns (address) {
function run(address factoryAddress, bytes32 loginHash) public broadcast returns (address) {
AccountFactory factory = AccountFactory(factoryAddress);
return factory.createAccount(loginHash);
}
}

/*

ℹ️ HOW TO USE THIS SCRIPT USING A LEDGER:
forge script CreateAccount --rpc-url <RPC_URL> --ledger --sender <ACCOUNT_ADDRESS> [--broadcast]


ℹ️ HOW TO USE THIS SCRIPT WITH AN ARBITRARY PRIVATE KEY (NOT RECOMMENDED):
PRIVATE_KEY=<PRIVATE_KEY> forge script CreateAccount --rpc-url <RPC_URL> [--broadcast]


ℹ️ HOW TO USE THIS SCRIPT ON ANVIL IN DEFAULT MODE:
forge script CreateAccount --rpc-url http://127.0.0.1:8545 --broadcast --sender \
0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --mnemonics "test test test test test test test test test test test junk"

*/
23 changes: 17 additions & 6 deletions script/AccountFactory/CreateAndInitAccount.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import { AccountFactory } from "src/AccountFactory.sol";
import { BaseScript } from "../Base.s.sol";

/// @title Create an Account using an already deployed AccountFactory and init it
/**
* @notice
* forge script CreateAndInitAccount --sig run(address,uint256,uint256,bytes32,bytes,bytes)"
* " <...args> <...flags>
*/
/// @dev If you need to deploy an AccountFactory, use the Deploy script in this directory
contract CreateAndInitAccount is BaseScript {
function run(
Expand All @@ -21,10 +16,26 @@ contract CreateAndInitAccount is BaseScript {
bytes calldata nameServiceSignature // ℹ️ must be made by the nameServiceOwner of the AccountFactory
)
public
broadcaster
broadcast
returns (address)
{
AccountFactory factory = AccountFactory(factoryAddress);
return factory.createAndInitAccount(pubKeyX, pubKeyY, loginHash, credId, nameServiceSignature);
}
}

/*

ℹ️ HOW TO USE THIS SCRIPT USING A LEDGER:
forge script CreateAndInitAccount --rpc-url <RPC_URL> --ledger --sender <ACCOUNT_ADDRESS> [--broadcast]


ℹ️ HOW TO USE THIS SCRIPT WITH AN ARBITRARY PRIVATE KEY (NOT RECOMMENDED):
PRIVATE_KEY=<PRIVATE_KEY> forge script CreateAndInitAccount --rpc-url <RPC_URL> [--broadcast]


ℹ️ HOW TO USE THIS SCRIPT ON ANVIL IN DEFAULT MODE:
forge script CreateAndInitAccount --rpc-url http://127.0.0.1:8545 --broadcast --sender \
0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --mnemonics "test test test test test test test test test test test junk"

*/
19 changes: 17 additions & 2 deletions script/AccountFactory/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { AccountFactory } from "src/AccountFactory.sol";
import { BaseScript } from "../Base.s.sol";

/// @title Deploy an AccountFactory
/// @notice <...envs> forge script AccountFactoryDeploy
/// @dev You can pass environment variables to this script to tailor the deployment.
/// Do not deploy this script in production without changing the default values!
contract AccountFactoryDeploy is BaseScript {
Expand All @@ -14,11 +13,27 @@ contract AccountFactoryDeploy is BaseScript {
// This is the first account exposed by Anvil
address internal constant NAME_SERVICE_OWNER = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266;

function run() public broadcaster returns (AccountFactory) {
function run() public broadcast returns (AccountFactory) {
address entrypoint = vm.envOr("ENTRYPOINT", ENTRYPOINT);
address webAuthnVerifier = vm.envOr("WEBAUTHN_VERIFIER", address(0));
address nameServiceOwner = vm.envOr("NAME_SERVICE_OWNER", NAME_SERVICE_OWNER);

return new AccountFactory(entrypoint, webAuthnVerifier, nameServiceOwner);
}
}

/*

ℹ️ HOW TO USE THIS SCRIPT USING A LEDGER:
forge script AccountFactoryDeploy --rpc-url <RPC_URL> --ledger --sender <ACCOUNT_ADDRESS> [--broadcast]


ℹ️ HOW TO USE THIS SCRIPT WITH AN ARBITRARY PRIVATE KEY (NOT RECOMMENDED):
PRIVATE_KEY=<PRIVATE_KEY> forge script AccountFactoryDeploy --rpc-url <RPC_URL> [--broadcast]


ℹ️ HOW TO USE THIS SCRIPT ON ANVIL IN DEFAULT MODE:
forge script AccountFactoryDeploy --rpc-url http://127.0.0.1:8545 --broadcast --sender \
0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --mnemonics "test test test test test test test test test test test junk"

*/
25 changes: 7 additions & 18 deletions script/Base.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,15 @@ pragma solidity >=0.8.19 <0.9.0;
import { Script } from "forge-std/Script.sol";

abstract contract BaseScript is Script {
/// @dev Included to enable compilation of the script without a $MNEMONIC environment variable.
string internal constant TEST_MNEMONIC = "test test test test test test test test test test test junk";
/// @notice this modifier can be used as a generic broadcast solution. It will automatically either:
/// - use the private key provided as an environment variable to broadcast
/// - or starts the hardware wallet flow if the correct flags are provided and the env variable is not set
modifier broadcast() {
uint256 privateKey = vm.envOr("PRIVATE_KEY", uint256(0));
privateKey != 0 ? vm.startBroadcast(privateKey) : vm.startBroadcast();

/// @dev Needed for the deterministic deployments.
bytes32 internal constant ZERO_SALT = bytes32(0);

/// @dev The address of the contract deployer.
address internal deployer;

/// @dev Used to derive the deployer's address.
string internal mnemonic;

constructor() {
mnemonic = vm.envOr("MNEMONIC", TEST_MNEMONIC);
(deployer,) = deriveRememberKey({ mnemonic: mnemonic, index: 0 });
}

modifier broadcaster() {
vm.startBroadcast(deployer);
_;

vm.stopBroadcast();
}
}
Loading