Skip to content

Improve import private key via gemstone#1741

Merged
gemcoder21 merged 3 commits intomainfrom
fix/bitcoin-wif-import
Feb 27, 2026
Merged

Improve import private key via gemstone#1741
gemcoder21 merged 3 commits intomainfrom
fix/bitcoin-wif-import

Conversation

@DRadmir
Copy link
Contributor

@DRadmir DRadmir commented Feb 26, 2026

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

Add base58 encoding support for UTXO chains (Bitcoin, Litecoin, Doge, BitcoinCash)
and handle WIF format (37/38 byte decoded output) in private key decoding.
@DRadmir DRadmir self-assigned this Feb 26, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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

  • Base58 Encoding Support: Extended base58 encoding support to Bitcoin, Litecoin, Doge, and BitcoinCash chains for private key handling.
  • WIF Private Key Decoding: Implemented logic to correctly decode Wallet Import Format (WIF) private keys, specifically handling 37-byte (uncompressed) and 38-byte (compressed) base58 decoded outputs.
  • Test Coverage: Added a new unit test to validate the successful import and address derivation of Bitcoin private keys using the WIF format.

🧠 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
  • Packages/Keystore/Sources/Extensions/Chain+Keystore.swift
    • Enabled ".base58" encoding for Bitcoin, Litecoin, Doge, and BitcoinCash chains.
  • Packages/Keystore/Sources/Store/WalletKeyStore.swift
    • Modified the "decodeKey" function to identify and correctly parse WIF-formatted private keys (37 or 38 bytes after base58 decoding), extracting the core 32-byte private key.
  • Packages/Keystore/Tests/WalletKeyStoreTests.swift
    • Introduced "importBitcoinWIF" test to confirm that a Bitcoin WIF string can be decoded into the correct private key data and subsequently derive the expected address.
Activity
  • No human activity has occurred on this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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.

Comment on lines 51 to 59
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])
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This block has two critical issues related to WIF private key handling:

  1. 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 use Base58.decode to validate the checksum.

  2. 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 deriveAddress for 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.

Suggested change
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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

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.
@gemcoder21 gemcoder21 changed the title Add Bitcoin WIF private key import support (#1738) Improve import private key via gemstone Feb 27, 2026
@gemcoder21 gemcoder21 merged commit 9d6114f into main Feb 27, 2026
1 check passed
@gemcoder21 gemcoder21 deleted the fix/bitcoin-wif-import branch February 27, 2026 02:28
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.

iOS import hangs indefinitely when loading old legacy Bitcoin WIF

3 participants