Edit on GitHub

communex.key

 1from typing import TypeGuard
 2
 3from substrateinterface import Keypair
 4from substrateinterface.utils import ss58
 5
 6from communex.types import Ss58Address
 7
 8
 9def is_ss58_address(
10    address: str, ss58_format: int = 42
11) -> TypeGuard[Ss58Address]:
12    """
13    Validates whether the given string is a valid SS58 address.
14
15    Args:
16        address: The string to validate.
17        ss58_format: The SS58 format code to validate against.
18
19    Returns:
20        True if the address is valid, False otherwise.
21    """
22
23    return ss58.is_valid_ss58_address(address, valid_ss58_format=ss58_format)
24
25
26def check_ss58_address(
27    address: str | Ss58Address, ss58_format: int = 42
28) -> Ss58Address:
29    """
30    Validates whether the given string is a valid SS58 address.
31
32    Args:
33        address: The string to validate.
34        ss58_format: The SS58 format code to validate against.
35
36    Returns:
37        The validated SS58 address.
38
39    Raises:
40        AssertionError: If the address is invalid.
41    """
42
43    assert is_ss58_address(
44        address, ss58_format
45    ), f"Invalid SS58 address '{address}'"
46    return Ss58Address(address)
47
48
49def generate_keypair() -> Keypair:
50    """
51    Generates a new keypair.
52    """
53    mnemonic = Keypair.generate_mnemonic()
54    keypair = Keypair.create_from_mnemonic(mnemonic)
55    return keypair
def is_ss58_address( address: str, ss58_format: int = 42) -> TypeGuard[communex.types.Ss58Address]:
10def is_ss58_address(
11    address: str, ss58_format: int = 42
12) -> TypeGuard[Ss58Address]:
13    """
14    Validates whether the given string is a valid SS58 address.
15
16    Args:
17        address: The string to validate.
18        ss58_format: The SS58 format code to validate against.
19
20    Returns:
21        True if the address is valid, False otherwise.
22    """
23
24    return ss58.is_valid_ss58_address(address, valid_ss58_format=ss58_format)

Validates whether the given string is a valid SS58 address.

Arguments:
  • address: The string to validate.
  • ss58_format: The SS58 format code to validate against.
Returns:

True if the address is valid, False otherwise.

def check_ss58_address( address: Union[str, communex.types.Ss58Address], ss58_format: int = 42) -> communex.types.Ss58Address:
27def check_ss58_address(
28    address: str | Ss58Address, ss58_format: int = 42
29) -> Ss58Address:
30    """
31    Validates whether the given string is a valid SS58 address.
32
33    Args:
34        address: The string to validate.
35        ss58_format: The SS58 format code to validate against.
36
37    Returns:
38        The validated SS58 address.
39
40    Raises:
41        AssertionError: If the address is invalid.
42    """
43
44    assert is_ss58_address(
45        address, ss58_format
46    ), f"Invalid SS58 address '{address}'"
47    return Ss58Address(address)

Validates whether the given string is a valid SS58 address.

Arguments:
  • address: The string to validate.
  • ss58_format: The SS58 format code to validate against.
Returns:

The validated SS58 address.

Raises:
  • AssertionError: If the address is invalid.
def generate_keypair() -> substrateinterface.keypair.Keypair:
50def generate_keypair() -> Keypair:
51    """
52    Generates a new keypair.
53    """
54    mnemonic = Keypair.generate_mnemonic()
55    keypair = Keypair.create_from_mnemonic(mnemonic)
56    return keypair

Generates a new keypair.