Edit on GitHub

communex.key

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

Generates a new keypair.