BIP38¶
- class bip38.BIP38(cryptocurrency: Type[ICryptocurrency], network: str = 'mainnet')¶
A class for BIP38 encryption and decryption of Wallet Import Format (WIF) keys.
- Parameters:
cryptocurrency (Type[ICryptocurrency]) – The cryptocurrency class to use (e.g., Bitcoin).
network (str) – The network for the WIF key (e.g., ‘mainnet’ or ‘testnet’). Defaults to ‘mainnet’.
- classmethod intermediate_code(passphrase: str, lot: int | None = None, sequence: int | None = None, owner_salt: str | bytes | None = None, N: int = 16384, r: int = 8, p: int = 8) str¶
Generates an intermediate passphrase.
- Parameters:
passphrase (str) – The passphrase or password.
lot (Optional[int]) – Optional lot number (100000-999999).
sequence (Optional[int]) – Optional sequence number (0-4095).
owner_salt (Optional[str, bytes]) – Optional owner salt. (default:
random 8 bytes)N (int) – CPU/memory cost parameter (must be a power of two > 1). Higher values increase security but require more resources. (default:
16384)r (int) – Block size parameter. Controls memory usage. Must be > 0. (default:
8)p (int) – Parallelization parameter. Defines how many threads can run in parallel. Must be > 0. (default:
8)
- Returns:
The intermediate passphrase.
- Return type:
str
>>> from bip38 import BIP38 >>> from bip38.cryptocurrencies import Bitcoin >>> bip38: BIP38 = BIP38(cryptocurrency=Bitcoin, network="mainnet") >>> bip38.intermediate_code(passphrase="TestingOneTwoThree") 'passphraseqVKbgU4mWMakKGgCtaeVWoETQdzMBy5696bG2w7ckVBeQmoLhMF9vLaxhmzhT3' >>> bip38.intermediate_code(passphrase="TestingOneTwoThree", lot=199999, sequence=1, owner_salt="75ed1cdeb254cb38") 'passphraseb7ruSN4At4Rb8hPTNcAVezfsjonvUs4Qo3xSp1fBFsFPvVGSbpP2WTJMhw3mVZ'
- encrypt(wif: str, passphrase: str, network: str | None = None, N: int = 16384, r: int = 8, p: int = 8) str¶
Encrypts a Wallet Import Format (WIF) key using a passphrase with BIP38 encryption.
- Parameters:
wif (str) – The WIF key to be encrypted.
passphrase (str) – The passphrase for encryption.
network (Optional[str]) – Optional network for encryption. Defaults to the class’s network if not provided.
N (int) – CPU/memory cost parameter (must be a power of two > 1). Higher values increase security but require more resources. (default:
16384)r (int) – Block size parameter. Controls memory usage. Must be > 0. (default:
8)p (int) – Parallelization parameter. Defines how many threads can run in parallel. Must be > 0. (default:
8)
- Returns:
Encrypted WIF key as a string.
- Return type:
str
>>> from bip38 import BIP38 >>> from bip38.cryptocurrencies import Bitcoin >>> bip38: BIP38 = BIP38(cryptocurrency=Bitcoin, network="mainnet") >>> bip38.encrypt(wif="5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR", passphrase="TestingOneTwoThree") '6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg' >>> bip38.encrypt(wif="L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP", passphrase="TestingOneTwoThree") '6PYNKZ1EAgYgmQfmNVamxyXVWHzK5s6DGhwP4J5o44cvXdoY7sRzhtpUeo' >>> bip38.encrypt(wif="938jwjergAxARSWx2YSt9nSBWBz24h8gLhv7EUfgEP1wpMLg6iX", passphrase="TestingOneTwoThree", network="testnet") '6PRL8jj6dLQjBBJjHMdUKLSNLEpjTyAfmt8GnCnfT87NeQ2BU5eAW1tcsS' >>> bip38.encrypt(wif="cURAYbG6FtvUasdBsooEmmY9MqUfhJ8tdybQWV7iA4BAwunCT2Fu", passphrase="TestingOneTwoThree", network="testnet") '6PYVB5nHnumbUua1UmsAMPHWHa76Ci48MY79aKYnpKmwxeGqHU2XpXtKvo'
- create_new_encrypted_wif(intermediate_passphrase: str, wif_type: str = 'wif', seed: str | bytes | None = None, network: str | None = None) dict¶
Creates a new encrypted WIF (Wallet Import Format).
- Parameters:
intermediate_passphrase (str) – The intermediate passphrase.
wif_type (str) – The WIF type, either
wiforwif-compressed. (default iswif)seed (Optional[str, bytes]) – Optional seed (default: random 24 bytes).
network (Optional[str]) – Optional network for encryption. Defaults to the class’s network if not provided.
- Returns:
A dictionary containing the encrypted WIF.
- Return type:
dict
>>> from bip38 import BIP38 >>> from bip38.cryptocurrencies import Bitcoin >>> bip38: BIP38 = BIP38(cryptocurrency=Bitcoin, network="testnet") >>> bip38.create_new_encrypted_wif(intermediate_passphrase="passphraseb7ruSN4At4Rb8hPTNcAVezfsjonvUs4Qo3xSp1fBFsFPvVGSbpP2WTJMhw3mVZ", wif_type="wif") {'encrypted_wif': '6PgMqfwt1nqJeUkCTRVf4TV6FJoqA8GFCGaj6RsTW1t3XgQNUDfKW9u9Px', 'confirmation_code': 'cfrm38V8ZR17HkU8Xdu8RunWf8CZauXphNQa5HFJd4cxEYckXHS6tfo9M73yL3FPmWv1xqBQsgG', 'public_key': '04fdfbd938b4bb220c11fc1c22e87c5306d105130dd05d7ece4013aa1d2382f3a2a8673fd7b4b2f55c48cd0ebda7d88089783b3394210a0853159803b5eb99097e', 'seed': '0a89d39d0af0f0d7abc655baa0e399f8ddcd372bb9aaebce', 'public_key_type': 'uncompressed', 'address': 'myRfjUS74ab2ZbEbQuiWNzPHb5fSYuFvm4'} >>> bip38.create_new_encrypted_wif(intermediate_passphrase="passphraseb7ruSN4At4Rb8hPTNcAVezfsjonvUs4Qo3xSp1fBFsFPvVGSbpP2WTJMhw3mVZ", wif_type="wif-compressed", seed="99241d58245c883896f80843d2846672d7312e6195ca1a6c") {'encrypted_wif': '6PoH364JVeoBPsJBveXCwfWpX2H82N5qiAervtynak7r7dzZF2TBFxZAXE', 'confirmation_code': 'cfrm38VX6jSx7C3nbxWCVEdGna7JMpm57zHdbuofbpCA9EiN57aEt4s5fh9k19b5cTmyZ5jMkE2', 'public_key': '02100bb0440ff4106a1743750813271e66a7017431e92921e520319f537c7357c1', 'seed': '99241d58245c883896f80843d2846672d7312e6195ca1a6c', 'public_key_type': 'compressed', 'address': 'mjurfzLk2ryxCyfm4nMk5qarvNRhbNCtK8'}
- confirm_code(passphrase: str, confirmation_code: str, network: str | None = None, detail: bool = False, N: int = 16384, r: int = 8, p: int = 8) str | dict¶
Confirms the passphrase using a confirmation code.
- Parameters:
passphrase (str) – The passphrase or password.
confirmation_code (str) – The confirmation code.
network (Optional[str]) – Optional network for encryption. Defaults to the class’s network if not provided.
detail (bool) – Whether to return detailed info. (default:
False)N (int) – CPU/memory cost parameter (must be a power of two > 1). Higher values increase security but require more resources. (default:
16384)r (int) – Block size parameter. Controls memory usage. Must be > 0. (default:
8)p (int) – Parallelization parameter. Defines how many threads can run in parallel. Must be > 0. (default:
8)
- Returns:
Confirmation result as a string or detailed info as a dictionary.
- Return type:
Union[str, dict]
>>> from bip38 import BIP38 >>> from bip38.cryptocurrencies import Bitcoin >>> bip38: BIP38 = BIP38(cryptocurrency=Bitcoin, network="testnet") >>> bip38.confirm_code(passphrase="TestingOneTwoThree", confirmation_code="cfrm38V8ZQSdCuzcrYYKGNXVwbHgdjsUEfAbFGoEUouB4YEKaXVdFiMcBWai1Exdu8jN7DcoKtM") 'mwW38M23zvDmhbsTdnVFzw3bVnueDhrKec' >>> bip38.confirm_code(passphrase="TestingOneTwoThree", confirmation_code="cfrm38V8Foq3WpRPMXJD34SF6pGT6ht5ihYMWWMbezkzHgPpA1jVkfbTHwQzvuSA4ReF86PHZJY", network="mainnet") '1JbyXoVN4hXWirGB265q9VE4pQ6qbY6kmr' >>> bip38.confirm_code(passphrase="TestingOneTwoThree", confirmation_code="cfrm38VXL5T6qVke13sHUWtEjibAkK1RquBqMXb2azCv1Zna6JKvBhD1Gf2b15wBj7UPv2BQnf6", network="mainnet", detail=True) {'public_key': '02100bb0440ff4106a1743750813271e66a7017431e92921e520319f537c7357c1', 'public_key_type': 'compressed', 'address': '15PuNwFmDqYhRsC9MDPNFvNY4Npzibm67c', 'lot': 199999, 'sequence': 1}
- decrypt(encrypted_wif: str, passphrase: str, network: str | None = None, detail: bool = False, N: int = 16384, r: int = 8, p: int = 8) str | dict¶
Decrypts an encrypted WIF (Wallet Import Format) using a passphrase.
- Parameters:
encrypted_wif (str) – The encrypted WIF.
passphrase (str) – The passphrase or password.
network (Optional[str]) – Optional network for encryption. Defaults to the class’s network if not provided.
detail (bool) – Whether to return detailed info. (default:
False)N (int) – CPU/memory cost parameter (must be a power of two > 1). Higher values increase security but require more resources. (default:
16384)r (int) – Block size parameter. Controls memory usage. Must be > 0. (default:
8)p (int) – Parallelization parameter. Defines how many threads can run in parallel. Must be > 0. (default:
8)
- Returns:
The decrypted WIF or detailed private key info.
- Return type:
Union[str, dict]
>>> from bip38 import BIP38 >>> from bip38.cryptocurrencies import Bitcoin >>> bip38: BIP38 = BIP38(cryptocurrency=Bitcoin, network="mainnet") >>> bip38.decrypt(encrypted_wif="6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg", passphrase="TestingOneTwoThree") '5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR' >>> bip38.decrypt(encrypted_wif="6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg", passphrase="TestingOneTwoThree", detail=True) {'wif': '5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR', 'private_key': 'cbf4b9f70470856bb4f40f80b87edb90865997ffee6df315ab166d713af433a5', 'wif_type': 'wif', 'public_key': '04d2ce831dd06e5c1f5b1121ef34c2af4bcb01b126e309234adbc3561b60c9360ea7f23327b49ba7f10d17fad15f068b8807dbbc9e4ace5d4a0b40264eefaf31a4', 'public_key_type': 'uncompressed', 'seed': None, 'address': '1Jq6MksXQVWzrznvZzxkV6oY57oWXD9TXB', 'lot': None, 'sequence': None} >>> bip38.decrypt(encrypted_wif="6PRL8jj6dLQjBBJjHMdUKLSNLEpjTyAfmt8GnCnfT87NeQ2BU5eAW1tcsS", passphrase="TestingOneTwoThree", network="testnet") '938jwjergAxARSWx2YSt9nSBWBz24h8gLhv7EUfgEP1wpMLg6iX' >>> bip38.decrypt(encrypted_wif="6PRL8jj6dLQjBBJjHMdUKLSNLEpjTyAfmt8GnCnfT87NeQ2BU5eAW1tcsS", passphrase="TestingOneTwoThree", network="testnet", detail=True) {'wif': '938jwjergAxARSWx2YSt9nSBWBz24h8gLhv7EUfgEP1wpMLg6iX', 'private_key': 'cbf4b9f70470856bb4f40f80b87edb90865997ffee6df315ab166d713af433a5', 'wif_type': 'wif', 'public_key': '04d2ce831dd06e5c1f5b1121ef34c2af4bcb01b126e309234adbc3561b60c9360ea7f23327b49ba7f10d17fad15f068b8807dbbc9e4ace5d4a0b40264eefaf31a4', 'public_key_type': 'uncompressed', 'seed': None, 'address': 'myM3eoxWDWxFe7GYHZw8K21rw7QDNZeDYM', 'lot': None, 'sequence': None}