-
Notifications
You must be signed in to change notification settings - Fork 1
Problem: Cannot send to EVM address #54
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,52 @@ | ||||||||||||||||||||
| import "package:blockchain_utils/bech32/bech32_base.dart"; | ||||||||||||||||||||
| import "package:blockchain_utils/bip/address/addr_dec_utils.dart"; | ||||||||||||||||||||
| import "package:blockchain_utils/bip/address/eth_addr.dart"; | ||||||||||||||||||||
| import "package:blockchain_utils/bip/coin_conf/constant/coins_conf.dart"; | ||||||||||||||||||||
| import "package:blockchain_utils/hex/hex.dart"; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| class EthBech32Converter { | ||||||||||||||||||||
| /// Encodes an Ethereum address in Bech32 format. | ||||||||||||||||||||
| /// This method takes a hexadecimal Ethereum address and a prefix, | ||||||||||||||||||||
| /// converts the address to bytes, and then encodes to Bech32-format address. | ||||||||||||||||||||
| /// | ||||||||||||||||||||
| /// Parameters: | ||||||||||||||||||||
| /// - hexAddress: The hexadecimal representation of the Ethereum address. | ||||||||||||||||||||
| /// - prefix: The Bech32 prefix to be used for encoding. | ||||||||||||||||||||
| /// | ||||||||||||||||||||
| /// Returns: | ||||||||||||||||||||
| /// A Bech32-encoded address. | ||||||||||||||||||||
| /// | ||||||||||||||||||||
| /// Throws: | ||||||||||||||||||||
| /// - AssertionError: If the length of the cleaned hexadecimal address is not equal to the expected Ethereum address length. | ||||||||||||||||||||
| static String ethAddressToBech32(String ethAddress, prefix) { | ||||||||||||||||||||
|
|
||||||||||||||||||||
| 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); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /// Decodes a Bech32-encoded address. | ||||||||||||||||||||
| /// This method takes a Bech32-encoded address | ||||||||||||||||||||
| /// and decodes it to its hexadecimal representation. | ||||||||||||||||||||
| /// | ||||||||||||||||||||
| /// Parameters: | ||||||||||||||||||||
| /// - bech32Address: The Bech32-encoded Ethereum address. | ||||||||||||||||||||
| /// | ||||||||||||||||||||
| /// Returns: | ||||||||||||||||||||
| /// A string representing the Ethereum address. | ||||||||||||||||||||
| static String bech32ToEthAddress(String bech32Address, prefix) { | ||||||||||||||||||||
| final decoded = Bech32Decoder.decode(prefix, bech32Address); | ||||||||||||||||||||
| final hexEncoded = hex.encode(decoded); | ||||||||||||||||||||
| assert( | ||||||||||||||||||||
| hexEncoded.length == EthAddrConst.addrLen, | ||||||||||||||||||||
| "Invalid Ethereum address length: ${hexEncoded.length}, expected: ${EthAddrConst.addrLen}" | ||||||||||||||||||||
| ); | ||||||||||||||||||||
|
Comment on lines
+46
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| return "${CoinsConf.ethereum.params.addrPrefix!}$hexEncoded"; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,7 @@ | ||||||||||||||||||||||||||||||||||
| import "package:cosmos_sdk/cosmos_sdk.dart"; | ||||||||||||||||||||||||||||||||||
| import "package:hnotes/application/wallet/utils.dart"; | ||||||||||||||||||||||||||||||||||
| import "package:hnotes/infrastructure/blockchain/wallet_repository.dart"; | ||||||||||||||||||||||||||||||||||
| import "package:hnotes/infrastructure/constants.dart"; | ||||||||||||||||||||||||||||||||||
| import "package:hnotes/infrastructure/local_storage/secrets/secrets_repository.dart"; | ||||||||||||||||||||||||||||||||||
| import "package:rxdart/rxdart.dart"; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -40,6 +42,13 @@ class WalletBloc { | |||||||||||||||||||||||||||||||||
| String receiver, | ||||||||||||||||||||||||||||||||||
| {TransactionConfirmationCallback? confirmTransaction} | ||||||||||||||||||||||||||||||||||
| ) async { | ||||||||||||||||||||||||||||||||||
| if (receiver.startsWith("0x")) { | ||||||||||||||||||||||||||||||||||
| receiver = EthBech32Converter.ethAddressToBech32( | ||||||||||||||||||||||||||||||||||
| receiver, | ||||||||||||||||||||||||||||||||||
| chainAddressPrefix | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+45
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 - 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
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| final message = MsgSend( | ||||||||||||||||||||||||||||||||||
| fromAddress: CosmosBaseAddress(sender), | ||||||||||||||||||||||||||||||||||
| toAddress: CosmosBaseAddress(receiver), | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
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.
🤖 Prompt for AI Agents