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

Add DIP 14 derivation and DIP 9 known derivation Paths. #39

Merged
merged 13 commits into from
Oct 17, 2024

Conversation

QuantumExplorer
Copy link
Member

@QuantumExplorer QuantumExplorer commented Oct 17, 2024

Summary by CodeRabbit

  • New Features

    • Updated the package version to 0.32.0, enhancing overall capabilities.
    • Introduced new 256-bit index support for hierarchical deterministic wallets.
    • Added a new module for managing derivation paths in cryptocurrency transactions.
    • Enhanced error handling capabilities with new error variants for BLS signatures and Ed25519 operations.
  • Bug Fixes

    • Improved error handling for child number formats in key derivation.
  • Tests

    • Updated PSBT integration tests to reflect changes in the extended private key.
    • Expanded test coverage for script functionality, including new test cases and improved error handling.

Copy link

coderabbitai bot commented Oct 17, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The changes in this pull request involve updates to the Cargo.toml file, enhancements to the BIP32 implementation in bip32.rs, the introduction of a new module dip9 in lib.rs, and modifications to PSBT integration tests in psbt.rs. The Cargo.toml file's version was updated and several new dependencies were added. The bip32.rs file saw the addition of new enum variants and methods for handling 256-bit child numbers. The new dip9 module defines types and constants for derivation paths, while the PSBT tests were adjusted to accommodate an updated extended private key.

Changes

File Change Summary
dash/Cargo.toml Updated package version to "0.32.0"; added new dependencies: bitflags, bls-signatures, serde_repr, strum, lazy_static, and ed25519-dalek. Updated dev-dependencies to replace bincode with bincode_test. Added new features bls and eddsa.
dash/src/bip32.rs Added Normal256 and Hardened256 enum variants; introduced methods for 256-bit child numbers; updated encoding/decoding methods for ExtendedPrivKey and ExtendedPubKey. Added new methods in DerivationPath for identity-related operations.
dash/src/dip9.rs Introduced new enum DerivationPathReference, struct DerivationPathType, and IndexConstPath; added methods for key derivation and constants for various paths.
dash/src/lib.rs Added new module dip9 and conditional exports for bls_signatures and ed25519_dalek.
dash/tests/psbt.rs Updated expected extended private key in build_extended_private_key function; test structure remains unchanged.
dash/src/blockdata/script/tests.rs Added new test cases and updated existing tests for script functionality, enhancing coverage and correctness.
dash/src/crypto/key.rs Added new error variants in Error enum for BLS signatures and Ed25519 operations.
dash/src/crypto/sighash.rs Updated type usage in signature hash methods and refined error handling; adjusted test cases.
dash/src/blockdata/witness.rs Replaced bincode with bincode_test for serialization in tests.
hashes/Cargo.toml Updated package version to "0.14.0"; added comments regarding dependency management.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant PSBT
    participant BIP32
    participant DIP9

    User->>PSBT: Initiate PSBT workflow
    PSBT->>BIP32: Derive keys using updated private key
    BIP32->>DIP9: Access derivation paths and types
    DIP9-->>BIP32: Provide derivation paths
    BIP32-->>PSBT: Return derived keys
    PSBT-->>User: Complete PSBT workflow
Loading

🐇 "In the world of code, where changes bloom,
New paths and keys, dispelling the gloom.
With bitflags added, and bip32 refined,
Our library grows, with features aligned.
Hopping through tests, with keys that are bright,
We celebrate progress, in the soft moonlight!" 🌙

Possibly related PRs

  • Add DIP 14 derivation and DIP 9 known derivation Paths. #39: The changes in this PR involve updates to the Cargo.toml file, including the addition of the bitflags dependency, which is also present in the main PR. Both PRs modify the same file and introduce similar dependencies, indicating a related focus on package management and dependency updates.
  • chore: clean x11 into feature #40: This PR also updates the Cargo.toml file for the dashcore package, including the addition of the bitflags dependency. The changes in both PRs reflect a common goal of enhancing the package's capabilities through dependency management, linking them at the code level.

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

🧹 Outside diff range and nitpick comments (10)
dash/Cargo.toml (1)

60-60: LGTM: Addition of bitflags dependency.

The addition of the bitflags crate (version 2.6.0) is appropriate. This widely-used library will provide efficient bit flag manipulation capabilities to the project.

Consider adding a comment in the code where bitflags is first used to explain its purpose in the context of this project.

dash/src/lib.rs (1)

105-105: LGTM. Consider visibility and documentation for the new dip9 module.

The addition of the dip9 module is consistent with the file structure and aligns with the PR objectives. However, consider the following suggestions:

  1. Evaluate whether this module should be public (pub mod dip9;) to allow access from outside the crate.
  2. Add a brief doc comment explaining the purpose of this module, e.g.:
    /// Module implementing DIP 9 and DIP 14 derivation paths.
    mod dip9;
  3. If appropriate, consider re-exporting key types or functions from this module at the crate level for easier access.
dash/tests/psbt.rs (2)

128-128: LGTM! Consider adding a comment explaining the key change.

The update to the extended private key string looks good. This change likely aligns the test with updated specifications or new test vectors.

Consider adding a brief comment explaining why the extended private key was changed, to provide context for future maintainers.

🧰 Tools
🪛 Gitleaks

128-128: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


128-128: Add a comment to prevent false positives in static analysis.

The extended private key string is being flagged as a potential API key by static analysis tools. While this is a false positive, adding a comment can help prevent future confusion.

Consider adding a comment like the following above the extended_private_key declaration:

+    // This is a test vector for an extended private key, not an API key
     let extended_private_key = "tprv8ZgxMBicQKsPd9TeAdPADNnSyH9SSUUbTVeFszDE23Ki6TBB5nCefAdHkK8Fm3qMQR6sHwA56zqRmKmxnHk37JkiFzvncDqoKmPWubu7hDF";
🧰 Tools
🪛 Gitleaks

128-128: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

dash/src/dip9.rs (4)

1-19: Consider adding documentation comments to DerivationPathReference enum

Adding documentation comments to the DerivationPathReference enum and its variants would improve code readability and help users understand the purpose of each variant.


27-47: Consider adding documentation comments to DerivationPathType struct and flags

Providing documentation comments for the DerivationPathType struct and its associated flags would enhance clarity and assist users in understanding the different path types and their usage.


49-55: Consider adding documentation to IndexConstPath struct

Adding documentation comments to the IndexConstPath struct would help explain its purpose and how it should be used, benefiting other developers who interact with this code.


1-206: Consider adding unit tests for new derivation paths and methods

Including unit tests for the newly added derivation paths and associated methods would help ensure their correctness and prevent future regressions.

dash/src/bip32.rs (2)

276-281: Consistent formatting in fmt::Display for 256-bit indices.

In the fmt::Display implementation for ChildNumber, ensure that the formatting of 256-bit indices is consistent and unambiguous. For example, always include the ' or h suffix for hardened keys, and consider using a consistent prefix like 0x for hexadecimal representations.


560-562: Consider providing more descriptive error variants.

The Error::Hex(hashesHex::Error) variant is added to handle hex decoding errors. For better error reporting, consider wrapping the underlying error with additional context or creating a specific error variant for child number parsing errors.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 648b166 and 53b6e38.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (5)
  • dash/Cargo.toml (1 hunks)
  • dash/src/bip32.rs (20 hunks)
  • dash/src/dip9.rs (1 hunks)
  • dash/src/lib.rs (1 hunks)
  • dash/tests/psbt.rs (2 hunks)
🧰 Additional context used
🪛 Gitleaks
dash/tests/psbt.rs

128-128: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

🔇 Additional comments (3)
dash/src/dip9.rs (2)

112-115: Verify if FEATURE_PURPOSE_IDENTITIES_SUBFEATURE_AUTHENTICATION should have a value of 0

The constant FEATURE_PURPOSE_IDENTITIES_SUBFEATURE_AUTHENTICATION is set to 0. Please confirm if assigning 0 to this sub-feature index is intentional. In some cases, using 0 might have special implications or might be used to represent a default or uninitialized state in derivation paths.


117-206: Ensure consistency in derivation path constants

The derivation path constants for mainnet and testnet are defined with their respective coin types and sub-feature indices. Please verify that all paths are correctly defined and that the indices correspond to the intended hierarchical levels as per the standards.

dash/src/bip32.rs (1)

1046-1051: Verify version bytes in encode_256 for ExtendedPubKey.

The version bytes used in the encode_256 method for ExtendedPubKey are [0x0E, 0xEC, 0xEF, 0xC5] for Network::Dash and [0x0E, 0xED, 0x27, 0x0B] for Network::Testnet. Ensure that these version bytes are correct and align with the DIP-14 specifications to prevent compatibility issues.

You can verify the correctness of the version bytes by checking the DIP-14 documentation or official Dash sources.

@@ -34,6 +34,7 @@ macro_rules! hex_psbt {
};
}

#[ignore]
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

#[ignore] attribute is still present in dash/tests/psbt.rs.

  • Remove the #[ignore] attribute to ensure the bip174_psbt_workflow test runs as part of the test suite.
🔗 Analysis chain

Consider removing #[ignore] and updating the entire test.

The change in the extended private key will affect the entire PSBT workflow test. Since this test is currently ignored, there's a risk that the changes haven't been fully validated against the new key.

  1. Please review and update all parts of the bip174_psbt_workflow test that depend on this key.
  2. Consider removing the #[ignore] attribute to ensure this test runs regularly and catches any regressions.

To help verify the impact, you can run the following script:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for occurrences of the old and new extended private key in the test file

OLD_KEY="tprv8ZgxMBicQKsPeUbMS6kswJc11zgVEXUnUZuGo3bF6bBrAg1ieFfUdPc9UHqbD5HcXizThrcKike1c4z6xHrz6MWGwy8L6YKVbgJMeQHdWDp"
NEW_KEY="tprv8ZgxMBicQKsPd9TeAdPADNnSyH9SSUUbTVeFszDE23Ki6TBB5nCefAdHkK8Fm3qMQR6sHwA56zqRmKmxnHk37JkiFzvncDqoKmPWubu7hDF"

echo "Checking for old key occurrences:"
rg "$OLD_KEY" dash/tests/psbt.rs

echo "Checking for new key occurrences:"
rg "$NEW_KEY" dash/tests/psbt.rs

echo "Checking for #[ignore] attribute:"
rg "#\[ignore\]" dash/tests/psbt.rs

Length of output: 940

Comment on lines +79 to +83
let secp = Secp256k1::new();
let sk = ExtendedPrivKey::new_master(network, seed)?;
let path = self.append_path(add_derivation_path);
sk.derive_priv(&secp, &path)
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Reuse Secp256k1 context for improved performance

The methods derive_priv_for_master_seed, derive_pub_for_master_seed, and derive_pub_for_master_extended_public_key each create a new Secp256k1 context instance using Secp256k1::new(). Since creating a Secp256k1 context is relatively inexpensive but can be reused, consider passing a reference to a single Secp256k1 context or using a static instance to improve performance.

Also applies to: 91-94, 101-104

Comment on lines +261 to +262
ChildNumber::Normal256 { .. } => u32::MAX,
ChildNumber::Hardened256 { .. } => u32::MAX,
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

⚠️ Potential issue

Potential issue with From<ChildNumber> implementation for 256-bit indices.

In the implementation of From<ChildNumber> for u32, mapping ChildNumber::Normal256 and ChildNumber::Hardened256 to u32::MAX could lead to unintended behavior or silent errors, as u32::MAX is a valid 32-bit child number. This could cause collisions or misrepresentations when converting ChildNumber variants to u32. Consider returning a Result<u32, Error> or an Option<u32> to handle cases where a 256-bit index cannot be represented as a u32.

Comment on lines +1284 to 1291
Normal { .. } | ChildNumber::Normal256 { .. } => {
let pk2 = pk.ckd_pub(secp, num).unwrap();
pk = ExtendedPubKey::from_priv(secp, &sk);
assert_eq!(pk, pk2);
}
Hardened { .. } => {
Hardened { .. } | ChildNumber::Hardened256 { .. } => {
assert_eq!(pk.ckd_pub(secp, num), Err(Error::CannotDeriveFromHardenedKey));
pk = ExtendedPubKey::from_priv(secp, &sk);
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

⚠️ Potential issue

Expand test coverage for 256-bit child number variants.

In the test suite, consider adding more test cases specifically targeting the new Normal256 and Hardened256 variants. This includes testing edge cases like maximum and minimum possible values, incrementing behavior, serialization/deserialization, and ensuring that the new variants interact correctly with existing functionality.

Comment on lines +208 to +241
ChildNumber::Normal256 { mut index } => {
// Increment the 256-bit big-endian number represented by index
let mut carry = 1u8;
for byte in index.iter_mut().rev() {
let (new_byte, overflow) = byte.overflowing_add(carry);
*byte = new_byte;
carry = if overflow { 1 } else { 0 };
if carry == 0 {
break;
}
}
if carry != 0 {
// Overflow occurred
return Err(Error::InvalidChildNumber(0)); // Or define a suitable error
}
Ok(ChildNumber::Normal256 { index })
}
ChildNumber::Hardened256 { mut index } => {
// Increment the 256-bit big-endian number represented by index
let mut carry = 1u8;
for byte in index.iter_mut().rev() {
let (new_byte, overflow) = byte.overflowing_add(carry);
*byte = new_byte;
carry = if overflow { 1 } else { 0 };
if carry == 0 {
break;
}
}
if carry != 0 {
// Overflow occurred
return Err(Error::InvalidChildNumber(0)); // Or define a suitable error
}
Ok(ChildNumber::Hardened256 { index })
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Optimize the increment logic for 256-bit indices.

The current implementation of the increment method for 256-bit indices manually handles carry-over using byte-wise addition. This can be optimized for readability and performance by utilizing existing crates like uint or num-bigint that offer arbitrary-sized integer arithmetic.

Consider refactoring the increment logic as follows:

use num_bigint::BigUint;
use num_traits::One;

impl ChildNumber {
    // ...

    ChildNumber::Normal256 { index } => {
        let value = BigUint::from_bytes_be(&index);
        let incremented = value + BigUint::one();
        let new_index = incremented.to_bytes_be();

        if new_index.len() > 32 {
            return Err(Error::InvalidChildNumber(0)); // Overflow occurred
        }

        let mut index_bytes = [0u8; 32];
        index_bytes[32 - new_index.len()..].copy_from_slice(&new_index);
        Ok(ChildNumber::Normal256 { index: index_bytes })
    }

    // Similar changes for Hardened256
}

Comment on lines +840 to +848
_ => panic!("Invalid child number for 256-bit format"),
};
ret[9] = hardening_byte;

// Child number (32 bytes)
let child_number_bytes = match self.child_number {
ChildNumber::Normal256 { index } | ChildNumber::Hardened256 { index } => index,
_ => panic!("Invalid child number for 256-bit format"),
};
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

⚠️ Potential issue

Avoid using panic! in library code; handle errors gracefully.

Using panic! in the encode_256 method for invalid child numbers can cause the application to terminate unexpectedly. Instead, consider returning an Error to allow the calling code to handle the situation gracefully.

Apply this diff to handle errors without panicking:

 fn encode_256(&self) -> Result<[u8; 107], Error> {
     let mut ret = [0u8; 107];

     // Version bytes
     let version: [u8; 4] = match self.network {
         Network::Dash => [0x0E, 0xEC, 0xF0, 0x2E],
         Network::Testnet | Network::Devnet | Network::Regtest => [0x0E, 0xED, 0x27, 0x74],
     };
     ret[0..4].copy_from_slice(&version);

     // Depth
     ret[4] = self.depth;

     // Parent fingerprint
     ret[5..9].copy_from_slice(&self.parent_fingerprint[..]);

     // Hardening byte
     let hardening_byte = match self.child_number {
         ChildNumber::Normal256 { .. } => 0x00,
         ChildNumber::Hardened256 { .. } => 0x01,
-        _ => panic!("Invalid child number for 256-bit format"),
+        _ => return Err(Error::InvalidChildNumberFormat),
     };
     ret[9] = hardening_byte;

     // Child number (32 bytes)
     let child_number_bytes = match self.child_number {
         ChildNumber::Normal256 { index } | ChildNumber::Hardened256 { index } => index,
-        _ => panic!("Invalid child number for 256-bit format"),
+        _ => return Err(Error::InvalidChildNumberFormat),
     };
     ret[10..42].copy_from_slice(&child_number_bytes);

     // Chain code (32 bytes)
     ret[42..74].copy_from_slice(&self.chain_code[..]);

     // Key data (33 bytes)
     ret[74] = 0x00; // Padding for private key
     ret[75..107].copy_from_slice(&self.private_key[..]);

-    ret
+    Ok(ret)
 }
📝 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
_ => panic!("Invalid child number for 256-bit format"),
};
ret[9] = hardening_byte;
// Child number (32 bytes)
let child_number_bytes = match self.child_number {
ChildNumber::Normal256 { index } | ChildNumber::Hardened256 { index } => index,
_ => panic!("Invalid child number for 256-bit format"),
};
_ => return Err(Error::InvalidChildNumberFormat),
};
ret[9] = hardening_byte;
// Child number (32 bytes)
let child_number_bytes = match self.child_number {
ChildNumber::Normal256 { index } | ChildNumber::Hardened256 { index } => index,
_ => return Err(Error::InvalidChildNumberFormat),
};

Comment on lines +290 to +315
let is_hardened = inp.ends_with('\'') || inp.ends_with('h');
let index_str = if is_hardened { &inp[..inp.len() - 1] } else { inp };

if index_str.starts_with("0x") || index_str.starts_with("0X") {
// Parse as a 256-bit hex number
let hex_str = &index_str[2..];
let hex_bytes = hex::decode(hex_str).map_err(|_| Error::InvalidChildNumberFormat)?;
if hex_bytes.len() != 32 {
return Err(Error::InvalidChildNumberFormat);
}
let mut index_bytes = [0u8; 32];
index_bytes[32 - hex_bytes.len()..].copy_from_slice(&hex_bytes);
if is_hardened {
Ok(ChildNumber::Hardened256 { index: index_bytes })
} else {
Ok(ChildNumber::Normal256 { index: index_bytes })
}
} else {
ChildNumber::from_normal_idx(inp.parse().map_err(|_| Error::InvalidChildNumberFormat)?)?
})
// Parse as a u32 number
let index = index_str.parse::<u32>().map_err(|_| Error::InvalidChildNumberFormat)?;
if is_hardened {
ChildNumber::from_hardened_idx(index)
} else {
ChildNumber::from_normal_idx(index)
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Validate 256-bit index length in FromStr implementation.

When parsing a hexadecimal string for a 256-bit index in the FromStr implementation of ChildNumber, the code does not enforce that the hex string represents exactly 64 hexadecimal characters (32 bytes). If the input is shorter, the index is left-padded with zeros, which might not be intended and could lead to incorrect child numbers. It's advisable to enforce the exact length requirement for 256-bit indices and return an error if the input length is incorrect.

Apply this diff to enforce the length check:

 if index_str.starts_with("0x") || index_str.starts_with("0X") {
     // Parse as a 256-bit hex number
     let hex_str = &index_str[2..];
+    if hex_str.len() != 64 {
+        return Err(Error::InvalidChildNumberFormat);
+    }
     let hex_bytes = hex::decode(hex_str).map_err(|_| Error::InvalidChildNumberFormat)?;
     let mut index_bytes = [0u8; 32];
     index_bytes.copy_from_slice(&hex_bytes);
     if is_hardened {
         Ok(ChildNumber::Hardened256 { index: index_bytes })
     } else {
         Ok(ChildNumber::Normal256 { index: index_bytes })
     }
 }
📝 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
let is_hardened = inp.ends_with('\'') || inp.ends_with('h');
let index_str = if is_hardened { &inp[..inp.len() - 1] } else { inp };
if index_str.starts_with("0x") || index_str.starts_with("0X") {
// Parse as a 256-bit hex number
let hex_str = &index_str[2..];
let hex_bytes = hex::decode(hex_str).map_err(|_| Error::InvalidChildNumberFormat)?;
if hex_bytes.len() != 32 {
return Err(Error::InvalidChildNumberFormat);
}
let mut index_bytes = [0u8; 32];
index_bytes[32 - hex_bytes.len()..].copy_from_slice(&hex_bytes);
if is_hardened {
Ok(ChildNumber::Hardened256 { index: index_bytes })
} else {
Ok(ChildNumber::Normal256 { index: index_bytes })
}
} else {
ChildNumber::from_normal_idx(inp.parse().map_err(|_| Error::InvalidChildNumberFormat)?)?
})
// Parse as a u32 number
let index = index_str.parse::<u32>().map_err(|_| Error::InvalidChildNumberFormat)?;
if is_hardened {
ChildNumber::from_hardened_idx(index)
} else {
ChildNumber::from_normal_idx(index)
}
}
let is_hardened = inp.ends_with('\'') || inp.ends_with('h');
let index_str = if is_hardened { &inp[..inp.len() - 1] } else { inp };
if index_str.starts_with("0x") || index_str.starts_with("0X") {
// Parse as a 256-bit hex number
let hex_str = &index_str[2..];
if hex_str.len() != 64 {
return Err(Error::InvalidChildNumberFormat);
}
let hex_bytes = hex::decode(hex_str).map_err(|_| Error::InvalidChildNumberFormat)?;
let mut index_bytes = [0u8; 32];
index_bytes.copy_from_slice(&hex_bytes);
if is_hardened {
Ok(ChildNumber::Hardened256 { index: index_bytes })
} else {
Ok(ChildNumber::Normal256 { index: index_bytes })
}
} else {
// Parse as a u32 number
let index = index_str.parse::<u32>().map_err(|_| Error::InvalidChildNumberFormat)?;
if is_hardened {
ChildNumber::from_hardened_idx(index)
} else {
ChildNumber::from_normal_idx(index)
}
}

@QuantumExplorer QuantumExplorer merged commit 6a0ec16 into master Oct 17, 2024
0 of 24 checks passed
@QuantumExplorer QuantumExplorer deleted the chore/smallfmt branch October 17, 2024 15:51
@coderabbitai coderabbitai bot mentioned this pull request Oct 30, 2024
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