Skip to content

Commit

Permalink
Use BytesUtils instead of binascii
Browse files Browse the repository at this point in the history
  • Loading branch information
ebellocchia committed May 4, 2024
1 parent 021cf8b commit 0cdc95e
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 34 deletions.
10 changes: 4 additions & 6 deletions examples/bip38_ec.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)}")


Expand All @@ -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)}")


Expand All @@ -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)}")
8 changes: 3 additions & 5 deletions examples/bip38_no_ec.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)}")
7 changes: 3 additions & 4 deletions examples/bip39_to_algorand.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down Expand Up @@ -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("")

Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions examples/cardano_byron.py
Original file line number Diff line number Diff line change
@@ -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
)


Expand All @@ -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()}")
Expand Down
10 changes: 4 additions & 6 deletions examples/electrum.py
Original file line number Diff line number Diff line change
@@ -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
)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions examples/monero.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down
6 changes: 2 additions & 4 deletions examples/monero_bip44.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 0cdc95e

Please sign in to comment.