Skip to content

Conversation

horologger
Copy link

Summary

This PR adds a new --hex-secret (short option -x) to both the exportwallet and importwallet subcommands in space-cli, enabling users to work with hex-encoded private keys directly.

Features Added

Export Wallet with Hex Secret (exportwallet -x)

  • New option: --hex-secret / -x flag for space-cli exportwallet
  • Output enhancement: When -x is provided, the exported JSON includes a hex-secret field containing the hex-encoded private key
  • Placement: The hex-secret field is positioned after the label element in the JSON output
  • Backward compatibility: Existing export functionality remains unchanged when -x is not used

Import Wallet with Hex Secret (importwallet -x)

  • New option: --hex-secret / -x flag for space-cli importwallet
  • Hex-to-xprv conversion: Automatically converts hex secrets to proper xprv (extended private key) format
  • Descriptor generation: Creates valid Bitcoin taproot descriptors using the /86'/0'/0'/0/* derivation path
  • Flexible input: When -x is used, the descriptor field becomes optional in the import JSON
  • Validation: Ensures hex secrets are exactly 32 bytes (64 hex characters)

Technical Implementation

Core Changes

  1. Wallet Export Enhancement (wallet/src/export.rs, wallet/src/lib.rs)
    Added hex_secret field to WalletExport struct
    Implemented extract_hex_secret() method in SpacesWallet
    Modified export_wallet() to conditionally include hex secret

  2. RPC Interface Updates (client/src/rpc.rs)
    Updated wallet_export RPC method to accept hex_secret parameter
    Added xpriv_from_hex_secret() function for proper hex-to-xprv conversion
    Implemented hex-based descriptor generation in wallet creation process
    Added remove_checksum() helper function for descriptor formatting

  3. CLI Command Enhancements (client/src/bin/space-cli.rs)
    Added --hex-secret / -x options to both export and import commands
    Implemented logic to skip descriptor field validation when using hex import
    Added proper error handling for hex secret validation

Key Technical Details

  • Hex Secret Validation: Ensures 32-byte (64 hex character) input
  • Xprv Conversion: Uses Bitcoin's secp256k1 to create proper extended private keys
  • Descriptor Generation: Creates BIP86 taproot descriptors with proper derivation paths
  • Wallet Creation: Uses temporary wallet creation to extract properly formatted descriptor strings
  • Error Handling: Comprehensive validation and user-friendly error messages

Usage Examples

Export with Hex Secret

space-cli exportwallet -x ./wallet.json
Output includes:

{
  "descriptor": "tr([xprv...]/86'/0'/0'/0/*)",
  "blockheight": 910000,
  "label": "my_wallet",
  "hex_secret": "8e91ef66b88fd8ce4daefeb357e300a6906ead06dc25882915a9c5d4624ebd5e"
}

Import with Hex Secret

space-cli importwallet -x ./wallet.json
Input JSON (descriptor field optional):

{
  "blockheight": 910000,
  "label": "imported_wallet",
  "hex_secret": "8e91ef66b88fd8ce4daefeb357e300a6906ead06dc25882915a9c5d4624ebd5e"
}

Testing

  • ✅ Import Success: Successfully imports wallets using hex secrets
  • ✅ Descriptor Generation: Creates valid Bitcoin taproot descriptors
  • ✅ Validation: Properly validates hex secret format and length
  • ✅ Error Handling: Clear error messages for invalid inputs
  • ✅ Backward Compatibility: Existing functionality remains unchanged

Files Modified

  • client/src/bin/space-cli.rs - CLI command definitions and argument handling
  • client/src/rpc.rs - RPC interface and wallet creation logic
  • wallet/src/export.rs - Wallet export structure and serialization
  • wallet/src/lib.rs - Core wallet functionality for hex secret extraction

Breaking Changes

None. This is a purely additive feature that maintains full backward compatibility.

Related Issues

Resolves the need for direct hex private key support in wallet import/export operations, enabling easier integration with external key management systems and tools that work with raw hex-encoded private keys.

@horologger
Copy link
Author

Testing showed hex of secret was being used as seed not directly. round trip created different hex. Fixed.

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