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

Exchange address support #53

Merged
merged 7 commits into from
Jul 3, 2024
Merged

Exchange address support #53

merged 7 commits into from
Jul 3, 2024

Conversation

levonpetrosyan93
Copy link

@levonpetrosyan93 levonpetrosyan93 commented May 27, 2024

Summary by CodeRabbit

  • New Features

    • Introduced support for a new address type EXP2PKH.
    • Updated address handling functions to accommodate EXP2PKH.
  • Enhancements

    • Added EXP2PKH type to Ledger and Trezor plugins for improved hardware wallet compatibility.
    • Enhanced script and transaction handling to include EXP2PKH type.
  • Bug Fixes

    • Improved address conversion and validation functions to handle different address lengths accurately.

These updates improve compatibility and support for a new address type, enhancing both usability and security.

Copy link

coderabbitai bot commented Jun 3, 2024

Walkthrough

The recent changes to electrum_dash primarily introduce support for a new address type called EXP2PKH. This involves updates across multiple files for compatibility, including core functions related to address handling, script generation, and hardware wallet integrations with Ledger and Trezor devices. These additions ensure seamless operation with the new address type while maintaining existing functionality.

Changes

Files Change Summary
bitcoin.py Added OP_EXCHANGEADDR opcode, new functions for EXP2PKH, updated existing functions to support EXP2PKH.
constants.py Introduced ADDRTYPE_EXP2PKH constant for different network types.
transaction.py Added SCRIPTPUBKEY_TEMPLATE_EXP2PKH, updated functions for handling EXP2PKH script type.
plugins/ledger/ledger.py Added ADDRTYPE_EXP2PKH, updated methods to support EXP2PKH.
plugins/trezor/trezor.py Updated input and output script type functions to support EXP2PKH.

Poem

In the depths of crypto's bash,
Comes an update for Electrum Dash.
With new EXP2PKH delight,
Wallets now handle it just right.
Ledger and Trezor with a tweak,
Crypto future's looking sleek.
Here's to changes, swift and bright,
Making transactions day and night! 🎉

~ CodeRabbit 🐇


Tip

Early access features: disabled

We are currently testing the following features in early access:

  • OpenAI gpt-4o model for code reviews and chat: OpenAI claims that this model is better at understanding and generating code than the previous models. We seek your feedback over the next few weeks before making it generally available.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.
  • Please join our Discord Community to provide feedback and report issues.

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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: 2

Outside diff range and nitpick comments (11)
electrum_dash/constants.py (1)

Line range hint 44-44: Avoid using bare except statements.

Using bare except can catch unexpected exceptions and obscure programming errors. Consider specifying the exception type or handling specific exceptions.

- except:
+ except Exception as e:
+     # Handle or log the exception e

Also applies to: 55-55

electrum_dash/bitcoin.py (2)

Line range hint 26-26: Remove unused imports.

These imports are not used in the file and should be removed to clean up the code.

- import hashlib
- from typing import List
- from . import version
- from .crypto import hmac_oneshot

Also applies to: 27-27, 32-32, 35-35


Line range hint 306-306: Refactor to avoid multiple statements on one line.

Multiple statements on a single line can reduce code readability. Consider refactoring to separate lines.

- if net is None: net = constants.net
+ if net is None:
+     net = constants.net

Also applies to: 372-372, 376-376, 380-380, 384-384, 388-388, 392-392, 403-403, 417-417, 443-443, 494-494, 667-667, 678-678

electrum_dash/plugins/ledger/ledger.py (1)

Line range hint 1-1: Address unused imports and other static analysis findings.

- from struct import pack, unpack
- import sys
- import traceback
- from electrum_firo.transaction import PartialTransaction, PartialTxInput, PartialTxOutput
- from electrum_firo.util import bh2u
- import hid
- from btchip.btchipComm import HIDDongleHIDAPI, DongleWait
- from btchip.btchipUtils import compress_public_key, format_transaction, get_regular_input_script, get_p2sh_input_script
- from btchip.btchipFirmwareWizard import checkFirmware, updateFirmware
- indexOutput = 0  # This variable is assigned but never used
- except:  # Replace bare except with specific exceptions to handle
+ from btchip.btchipComm import HIDDongleHIDAPI
+ from btchip.btchipUtils import compress_public_key
+ from btchip.btchipFirmwareWizard import checkFirmware

This change addresses multiple issues flagged by static analysis, including unused imports and a bare except statement. It's important to handle exceptions explicitly to avoid catching unexpected exceptions and to remove unused imports to keep the code clean and maintainable.

Also applies to: 3-4, 16-16, 18-18, 37-41, 120-120, 683-683

electrum_dash/transaction.py (7)

Line range hint 31-31: Remove unused import: traceback.

- import traceback
Tools
Ruff

45-45: .util.profiler imported but unused


49-49: .bitcoin.push_script imported but unused


50-50: .bitcoin.add_number_to_script imported but unused


Line range hint 32-32: Remove unused import: sys.

- import sys
Tools
Ruff

45-45: .util.profiler imported but unused


49-49: .bitcoin.push_script imported but unused


50-50: .bitcoin.add_number_to_script imported but unused


Line range hint 35-35: Remove unused import: typing.Iterable.

- from typing import (Sequence, Union, NamedTuple, Tuple, Optional, Iterable,
+ from typing import (Sequence, Union, NamedTuple, Tuple, Optional,
Tools
Ruff

45-45: .util.profiler imported but unused


49-49: .bitcoin.push_script imported but unused


50-50: .bitcoin.add_number_to_script imported but unused


Line range hint 36-36: Remove unused import: typing.Dict.

- from typing import (Sequence, Union, NamedTuple, Tuple, Optional, Iterable,
-                     Callable, List, Dict, Set, TYPE_CHECKING)
+ from typing import (Sequence, Union, NamedTuple, Tuple, Optional, Iterable,
+                     Callable, List, Set, TYPE_CHECKING)
Tools
Ruff

45-45: .util.profiler imported but unused


49-49: .bitcoin.push_script imported but unused


50-50: .bitcoin.add_number_to_script imported but unused


Line range hint 384-384: Refactor to avoid multiple statements on one line.

- def read_boolean(self) -> bool: return self.read_bytes(1) != b'\x00'
+ def read_boolean(self) -> bool:
+     return self.read_bytes(1) != b'\x00'

Also applies to: 385-385, 388-388, 389-389, 392-392, 393-393, 1017-1017, 1038-1038, 1057-1057, 1162-1162, 1168-1168, 1181-1181, 1192-1192, 1197-1197, 1356-1356, 1361-1361, 1451-1451, 1472-1472, 1494-1494, 1503-1503, 1507-1507

Tools
Ruff

45-45: .util.profiler imported but unused


49-49: .bitcoin.push_script imported but unused


50-50: .bitcoin.add_number_to_script imported but unused


Line range hint 928-928: Replace bare except with specific exceptions to avoid catching unexpected exceptions.

- except:
+ except SpecificException:

Also applies to: 933-933, 939-939

Tools
Ruff

45-45: .util.profiler imported but unused


49-49: .bitcoin.push_script imported but unused


50-50: .bitcoin.add_number_to_script imported but unused


Line range hint 1149-1149: Remove f-strings without placeholders as they are unnecessary.

- raise Exception(f"size {size} too large for compact_size")
+ raise Exception("size too large for compact_size")

Also applies to: 1150-1150, 1154-1154, 1155-1155, 1459-1459, 1486-1486, 1589-1589, 1590-1590

Tools
Ruff

45-45: .util.profiler imported but unused


49-49: .bitcoin.push_script imported but unused


50-50: .bitcoin.add_number_to_script imported but unused

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 137e8e8 and 0900ea3.

Files selected for processing (4)
  • electrum_dash/bitcoin.py (8 hunks)
  • electrum_dash/constants.py (4 hunks)
  • electrum_dash/plugins/ledger/ledger.py (2 hunks)
  • electrum_dash/transaction.py (8 hunks)
Additional context used
Ruff
electrum_dash/constants.py

44-44: Do not use bare except


55-55: Do not use bare except

electrum_dash/bitcoin.py

26-26: hashlib imported but unused


27-27: typing.List imported but unused


32-32: .version imported but unused


35-35: .crypto.hmac_oneshot imported but unused


306-306: Do not compare types, use isinstance()


372-372: Multiple statements on one line (colon)


376-376: Multiple statements on one line (colon)


380-380: Multiple statements on one line (colon)


384-384: Multiple statements on one line (colon)


388-388: Multiple statements on one line (colon)


392-392: Multiple statements on one line (colon)


403-403: Multiple statements on one line (colon)


417-417: Multiple statements on one line (colon)


443-443: Multiple statements on one line (colon)


494-494: Multiple statements on one line (colon)


634-634: Local variable e is assigned to but never used


667-667: Multiple statements on one line (colon)


671-671: Local variable e is assigned to but never used


678-678: Multiple statements on one line (colon)


686-686: Local variable e is assigned to but never used

electrum_dash/plugins/ledger/ledger.py

1-1: struct.pack imported but unused


1-1: struct.unpack imported but unused


3-3: sys imported but unused


4-4: traceback imported but unused


16-16: electrum_firo.transaction.PartialTransaction imported but unused


16-16: electrum_firo.transaction.PartialTxInput imported but unused


16-16: electrum_firo.transaction.PartialTxOutput imported but unused


18-18: electrum_firo.util.bh2u imported but unused


37-37: btchip.btchipComm.DongleWait imported but unused; consider using importlib.util.find_spec to test for availability


39-39: btchip.btchipUtils.format_transaction imported but unused; consider using importlib.util.find_spec to test for availability


39-39: btchip.btchipUtils.get_regular_input_script imported but unused; consider using importlib.util.find_spec to test for availability


39-39: btchip.btchipUtils.get_p2sh_input_script imported but unused; consider using importlib.util.find_spec to test for availability


41-41: btchip.btchipFirmwareWizard.updateFirmware imported but unused; consider using importlib.util.find_spec to test for availability


120-120: Local variable indexOutput is assigned to but never used


683-683: Do not use bare except

electrum_dash/transaction.py

31-31: traceback imported but unused


32-32: sys imported but unused


35-35: typing.Iterable imported but unused


36-36: typing.Dict imported but unused


45-45: .util.profiler imported but unused


49-49: .bitcoin.push_script imported but unused


50-50: .bitcoin.add_number_to_script imported but unused


384-384: Multiple statements on one line (colon)


385-385: Multiple statements on one line (colon)


388-388: Multiple statements on one line (colon)


389-389: Multiple statements on one line (colon)


392-392: Multiple statements on one line (colon)


393-393: Multiple statements on one line (colon)


928-928: Do not use bare except


933-933: Do not use bare except


939-939: Do not use bare except


1017-1017: Multiple statements on one line (colon)


1038-1038: Multiple statements on one line (colon)


1057-1057: Multiple statements on one line (colon)


1149-1149: f-string without any placeholders


1150-1150: f-string without any placeholders


1154-1154: f-string without any placeholders


1155-1155: f-string without any placeholders


1162-1162: Multiple statements on one line (colon)


1168-1168: Multiple statements on one line (colon)


1181-1181: Multiple statements on one line (colon)


1192-1192: Multiple statements on one line (colon)


1197-1197: Multiple statements on one line (colon)


1356-1356: Multiple statements on one line (colon)


1361-1361: Multiple statements on one line (colon)


1451-1451: Multiple statements on one line (colon)


1459-1459: f-string without any placeholders


1472-1472: Multiple statements on one line (colon)


1486-1486: f-string without any placeholders


1494-1494: Multiple statements on one line (colon)


1503-1503: Multiple statements on one line (colon)


1507-1507: Multiple statements on one line (colon)


1589-1589: f-string without any placeholders


1590-1590: f-string without any placeholders

Additional comments not posted (15)
electrum_dash/constants.py (4)

79-79: Added ADDRTYPE_EXP2PKH to AbstractNet class.

This addition is consistent with the PR's objective to support the new EXP2PKH address type.


96-96: Added ADDRTYPE_EXP2PKH to BitcoinMainnet class.

This ensures that the mainnet configuration supports the new address type.


134-134: Added ADDRTYPE_EXP2PKH to BitcoinTestnet class.

This ensures that the testnet configuration supports the new address type.


172-172: Added ADDRTYPE_EXP2PKH to BitcoinRegtest class.

This ensures that the regtest configuration supports the new address type.

electrum_dash/bitcoin.py (9)

195-195: Added OP_EXCHANGEADDR opcode.

This addition is necessary for the new EXP2PKH address type operations.


363-369: Updated b58_address_to_hash160 to handle 21 and 23 byte addresses.

This update is crucial for supporting the new EXP2PKH address type.


375-377: Added hash160_to_exp2pkh function.

This function is essential for converting hash160 to EXP2PKH addresses.

Tools
Ruff

376-376: Multiple statements on one line (colon)


387-389: Added public_key_to_exp2pkh function.

This function supports converting public keys to EXP2PKH addresses, aligning with the new address type support.

Tools
Ruff

388-388: Multiple statements on one line (colon)


395-396: Updated pubkey_to_address to handle EXP2PKH.

This update ensures that the function can handle the new EXP2PKH address type.


423-424: Updated address_to_script to handle EXP2PKH.

This update is necessary for script generation for EXP2PKH addresses.


437-437: Added EXP2PKH to OnchainOutputType.

This addition is necessary for identifying EXP2PKH type outputs.


449-450: Updated address_to_hash to handle EXP2PKH.

This update ensures that the function can correctly identify and handle EXP2PKH addresses.


477-485: Added pubkeyhash_to_exp2pkh_script function.

This function is essential for generating scripts for EXP2PKH addresses.

electrum_dash/plugins/ledger/ledger.py (2)

300-300: Ensure the new address type EXP2PKH is correctly integrated.

This line correctly integrates the new EXP2PKH address type into the setAlternateCoinVersions function, aligning with the PR's objectives to support the new address type.


535-535: Verify the handling of address types in the conditional logic.

This verification is crucial to ensure that the handling of the new EXP2PKH address type is consistent and correct throughout the codebase.

Verification successful

The handling of the ADDRTYPE_EXP2PKH address type appears to be consistent across the codebase. The search results show that ADDRTYPE_EXP2PKH is used in relevant places alongside ADDRTYPE_P2PKH, ensuring that the new address type is correctly integrated.

  • electrum_dash/transaction.py
  • electrum_dash/constants.py
  • electrum_dash/bitcoin.py
  • electrum_dash/plugins/ledger/ledger.py

These files include the necessary handling for ADDRTYPE_EXP2PKH, confirming that the integration is consistent.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the handling of address types in the conditional logic is correct and consistent across the codebase.

# Test: Search for similar conditional logic handling address types.
rg --type python $'if v == constants.net.ADDRTYPE_P2PKH or v == constants.net.ADDRTYPE_EXP2PKH'

Length of output: 127



Script:

#!/bin/bash
# Description: Verify that the handling of address types in the conditional logic is correct and consistent across the codebase.

# Test: Search for similar conditional logic handling address types.
rg --type py $'if v == constants.net.ADDRTYPE_P2PKH or v == constants.net.ADDRTYPE_EXP2PKH'

Length of output: 232



Script:

#!/bin/bash
# Description: Verify that the handling of address types is consistent across the codebase.

# Test: Search for all instances where ADDRTYPE_P2PKH is used and check if ADDRTYPE_EXP2PKH is also handled.
rg --type py 'ADDRTYPE_P2PKH'

Length of output: 1299



Script:

#!/bin/bash
# Description: Verify that the handling of ADDRTYPE_EXP2PKH is consistent across the codebase.

# Test: Search for all instances where ADDRTYPE_EXP2PKH is used in the identified files.
rg --type py 'ADDRTYPE_EXP2PKH'

Length of output: 959

@@ -44,7 +44,7 @@
from .bip32 import BIP32Node
from .util import profiler, to_bytes, bh2u, bfh, chunks, is_hex_str
Copy link

Choose a reason for hiding this comment

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

Remove unused import: .util.profiler.

- from .util import profiler, to_bytes, bh2u, bfh, chunks, is_hex_str
+ from .util import to_bytes, bh2u, bfh, chunks, is_hex_str
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.

Suggested change
from .util import profiler, to_bytes, bh2u, bfh, chunks, is_hex_str
from .util import to_bytes, bh2u, bfh, chunks, is_hex_str
Tools
Ruff

45-45: .util.profiler imported but unused

Comment on lines 49 to 50
int_to_hex, push_script, b58_address_to_hash160,
opcodes, add_number_to_script, base_decode, base_encode,
Copy link

Choose a reason for hiding this comment

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

Remove unused imports: .bitcoin.push_script and .bitcoin.add_number_to_script.

-                       opcodes, add_number_to_script, base_decode, base_encode,
+                       opcodes, base_decode, base_encode,
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.

Suggested change
int_to_hex, push_script, b58_address_to_hash160,
opcodes, add_number_to_script, base_decode, base_encode,
int_to_hex, push_script, b58_address_to_hash160,
opcodes, base_decode, base_encode,
Tools
Ruff

49-49: .bitcoin.push_script imported but unused


50-50: .bitcoin.add_number_to_script imported but unused

@justanwar justanwar mentioned this pull request Jun 3, 2024
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0900ea3 and a8d1255.

Files selected for processing (1)
  • electrum_dash/bitcoin.py (9 hunks)
Additional context used
Ruff
electrum_dash/bitcoin.py

26-26: hashlib imported but unused (F401)


27-27: typing.List imported but unused (F401)


32-32: .version imported but unused (F401)


35-35: .crypto.hmac_oneshot imported but unused (F401)


306-306: Do not compare types, use isinstance() (E721)


377-377: Multiple statements on one line (colon) (E701)


381-381: Multiple statements on one line (colon) (E701)


385-385: Multiple statements on one line (colon) (E701)


389-389: Multiple statements on one line (colon) (E701)


393-393: Multiple statements on one line (colon) (E701)


397-397: Multiple statements on one line (colon) (E701)


408-408: Multiple statements on one line (colon) (E701)


422-422: Multiple statements on one line (colon) (E701)


448-448: Multiple statements on one line (colon) (E701)


499-499: Multiple statements on one line (colon) (E701)


639-639: Local variable e is assigned to but never used (F841)


672-672: Multiple statements on one line (colon) (E701)


676-676: Local variable e is assigned to but never used (F841)


683-683: Multiple statements on one line (colon) (E701)


691-691: Local variable e is assigned to but never used (F841)

Additional comments not posted (9)
electrum_dash/bitcoin.py (9)

195-196: Added new opcode OP_EXCHANGEADDR for EXP2PKH address type.

This addition aligns with the PR's objective to support a new address type.


368-374: Updated b58_address_to_hash160 to handle addresses of 21 and 23 bytes.

This update is necessary for supporting the new EXP2PKH address type.


380-383: Introduced hash160_to_exp2pkh function to convert hash160 to EXP2PKH address.

This function is essential for the new address type support.

Tools
Ruff

381-381: Multiple statements on one line (colon) (E701)


392-395: Introduced public_key_to_exp2pkh function to convert public key to EXP2PKH address.

This function is crucial for generating EXP2PKH addresses from public keys.

Tools
Ruff

393-393: Multiple statements on one line (colon) (E701)


400-401: Updated pubkey_to_address to support EXP2PKH address type.

This update ensures that the function can handle the new address type.


428-429: Updated address_to_script to handle EXP2PKH address type.

This update is necessary for script generation for the new address type.


442-442: Added EXP2PKH to OnchainOutputType.

This addition is necessary to handle EXP2PKH outputs correctly.


454-455: Updated address_to_hash to handle EXP2PKH address type.

This update ensures that the function can correctly identify and process EXP2PKH addresses.


482-490: Introduced pubkeyhash_to_exp2pkh_script function for generating EXP2PKH scripts.

This function is essential for script generation for the new address type.

Comment on lines 355 to 359
if addrtype == 185:
first_byte = 1
third_byte = 187
s = first_byte.to_bytes(1, 'little') + bytes([addrtype]) + third_byte.to_bytes(1, 'little') + h160

Copy link

Choose a reason for hiding this comment

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

Ensure consistent byte handling in hash160_to_b58_address.

- first_byte = 1
- third_byte = 187
- s =  first_byte.to_bytes(1, 'little')  + bytes([addrtype]) + third_byte.to_bytes(1, 'little') + h160
+ s = bytes([1, addrtype, 187]) + h160

This change simplifies the byte handling and ensures that the bytes are correctly ordered.

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 addrtype == 185:
first_byte = 1
third_byte = 187
s = first_byte.to_bytes(1, 'little') + bytes([addrtype]) + third_byte.to_bytes(1, 'little') + h160
if addrtype == 185:
s = bytes([1, addrtype, 187]) + h160

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 (13)
electrum_dash/bitcoin.py (6)

Line range hint 26-26: Remove unused import: hashlib.

- import hashlib

Line range hint 27-27: Remove unused import: typing.List.

- from typing import List

Line range hint 32-32: Remove unused import: .version.

- from . import version

Line range hint 35-35: Remove unused import: .crypto.hmac_oneshot.

- from .crypto import sha256d, sha256, hash_160, hmac_oneshot
+ from .crypto import sha256d, sha256, hash_160

Line range hint 210-210: Convert to f-string for better readability and performance.

- raise TypeError('{} instead of int'.format(i))
+ raise TypeError(f'{i} instead of int')

- raise OverflowError('cannot convert int {} to hex ({} bytes)'.format(i, length))
+ raise OverflowError(f'cannot convert int {i} to hex ({length} bytes)')

- raise ValueError('not supported base: {}'.format(base))
+ raise ValueError(f'not supported base: {base}')

- raise BitcoinException('unknown script type: {}'.format(txin_type))
+ raise BitcoinException(f'unknown script type: {txin_type}')

- raise BitcoinException(f'calculated {bh2u(csum_calculated)}, found {bh2u(csum_found)}')
+ raise BitcoinException(f'calculated {bh2u(csum_calculated)}, found {bh2u(csum_found)}')

- raise BitcoinException('invalid prefix ({}) for WIF key (1)'.format(vch[0]))
+ raise BitcoinException(f'invalid prefix ({vch[0]}) for WIF key (1)')

- raise BitcoinException('invalid prefix ({}) for WIF key (2)'.format(vch[0]))
+ raise BitcoinException(f'invalid prefix ({vch[0]}) for WIF key (2)')

- raise BaseDecodeError(f"cannot deserialize privkey {neutered_privkey}")
+ raise BaseDecodeError(f"cannot deserialize privkey {neutered_privkey}")

- raise BitcoinException('invalid WIF key. length suggests compressed pubkey, but last byte is {vch[33]} != 0x01')
+ raise BitcoinException(f'invalid WIF key. length suggests compressed pubkey, but last byte is {vch[33]} != 0x01')

Also applies to: 213-213, 503-503, 537-537, 546-546, 613-613, 624-624, 637-637, 641-641, 644-644


Line range hint 636-636: Remove assignment to unused variable e.

- except Exception as e:
+ except Exception:

Also applies to: 673-673, 688-688

electrum_dash/transaction.py (7)

Line range hint 31-36: Remove unused imports to clean up the codebase.

- import traceback
- import sys
- from typing import Iterable, Dict

Line range hint 240-240: Remove unnecessary inheritance from object in Python 3.

- class BCDataStream(object):
+ class BCDataStream:

Line range hint 384-393: Use raise ... from None to explicitly chain exceptions or suppress context.

- except IndexError: raise MalformedBitcoinScript()
+ except IndexError: raise MalformedBitcoinScript() from None
- except struct.error: raise MalformedBitcoinScript()
+ except struct.error: raise MalformedBitcoinScript() from None

Line range hint 928-939: Avoid using bare except statements. Specify exception types to catch specific errors.

- except:
+ except Exception:
- except:
+ except Exception:
- except:
+ except Exception:

Line range hint 1147-1155: Remove unnecessary f-string prefixes where no variables are being interpolated.

- f"PSBT global xpub has mismatching depth ({bip32node.depth}) and derivation prefix len ({len(path)})"
+ "PSBT global xpub has mismatching depth ({}) and derivation prefix len ({})".format(bip32node.depth, len(path))
- f"PSBT global xpub has inconsistent child_number and derivation prefix"
+ "PSBT global xpub has inconsistent child_number and derivation prefix"

Line range hint 1352-1361: Use contextlib.suppress instead of try-except-pass for cleaner exception handling when exceptions are to be ignored.

from contextlib import suppress

with suppress(ValueError):
    # your code here

Line range hint 1738-1738: Convert .format() to an f-string for consistency and readability.

- "PSBT global xpub has mismatching depth ({}) and derivation prefix len ({})".format(bip32node.depth, len(path))
+ f"PSBT global xpub has mismatching depth ({bip32node.depth}) and derivation prefix len ({len(path)})"
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a8d1255 and d1205f9.

Files selected for processing (2)
  • electrum_dash/bitcoin.py (9 hunks)
  • electrum_dash/transaction.py (8 hunks)
Additional context used
Ruff
electrum_dash/bitcoin.py

1-1: UTF-8 encoding declaration is unnecessary (UP009)

Remove unnecessary coding comment


26-26: hashlib imported but unused (F401)

Remove unused import: hashlib


27-27: typing.List imported but unused (F401)

Remove unused import: typing.List


32-32: .version imported but unused (F401)

Remove unused import: .version


35-35: .crypto.hmac_oneshot imported but unused (F401)

Remove unused import: .crypto.hmac_oneshot


210-210: Use f-string instead of format call (UP032)

Convert to f-string


213-213: Use f-string instead of format call (UP032)

Convert to f-string


306-306: Do not compare types, use isinstance() (E721)


374-374: Multiple statements on one line (colon) (E701)


378-378: Multiple statements on one line (colon) (E701)


382-382: Multiple statements on one line (colon) (E701)


386-386: Multiple statements on one line (colon) (E701)


390-390: Multiple statements on one line (colon) (E701)


394-394: Multiple statements on one line (colon) (E701)


405-405: Multiple statements on one line (colon) (E701)


419-419: Multiple statements on one line (colon) (E701)


445-445: Multiple statements on one line (colon) (E701)


496-496: Multiple statements on one line (colon) (E701)


503-503: Use f-string instead of format call (UP032)

Convert to f-string


537-537: Use f-string instead of format call (UP032)

Convert to f-string


546-546: Use f-string instead of format call (UP032)

Convert to f-string


613-613: Use f-string instead of format call (UP032)

Convert to f-string


624-624: Use f-string instead of format call (UP032)

Convert to f-string


636-636: Local variable e is assigned to but never used (F841)

Remove assignment to unused variable e


637-637: Use f-string instead of format call (UP032)

Convert to f-string


641-641: Use f-string instead of format call (UP032)

Convert to f-string


644-644: Use f-string instead of format call (UP032)

Convert to f-string


669-669: Multiple statements on one line (colon) (E701)


673-673: Local variable e is assigned to but never used (F841)

Remove assignment to unused variable e


680-680: Multiple statements on one line (colon) (E701)


688-688: Local variable e is assigned to but never used (F841)

Remove assignment to unused variable e

electrum_dash/transaction.py

31-31: traceback imported but unused (F401)

Remove unused import: traceback


32-32: sys imported but unused (F401)

Remove unused import: sys


35-35: typing.Iterable imported but unused (F401)

Remove unused import


36-36: typing.Dict imported but unused (F401)

Remove unused import


240-240: Class BCDataStream inherits from object (UP004)

Remove object inheritance


384-384: Multiple statements on one line (colon) (E701)


385-385: Multiple statements on one line (colon) (E701)


385-385: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling (B904)


388-388: Multiple statements on one line (colon) (E701)


389-389: Multiple statements on one line (colon) (E701)


389-389: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling (B904)


392-392: Multiple statements on one line (colon) (E701)


393-393: Multiple statements on one line (colon) (E701)


393-393: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling (B904)


928-928: Do not use bare except (E722)


933-933: Do not use bare except (E722)


939-939: Do not use bare except (E722)


956-958: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling (B904)


1017-1017: Multiple statements on one line (colon) (E701)


1038-1038: Multiple statements on one line (colon) (E701)


1057-1057: Multiple statements on one line (colon) (E701)


1147-1148: Use a single if statement instead of nested if statements (SIM102)


1149-1149: f-string without any placeholders (F541)

Remove extraneous f prefix


1150-1150: f-string without any placeholders (F541)

Remove extraneous f prefix


1154-1154: f-string without any placeholders (F541)

Remove extraneous f prefix


1155-1155: f-string without any placeholders (F541)

Remove extraneous f prefix


1158-1161: Use contextlib.suppress(ValueError) instead of try-except-pass (SIM105)


1162-1162: Multiple statements on one line (colon) (E701)


1168-1168: Multiple statements on one line (colon) (E701)


1181-1181: Multiple statements on one line (colon) (E701)


1192-1192: Multiple statements on one line (colon) (E701)


1197-1197: Multiple statements on one line (colon) (E701)


1352-1355: Use contextlib.suppress(ValueError) instead of try-except-pass (SIM105)


1356-1356: Multiple statements on one line (colon) (E701)


1361-1361: Multiple statements on one line (colon) (E701)


1444-1447: Use contextlib.suppress(ValueError) instead of try-except-pass (SIM105)


1451-1451: Multiple statements on one line (colon) (E701)


1459-1459: f-string without any placeholders (F541)

Remove extraneous f prefix


1468-1471: Use contextlib.suppress(ValueError) instead of try-except-pass (SIM105)


1472-1472: Multiple statements on one line (colon) (E701)


1486-1486: f-string without any placeholders (F541)

Remove extraneous f prefix


1494-1494: Multiple statements on one line (colon) (E701)


1503-1503: Multiple statements on one line (colon) (E701)


1507-1507: Multiple statements on one line (colon) (E701)


1589-1589: f-string without any placeholders (F541)

Remove extraneous f prefix


1590-1590: f-string without any placeholders (F541)

Remove extraneous f prefix


1738-1738: Use f-string instead of format call (UP032)

Convert to f-string

Additional comments not posted (2)
electrum_dash/bitcoin.py (2)

195-196: Addition of OP_EXCHANGEADDR opcode.

This new opcode is correctly defined and follows the existing pattern for opcode definitions.


355-356: Enhancements to support EXP2PKH address type.

The changes across these lines introduce and integrate the new EXP2PKH address type effectively, ensuring compatibility with existing functionalities and extending support as required.

Also applies to: 365-371, 377-380, 389-392, 397-398, 425-426, 451-452, 479-487

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)
electrum_dash/plugins/trezor/trezor.py (1)

Line range hint 1-1: Clean up code by addressing static analysis findings.

Remove unused imports and improve code style by using f-strings and simplifying conditional structures.

- import traceback
- import sys
- from typing import NamedTuple, Any, Optional, Dict, Union, List, Tuple, TYPE_CHECKING
- from electrum_firo.util import bfh, bh2u, versiontuple, UserCancelled, UserFacingException
- from electrum_firo.bip32 import BIP32Node, convert_bip32_path_to_list_of_uint32 as parse_path
- from electrum_firo.transaction import Transaction, PartialTransaction, PartialTxInput, PartialTxOutput
+ from electrum_firo.util import bfh, bh2u, UserCancelled, UserFacingException
+ from electrum_firo.bip32 import BIP32Node
+ from electrum_firo.transaction import Transaction, PartialTransaction, PartialTxInput

- raise ValueError('unexpected txin type: {}'.format(electrum_txin_type))
+ raise ValueError(f'unexpected txin type: {electrum_txin_type}')

- if txin.script_sig.startswith(b"\xc4") and txin.nsequence == 0xffffffff:
-     prev_index = 1
- else:
-     prev_index = txin.nsequence
+ prev_index = 1 if txin.script_sig.startswith(b"\xc4") and txin.nsequence == 0xffffffff else txin.nsequence

Also applies to: 2-2, 3-3, 5-5, 6-6, 11-11, 327-327, 334-334, 464-469

Tools
Ruff

327-327: Use f-string instead of format call

Convert to f-string

(UP032)

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d1205f9 and dbb3529.

Files selected for processing (1)
  • electrum_dash/plugins/trezor/trezor.py (1 hunks)
Additional context used
Ruff
electrum_dash/plugins/trezor/trezor.py

1-1: traceback imported but unused

Remove unused import: traceback

(F401)


2-2: sys imported but unused

Remove unused import: sys

(F401)


3-3: typing.Dict imported but unused

Remove unused import

(F401)


3-3: typing.Union imported but unused

Remove unused import

(F401)


3-3: typing.List imported but unused

Remove unused import

(F401)


3-3: typing.Tuple imported but unused

Remove unused import

(F401)


3-3: typing.TYPE_CHECKING imported but unused

Remove unused import

(F401)


5-5: electrum_firo.util.versiontuple imported but unused

Remove unused import: electrum_firo.util.versiontuple

(F401)


6-6: electrum_firo.bip32.convert_bip32_path_to_list_of_uint32 imported but unused

Remove unused import: electrum_firo.bip32.convert_bip32_path_to_list_of_uint32

(F401)


11-11: electrum_firo.transaction.PartialTxOutput imported but unused

Remove unused import: electrum_firo.transaction.PartialTxOutput

(F401)


327-327: Use f-string instead of format call

Convert to f-string

(UP032)


334-334: Use f-string instead of format call

Convert to f-string

(UP032)


464-469: Use a single if statement instead of nested if statements

(SIM102)

Additional comments not posted (2)
electrum_dash/plugins/trezor/trezor.py (2)

323-323: Approved the addition of exp2pkh support in input script type handling.

The update correctly extends the existing functionality to include the new address type, aligning with the PR objectives.


330-330: Approved the addition of exp2pkh support in output script type handling.

This change ensures that the new address type can be correctly processed during transaction outputs, maintaining consistency with the overall PR objectives.

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 (3)
electrum_dash/plugins/ledger/ledger.py (3)

Line range hint 1-1: Remove unused imports.

Several imports are unused and should be removed to clean up the codebase:

  • struct.pack, struct.unpack
  • sys
  • traceback
  • PartialTransaction, PartialTxInput, PartialTxOutput from electrum_firo.transaction
  • bh2u from electrum_firo.util
  • Several from btchip.btchipComm, btchip.btchipUtils, and btchip.btchipFirmwareWizard
- from struct import pack, unpack
- import sys
- import traceback
- from electrum_firo.transaction import Transaction, PartialTransaction, PartialTxInput, PartialTxOutput
- from electrum_firo.util import bfh, bh2u, versiontuple, UserFacingException
- from btchip.btchipComm import HIDDongleHIDAPI, DongleWait
- from btchip.btchipUtils import compress_public_key, format_transaction, get_regular_input_script, get_p2sh_input_script
- from btchip.btchipFirmwareWizard import checkFirmware, updateFirmware

Also applies to: 3-4, 16-16, 18-18, 37-41


Line range hint 68-68: Improve exception handling and formatting.

  1. Use raise ... from e for clearer exception chaining.
  2. Replace percent formatting with .format() or f-strings for better readability and performance.
- params = bytearray.fromhex("%.8x" % (index))
+ params = bytearray.fromhex("{:08x}".format(index))

- raise UserFacingException(_('Your Ledger is locked. Please unlock it.'))
+ raise UserFacingException(_('Your Ledger is locked. Please unlock it.')) from e

Also applies to: 82-82, 304-310


Line range hint 120-120: Code Quality Improvements: Unused Variables and Nested If Statements.

  1. Remove the unused variable indexOutput.
  2. Simplify nested if statements for better readability.
- indexOutput = 0  # Remove this line as it's unused

- if client_electrum.is_hw1() and txout.address and not is_b58_address(txout.address):
+ if client_electrum.is_hw1() and txout.address and not is_b58_address(txout.address):

- if v == constants.net.ADDRTYPE_P2PKH:
+ if v in [constants.net.ADDRTYPE_P2PKH, constants.net.ADDRTYPE_EXP2PKH]:
+     output = hash160_to_b58_address(h, 0 if v == constants.net.ADDRTYPE_P2PKH else 185)

Also applies to: 189-192, 501-502

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between dbb3529 and 0cda598.

Files selected for processing (1)
  • electrum_dash/plugins/ledger/ledger.py (2 hunks)
Additional context used
Ruff
electrum_dash/plugins/ledger/ledger.py

1-1: struct.pack imported but unused

Remove unused import

(F401)


1-1: struct.unpack imported but unused

Remove unused import

(F401)


3-3: sys imported but unused

Remove unused import: sys

(F401)


4-4: traceback imported but unused

Remove unused import: traceback

(F401)


16-16: electrum_firo.transaction.PartialTransaction imported but unused

Remove unused import

(F401)


16-16: electrum_firo.transaction.PartialTxInput imported but unused

Remove unused import

(F401)


16-16: electrum_firo.transaction.PartialTxOutput imported but unused

Remove unused import

(F401)


18-18: electrum_firo.util.bh2u imported but unused

Remove unused import: electrum_firo.util.bh2u

(F401)


37-37: btchip.btchipComm.DongleWait imported but unused; consider using importlib.util.find_spec to test for availability

(F401)


39-39: btchip.btchipUtils.format_transaction imported but unused; consider using importlib.util.find_spec to test for availability

(F401)


39-39: btchip.btchipUtils.get_regular_input_script imported but unused; consider using importlib.util.find_spec to test for availability

(F401)


39-39: btchip.btchipUtils.get_p2sh_input_script imported but unused; consider using importlib.util.find_spec to test for availability

(F401)


41-41: btchip.btchipFirmwareWizard.updateFirmware imported but unused; consider using importlib.util.find_spec to test for availability

(F401)


68-68: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


82-82: Use format specifiers instead of percent format

Replace with format specifiers

(UP031)


120-120: Local variable indexOutput is assigned to but never used

Remove assignment to unused variable indexOutput

(F841)


189-192: Use a single if statement instead of nested if statements

(SIM102)


304-304: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


306-306: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


309-310: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


501-502: Use a single if statement instead of nested if statements

(SIM102)


685-685: Do not use bare except

(E722)

Additional comments not posted (1)
electrum_dash/plugins/ledger/ledger.py (1)

300-300: Update to support EXP2PKH address type.

The changes to the perform_hw1_preflight and sign_transaction methods include support for the new EXP2PKH address type by setting alternate coin versions and handling base58 address conversions. These are crucial updates for the functionality of the new address type.

Also applies to: 537-538

@levonpetrosyan93 levonpetrosyan93 merged commit 663660c into master Jul 3, 2024
2 of 3 checks passed
@levonpetrosyan93 levonpetrosyan93 deleted the exchange_addresses branch July 3, 2024 05:06
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.

1 participant