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

feat: add approved session keys to registry script #21

Merged
merged 7 commits into from
Mar 5, 2025
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
129 changes: 129 additions & 0 deletions deploy/add-approved-session-keys-to-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import * as hre from "hardhat";
import { Contract } from "zksync-ethers";
import { getWallet } from "../deploy/utils";
import {
PolicyConfig,
PolicyType,
sessionKeysToApprove,
Status,
} from "./const/session-key-configs";

export default async function (): Promise<void> {
// Get the wallet for signing transactions
const wallet = getWallet(hre);

// Use the default registry contract address from create2 or override if one is provided via CLI args
const DEFAULT_REGISTRY_ADDRESS = "0x7dA0211D162a26839Bbb8232Da13fbE068440d95";
const registryAddress =
process.argv.slice(2).indexOf("--registry-address") >= 0
? process.argv[process.argv.slice(2).indexOf("--registry-address") + 3]
: DEFAULT_REGISTRY_ADDRESS;

console.log(
`Using SessionKeyPolicyRegistry at address: ${registryAddress}\n`
);

// Connect to the registry contract
const artifact = await hre.zksyncEthers.loadArtifact(
"SessionKeyPolicyRegistry"
);
const registry = new Contract(registryAddress, artifact.abi, wallet);

// Check if the wallet has the MANAGER_ROLE to add policies to the registry
const MANAGER_ROLE = await registry.MANAGER_ROLE();
const hasRole = await registry.hasRole(MANAGER_ROLE, wallet.address);
if (!hasRole) {
console.error(
`Wallet ${wallet.address} does not have the MANAGER_ROLE. Cannot add session keys.`
);
return;
}

// Helper function to process a policy configuration
async function processPolicyConfig(config: PolicyConfig): Promise<void> {
let currentStatus;
let tx;
let description = "";

switch (config.type) {
case PolicyType.Call:
description = `Call policy for target: ${config.target}, selector: ${config.selector}`;
currentStatus = await registry.getCallPolicyStatus(
config.target,
config.selector
);

if (currentStatus === config.status) {
console.warn(
`${description} already set to ${Status[config.status]}. Skipping.`
);
return;
}

tx = await registry.setCallPolicyStatus(
config.target,
config.selector,
config.status
);
break;

case PolicyType.Transfer:
description = `Transfer policy for target: ${config.target}`;
currentStatus = await registry.getTransferPolicyStatus(config.target);

if (currentStatus === config.status) {
console.warn(
`${description} already set to ${Status[config.status]}. Skipping.`
);
return;
}

tx = await registry.setTransferPolicyStatus(
config.target,
config.status
);
break;

case PolicyType.ApprovalTarget:
description = `Approval Target policy for token: ${config.token}, target: ${config.target}`;
currentStatus = await registry.getApprovalTargetStatus(
config.token,
config.target
);

if (currentStatus === config.status) {
console.warn(
`${description} already set to ${Status[config.status]}. Skipping.`
);
return;
}

tx = await registry.setApprovalTargetStatus(
config.token,
config.target,
config.status
);
break;

default:
console.error(
`Unknown policy type: ${(config as any).type}. Skipping.`
);
return;
}

await tx.wait();
console.log(
`Added ${description}` + `\n` + `Transaction hash: ${tx.hash}` + `\n`
);
}

// Process each session key configuration
for (const config of sessionKeysToApprove) {
await processPolicyConfig(config);
}

console.log(
`Successfully added ${sessionKeysToApprove.length} session key configurations to the registry.`
);
}
Loading
Loading