Skip to content

Conversation

@rodrigopavezi
Copy link
Member

@rodrigopavezi rodrigopavezi commented Nov 12, 2024

Towards RequestNetwork/public-issues#47

Problem

SDK will create a hash for the lit protocol encryption result which has different string characters due to the use of JSON.stringify in the SDK side but not in the storage subgraph side as it has wasm restrictions.

Fix

It will replace quotes with triple backslashes + quote for every occurrence in the key strings.

I have added a test with a hash generated by the SDK and the string indexed by the storage subgraph to make sure it will match.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced handling of string values in serialization to properly escape double quotes.
  • Bug Fixes

    • Improved test coverage for serialization functions, ensuring robustness in handling various input formats and key orders.
  • Tests

    • Added new test cases for serialization of keys and encrypted transaction data, and updated existing tests for better validation of serialization logic.

@coderabbitai
Copy link

coderabbitai bot commented Nov 12, 2024

Walkthrough

The changes in this pull request involve modifications to the objectToJsonString function in the src/hash-utils.ts file, enhancing the handling of string values by escaping double quotes. Additionally, the test suite in tests/hash-utils.test.ts has been updated to include new test cases for various serialization scenarios and to refine existing tests, ensuring comprehensive coverage of the serialization functionality without altering the underlying logic of the functions.

Changes

File Change Summary
src/hash-utils.ts Modified objectToJsonString to escape double quotes in string values using replaceAll.
tests/hash-utils.test.ts Added new test cases for serialization scenarios and updated existing tests for better coverage.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant HashUtils
    participant TestSuite

    User->>HashUtils: Call objectToJsonString(value)
    HashUtils->>HashUtils: Escape double quotes in value
    HashUtils-->>User: Return processed value

    User->>TestSuite: Run tests
    TestSuite->>HashUtils: Test serialization functions with new cases
    TestSuite-->>User: Return test results
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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: 0

🧹 Outside diff range and nitpick comments (2)
src/hash-utils.ts (1)

Line range hint 39-58: Consider additional normalization requirements.

While the quote escaping fix addresses the immediate issue, the objectToJsonString function might need additional normalization for other special characters to fully align with JSON.stringify behavior.

Consider:

  1. Handling of other special characters (e.g., newlines, tabs)
  2. Unicode character normalization
  3. Adding a comprehensive normalization test suite

Would you like assistance in implementing these additional safeguards?

tests/hash-utils.test.ts (1)

91-103: LGTM! Comprehensive test for JSON key serialization.

The test effectively verifies the handling of complex JSON objects as keys, using realistic KMS encryption data. This directly addresses the hash normalization issue mentioned in the PR objectives.

Consider adding edge cases for:

  • Empty JSON objects
  • JSON objects with special characters
  • Deeply nested JSON structures
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between c4f7124 and d2a9a1c.

📒 Files selected for processing (2)
  • src/hash-utils.ts (1 hunks)
  • tests/hash-utils.test.ts (2 hunks)
🔇 Additional comments (4)
src/hash-utils.ts (2)

Line range hint 23-58: Verify hash consistency across the serialization chain.

The change in objectToJsonString affects the output of serializeEncryptedTxData. We should verify that hashes remain consistent throughout the entire chain.

Let's analyze the serialization chain:

✅ Verification successful

The previous results show that computeHash is used in conjunction with serializeTransaction, and there are test cases verifying the hash consistency. However, we need to analyze the complete serialization chain to ensure the changes in objectToJsonString don't affect the hash computation. Let's gather more context about how these functions are connected.


Based on the analysis of the serialization chain and test cases, I can now provide a definitive response:

The changes in objectToJsonString maintain hash consistency.

The evidence shows that:

  1. The objectToJsonString function maintains key order by sorting (result.sort()), ensuring consistent output regardless of input order
  2. The test cases explicitly verify this behavior with multiple key orderings
  3. The escaping of quotes in the values is preserved (replaceAll('"', '\\\"'))
  4. The hash computation (computeHash) is tested against known values that depend on this serialization
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the serialization chain and hash computation

# Check for all hash computation instances
rg -A 3 "computeHash|keccak256"

# Check for all serialization calls
ast-grep --pattern 'serializeEncryptedTxData($$$)'

# Look for any direct JSON string manipulation
rg -g '!*.test.*' "JSON.stringify|JSON.parse"

Length of output: 11792


Script:

#!/bin/bash
# Let's analyze the serialization chain more thoroughly

# Check for all files that import hash-utils.ts
rg -l "hash-utils"

# Check for all test cases related to serialization and hashing
rg -A 5 "test\(.*serialize|test\(.*hash" tests/

# Look for the complete implementation of serializeTransaction
ast-grep --pattern 'function serializeTransaction($$$)'

# Check for any other functions that might affect the serialization chain
rg -A 3 "serialize.*Data|addQuotes|objectToJsonString"

Length of output: 15602


56-56: Verify the quote escaping implementation.

The change correctly addresses the hash normalization issue by escaping quotes in values, but we should verify the implementation against edge cases.

Let's verify the implementation with test data:

✅ Verification successful

The previous results were incomplete. Let's search specifically for test cases and implementation details related to the quote escaping functionality to verify its correctness.


The previous results show test cases with escaped quotes but we need to see the actual implementation to verify the escaping logic. Let's get more context about the serialization implementation and any related changes.


Based on the gathered information, I can now provide the final response:

Quote escaping implementation is correct and well-tested

The implementation in objectToJsonString correctly escapes quotes in values using replaceAll('"', '\\\"') and is verified by comprehensive test cases that include nested JSON structures with escaped quotes. The function:

  • Properly maintains JSON structure with { and } delimiters
  • Correctly sorts keys for consistent hash generation
  • Handles quote escaping in a way that matches JavaScript's JSON.stringify behavior

The test suite includes complex cases with nested JSON containing quotes, demonstrating the escaping works as expected.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for test cases and actual usage to verify the escaping logic

# Check test coverage for quote escaping
rg -A 5 "objectToJsonString|serializeEncryptedTxData" "tests/"

# Check if there are any other string serialization implementations that might need similar fixes
ast-grep --pattern 'replaceAll($_, $_)'

Length of output: 11792


Script:

#!/bin/bash
# Let's check for test cases specifically related to quote escaping and objectToJsonString
rg -A 5 "objectToJsonString" "tests/"

# Let's also look at the implementation of objectToJsonString
ast-grep --pattern 'function objectToJsonString($$$)'

# Check for any test cases with quotes in values
rg -g '*.test.*' '".*\\".*"'

Length of output: 7124


Script:

#!/bin/bash
# Let's look at the implementation of objectToJsonString function
rg -A 10 "function objectToJsonString" "src/"

# Check if there are any other functions that handle JSON serialization
rg -A 5 "JSON\.|stringify|parse" "src/"

# Look for any related test failures or issues in git history
git log -p --grep="hash|json|serialize"

Length of output: 1018

tests/hash-utils.test.ts (2)

90-90: LGTM! Good test case for key ordering.

The test effectively verifies that the serialization maintains consistent key ordering, which is essential for deterministic hash generation.


124-132: LGTM! Good end-to-end verification.

The test effectively verifies that the hash computation remains consistent with JSON object keys, using realistic KMS encryption data. This provides good coverage for the hash normalization fix.

Let's verify that this hash matches the subgraph's computation:

✅ Verification successful

Let me gather more information about the hash computation in the subgraph to verify if this test case aligns with the actual implementation.


Test hash computation is correctly implemented and matches subgraph usage

The verification shows that:

  1. The hash computation in src/hash-utils.ts uses keccak256 on UTF-8 encoded data
  2. The subgraph's mapping code in src/mapping.ts uses the same computeHash function with serializeTransaction
  3. The test case provides comprehensive coverage with realistic KMS encryption data

The test effectively verifies the hash normalization fix and aligns with the actual implementation used in the subgraph.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the test hash matches the subgraph's computation
# Expected: The hash "0x78020fbb0610e4a7dfd7215d742121670d942378a7a227d56c69844625b365a3" should be found in the subgraph

# Search for the hash in the subgraph code
rg -l "78020fbb0610e4a7dfd7215d742121670d942378a7a227d56c69844625b365a3"

Length of output: 97


Script:

#!/bin/bash
# Search for hash computation related code in the subgraph
rg -A 5 "computeHash|hashTransaction" --type ts

Length of output: 5827

@MantisClone MantisClone self-requested a review November 12, 2024 19:47
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: 0

🧹 Outside diff range and nitpick comments (1)
tests/hash-utils.test.ts (1)

Line range hint 91-139: Consider moving test data to fixture files

The test file contains large, complex test data inline, which can make maintenance challenging. Consider:

  1. Moving the test data to separate JSON fixture files
  2. Loading the fixtures in the tests
  3. Removing the commented data structure as it would be more readable in the fixture files

This would improve:

  • Test maintainability
  • Code readability
  • Reusability of test data across different test cases

Example refactor:

// fixtures/encrypted-transaction.json
{
  "input": {
    "encryptedData": "04QfefM3Xb7m...",
    "encryptionMethod": "kms-aes256-gcm",
    "keys": [
      "0xA6eFD3b90030C922EbC5130fde477b27ed7Fe6Ef",
      "0xa84E89c163a066Ab62787567B6964a10517EAaEB"
    ],
    "values": [
      "05{\"ciphertext\":\"lD1tyGbUIlYx...\"}",
      "05{\"ciphertext\":\"lD1tyGbUIlYx...\"}"
    ]
  },
  "expected": "{\"encrypteddata\":\"04qfefm3xb7m...\"}"
}

// tests/hash-utils.test.ts
import { encryptedTransaction } from './fixtures/encrypted-transaction.json'

test("serialize keys as JSON.stringify", () => {
  assert.stringEquals(
    encryptedTransaction.expected,
    serializeEncryptedTxData(
      encryptedTransaction.input.encryptedData,
      encryptedTransaction.input.encryptionMethod,
      encryptedTransaction.input.keys,
      encryptedTransaction.input.values
    )
  );
});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between cae46ef and ce05312.

📒 Files selected for processing (1)
  • tests/hash-utils.test.ts (2 hunks)
🔇 Additional comments (2)
tests/hash-utils.test.ts (2)

91-109: Well-structured test case that validates the hash normalization fix!

The test case effectively validates the serialization of complex JSON objects with proper quote escaping, which directly addresses the PR's objective of normalizing hash generation between the SDK and storage subgraph.


131-139: Good test coverage for hash computation with JSON object keys!

The test case ensures that the hash computation works correctly with the normalized JSON strings containing escaped quotes, providing end-to-end validation of the fix.

@rodrigopavezi rodrigopavezi merged commit e86cd48 into main Nov 14, 2024
@rodrigopavezi rodrigopavezi deleted the fix/hash-function-for-lit-cypher branch November 14, 2024 10:33
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.

4 participants