candidateblock_bitcoin_library.keys module

class candidateblock_bitcoin_library.keys.Keys

Bases: object

Bitcoin Private and Public Key Class

Bitcoin has a Private Key (that should remain secret) and a Public Key that can be shared. A Private key is just a random 256-Bit (32-byte) number (with some size limitation due to discrete Elliptic Curve). A Public key is generated from a one way cryptographic function called secp256k1 which uses an Elliptic Curve.

classmethod btc_address_p2pkh(pub_key: bytes = b'', is_mainnet: bool = True) str

Convert the Public Key to a Pay To PubKey Hash (P2PKH) Bitcoin address

The Public Key can be compressed (33-Bytes) or uncompressed (32-Bytes) it is hashed via HASH160 resulting in 160-Bits, 20-Bytes (a reduction) The result is prefixed with PayToPubKeyHash 0x00 and Base58Check encoded

Parameters:

pub_key (bytes) – Public Key 32/33-bytes, 256/264-bits

Returns:

P2PKH Bitcoin address Base58Check encoded

Return type:

str

classmethod generate_priv_key() bytes

Generate Private Key

Generate a 256-Bit Private Key by using a cryptographically secure random number and checking valid over Elliptic Curve finite field

Returns:

Raw 256-bit (32-byte) Private Key = Random Number

Return type:

bytes

classmethod generate_pub_key(priv_key: bytes = b'', is_compressed: bool = True) bytes

Calculate Public key from the Private Key

The public key is calculated from the private key using elliptic curve multiplication, which is irreversible: K = k * G, where k is the private key, G is a constant point called the generator point, and K is the resulting public key.

The secp256k1 curve is defined by the following function, y^2 = (X^3 + 7) over Fp or Y^2 mod p = (x^3 +7) mod p The mod p (modulo prime number p) indicates that this curve is over a finite field of prime order p, where p = 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 1, a very large prime number.

“Uncompressed” Public Key Format “x04” prefix (1-Byte) -> “Uncompressed” + X Coordinate as 256-Bit (32-Byte) + Y Coordinate as 256-Bit (32-Byte) result string = 520-Bit (65-Byte)

“Compressed” Public Key Format “Compressed” means store x coordinate value and y coordinate sign “x02” prefix (1-Byte) if y is even “x03” prefix (1-Byte) if y is odd Even or Odd Hex prefix -> “Compressed” + X Coordinate as 256-Bit (32-Byte) result string = 264-Bit (33) This results in nearly a 50% size reduction compared to the “Uncompressed” Private Key size of 520-Bit (65-Bytes) 264 / 520 = 50.8%

Parameters:
  • priv_key (bytes) – private key 32-bytes, 256-bits

  • is_compressed (bool) – private key format

Returns:

Public key 32-bytes, 256-bits

Return type:

bytes

classmethod is_priv_key_valid(priv_key: bytes) bool

Verify if Private Key Valid

Check if number (containted in input bytes) is within the range limits of the Elliptic Curve which is defined over a finite field of prime order instead of over the real numbers

Parameters:

priv_key (bytes) – private key 32-bytes, 256-bits

Returns:

private key is valid

Return type:

bool

classmethod priv_key_wif_decode(wif_b58: str = '') tuple

Private Key ‘Wallet Import Format’ (Base58 Check) decoded to private key, compressed, network

Decode Private key WIF back to constituent parts. Verify length, Base58 Check checksum and Version Byte Prefix for Mainnet or Testnet

Parameters:

wif_b58 (str) – Base58 encoded WIF Private Key

Returns:

A tuple containing, respectively, a bytes (private key 32-bytes, 256-bits) and a bool (Should this private key be used to generate compressed public keys) and a bool (Network for the private key to be used on [Mainnet or Testnet]).

classmethod priv_key_wif_encode(priv_key: bytes = b'', is_compressed: bool = True, is_mainnet: bool = True) str

Private Key encoded to ‘Wallet Import Format’ (Base58 Check)

A WIF private key is a standard private key, but with a few added extras: 1. Version Byte prefix - Indicates which network the private key is to be used on. 2. Compression Byte suffix (optional) 3. Checksum

Parameters:
  • priv_key (bytes) – private key 32-bytes, 256-bits

  • is_compressed (bool) – Should this private key be used to generate compressed public keys

  • is_mainnet (bool) – Network for the private key to be used on (Mainnet or Testnet)

Returns:

Base58 Check encoded WIF Private Key

Return type:

str