Source code for stellar_sdk.xdr.signer_key

# This is an automatically generated file.
# DO NOT EDIT or your changes may be overwritten
from __future__ import annotations

import base64
import json

from xdrlib3 import Packer, Unpacker

from .base import DEFAULT_XDR_MAX_DEPTH
from .signer_key_ed25519_signed_payload import SignerKeyEd25519SignedPayload
from .signer_key_type import SignerKeyType
from .uint256 import Uint256

__all__ = ["SignerKey"]


[docs] class SignerKey: """ XDR Source Code:: union SignerKey switch (SignerKeyType type) { case SIGNER_KEY_TYPE_ED25519: uint256 ed25519; case SIGNER_KEY_TYPE_PRE_AUTH_TX: /* SHA-256 Hash of TransactionSignaturePayload structure */ uint256 preAuthTx; case SIGNER_KEY_TYPE_HASH_X: /* Hash of random 256 bit preimage X */ uint256 hashX; case SIGNER_KEY_TYPE_ED25519_SIGNED_PAYLOAD: struct { /* Public key that must sign the payload. */ uint256 ed25519; /* Payload to be raw signed by ed25519. */ opaque payload<64>; } ed25519SignedPayload; }; """ def __init__( self, type: SignerKeyType, ed25519: Uint256 | None = None, pre_auth_tx: Uint256 | None = None, hash_x: Uint256 | None = None, ed25519_signed_payload: SignerKeyEd25519SignedPayload | None = None, ) -> None: self.type = type self.ed25519 = ed25519 self.pre_auth_tx = pre_auth_tx self.hash_x = hash_x self.ed25519_signed_payload = ed25519_signed_payload def pack(self, packer: Packer) -> None: self.type.pack(packer) if self.type == SignerKeyType.SIGNER_KEY_TYPE_ED25519: if self.ed25519 is None: raise ValueError("ed25519 should not be None.") self.ed25519.pack(packer) return if self.type == SignerKeyType.SIGNER_KEY_TYPE_PRE_AUTH_TX: if self.pre_auth_tx is None: raise ValueError("pre_auth_tx should not be None.") self.pre_auth_tx.pack(packer) return if self.type == SignerKeyType.SIGNER_KEY_TYPE_HASH_X: if self.hash_x is None: raise ValueError("hash_x should not be None.") self.hash_x.pack(packer) return if self.type == SignerKeyType.SIGNER_KEY_TYPE_ED25519_SIGNED_PAYLOAD: if self.ed25519_signed_payload is None: raise ValueError("ed25519_signed_payload should not be None.") self.ed25519_signed_payload.pack(packer) return raise ValueError("Invalid type.") @classmethod def unpack( cls, unpacker: Unpacker, depth_limit: int = DEFAULT_XDR_MAX_DEPTH ) -> SignerKey: if depth_limit <= 0: raise ValueError("Maximum decoding depth reached") type = SignerKeyType.unpack(unpacker) if type == SignerKeyType.SIGNER_KEY_TYPE_ED25519: ed25519 = Uint256.unpack(unpacker, depth_limit - 1) return cls(type=type, ed25519=ed25519) if type == SignerKeyType.SIGNER_KEY_TYPE_PRE_AUTH_TX: pre_auth_tx = Uint256.unpack(unpacker, depth_limit - 1) return cls(type=type, pre_auth_tx=pre_auth_tx) if type == SignerKeyType.SIGNER_KEY_TYPE_HASH_X: hash_x = Uint256.unpack(unpacker, depth_limit - 1) return cls(type=type, hash_x=hash_x) if type == SignerKeyType.SIGNER_KEY_TYPE_ED25519_SIGNED_PAYLOAD: ed25519_signed_payload = SignerKeyEd25519SignedPayload.unpack( unpacker, depth_limit - 1 ) return cls(type=type, ed25519_signed_payload=ed25519_signed_payload) raise ValueError("Invalid type.") def to_xdr_bytes(self) -> bytes: packer = Packer() self.pack(packer) return packer.get_buffer() @classmethod def from_xdr_bytes(cls, xdr: bytes) -> SignerKey: unpacker = Unpacker(xdr) result = cls.unpack(unpacker) remaining = len(xdr) - unpacker.get_position() if remaining != 0: raise ValueError(f"Unexpected trailing {remaining} bytes in XDR data") return result def to_xdr(self) -> str: xdr_bytes = self.to_xdr_bytes() return base64.b64encode(xdr_bytes).decode() @classmethod def from_xdr(cls, xdr: str) -> SignerKey: xdr_bytes = base64.b64decode(xdr.encode()) return cls.from_xdr_bytes(xdr_bytes) def to_json(self) -> str: return json.dumps(self.to_json_dict()) @classmethod def from_json(cls, json_str: str) -> SignerKey: return cls.from_json_dict(json.loads(json_str)) def to_json_dict(self) -> str: from ..strkey import StrKey from .signer_key_type import SignerKeyType if self.type == SignerKeyType.SIGNER_KEY_TYPE_ED25519: assert self.ed25519 is not None return StrKey.encode_ed25519_public_key(self.ed25519.uint256) if self.type == SignerKeyType.SIGNER_KEY_TYPE_PRE_AUTH_TX: assert self.pre_auth_tx is not None return StrKey.encode_pre_auth_tx(self.pre_auth_tx.uint256) if self.type == SignerKeyType.SIGNER_KEY_TYPE_HASH_X: assert self.hash_x is not None return StrKey.encode_sha256_hash(self.hash_x.uint256) if self.type == SignerKeyType.SIGNER_KEY_TYPE_ED25519_SIGNED_PAYLOAD: assert self.ed25519_signed_payload is not None from xdrlib3 import Packer as _Packer packer = _Packer() self.ed25519_signed_payload.pack(packer) return StrKey.encode_ed25519_signed_payload(packer.get_buffer()) raise ValueError(f"Unknown SignerKey type: {self.type}") @classmethod def from_json_dict(cls, json_value: str) -> SignerKey: from ..strkey import StrKey from .signer_key_type import SignerKeyType from .uint256 import Uint256 if json_value.startswith("G"): raw = StrKey.decode_ed25519_public_key(json_value) return cls(type=SignerKeyType.SIGNER_KEY_TYPE_ED25519, ed25519=Uint256(raw)) if json_value.startswith("T"): raw = StrKey.decode_pre_auth_tx(json_value) return cls( type=SignerKeyType.SIGNER_KEY_TYPE_PRE_AUTH_TX, pre_auth_tx=Uint256(raw) ) if json_value.startswith("X"): raw = StrKey.decode_sha256_hash(json_value) return cls(type=SignerKeyType.SIGNER_KEY_TYPE_HASH_X, hash_x=Uint256(raw)) if json_value.startswith("P"): from xdrlib3 import Unpacker as _Unpacker from .signer_key_ed25519_signed_payload import SignerKeyEd25519SignedPayload raw = StrKey.decode_ed25519_signed_payload(json_value) unpacker = _Unpacker(raw) payload = SignerKeyEd25519SignedPayload.unpack(unpacker) return cls( type=SignerKeyType.SIGNER_KEY_TYPE_ED25519_SIGNED_PAYLOAD, ed25519_signed_payload=payload, ) raise ValueError(f"Invalid SignerKey strkey: {json_value}") def __hash__(self): return hash( ( self.type, self.ed25519, self.pre_auth_tx, self.hash_x, self.ed25519_signed_payload, ) ) def __eq__(self, other: object): if not isinstance(other, self.__class__): return NotImplemented return ( self.type == other.type and self.ed25519 == other.ed25519 and self.pre_auth_tx == other.pre_auth_tx and self.hash_x == other.hash_x and self.ed25519_signed_payload == other.ed25519_signed_payload ) def __repr__(self): out = [] out.append(f"type={self.type}") if self.ed25519 is not None: out.append(f"ed25519={self.ed25519}") if self.pre_auth_tx is not None: out.append(f"pre_auth_tx={self.pre_auth_tx}") if self.hash_x is not None: out.append(f"hash_x={self.hash_x}") if self.ed25519_signed_payload is not None: out.append(f"ed25519_signed_payload={self.ed25519_signed_payload}") return f"<SignerKey [{', '.join(out)}]>"