File: signed_pre_key_pair.py

package info (click to toggle)
python-x3dh 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 356 kB
  • sloc: python: 1,259; makefile: 15
file content (86 lines) | stat: -rw-r--r-- 2,591 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from __future__ import annotations

import json
from typing import NamedTuple, cast

import xeddsa

from .migrations import parse_signed_pre_key_pair_model
from .models import SignedPreKeyPairModel
from .types import JSONObject


__all__ = [
    "SignedPreKeyPair"
]


class SignedPreKeyPair(NamedTuple):
    """
    A signed pre key, i.e. a pre key whose public key was encoded using an application-specific encoding
    format, then signed by the identity key, and stored together with a generation timestamp for periodic
    rotation.
    """

    priv: bytes
    sig: bytes
    timestamp: int

    @property
    def pub(self) -> bytes:
        """
        Returns:
            The public key of this signed pre key.
        """

        return xeddsa.priv_to_curve25519_pub(self.priv)

    @property
    def model(self) -> SignedPreKeyPairModel:
        """
        Returns:
            The internal state of this :class:`SignedPreKeyPair` as a pydantic model.
        """

        return SignedPreKeyPairModel(priv=self.priv, sig=self.sig, timestamp=self.timestamp)

    @property
    def json(self) -> JSONObject:
        """
        Returns:
            The internal state of this :class:`SignedPreKeyPair` as a JSON-serializable Python object.
        """

        return cast(JSONObject, json.loads(self.model.json()))

    @staticmethod
    def from_model(model: SignedPreKeyPairModel) -> "SignedPreKeyPair":
        """
        Args:
            model: The pydantic model holding the internal state of a :class:`SignedPreKeyPair`, as produced
                by :attr:`model`.

        Returns:
            A configured instance of :class:`SignedPreKeyPair`, with internal state restored from the model.

        Warning:
            Migrations are not provided via the :attr:`model`/:meth:`from_model` API. Use
            :attr:`json`/:meth:`from_json` instead. Refer to :ref:`serialization_and_migration` in the
            documentation for details.
        """

        return SignedPreKeyPair(priv=model.priv, sig=model.sig, timestamp=model.timestamp)

    @staticmethod
    def from_json(serialized: JSONObject) -> "SignedPreKeyPair":
        """
        Args:
            serialized: A JSON-serializable Python object holding the internal state of a
                :class:`SignedPreKeyPair`, as produced by :attr:`json`.

        Returns:
            A configured instance of :class:`SignedPreKeyPair`, with internal state restored from the
            serialized data.
        """

        return SignedPreKeyPair.from_model(parse_signed_pre_key_pair_model(serialized))