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

refactor: zkstack based plugins #1774

Closed
wants to merge 12 commits into from

Conversation

aalimsahin
Copy link
Contributor

Relates to:

N/A (Improving zkstack plugin architecture and consistency)

Risks

Low. The changes involve refactoring and updating multiple plugins (plugin-abstract, plugin-zksync-era, plugin-cronoszkevm) to use viem for consistency, improving environment validation and fixing outdated codes.

Background

What does this PR do?

Environment Validation for plugin-abstract, plugin-cronoszkevm, plugin-zksync-era.
Integrated viem for functionality in plugin-cronoszkevm, plugin-zksync-era for maintaince.
Fixed issues in plugin-zksync-era and plugin-cronoszkevm where previous changes caused them to break.

What kind of change is this?

Fixed cronoszkevm and zksync era plugins, moved them to viem and some refactors

Bug fixes and Improvements

Documentation changes needed?

As I check, no

Testing

Where should a reviewer start?

Almost all plugins are the same, so you can choose one. After that, others will be the same.

Detailed testing steps

Initialize plugins in character.
You can choose to test one of them.
Add envs for the plugin. Try to wrong one to test first.
Start one client to check it. You can say to agent like send x amount eth to y address in z chain

Screenshot 2025-01-03 at 21 33 42 Screenshot 2025-01-03 at 21 30 20

Discord username

aalimsahin

Comment on lines 168 to 173
content.tokenAddress.toLowerCase() !== ETH_ADDRESS.toLowerCase()
) {
// Convert amount to proper token decimals
const tokenInfo =
ERC20_OVERRIDE_INFO[content.tokenAddress.toLowerCase()];
const decimals = tokenInfo?.decimals ?? 18; // Default to 18 decimals if not specified

Choose a reason for hiding this comment

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

@aalimsahin The handling of token amounts should include error handling for invalid inputs. Ensure that 'content.amount' is a valid number before proceeding with calculations to avoid runtime errors.

Comment on lines +9 to +10
.refine((address) => isAddress(address, { strict: false }), {
message: "Abstract address must be a valid address",

Choose a reason for hiding this comment

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

@aalimsahin The use of 'isAddress' from 'viem' should be reviewed for security. Ensure that the address validation logic is robust against potential attacks, such as address spoofing. Consider adding additional checks or using a well-established library for address validation.

Comment on lines +15 to +17
.refine((key) => /^[a-fA-F0-9]{64}$/.test(key), {
message:
"Abstract private key must be a 64-character hexadecimal string (32 bytes) without the '0x' prefix",

Choose a reason for hiding this comment

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

@aalimsahin The regex used for validating the private key may not cover all edge cases. Ensure that it strictly enforces the expected format and consider using a more comprehensive validation method. For example, you could use a library designed for cryptographic key validation.

Comment on lines 158 to 159
const PRIVATE_KEY = runtime.getSetting("CRONOSZKEVM_PRIVATE_KEY");
const account = privateKeyToAccount(`0x${PRIVATE_KEY}`);

Choose a reason for hiding this comment

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

@aalimsahin The PRIVATE_KEY should be handled securely. Avoid exposing sensitive information in logs or error messages. Consider using environment variables or secure vaults for sensitive data.

// Check if the token is native
if (
content.tokenAddress.toLowerCase() !==
ZKCRO_ADDRESS.toLowerCase()

Choose a reason for hiding this comment

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

@aalimsahin Ensure that the ZKCRO_ADDRESS is not hardcoded. This can lead to issues if the address changes. Consider fetching it from a configuration file or environment variable.

// Convert amount to proper token decimals
const tokenInfo =
ERC20_OVERRIDE_INFO[content.tokenAddress.toLowerCase()];
const decimals = tokenInfo?.decimals ?? 18; // Default to 18 decimals if not specified

Choose a reason for hiding this comment

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

@aalimsahin The default decimal value of 18 may not be appropriate for all tokens. Ensure that the token's actual decimal value is fetched or validated to prevent potential transfer errors.

Comment on lines +9 to +11
.refine((address) => isAddress(address, { strict: false }), {
message: "Cronos zkEVM address must be a valid address",
}),

Choose a reason for hiding this comment

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

@aalimsahin The use of 'isAddress' from 'viem' should be validated for security. Ensure that the library is well-maintained and does not have known vulnerabilities. Consider adding unit tests to verify the behavior of this validation.

Comment on lines +15 to +17
.refine((key) => /^[a-fA-F0-9]{64}$/.test(key), {
message:
"Cronos zkEVM private key must be a 64-character hexadecimal string (32 bytes) without the '0x' prefix",

Choose a reason for hiding this comment

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

@aalimsahin The regex used for validating the private key should be reviewed for potential edge cases. Ensure that it strictly enforces the expected format and consider using a more robust validation library if necessary.

const tokenInfo =
ERC20_OVERRIDE_INFO[content.tokenAddress.toLowerCase()];
const decimals = tokenInfo?.decimals ?? 18; // Default to 18 decimals if not specified
const tokenAmount = parseUnits(

Choose a reason for hiding this comment

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

@aalimsahin Ensure that the token amount conversion handles potential overflows or underflows. Use libraries like 'bignumber.js' for safe arithmetic operations.

Comment on lines 5 to 19
export const zksyncEnvSchema = z.object({
ZKSYNC_ADDRESS: z.string().min(1, "ZKsync address is required"),
ZKSYNC_PRIVATE_KEY: z.string().min(1, "ZKsync private key is required"),
ZKSYNC_ADDRESS: z
.string()
.min(1, "ZKsync Era address is required")
.refine((address) => isAddress(address, { strict: false }), {
message: "ZKsync Era address must be a valid address",
}),
ZKSYNC_PRIVATE_KEY: z
.string()
.min(1, "ZKsync Era private key is required")
.refine((key) => /^[a-fA-F0-9]{64}$/.test(key), {
message:
"ZKsync Era private key must be a 64-character hexadecimal string (32 bytes) without the '0x' prefix",
}),
});

Choose a reason for hiding this comment

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

@aalimsahin The validation for the private key should also include a check for its length to ensure it is exactly 64 characters. This is crucial for security to prevent invalid keys from being accepted.

@aalimsahin aalimsahin closed this Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants