Skip to content

ProofSystem/include/ProofSystem/BitcoinKeyGenerator.hpp

Bitcoin address generator header file. More...

Namespaces

Name
bitcoin

Classes

Name
class bitcoin::BitcoinKeyGenerator
Creates a pair of ECDSA keys and a Bitcoin address from a compressed key.

Detailed Description

Bitcoin address generator header file.

Date: 2023-12-08 Super Genius ([email protected]) Henrique A. Klein ([email protected])

Source code

#ifndef BITCOIN_KEY_GENERATOR_HPP
#define BITCOIN_KEY_GENERATOR_HPP

#include <string>
#include <vector>
#include <memory>
#include <cstdint>
#include <utility>

#include "ProofSystem/BitcoinKeyPairParams.hpp"
#include "ProofSystem/ext_private_key.hpp"
#include "ProofSystem/ECDSAPublicKey.hpp"

namespace bitcoin
{
    using namespace nil::crypto3;

    class BitcoinKeyGenerator
    {
    public:
        using PubKeyPair_t = std::pair<std::vector<std::uint8_t>, std::vector<std::uint8_t>>;

        BitcoinKeyGenerator();
        BitcoinKeyGenerator( std::string_view private_key );
        BitcoinKeyGenerator( const scalar_field_value_type &private_key );
        const pubkey::ext_private_key<policy_type> &get_private_key() const
        {
            return *privkey;
        }
        const pubkey::public_key<policy_type> &get_public_key() const
        {
            return *pubkey;
        }
        const std::string &get_address() const
        {
            return address;
        }
        const std::string GetUsedPubKeyValue() const
        {
            return *pubkey_info;
        }
        const std::string GetEntirePubValue() const
        {
            return pubkey_info->GetEntireKey();
        }
        template <typename T>
        static T ExtractPubKeyFromField( const pubkey::public_key<policy_type> &pub_key );
        static std::string DeriveAddress( const std::vector<std::uint8_t> &pub_key_vect );

        static std::shared_ptr<pubkey::ext_private_key<policy_type>> CreateKeys();

    private:
        static random_generator_type                          key_gen; 
        std::shared_ptr<pubkey::ext_private_key<policy_type>> privkey; 
        std::shared_ptr<pubkey::public_key<policy_type>>      pubkey;  

        class BitcoinECDSAPublicKey : public ECDSAPublicKey
        {
            using ECDSAPublicKey::ECDSAPublicKey;

            std::string CalcPubkeyUsedValue() const override
            {
                const std::string compressed_byte_id = ( ( X_vect.front() % 2 ) ? "03" : "02" );
                return ( compressed_byte_id + X );
            }
        };

        std::shared_ptr<BitcoinECDSAPublicKey> pubkey_info; 
        std::string                                           address; 

        static constexpr std::uint8_t MAIN_NETWORK_ID     = 0; 
        static constexpr std::uint8_t PARITY_EVEN_ID      = 2; 
        static constexpr std::uint8_t PARITY_ODD_ID       = 3; 
        static constexpr std::uint8_t CHECKSUM_SIZE_BYTES = 4; 

        std::string DeriveAddress( void );
    };
}
#endif // BITCOIN_KEY_GENERATOR_HPP

Updated on 2026-03-04 at 13:10:44 -0800