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

Sanitize Pimlico Errors #60

Merged
merged 2 commits into from
Sep 22, 2024
Merged

Sanitize Pimlico Errors #60

merged 2 commits into from
Sep 22, 2024

Conversation

bh2smith
Copy link
Collaborator

@bh2smith bh2smith commented Sep 22, 2024

User description

Exclude API Key from errors raised by pimlico requests.


PR Type

Enhancement, Bug fix


Description

  • Renamed TransactionManager to NearSafe across multiple files.
  • Added stripApiKey function to sanitize error messages and updated error handling.
  • Introduced NearSafeConfig interface for better configuration management.
  • Updated tests to cover new functionality and renaming.

Changes walkthrough 📝

Relevant files
Enhancement
send-tx.ts
Rename `TransactionManager` to `NearSafe` and update method calls

examples/send-tx.ts

  • Renamed TransactionManager to NearSafe.
  • Updated method call from safeSufficientlyFunded to sufficientlyFunded.

  • +3/-3     
    index.ts
    Update export to `near-safe`                                                         

    src/index.ts

    • Updated export from tx-manager to near-safe.
    +1/-1     
    safe.ts
    Rename `ContractSuite` to `SafeContractSuite`                       

    src/lib/safe.ts

    • Renamed ContractSuite to SafeContractSuite.
    +3/-3     
    near-safe.ts
    Refactor TransactionManager to NearSafe with configuration interface

    src/near-safe.ts

  • Renamed TransactionManager to NearSafe.
  • Added NearSafeConfig interface.
  • Refactored create method to use NearSafeConfig.
  • Moved bundlerForChainId method to private.
  • +79/-85 
    Bug fix
    bundler.ts
    Add `stripApiKey` function and update error handling         

    src/lib/bundler.ts

  • Added stripApiKey function to sanitize error messages.
  • Updated error handling to use sanitized messages.
  • +17/-8   
    Tests
    bundler.spec.ts
    Add tests for `stripApiKey` and update error message tests

    tests/lib/bundler.spec.ts

  • Added tests for stripApiKey function.
  • Updated error message expectations.
  • +16/-4   
    safe.spec.ts
    Rename `ContractSuite` to `SafeContractSuite` in tests     

    tests/lib/safe.spec.ts

    • Renamed ContractSuite to SafeContractSuite.
    +1/-1     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @mintbase-codium-pr-agent mintbase-codium-pr-agent bot added enhancement New feature or request Bug fix labels Sep 22, 2024
    @mintbase-codium-pr-agent
    Copy link

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Error Handling
    The error handling in handleRequest function might suppress the original error details which could be useful for debugging. Consider logging the original error message before sanitizing or reformatting it for the user.

    Constructor Initialization
    In the NearSafe constructor, ensure that all properties are correctly initialized to prevent runtime errors. The safePack property initialization seems to be missing.

    API Key Sanitization
    The stripApiKey function uses a regex to sanitize the API key. Ensure that this method covers all potential formats of the API key in the error messages. Consider adding more robust tests to cover various scenarios.

    @mintbase-codium-pr-agent
    Copy link

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Add null checks to prevent potential runtime errors

    Add a check to ensure entryPoint is not null or undefined before using it in
    bundlerForChainId to prevent runtime errors.

    src/lib/safe.ts [242]

    -this.safePack.entryPoint.address,
    +this.safePack.entryPoint?.address ?? throw new Error("EntryPoint is undefined");
     
    Suggestion importance[1-10]: 10

    Why: Adding a null check for entryPoint before using it prevents potential runtime errors, which is crucial for ensuring the stability and reliability of the application.

    10
    Enhancement
    Use a specific error type for better error handling and clarity

    Refactor the error handling to use a more specific error type or to include more
    context about the failure.

    src/lib/bundler.ts [142]

    -throw new Error(`Failed to send user op with: ${message}`);
    +throw new RpcOperationError(`Failed to send user operation with: ${message}`);
     
    Suggestion importance[1-10]: 9

    Why: Using a specific error type like RpcOperationError improves error handling and provides more clarity about the nature of the error. This change enhances the robustness of the code.

    9
    Best practice
    Ensure safeSaltNonce is always defined by setting a default value

    Ensure that the safeSaltNonce is always defined by providing a default value in the
    NearSafeConfig interface.

    src/near-safe.ts [37]

    -safeSaltNonce?: string;
    +safeSaltNonce: string = "0";
     
    Suggestion importance[1-10]: 8

    Why: Providing a default value for safeSaltNonce ensures that it is always defined, which can prevent potential runtime errors. This is a good practice and enhances code reliability.

    8
    Maintainability
    Use a constant for the error message to improve maintainability

    Replace the hardcoded error message with a constant to ensure consistency and ease
    of maintenance.

    src/lib/bundler.ts [135-136]

    -throw new Error("Unauthorized request. Please check your Pimlico API key.");
    +throw new Error(ERROR_MESSAGES.UNAUTHORIZED);
     
    Suggestion importance[1-10]: 7

    Why: Using a constant for error messages improves maintainability and consistency across the codebase. However, this change is not critical and does not address a major bug or issue.

    7

    tests/lib/bundler.spec.ts Outdated Show resolved Hide resolved
    @bh2smith bh2smith merged commit b88cb18 into main Sep 22, 2024
    1 check passed
    @bh2smith bh2smith deleted the sanitize-error branch September 22, 2024 12:20
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant