From 0cdc95e97e818fa609753ef4920fe52e1918b20c Mon Sep 17 00:00:00 2001 From: Emanuele Bellocchia <54482000+ebellocchia@users.noreply.github.com> Date: Sat, 4 May 2024 13:07:08 +0200 Subject: [PATCH] Use BytesUtils instead of binascii --- examples/bip38_ec.py | 10 ++++------ examples/bip38_no_ec.py | 8 +++----- examples/bip39_to_algorand.py | 7 +++---- examples/cardano_byron.py | 8 +++----- examples/electrum.py | 10 ++++------ examples/monero.py | 6 ++---- examples/monero_bip44.py | 6 ++---- 7 files changed, 21 insertions(+), 34 deletions(-) diff --git a/examples/bip38_ec.py b/examples/bip38_ec.py index c454a9f6..5d81a9c8 100644 --- a/examples/bip38_ec.py +++ b/examples/bip38_ec.py @@ -1,8 +1,6 @@ """Example of private key encryption/decryption with EC multiplication using BIP38.""" -import binascii - -from bip_utils import Bip38Decrypter, Bip38EcKeysGenerator, Bip38Encrypter, Bip38PubKeyModes, WifEncoder +from bip_utils import Bip38Decrypter, Bip38EcKeysGenerator, Bip38Encrypter, Bip38PubKeyModes, BytesUtils, WifEncoder # BIP38 passphrase @@ -16,7 +14,7 @@ print(f"Encrypted private key (no lot/sequence): {priv_key_enc}") # Decrypt priv_key_dec, pub_key_mode = Bip38Decrypter.DecryptEc(priv_key_enc, passphrase) -print(f"Decrypted private key (bytes): {binascii.hexlify(priv_key_dec)}") +print(f"Decrypted private key (bytes): {BytesUtils.ToHexString(priv_key_dec)}") print(f"Decrypted private key (WIF): {WifEncoder.Encode(priv_key_dec, pub_key_mode=pub_key_mode)}") @@ -30,7 +28,7 @@ print(f"Encrypted private key (with lot/sequence): {priv_key_enc}") # Decrypt priv_key_dec, pub_key_mode = Bip38Decrypter.DecryptEc(priv_key_enc, passphrase) -print(f"Decrypted private key (bytes): {binascii.hexlify(priv_key_dec)}") +print(f"Decrypted private key (bytes): {BytesUtils.ToHexString(priv_key_dec)}") print(f"Decrypted private key (WIF): {WifEncoder.Encode(priv_key_dec, pub_key_mode=pub_key_mode)}") @@ -42,5 +40,5 @@ print(f"Encrypted private key (with Bip38Encrypter): {priv_key_enc}") # Decrypt priv_key_dec, pub_key_mode = Bip38Decrypter.DecryptEc(priv_key_enc, passphrase) -print(f"Decrypted private key (bytes): {binascii.hexlify(priv_key_dec)}") +print(f"Decrypted private key (bytes): {BytesUtils.ToHexString(priv_key_dec)}") print(f"Decrypted private key (WIF): {WifEncoder.Encode(priv_key_dec, pub_key_mode=pub_key_mode)}") diff --git a/examples/bip38_no_ec.py b/examples/bip38_no_ec.py index e3303ac3..999b8393 100644 --- a/examples/bip38_no_ec.py +++ b/examples/bip38_no_ec.py @@ -1,8 +1,6 @@ """Example of private key encryption/decryption without EC multiplication using BIP38.""" -import binascii - -from bip_utils import Bip38Decrypter, Bip38Encrypter, WifDecoder, WifEncoder +from bip_utils import Bip38Decrypter, Bip38Encrypter, BytesUtils, WifDecoder, WifEncoder # BIP38 passphrase @@ -21,7 +19,7 @@ # Decrypt priv_key_dec, pub_key_mode = Bip38Decrypter.DecryptNoEc(priv_key_enc, passphrase) -print(f"Decrypted private key (bytes): {binascii.hexlify(priv_key_dec)}") +print(f"Decrypted private key (bytes): {BytesUtils.ToHexString(priv_key_dec)}") print(f"Decrypted private key (WIF): {WifEncoder.Encode(priv_key_dec, pub_key_mode=pub_key_mode)}") # WIF private key correspondent to an uncompressed public key @@ -36,5 +34,5 @@ # Decrypt priv_key_dec, pub_key_mode = Bip38Decrypter.DecryptNoEc(priv_key_enc, passphrase) -print(f"Decrypted private key (bytes): {binascii.hexlify(priv_key_dec)}") +print(f"Decrypted private key (bytes): {BytesUtils.ToHexString(priv_key_dec)}") print(f"Decrypted private key (WIF): {WifEncoder.Encode(priv_key_dec, pub_key_mode=pub_key_mode)}") diff --git a/examples/bip39_to_algorand.py b/examples/bip39_to_algorand.py index 77f68dd7..d001e5bf 100644 --- a/examples/bip39_to_algorand.py +++ b/examples/bip39_to_algorand.py @@ -6,12 +6,11 @@ https://algorand.oortnet.com/ """ -import binascii from enum import Enum, auto, unique from bip_utils import ( AlgorandMnemonicGenerator, Bip32KholawEd25519, Bip32Slip10Ed25519, Bip32Slip10Secp256k1, Bip39SeedGenerator, - Ed25519PrivateKey + BytesUtils, Ed25519PrivateKey ) @@ -45,7 +44,7 @@ def convert_seed(bip39_seed_bytes: bytes, # Print result print(f"Derivation method: {der_method}") - print(f" Algorand private key: {binascii.hexlify(priv_key_bytes)}") + print(f" Algorand private key: {BytesUtils.ToHexString(priv_key_bytes)}") print(f" Algorand mnemonic: {algorand_mnemonic}") print("") @@ -57,7 +56,7 @@ def convert_seed(bip39_seed_bytes: bytes, seed_bytes = Bip39SeedGenerator(mnemonic).Generate() # Print print(f"BIP39 mnemonic: {mnemonic}") -print(f"BIP39 seed: {binascii.hexlify(seed_bytes)}") +print(f"BIP39 seed: {BytesUtils.ToHexString(seed_bytes)}") print("") # Convert with all possible methods diff --git a/examples/cardano_byron.py b/examples/cardano_byron.py index 2f570f5c..48827cfb 100644 --- a/examples/cardano_byron.py +++ b/examples/cardano_byron.py @@ -1,10 +1,8 @@ """Example of key derivation for Cardano (Byron addresses).""" -import binascii - from bip_utils import ( - Bip39MnemonicGenerator, Bip39SeedGenerator, Bip39WordsNum, Bip44, Bip44Changes, Bip44Coins, CardanoByronLegacy, - CardanoByronLegacySeedGenerator, CardanoIcarusSeedGenerator + Bip39MnemonicGenerator, Bip39SeedGenerator, Bip39WordsNum, Bip44, Bip44Changes, Bip44Coins, BytesUtils, + CardanoByronLegacy, CardanoByronLegacySeedGenerator, CardanoIcarusSeedGenerator ) @@ -27,7 +25,7 @@ # Construct from seed byron_legacy = CardanoByronLegacy.FromSeed(seed_bytes) # Print HD path key -print(f"HD path key (bytes): {binascii.hexlify(byron_legacy.HdPathKey())}") +print(f"HD path key (bytes): {BytesUtils.ToHexString(byron_legacy.HdPathKey())}") # Print master key print(f"Master chain code (bytes): {byron_legacy.MasterPrivateKey().ChainCode().ToHex()}") print(f"Master private key (bytes): {byron_legacy.MasterPrivateKey().Raw().ToHex()}") diff --git a/examples/electrum.py b/examples/electrum.py index 1c842e0d..2b3c4427 100644 --- a/examples/electrum.py +++ b/examples/electrum.py @@ -1,9 +1,7 @@ """Example of mnemonic generation and keys derivation like the Electrum wallet.""" -import binascii - from bip_utils import ( - CoinsConf, ElectrumV1, ElectrumV1MnemonicGenerator, ElectrumV1SeedGenerator, ElectrumV1WordsNum, + BytesUtils, CoinsConf, ElectrumV1, ElectrumV1MnemonicGenerator, ElectrumV1SeedGenerator, ElectrumV1WordsNum, ElectrumV2MnemonicGenerator, ElectrumV2MnemonicTypes, ElectrumV2SeedGenerator, ElectrumV2Segwit, ElectrumV2Standard, ElectrumV2WordsNum, IPrivateKey, WifEncoder, WifPubKeyModes ) @@ -25,7 +23,7 @@ def priv_to_wif(priv_key: IPrivateKey, v1_mnemonic = ElectrumV1MnemonicGenerator().FromWordsNumber(ElectrumV1WordsNum.WORDS_NUM_12) print(f"Mnemonic: {v1_mnemonic}") v1_seed_bytes = ElectrumV1SeedGenerator(v1_mnemonic).Generate() -print(f"Seed: {binascii.hexlify(v1_seed_bytes)}") +print(f"Seed: {BytesUtils.ToHexString(v1_seed_bytes)}") # Construct from seed electrum_v1 = ElectrumV1.FromSeed(v1_seed_bytes) # Print master key @@ -43,7 +41,7 @@ def priv_to_wif(priv_key: IPrivateKey, print(f"Mnemonic: {v2_standard_mnemonic}") # Generate seed from mnemonic v2_standard_seed_bytes = ElectrumV2SeedGenerator(v2_standard_mnemonic).Generate() -print(f"Seed: {binascii.hexlify(v2_standard_seed_bytes)}") +print(f"Seed: {BytesUtils.ToHexString(v2_standard_seed_bytes)}") # Construct from seed electrum_v2_standard = ElectrumV2Standard.FromSeed(v2_standard_seed_bytes) # Print master key @@ -61,7 +59,7 @@ def priv_to_wif(priv_key: IPrivateKey, print(f"Mnemonic: {v2_segwit_mnemonic}") # Generate seed from mnemonic v2_segwit_seed_bytes = ElectrumV2SeedGenerator(v2_segwit_mnemonic).Generate() -print(f"Seed: {binascii.hexlify(v2_segwit_seed_bytes)}") +print(f"Seed: {BytesUtils.ToHexString(v2_segwit_seed_bytes)}") # Construct from seed electrum_v2_segwit = ElectrumV2Segwit.FromSeed(v2_segwit_seed_bytes) # Print master key diff --git a/examples/monero.py b/examples/monero.py index a8fe6f2b..fedee6f7 100644 --- a/examples/monero.py +++ b/examples/monero.py @@ -1,8 +1,6 @@ """Example of keys derivation for Monero (same addresses of official wallet).""" -import binascii - -from bip_utils import Monero, MoneroMnemonicGenerator, MoneroSeedGenerator, MoneroWordsNum +from bip_utils import BytesUtils, Monero, MoneroMnemonicGenerator, MoneroSeedGenerator, MoneroWordsNum # Generate random mnemonic @@ -23,7 +21,7 @@ # Print primary address print(f"Monero primary address: {monero.PrimaryAddress()}") # Print integrated address -payment_id = binascii.unhexlify(b"d6f093554c0daa94") +payment_id = BytesUtils.FromHexString("d6f093554c0daa94") print(f"Monero integrated address: {monero.IntegratedAddress(payment_id)}") # Print the first 5 subaddresses for account 0 and 1 for acc_idx in range(2): diff --git a/examples/monero_bip44.py b/examples/monero_bip44.py index aefabab4..6f56b873 100644 --- a/examples/monero_bip44.py +++ b/examples/monero_bip44.py @@ -1,8 +1,6 @@ """Example of keys derivation for Monero based on BIP44.""" -import binascii - -from bip_utils import Bip39MnemonicGenerator, Bip39SeedGenerator, Bip39WordsNum, Bip44, Bip44Coins, Monero +from bip_utils import Bip39MnemonicGenerator, Bip39SeedGenerator, Bip39WordsNum, Bip44, Bip44Coins, BytesUtils, Monero # Generate random mnemonic @@ -32,7 +30,7 @@ # Print primary address print(f"Monero primary address: {monero.PrimaryAddress()}") # Print integrated address -payment_id = binascii.unhexlify(b"d6f093554c0daa94") +payment_id = BytesUtils.FromHexString("d6f093554c0daa94") print(f"Monero integrated address: {monero.IntegratedAddress(payment_id)}") # Print the first 5 subaddresses for account 0 and 1 for acc_idx in range(2):