Skip to content

Conversation

@BigtoC
Copy link
Owner

@BigtoC BigtoC commented Jul 20, 2025

Summary by CodeRabbit

  • New Features
    • Added support for converting Ethereum addresses between hexadecimal and Bech32 formats.
    • Sending tokens now automatically converts Ethereum-style addresses to Bech32 format when needed.

@BigtoC BigtoC self-assigned this Jul 20, 2025
@coderabbitai
Copy link

coderabbitai bot commented Jul 20, 2025

Walkthrough

A new utility class for converting Ethereum addresses between hexadecimal and Bech32 formats was added. The token sending logic was updated to automatically convert receiver addresses from hex to Bech32 if needed, using this new utility. Supporting imports for the converter and chain prefix were introduced.

Changes

File(s) Change Summary
lib/application/wallet/utils.dart Added EthBech32Converter class with static methods for Ethereum hex/Bech32 address conversion.
lib/application/wallet/wallet_bloc.dart Updated sendToken to convert hex addresses to Bech32; added imports for converter and prefix.

Poem

A hex to Bech32 hop,
Now tokens know where to stop!
With bytes and prefixes in the breeze,
Addresses transform with ease.
Rabbits cheer, their tails a-twitch—
Conversion magic, code made rich!
🐇✨


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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: 3

🧹 Nitpick comments (4)
lib/application/wallet/utils.dart (3)

21-21: Add type annotation for the prefix parameter.

The prefix parameter is missing a type annotation, which reduces code clarity and IDE support.

-  static String ethAddressToBech32(String ethAddress, prefix) {
+  static String ethAddressToBech32(String ethAddress, String prefix) {

43-43: Add type annotation and improve documentation.

The prefix parameter is missing both type annotation and documentation.

-  /// Parameters:
-  ///   - bech32Address: The Bech32-encoded Ethereum address.
-  ///
+  /// Parameters:
+  ///   - bech32Address: The Bech32-encoded Ethereum address.
+  ///   - prefix: The Bech32 prefix used for decoding.
+  ///
-  static String bech32ToEthAddress(String bech32Address, prefix) {
+  static String bech32ToEthAddress(String bech32Address, String prefix) {

7-52: Consider adding input validation and error handling.

The class lacks comprehensive input validation for edge cases such as null inputs, empty strings, or malformed addresses. Consider adding validation at the beginning of each method.

Example for enhanced validation:

  static String ethAddressToBech32(String ethAddress, String prefix) {
+    if (ethAddress.isEmpty || prefix.isEmpty) {
+      throw ArgumentError("Address and prefix cannot be empty");
+    }
+    
    final cleanHex = AddrDecUtils.validateAndRemovePrefix(
        ethAddress, CoinsConf.ethereum.params.addrPrefix!
    );
    // ... rest of method
  }
lib/application/wallet/wallet_bloc.dart (1)

45-50: Consider using a local variable instead of mutating the parameter.

Mutating the receiver parameter can be confusing and makes the code harder to debug. Consider using a local variable for clarity.

-    if (receiver.startsWith("0x")) {
-      receiver = EthBech32Converter.ethAddressToBech32(
-          receiver,
-          chainAddressPrefix
-      );
-    }
+    String processedReceiver = receiver;
+    if (receiver.startsWith("0x")) {
+      try {
+        processedReceiver = EthBech32Converter.ethAddressToBech32(
+            receiver,
+            chainAddressPrefix
+        );
+      } catch (e) {
+        throw ArgumentError("Failed to convert Ethereum address: $e");
+      }
+    }

     final message = MsgSend(
         fromAddress: CosmosBaseAddress(sender),
-        toAddress: CosmosBaseAddress(receiver),
+        toAddress: CosmosBaseAddress(processedReceiver),
         amount: [Coin(denom: denom, amount: amount)]
     );
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 349bd11 and c35a4e3.

📒 Files selected for processing (2)
  • lib/application/wallet/utils.dart (1 hunks)
  • lib/application/wallet/wallet_bloc.dart (2 hunks)
🔇 Additional comments (3)
lib/application/wallet/utils.dart (1)

1-6: LGTM: Well-organized imports.

The imports are properly organized and include all necessary dependencies for Bech32 encoding, Ethereum address handling, and hex conversion.

lib/application/wallet/wallet_bloc.dart (2)

2-2: LGTM: Proper imports for new functionality.

The imports correctly include the new utility class and constants needed for address conversion.

Also applies to: 4-4


45-50: chainAddressPrefix Verified and Imported Correctly
The chainAddressPrefix constant is defined in lib/infrastructure/constants.dart ("mantra") and properly imported into lib/application/wallet/wallet_bloc.dart. No further changes needed.

Comment on lines +46 to +49
assert(
hexEncoded.length == EthAddrConst.addrLen,
"Invalid Ethereum address length: ${hexEncoded.length}, expected: ${EthAddrConst.addrLen}"
);
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Replace assertion with proper exception handling.

Similar to the previous method, use proper exception handling instead of assertions for production reliability.

-    assert(
-    hexEncoded.length == EthAddrConst.addrLen,
-    "Invalid Ethereum address length: ${hexEncoded.length}, expected: ${EthAddrConst.addrLen}"
-    );
+    if (hexEncoded.length != EthAddrConst.addrLen) {
+      throw ArgumentError(
+        "Invalid Ethereum address length: ${hexEncoded.length}, expected: ${EthAddrConst.addrLen}"
+      );
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
assert(
hexEncoded.length == EthAddrConst.addrLen,
"Invalid Ethereum address length: ${hexEncoded.length}, expected: ${EthAddrConst.addrLen}"
);
if (hexEncoded.length != EthAddrConst.addrLen) {
throw ArgumentError(
"Invalid Ethereum address length: ${hexEncoded.length}, expected: ${EthAddrConst.addrLen}"
);
}
🤖 Prompt for AI Agents
In lib/application/wallet/utils.dart around lines 46 to 49, replace the assert
statement that checks the Ethereum address length with proper exception handling
by throwing an exception when the length is invalid. This ensures the check is
enforced in production environments where assertions may be disabled.

Comment on lines +23 to +31
final cleanHex = AddrDecUtils.validateAndRemovePrefix(
ethAddress, CoinsConf.ethereum.params.addrPrefix!
);
assert(
cleanHex.length == EthAddrConst.addrLen,
"Invalid Ethereum address length: ${cleanHex.length}, expected: ${EthAddrConst.addrLen}"
);
final hexAddressBytes = hex.decode(cleanHex);
return Bech32Encoder.encode(prefix, hexAddressBytes);
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider replacing assertions with proper exception handling.

Using assertions for input validation can be problematic in production builds where assertions may be disabled. Consider throwing specific exceptions instead for better error handling and debugging.

-    assert(
-    cleanHex.length == EthAddrConst.addrLen,
-    "Invalid Ethereum address length: ${cleanHex.length}, expected: ${EthAddrConst.addrLen}"
-    );
+    if (cleanHex.length != EthAddrConst.addrLen) {
+      throw ArgumentError(
+        "Invalid Ethereum address length: ${cleanHex.length}, expected: ${EthAddrConst.addrLen}"
+      );
+    }
🤖 Prompt for AI Agents
In lib/application/wallet/utils.dart around lines 23 to 31, replace the
assertion that checks the Ethereum address length with proper exception
handling. Instead of using assert, throw a specific exception (e.g.,
ArgumentError) when the address length is invalid to ensure the validation works
in production and provides clear error feedback.

Comment on lines +45 to +50
if (receiver.startsWith("0x")) {
receiver = EthBech32Converter.ethAddressToBech32(
receiver,
chainAddressPrefix
);
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for address conversion.

The address conversion could fail due to invalid input addresses, but there's no error handling around the conversion. This could cause the entire sendToken operation to fail unexpectedly.

-    if (receiver.startsWith("0x")) {
-      receiver = EthBech32Converter.ethAddressToBech32(
-          receiver,
-          chainAddressPrefix
-      );
-    }
+    if (receiver.startsWith("0x")) {
+      try {
+        receiver = EthBech32Converter.ethAddressToBech32(
+            receiver,
+            chainAddressPrefix
+        );
+      } catch (e) {
+        throw ArgumentError("Failed to convert Ethereum address: $e");
+      }
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (receiver.startsWith("0x")) {
receiver = EthBech32Converter.ethAddressToBech32(
receiver,
chainAddressPrefix
);
}
if (receiver.startsWith("0x")) {
try {
receiver = EthBech32Converter.ethAddressToBech32(
receiver,
chainAddressPrefix
);
} catch (e) {
throw ArgumentError("Failed to convert Ethereum address: $e");
}
}
🤖 Prompt for AI Agents
In lib/application/wallet/wallet_bloc.dart around lines 45 to 50, the code
converts an Ethereum address to Bech32 format without error handling, which may
cause the sendToken operation to fail if the input address is invalid. Wrap the
conversion call in a try-catch block to catch any exceptions thrown during
conversion, and handle the error gracefully by logging it or emitting an error
state to prevent the entire operation from failing unexpectedly.

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