bip38_ec

Module for BIP38 encryption/decryption. Reference: https://github.com/bitcoin/bips/blob/master/bip-0038.mediawiki

class Bip38EcConst

Bases: object

Class container for BIP38 EC constants.

LOT_NUM_MIN_VAL: int = 0
LOT_NUM_MAX_VAL: int = 1048575
SEQ_NUM_MIN_VAL: int = 0
SEQ_NUM_MAX_VAL: int = 4095
OWNER_SALT_WITH_LOT_SEQ_BYTE_LEN: int = 4
OWNER_SALT_NO_LOT_SEQ_BYTE_LEN: int = 8
INT_PASS_ENC_BYTE_LEN: int = 49
INT_PASS_MAGIC_WITH_LOT_SEQ = b',\xe9\xb3\xe1\xff9\xe2Q'
INT_PASS_MAGIC_NO_LOT_SEQ = b',\xe9\xb3\xe1\xff9\xe2S'
SEED_B_BYTE_LEN: int = 24
ENC_BYTE_LEN: int = 39
ENC_KEY_PREFIX: bytes = b'\x01C'
FLAG_BIT_COMPRESSED: int = 5
FLAG_BIT_LOT_SEQ: int = 2
SCRYPT_PREFACTOR_KEY_LEN: int = 32
SCRYPT_PREFACTOR_N: int = 16384
SCRYPT_PREFACTOR_P: int = 8
SCRYPT_PREFACTOR_R: int = 8
SCRYPT_HALVES_KEY_LEN: int = 64
SCRYPT_HALVES_N: int = 1024
SCRYPT_HALVES_P: int = 1
SCRYPT_HALVES_R: int = 1
class Bip38EcKeysGenerator

Bases: object

BIP38 keys generator class. It generates intermediate codes and private keys using the algorithm specified in BIP38 with EC multiplication.

static GenerateIntermediatePassphrase(passphrase: str, lot_num: Optional[int] = None, sequence_num: Optional[int] = None) str

Generate an intermediate passphrase from the user passphrase as specified in BIP38.

Parameters:
  • passphrase (str) – Passphrase

  • lot_num (int, optional) – Lot number

  • sequence_num (int, optional) – Sequence number

Returns:

Intermediate passphrase encoded in base58

Return type:

str

static GeneratePrivateKey(int_passphrase: str, pub_key_mode: P2PKHPubKeyModes) str

Generate a random encrypted private key from the intermediate passphrase.

Parameters:
  • int_passphrase (str) – Intermediate passphrase

  • pub_key_mode (Bip38PubKeyModes) – Public key mode

Returns:

Encrypted private key

Return type:

str

Raises:
  • Base58ChecksumError – If base58 checksum is not valid

  • ValueError – If the intermediate code is not valid

class Bip38EcDecrypter

Bases: object

BIP38 decrypter class. It decrypts a private key using the algorithm specified in BIP38 with EC multiplication.

static Decrypt(priv_key_enc: str, passphrase: str) Tuple[bytes, P2PKHPubKeyModes]

Decrypt the specified private key.

Parameters:
  • priv_key_enc (str) – Encrypted private key bytes

  • passphrase (str) – Passphrase

Returns:

Decrypted private key (index 0), public key mode (index 1)

Return type:

tuple[bytes, Bip38PubKeyModes]

Raises:
  • Base58ChecksumError – If base58 checksum is not valid

  • ValueError – If the encrypted key is not valid