Improve import private key via gemstone#1741
Conversation
Add base58 encoding support for UTXO chains (Bitcoin, Litecoin, Doge, BitcoinCash) and handle WIF format (37/38 byte decoded output) in private key decoding.
Summary of ChangesHello @DRadmir, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the private key import capabilities for UTXO-based cryptocurrencies by introducing comprehensive base58 encoding support and specific handling for the Wallet Import Format (WIF). This ensures that users can seamlessly import Bitcoin, Litecoin, Doge, and BitcoinCash private keys, improving the overall compatibility and robustness of the key management system. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds support for importing Bitcoin WIF private keys. The changes correctly identify UTXO chains that use base58 encoding and add a test case for WIF import. However, there are critical issues in the key decoding logic. The implementation does not validate the WIF checksum, creating a risk of importing invalid keys. More importantly, it ignores the public key compression information from the WIF string, which will lead to incorrect address generation for legacy uncompressed keys and potential loss of access to funds. I've provided a detailed comment with a suggestion to fix the checksum validation and an explanation of the compression issue.
| if let decoded = Base58.decodeNoCheck(string: key) { | ||
| if decoded.count % 32 == 0 { | ||
| data = Data(decoded.prefix(32)) | ||
| } else if decoded.count == 37 || decoded.count == 38 { | ||
| // WIF format: [1 version] + [32 key] + [4 checksum] (37 bytes, uncompressed) | ||
| // or [1 version] + [32 key] + [1 compression flag] + [4 checksum] (38 bytes, compressed) | ||
| data = Data(decoded[1..<33]) | ||
| } | ||
| } |
There was a problem hiding this comment.
This block has two critical issues related to WIF private key handling:
-
Missing Checksum Validation: It uses
Base58.decodeNoCheck, which skips the checksum validation inherent to the WIF format. This could lead to importing a corrupted or mistyped key, potentially resulting in loss of funds. You should useBase58.decodeto validate the checksum. -
Ignoring Public Key Compression: The logic extracts the 32-byte key but ignores whether the WIF corresponds to a compressed (38 bytes decoded) or uncompressed (37 bytes decoded) public key. Since
deriveAddressfor Bitcoin defaults to using a compressed public key, this will generate an incorrect address for uncompressed WIF keys, making funds on those addresses inaccessible.
A proper implementation should validate the checksum and handle the compression flag. This likely requires refactoring to pass the compression information out of decodeKey so it can be used when deriving the address.
Here's a suggestion that fixes the checksum validation. The compression issue is more complex and may require changes outside this function.
| if let decoded = Base58.decodeNoCheck(string: key) { | |
| if decoded.count % 32 == 0 { | |
| data = Data(decoded.prefix(32)) | |
| } else if decoded.count == 37 || decoded.count == 38 { | |
| // WIF format: [1 version] + [32 key] + [4 checksum] (37 bytes, uncompressed) | |
| // or [1 version] + [32 key] + [1 compression flag] + [4 checksum] (38 bytes, compressed) | |
| data = Data(decoded[1..<33]) | |
| } | |
| } | |
| if let decoded = Base58.decode(string: key), (decoded.count == 37 || decoded.count == 38) { | |
| // WIF format with checksum validation. | |
| // Note: The compression flag is still being ignored here, which is a separate critical issue. | |
| // A full fix would require passing whether the key is compressed out of this function. | |
| data = Data(decoded[1..<33]) | |
| } else if let decoded = Base58.decodeNoCheck(string: key), decoded.count % 32 == 0 { | |
| // Fallback for non-checksummed keys (e.g., Solana). | |
| data = Data(decoded.prefix(32)) | |
| } |
| case .base58: | ||
| if let decoded = Base58.decodeNoCheck(string: key), decoded.count % 32 == 0 { | ||
| data = decoded.prefix(32) | ||
| if let decoded = Base58.decodeNoCheck(string: key) { |
There was a problem hiding this comment.
maybe move the base58 decoding logic to separate func decodeBase58Key ?
Rename and centralize private-key encoding APIs and adjust import flows. - Replace getPrivateKey(..., encoding:) with getPrivateKeyEncoded(...) across service, protocol, local keystore and mocks. - Remove per-chain EncodingType enum and Chain+Keystore extension; delegate encoding/decoding to GemstonePrimitives (decodePrivateKey / supportsPrivateKeyImport / Base58 handling moved). - Simplify LocalKeystore to return encoded private key based on chain type and zeroize raw bytes. - Move private-key decoding logic in WalletKeyStore to use GemstonePrimitives decode function and simplify validation. - Update ImportWallet flow to async completion handlers, add support check for private key import per-chain, present errors via alert, and adjust navigation handling. - Update Package.swift for Keystore to add GemstonePrimitives dependency and adjust targets; remove obsolete Package.resolved and deleted tests relying on removed encoding types. - Update tests to use the new encoded API and remove outdated WIF/encoding unit tests. - Bump core submodule commit. These changes centralize encoding logic, remove duplicated encoding types, and modernize async import handling and error reporting.
Add base58 encoding support for UTXO chains (Bitcoin, Litecoin, Doge, BitcoinCash) and handle WIF format (37/38 byte decoded output) in private key decoding.
Fix: #1738