Skip to content

Fix: Ensure PUBLIC_ADDRESS and PRIVATE_KEY are resolved from the same source in simulate-with-tenderly #17

@coderabbitai

Description

@coderabbitai

Summary

In simulate-with-tenderly/simulate-with-tenderly.ts, PUBLIC_ADDRESS and PRIVATE_KEY environment variables are currently resolved independently. This can result in a mismatch where an env-specified address is paired with a generated private key, or vice versa — causing the simulated UserOperation to be signed by a different owner than the one used to initialize the account, making it invalid.

Steps to Reproduce

  1. Set only PUBLIC_ADDRESS in the environment (without PRIVATE_KEY).
  2. Run the simulate-with-tenderly example.
  3. The account is initialized with the env address, but the UserOperation is signed with a freshly generated private key — the simulation will fail or produce an invalid result.

Expected Behavior

ownerPublicAddress and ownerPrivateKey must always come from the same source (either both from environment variables, or both from a freshly generated key pair). If only one of the two env vars is provided, an error should be thrown.

Suggested Fix

const ownerPrivateKeyGenerated = generatePrivateKey();
const owner = privateKeyToAccount(ownerPrivateKeyGenerated);
const envPublicAddress = process.env.PUBLIC_ADDRESS;
const envPrivateKey = process.env.PRIVATE_KEY;

if ((envPublicAddress && !envPrivateKey) || (!envPublicAddress && envPrivateKey)) {
    throw new Error("PUBLIC_ADDRESS and PRIVATE_KEY must be provided together");
}

const ownerPublicAddress = envPublicAddress ?? owner.address;
const ownerPrivateKey = envPrivateKey ?? ownerPrivateKeyGenerated;

References

Requested by @Sednaoui

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions