|
| 1 | +# ruff: noqa: D100, D101, D103, D105, RET505, UP017, PLR0913 |
| 2 | + |
| 3 | +from datetime import datetime, timezone |
| 4 | +from enum import IntEnum |
| 5 | + |
| 6 | +from catalyst_python.ed25519 import Ed25519Keys |
| 7 | +from catalyst_python.utils import base64_url |
| 8 | + |
| 9 | + |
| 10 | +class RoleID(IntEnum): |
| 11 | + ROLE_0 = 0 |
| 12 | + PROPOSER = 3 |
| 13 | + |
| 14 | + def __str__(self) -> str: |
| 15 | + return f"{int(self)}" |
| 16 | + |
| 17 | + |
| 18 | +# Default is set to URI format |
| 19 | +# Optional field = subnet, role id, rotation, username, nonce |
| 20 | +def generate_cat_id( |
| 21 | + network: str, |
| 22 | + role_0_key: Ed25519Keys, |
| 23 | + scheme: str | None = None, |
| 24 | + subnet: str | None = None, |
| 25 | + role_id: RoleID | None = None, |
| 26 | + rotation: str | None = None, |
| 27 | + username: str | None = None, |
| 28 | + nonce: str | None = None, |
| 29 | +) -> str: |
| 30 | + role0_pk_b64 = base64_url(bytes.fromhex(role_0_key.pk_hex())) |
| 31 | + |
| 32 | + # If nonce is set to none, use current timestamp |
| 33 | + # If set to empty string, use empty string (no nonce) |
| 34 | + if nonce is None: |
| 35 | + nonce = f"{int(datetime.now(timezone.utc).timestamp())}" |
| 36 | + |
| 37 | + # Authority part |
| 38 | + authority = "" |
| 39 | + if username: |
| 40 | + authority += f"{username}" |
| 41 | + if nonce: |
| 42 | + authority += f":{nonce}" |
| 43 | + authority += "@" |
| 44 | + |
| 45 | + if subnet: |
| 46 | + authority += f"{subnet}.{network}" |
| 47 | + else: |
| 48 | + authority += network |
| 49 | + |
| 50 | + # Path |
| 51 | + path = f"{role0_pk_b64}" |
| 52 | + if role_id is not None: |
| 53 | + path += f"/{role_id}" |
| 54 | + if rotation is not None: |
| 55 | + path += f"/{rotation}" |
| 56 | + |
| 57 | + if scheme: |
| 58 | + return f"{scheme}://{authority}/{path}" |
| 59 | + else: |
| 60 | + return f"{authority}/{path}" |
0 commit comments