bip38

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

class Bip38Encrypter

Bases: object

BIP38 encrypter class. It encrypts a private key using the algorithm specified in BIP38.

static EncryptNoEc(priv_key: Union[bytes, IPrivateKey], passphrase: str, pub_key_mode: P2PKHPubKeyModes = P2PKHPubKeyModes.COMPRESSED) str

Encrypt the specified private key without EC multiplication.

Parameters:
  • priv_key (bytes or IPrivateKey) – Private key bytes or object

  • passphrase (str) – Passphrase

  • pub_key_mode (Bip38PubKeyModes, optional) – Public key mode

Returns:

Encrypted private key

Return type:

str

Raises:
  • TypeError – If the private key is not a Secp256k1PrivateKey

  • ValueError – If the private key bytes are not valid

static GeneratePrivateKeyEc(passphrase: str, pub_key_mode: P2PKHPubKeyModes = P2PKHPubKeyModes.COMPRESSED, lot_num: Optional[int] = None, sequence_num: Optional[int] = None) str

Generate a random encrypted private key with EC multiplication, using the specified parameters. This will generate the intermediate passphrase and use it immediately for generating the private key.

Parameters:
  • passphrase (str) – Passphrase

  • pub_key_mode (Bip38PubKeyModes, optional) – Public key mode

  • lot_num (int, optional) – Lot number

  • sequence_num (int, optional) – Sequence number

Returns:

Encrypted private key

Return type:

str

class Bip38Decrypter

Bases: object

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

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

Decrypt the specified private key without EC multiplication.

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

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

Decrypt the specified private key with EC multiplication.

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