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(libp2p): multi signature schemes & session signing #2599

Merged
merged 6 commits into from
Oct 31, 2024

Conversation

Larkooo
Copy link
Collaborator

@Larkooo Larkooo commented Oct 31, 2024

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a new ProviderError variant for enhanced error handling.
    • Added a validate_signature function for improved signature verification across different types.
    • Introduced a new Signature enum for better signature representation in messages.
  • Bug Fixes

    • Enhanced error handling and logging in the signature verification process.
  • Tests

    • Expanded test coverage for type parsing and mapping, ensuring robust handling of primitive and complex types.

Copy link

coderabbitai bot commented Oct 31, 2024

Walkthrough

Ohayo, sensei! The changes in this pull request introduce several enhancements across multiple files in the crates/torii/libp2p module. A new error variant ProviderError is added to the Error enum, improving error handling. The mod.rs file sees the addition of a validate_signature function, streamlining signature verification in the run method of the Relay struct. The test suite is updated to enhance type parsing and mapping tests. Lastly, a new data structure Signature is introduced, refining how signatures are represented in messages.

Changes

File Path Change Summary
crates/torii/libp2p/src/errors.rs Added enum variant: ProviderError(#[from] ProviderError) in Error enum.
crates/torii/libp2p/src/server/mod.rs Added method: async fn validate_signature(...); refactored run method for signature verification.
crates/torii/libp2p/src/tests.rs Updated tests for type parsing and mapping; included Signature type in message publishing.
crates/torii/libp2p/src/types.rs Added enum: Signature; updated signature field in Message from Vec<Felt> to Signature.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Relay
    participant Provider
    Client->>Relay: Send message with signature
    Relay->>Relay: Call validate_signature
    Relay->>Provider: Validate signature
    Provider-->>Relay: Return validation result
    Relay-->>Client: Return response based on validation
Loading

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (2)
crates/torii/libp2p/src/types.rs (1)

12-17: Consider adding documentation for signature variants, sensei!

While the enum structure is solid, adding documentation would help explain:

  • The purpose and use case for each signature variant
  • The expected format and constraints of each variant's data
  • The relationship between TypedDataHash and Felt vectors in the Session variant

Example documentation:

/// Represents different signature schemes supported by the system
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Signature {
    /// WebAuthn signatures with variable length
    Webauthn(Vec<Felt>),
    /// Standard Starknet ECDSA signature (r, s)
    Starknet((Felt, Felt)),
    /// Session-based signature combining typed data hashes and signature components
    Session((Vec<TypedDataHash>, Vec<Felt>)),
}
crates/torii/libp2p/src/tests.rs (1)

686-686: Ohayo, sensei! Consider adding tests for Signature handling.

To ensure robustness, it would be beneficial to include unit tests that specifically verify the handling of different Signature variants in the publish method.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 126292e and af002f8.

📒 Files selected for processing (4)
  • crates/torii/libp2p/src/errors.rs (2 hunks)
  • crates/torii/libp2p/src/server/mod.rs (3 hunks)
  • crates/torii/libp2p/src/tests.rs (2 hunks)
  • crates/torii/libp2p/src/types.rs (1 hunks)
🔇 Additional comments (8)
crates/torii/libp2p/src/types.rs (2)

6-10: Ohayo! Clean and well-structured TypedDataHash implementation.

The struct is properly designed with clear field names and appropriate derive macros.


22-22: Verify message handlers for signature type change.

The signature field type change from Vec<Felt> to Signature is a breaking change. Let's verify that all message handlers are updated accordingly.

✅ Verification successful

Let me gather more information about the signature type changes.

The results will help us verify if all message handlers are properly updated to handle both Starknet and Ethereum signature types.


Let me check one more aspect of the message handling to ensure complete verification.


Ohayo sensei! After analyzing the codebase thoroughly, I can now provide a conclusive response about the signature type change.

Signature type change is properly handled across the codebase

The change from Vec<Felt> to Signature enum is well-implemented with proper handling:

  • All message handlers in libp2p/src/server/mod.rs correctly handle all three signature variants (Webauthn, Starknet, Session)
  • Test cases in libp2p/src/tests.rs demonstrate correct usage of the new signature type
  • No serialization issues found as the Message struct is only used within the libp2p crate
  • The signature conversion logic is properly implemented for each variant
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for potential message handling code that might need updates
echo "Searching for Message struct usage..."
rg -l "Message" --type rust

echo "Searching for signature field access patterns..."
rg "message.*signature" --type rust -A 2

Length of output: 2624


Script:

#!/bin/bash
# Check the old signature type definition
echo "Checking old signature type in git history..."
git log -p -S "Vec<Felt>" --type rust -- crates/torii/libp2p/src/types.rs

# Look for Signature enum definition and usage
echo "Looking for Signature enum definition..."
rg -p "enum Signature" --type rust -A 5

# Check for any other signature-related type handling
echo "Checking signature-related type handling..."
ast-grep --pattern 'fn $_($_: $$$) -> $_ {
  $$$
  signature
  $$$
}'

Length of output: 679


Script:

#!/bin/bash
# Check message handling implementations
echo "Checking message handling implementations..."
rg -p "impl.*Message" --type rust -A 10

# Look for signature type conversions
echo "Looking for signature conversions..."
rg "Signature::(Webauthn|Starknet|Session)" --type rust -A 2

# Check for message serialization
echo "Checking message serialization code..."
rg "serialize.*Message" --type rust -A 5

Length of output: 9817

crates/torii/libp2p/src/errors.rs (2)

7-7: Ohayo! Clean import addition, sensei!

The import statement is well-placed and follows the existing import organization pattern.


49-51: LGTM! Error variant follows best practices!

The new ProviderError variant maintains consistency with existing error handling patterns by using #[error(transparent)] and deriving From via #[from].

crates/torii/libp2p/src/server/mod.rs (2)

41-41: Ohayo sensei! Importing Signature and TypedDataHash looks good.

The added import statements are necessary for the new signature handling logic. Well done!


413-475: Ohayo sensei! The validate_signature function enhances modularity.

The addition of the validate_signature function encapsulates the signature verification logic, improving code organization and maintainability. Great work!

crates/torii/libp2p/src/tests.rs (2)

548-548: Ohayo, sensei! The Signature import is properly added.

The import statement correctly includes Signature from crate::types, which is necessary for the updated Message struct.


686-686: Ohayo, sensei! The usage of Signature::Starknet in publish is correct.

The publish method now sends a Message with the Signature::Starknet variant, correctly passing the (r, s) tuple. This aligns with the changes to the Signature enum.

crates/torii/libp2p/src/server/mod.rs Outdated Show resolved Hide resolved
crates/torii/libp2p/src/server/mod.rs Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
crates/torii/libp2p/src/server/mod.rs (1)

413-477: Consider adding unit tests for signature validation.

The new validate_signature function handles critical security functionality but lacks test coverage. Consider adding tests for:

  • Valid/invalid signatures for each signature type
  • Error cases (provider errors, encoding errors)
  • Edge cases (zero values, empty signatures)

Would you like me to help generate comprehensive test cases for this function?

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between af002f8 and 8570092.

📒 Files selected for processing (3)
  • crates/torii/libp2p/src/server/mod.rs (3 hunks)
  • crates/torii/libp2p/src/tests.rs (2 hunks)
  • crates/torii/libp2p/src/types.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/torii/libp2p/src/tests.rs
  • crates/torii/libp2p/src/types.rs
🔇 Additional comments (2)
crates/torii/libp2p/src/server/mod.rs (2)

40-41: Ohayo sensei! The new imports look good.

The imports are well-organized and necessary for the new signature validation functionality.


305-313: Previous review comment is still applicable.

crates/torii/libp2p/src/server/mod.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@glihm glihm left a comment

Choose a reason for hiding this comment

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

Nice to support the multiple signature verification.

Tests are running forever, will restart them to see if related.

Comment on lines +305 to +311
if !match validate_signature(
&self.provider,
entity_identity,
&data.message,
&data.signature,
)
.await
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since the function returns a bool, the construction of this and arms below may be much simpler:

if !validate_signature(...)? {
    error!(..);
    continue
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this wouldn't work though since we don't want to propagate the error. we should just log it and not exit. since we're taking in user input

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8570092 and d6ad202.

📒 Files selected for processing (3)
  • crates/torii/libp2p/src/server/mod.rs (3 hunks)
  • crates/torii/libp2p/src/tests.rs (2 hunks)
  • crates/torii/libp2p/src/types.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/torii/libp2p/src/tests.rs
  • crates/torii/libp2p/src/types.rs
🔇 Additional comments (2)
crates/torii/libp2p/src/server/mod.rs (2)

40-41: Ohayo sensei! Clean import additions.

The new imports for TypedData and Signature types are well-organized and properly scoped.


451-452: ⚠️ Potential issue

Ohayo sensei! Fix typo in function name.

The selector name contains a typo: "is_session_sigature_valid" should be "is_session_signature_valid".

-entry_point_selector: get_selector_from_name("is_session_sigature_valid")
+entry_point_selector: get_selector_from_name("is_session_signature_valid")

Likely invalid or redundant comment.

crates/torii/libp2p/src/server/mod.rs Show resolved Hide resolved
crates/torii/libp2p/src/server/mod.rs Show resolved Hide resolved
crates/torii/libp2p/src/server/mod.rs Show resolved Hide resolved
Copy link

codecov bot commented Oct 31, 2024

Codecov Report

Attention: Patch coverage is 63.33333% with 22 lines in your changes missing coverage. Please review.

Project coverage is 55.11%. Comparing base (126292e) to head (d6ad202).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
crates/torii/libp2p/src/server/mod.rs 59.25% 22 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2599      +/-   ##
==========================================
- Coverage   55.13%   55.11%   -0.03%     
==========================================
  Files         385      385              
  Lines       47748    47775      +27     
==========================================
+ Hits        26328    26331       +3     
- Misses      21420    21444      +24     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@glihm glihm merged commit 66b76e6 into dojoengine:main Oct 31, 2024
13 of 14 checks passed
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