Davide Grilli 54d282a36b feat: add Electrum-compatible wallet encryption
- Add src/crypto.py with AES-256-CBC pw_encode/pw_decode
  using double SHA256 key derivation (Electrum standard)
- Add encrypt_wallet() and decrypt_wallet() to src/hd_wallet.py
- Prompt for encryption password when saving HD wallet to file
- Add 13 encryption/decryption tests to tests/test_hd_wallet.py
- Add pycryptodome to requirements.txt
2026-03-09 13:35:33 +01:00
2026-03-09 12:17:07 +01:00

Bitcoin Address Generator

This program allows you to generate different types of Bitcoin addresses in a simple and interactive way. The output includes private keys, public keys, addresses and WIF formats, and can be saved to JSON files.

Address validity was verified using the external tool SecretScan.

Currently supported address types:

  • P2PK (Pay-to-PubKey)
  • P2PKH (Pay-to-PubKey-Hash)
  • P2SH (Pay-to-Script-Hash, with multisig support)
  • P2WPKH (Pay-to-Witness-PubKey-Hash, SegWit v0)
  • P2TR (Pay-to-Taproot, SegWit v1)
  • HD Wallet (BIP-32/39 with BIP-44/49/84/86 derivation)

In development:

  • P2WSH (Pay-to-Witness-Script-Hash)

How it works

The entry point is __main__.py. When run, it displays an interactive menu to choose the address type to generate:

python .

Example output:

=== BITCOIN ADDRESS GENERATOR ===
Select address type:
1. P2PK
2. P2PKH
3. P2SH
4. P2WPKH
5. P2TR
6. HD Wallet (BIP-44/49/84/86)

After selecting an option, the dedicated script runs and guides the user through:

  • Network selection (mainnet, testnet, regtest)
  • Optional use of compressed/uncompressed keys
  • Displaying and saving the data to a .json file

Each script is independent (src/p2pk.py, src/p2pkh.py, src/p2sh.py, src/p2wpkh.py, src/p2tr.py, src/hd_wallet.py) and implements the rules of the respective Bitcoin standard.


Supported address types

1. P2PK (Pay-to-PubKey)

  • Standard: Original Bitcoin format, defined in Satoshi Nakamoto's whitepaper
  • Address format: No standard address format — uses the public key directly
  • Script: <pubkey> OP_CHECKSIG
  • Pros: very simple, directly represents the public key, minimal transaction size
  • Cons: obsolete, incompatible with most modern wallets. Exposes the public key directly on the blockchain, vulnerable to quantum attacks
  • Current use: Mainly in coinbase transactions and very specific cases

2. P2PKH (Pay-to-PubKey-Hash)

  • Standard: BIP-13 (Base58Check), legacy standard format
  • Address format: Starts with '1' (mainnet), 'm' or 'n' (testnet)
  • Script: OP_DUP OP_HASH160 <pubkey_hash> OP_EQUALVERIFY OP_CHECKSIG
  • Encoding: Base58Check with prefix 0x00 (mainnet)
  • Pros: the "legacy" standard, widely used, supported by all wallets and exchanges. Protection against quantum attacks (public key hash)
  • Cons: addresses are longer and transaction fees are higher compared to modern (SegWit) types, larger transaction sizes

3. P2SH (Pay-to-Script-Hash)

  • Standard: BIP-16 (Pay to Script Hash), BIP-67 (Deterministic Pay-to-script-hash multi-signature addresses)
  • Address format: Starts with '3' (mainnet), '2' (testnet/regtest)
  • Script: OP_HASH160 <script_hash> OP_EQUAL
  • Encoding: Base58Check with prefix 0x05 (mainnet), 0xC4 (testnet/regtest)
  • Pros: enables addresses based on arbitrary scripts, ideal for multisig and complex contracts. Supported by all modern wallets. Greater flexibility and advanced features
  • Cons: fees are slightly higher than single-key addresses and requires revealing the script at spend time. Larger transaction sizes due to redeem script

Current implementation: Multisig P2SH support is currently limited to multisig scripts, which represent the most common P2SH use case.

Available options:

  • m-of-n configuration: choose how many signatures are required (m) out of a total number of keys (n)
    • Examples: 2-of-3, 3-of-5, 1-of-2, etc.
    • Limit: 1 ≤ m ≤ n ≤ 16
  • Network: mainnet, testnet, regtest
  • Compressed keys: option to use compressed (33 bytes) or uncompressed (65 bytes) public keys
  • BIP67 sorting: public keys are automatically sorted to prevent malleability

Implemented features:

  • Automatic generation of n key pairs (private/public)
  • Multisig redeem script construction
  • hash160 computation of the redeem script
  • Final P2SH address generation
  • Structured JSON output with all necessary data
  • Private key export in WIF format
  • Full JSON file save for backup and future use

4. P2WPKH (SegWit v0, Bech32)

  • Standard: BIP-141 (Segregated Witness), BIP-173 (Base32 address format)
  • Address format: Starts with 'bc1q' (mainnet), 'tb1q' (testnet), 'bcrt1q' (regtest)
  • Script: OP_0 <pubkey_hash> (20 bytes)
  • Encoding: Bech32 with HRP 'bc' (mainnet), 'tb' (testnet), 'bcrt' (regtest)
  • Witness program: version 0, 20 bytes (hash160 of the public key)
  • Pros: lower fees thanks to SegWit (witness data separated), more compact addresses, supported by almost all modern wallets, protection against transaction malleability
  • Cons: not all legacy services accept Bech32, requires SegWit support

5. P2TR (Taproot, SegWit v1, Bech32m)

  • Standard: BIP-340 (Schnorr Signatures), BIP-341 (Taproot), BIP-342 (Tapscript), BIP-350 (Bech32m)
  • Address format: Starts with 'bc1p' (mainnet), 'tb1p' (testnet), 'bcrt1p' (regtest)
  • Script: OP_1 <taproot_output> (32 bytes)
  • Encoding: Bech32m with HRP 'bc' (mainnet), 'tb' (testnet), 'bcrt' (regtest)
  • Witness program: version 1, 32 bytes (tweaked public key)
  • Pros: the most recent type, with greater privacy and flexibility (supports complex scripts hidden behind a single address). Low fees, more efficient Schnorr signatures, signature aggregation
  • Cons: still relatively new, not supported by all services, higher implementation complexity

6. HD Wallet (BIP-32/39/44/49/84/86)

  • Standards: BIP-32 (hierarchical deterministic wallets), BIP-39 (mnemonic phrases), BIP-44/49/84/86 (derivation paths)
  • Networks: mainnet, testnet
  • Derivation paths:
    • BIP-44 → P2PKH: m/44'/0'/account'/0/index — addresses start with 1 (mainnet)
    • BIP-49 → P2SH-P2WPKH: m/49'/0'/account'/0/index — addresses start with 3 (mainnet)
    • BIP-84 → P2WPKH: m/84'/0'/account'/0/index — addresses start with bc1q (mainnet)
    • BIP-86 → P2TR: m/86'/0'/account'/0/index — addresses start with bc1p (mainnet)
  • Extended key prefixes: xpub/xprv (BIP-44/86), ypub/yprv (BIP-49), zpub/zprv (BIP-84)
  • JSON output: compatible with Electrum wallet format (keystore, xpub, xprv, derivation, root_fingerprint, seed_version: 17)
  • Pros: deterministic key derivation from a single mnemonic, interoperable with Electrum and other BIP-39 compatible wallets
  • Cons: requires securely backing up the mnemonic phrase

Available options:

  • Generate a new 12-word mnemonic or import an existing one
  • Optional BIP-39 passphrase
  • Account index selection
  • Configurable number of receiving addresses to derive

In development

  • P2WSH (Pay-to-Witness-Script-Hash): SegWit version of P2SH, more efficient and secure.

Usage

  1. Clone or download the repository.

  2. Make sure Python 3 is installed and install the requirements:

    Linux/macOS (bash/zsh):

    # Recommended: use a virtual environment
    python3 -m venv venv   # or: python -m venv venv
    source venv/bin/activate
    
    # Install requirements
    pip install -r requirements.txt
    

    Windows (PowerShell):

    # Recommended: use a virtual environment
    python -m venv venv
    .\venv\Scripts\Activate.ps1
    
    # Install requirements
    pip install -r requirements.txt
    
  3. Run the program from the repository root:

    python .
    
  4. Follow the on-screen instructions to generate and save your address.

The data will be saved in a readable and reusable .json file.


License

This project is released under the MIT license.

Description
No description provided
Readme MIT 447 KiB
Languages
Python 54.1%
JavaScript 29.9%
CSS 11.1%
Dockerfile 4.2%
Shell 0.5%
Other 0.2%