Example #1
0
def test_swap_utils():

    assert len(generate_entropy(strength=128)) == 32

    assert is_mnemonic(
        "sceptre capter séquence girafe absolu relatif fleur zoologie muscle sirop saboter parure"
    )

    assert len(
        generate_mnemonic(language="chinese_traditional",
                          strength=128).split(" ")) == 12

    assert get_mnemonic_language(
        mnemonic=
        "sceptre capter séquence girafe absolu relatif fleur zoologie muscle sirop saboter parure"
    ) == "french"

    with pytest.raises(ValueError, match=r".*[128, 160, 192, 224, 256].*"):
        assert len(generate_entropy(strength=129).split(" ")) == 12

    assert is_mnemonic(mnemonic=MNEMONIC, language="korean")

    with pytest.raises(ValueError, match=r"invalid language, .*"):
        assert is_mnemonic(mnemonic=MNEMONIC, language="amharic")

    assert not is_mnemonic(mnemonic=12341234, language="english")

    with pytest.raises(ValueError, match="invalid 12 word mnemonic."):
        assert get_mnemonic_language("1234 meheret tesfaye")

    with pytest.raises(ValueError, match=r"invalid language, .*"):
        assert generate_mnemonic(language="amharic")

    with pytest.raises(ValueError,
                       match=r"Strength should be one of the following .*"):
        assert generate_mnemonic(strength=129)

    assert len(generate_passphrase(length=100)) == 100

    assert sha256("meherett") == \
        "d4f5c55a45c004660b95ec833bb24569eba1559f214e90efa6e8d0b3afa14394"

    assert double_sha256("meherett") == \
        "2803bf9ed1e5874825350b1b0753a96c00a99236b686bde337404453b11d3288"

    assert sha256("meherett".encode()) == \
        "d4f5c55a45c004660b95ec833bb24569eba1559f214e90efa6e8d0b3afa14394"

    assert double_sha256("meherett".encode()) == \
        "2803bf9ed1e5874825350b1b0753a96c00a99236b686bde337404453b11d3288"

    with pytest.raises(TypeError, match="data must be str/bytes format!"):
        sha256(int(123))

    with pytest.raises(TypeError, match="data must be str/bytes format!"):
        double_sha256(int(123))
Example #2
0
from swap.providers.bytom.wallet import Wallet, DEFAULT_PATH
from swap.providers.bytom.assets import BTM as ASSET
from swap.utils import generate_entropy, generate_passphrase
from typing import Optional

# Choose network mainnet, solonet or testnet
NETWORK: str = "mainnet"  # Default to mainnet
# Choose strength 128, 160, 192, 224 or 256
STRENGTH: int = 160  # Default is 128
# Choose language english, french, italian, spanish, chinese_simplified, chinese_traditional, japanese or korean
LANGUAGE: str = "english"  # Default is english
# Set passphrase length
LENGTH: int = 32  # Default is 32
# Generate new entropy
ENTROPY: str = generate_entropy(strength=STRENGTH)
# Generate new passphrase
PASSPHRASE: Optional[str] = None  # generate_passphrase(length=LENGTH)

# Initialize Bytom wallet
wallet: Wallet = Wallet(network=NETWORK)
# Get Bytom wallet from entropy
wallet.from_entropy(entropy=ENTROPY, language=LANGUAGE, passphrase=PASSPHRASE)
# Drive Bytom wallet from path
wallet.from_path(path=DEFAULT_PATH)

# Print all Bytom wallet info's
print("Strength:", wallet.strength())
print("Entropy:", wallet.entropy())
print("Mnemonic:", wallet.mnemonic())
print("Language:", wallet.language())