Skip to content

Utils

Cluster

solana.utils.cluster

Tools for getting RPC cluster information.

cluster_api_url(cluster=None, tls=True)

Retrieve the RPC API URL for the specified cluster.

:param cluster: The name of the cluster to use. :param tls: If True, use https. Defaults to True.

Source code in src/solana/utils/cluster.py
26
27
28
29
30
31
32
33
34
35
def cluster_api_url(cluster: Cluster | None = None, tls: bool = True) -> str:
    """Retrieve the RPC API URL for the specified cluster.

    :param cluster: The name of the cluster to use.
    :param tls: If True, use https. Defaults to True.
    """
    urls = ENDPOINT.https if tls else ENDPOINT.http
    if cluster is None:
        return urls.devnet
    return getattr(urls, cluster)

Security TXT

solana.utils.security_txt

Utils for security.txt.

FOOTER = '=======END SECURITY.TXT V1=======\x00' module-attribute

Footer of the security.txt.

HEADER = '=======BEGIN SECURITY.TXT V1=======\x00' module-attribute

Header of the security.txt.

NoSecurityTxtFoundError

Raise when security text is not found.

Source code in src/solana/utils/security_txt.py
32
33
class NoSecurityTxtFoundError(Exception):
    """Raise when security text is not found."""

SecurityTxt dataclass

Security txt data.

Source code in src/solana/utils/security_txt.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@dataclass
class SecurityTxt:
    """Security txt data."""

    name: str
    project_url: str
    contacts: str
    policy: str
    preferred_languages: str | None = None
    source_code: str | None = None
    encryption: str | None = None
    auditors: str | None = None
    acknowledgements: str | None = None
    expiry: str | None = None

parse_security_txt(data)

Parse and extract security.txt section from the data section of the compiled program.

Parameters:

Name Type Description Default
data bytes

Program data in bytes from the ProgramAccount.

required

Returns:

Type Description
SecurityTxt

The Security Txt.

Source code in src/solana/utils/security_txt.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def parse_security_txt(data: bytes) -> SecurityTxt:
    """Parse and extract security.txt section from the data section of the compiled program.

    Args:
        data: Program data in bytes from the ProgramAccount.

    Returns:
        The Security Txt.
    """
    if not isinstance(data, bytes):
        raise TypeError(f"data provided in parse(data) must be bytes, found: {type(data)}")

    header_bytes = HEADER.encode("utf-8")
    footer_bytes = FOOTER.encode("utf-8")
    s_idx = data.find(header_bytes)
    if s_idx == -1:
        raise NoSecurityTxtFoundError("Program doesn't have security.txt section")

    e_idx = data.find(footer_bytes, s_idx + len(header_bytes))
    content_bytes = data[s_idx + len(header_bytes) : e_idx]

    # Split by null byte, convert each segment to string, strip trailing empty
    parts = [segment.decode("utf-8") for segment in content_bytes.split(b"\x00")]
    if parts and parts[-1] == "":
        parts.pop()

    # Walk key-value pairs: field names alternate with values
    content_dict: dict[str, Any] = {}
    for i, part in enumerate(parts):
        if part in _KNOWN_KEYS and i + 1 < len(parts):
            content_dict[part] = parts[i + 1]

    try:
        security_txt = SecurityTxt(**content_dict)
    except TypeError as err:
        raise err
    return security_txt

Validation

solana.utils.validate

Validation utilities.

validate_instruction_keys(instruction, expected)

Verify length of AccountMeta list of a transaction instruction is at least the expected length.

Parameters:

Name Type Description Default
instruction Instruction

A Instruction object.

required
expected int

The expected length.

required
Source code in src/solana/utils/validate.py
11
12
13
14
15
16
17
18
19
def validate_instruction_keys(instruction: Instruction, expected: int) -> None:
    """Verify length of AccountMeta list of a transaction instruction is at least the expected length.

    Args:
        instruction: A Instruction object.
        expected: The expected length.
    """
    if len(instruction.accounts) < expected:
        raise ValueError(f"invalid instruction: found {len(instruction.accounts)} keys, expected at least {expected}")

validate_instruction_type(parsed_data, expected_type)

Check that the instruction type of the parsed data matches the expected instruction type.

Parameters:

Name Type Description Default
parsed_data Any

Parsed instruction data object with instruction_type field.

required
expected_type IntEnum

The expected instruction type.

required
Source code in src/solana/utils/validate.py
22
23
24
25
26
27
28
29
30
31
32
def validate_instruction_type(parsed_data: Any, expected_type: IntEnum) -> None:
    """Check that the instruction type of the parsed data matches the expected instruction type.

    Args:
        parsed_data: Parsed instruction data object with `instruction_type` field.
        expected_type: The expected instruction type.
    """
    if parsed_data.instruction_type != expected_type:
        raise ValueError(
            f"invalid instruction; instruction index mismatch {parsed_data.instruction_type} != {expected_type}"
        )