1- from unpaddedbase64 import encode_base64 , decode_base64
1+ # -*- coding: utf-8 -*-
2+
3+ # Copyright 2014 OpenMarket Ltd
4+ # Copyright 2020 The Matrix.org Foundation C.I.C
5+ #
6+ # Licensed under the Apache License, Version 2.0 (the "License");
7+ # you may not use this file except in compliance with the License.
8+ # You may obtain a copy of the License at
9+ #
10+ # http://www.apache.org/licenses/LICENSE-2.0
11+ #
12+ # Unless required by applicable law or agreed to in writing, software
13+ # distributed under the License is distributed on an "AS IS" BASIS,
14+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ # See the License for the specific language governing permissions and
16+ # limitations under the License.
17+
18+ from typing import Iterable , List , TextIO
19+
220import nacl .signing
21+ from unpaddedbase64 import decode_base64 , encode_base64
22+
23+ from signedjson .types import SigningKey , VerifyKey
324
425NACL_ED25519 = "ed25519"
526SUPPORTED_ALGORITHMS = [NACL_ED25519 ]
627
728
829def generate_signing_key (version ):
30+ # type: (str) -> SigningKey
931 """Generate a new signing key
1032 Args:
11- version (str) : Identifies this key out the keys for this entity.
33+ version: Identifies this key out the keys for this entity.
1234 Returns:
1335 A SigningKey object.
1436 """
@@ -19,6 +41,7 @@ def generate_signing_key(version):
1941
2042
2143def get_verify_key (signing_key ):
44+ # type: (SigningKey) -> VerifyKey
2245 """Get a verify key from a signing key"""
2346 verify_key = signing_key .verify_key
2447 verify_key .version = signing_key .version
@@ -27,11 +50,12 @@ def get_verify_key(signing_key):
2750
2851
2952def decode_signing_key_base64 (algorithm , version , key_base64 ):
53+ # type: (str, str, bytes) -> SigningKey
3054 """Decode a base64 encoded signing key
3155 Args:
32- algorithm (str) : The algorithm the key is for (currently "ed25519").
33- version (str) : Identifies this key out of the keys for this entity.
34- key_base64 (str) : Base64 encoded bytes of the key.
56+ algorithm: The algorithm the key is for (currently "ed25519").
57+ version: Identifies this key out of the keys for this entity.
58+ key_base64: Base64 encoded bytes of the key.
3559 Returns:
3660 A SigningKey object.
3761 """
@@ -46,26 +70,29 @@ def decode_signing_key_base64(algorithm, version, key_base64):
4670
4771
4872def encode_signing_key_base64 (key ):
73+ # type: (SigningKey) -> str
4974 """Encode a signing key as base64
5075 Args:
51- key (SigningKey) : A signing key to encode.
76+ key: A signing key to encode.
5277 Returns:
5378 base64 encoded string.
5479 """
5580 return encode_base64 (key .encode ())
5681
5782
5883def encode_verify_key_base64 (key ):
84+ # type: (VerifyKey) -> str
5985 """Encode a verify key as base64
6086 Args:
61- key (VerifyKey) : A signing key to encode.
87+ key: A signing key to encode.
6288 Returns:
6389 base64 encoded string.
6490 """
6591 return encode_base64 (key .encode ())
6692
6793
6894def is_signing_algorithm_supported (key_id ):
95+ # type: (str) -> bool
6996 """Is the signing algorithm for this key_id supported"""
7097 if key_id .startswith (NACL_ED25519 + ":" ):
7198 return True
@@ -74,10 +101,11 @@ def is_signing_algorithm_supported(key_id):
74101
75102
76103def decode_verify_key_bytes (key_id , key_bytes ):
104+ # type: (str, bytes) -> VerifyKey
77105 """Decode a raw verify key
78106 Args:
79- key_id (str) : Identifies this key out of the keys for this entity.
80- key_bytes (str) : Raw bytes of the key.
107+ key_id: Identifies this key out of the keys for this entity.
108+ key_bytes: Raw bytes of the key.
81109 Returns:
82110 A VerifyKey object.
83111 """
@@ -92,6 +120,7 @@ def decode_verify_key_bytes(key_id, key_bytes):
92120
93121
94122def read_signing_keys (stream ):
123+ # type: (Iterable[str]) -> List[SigningKey]
95124 """Reads a list of keys from a stream
96125 Args:
97126 stream : A stream to iterate for keys.
@@ -107,6 +136,7 @@ def read_signing_keys(stream):
107136
108137
109138def read_old_signing_keys (stream ):
139+ # type: (Iterable[str]) -> List[VerifyKey]
110140 """Reads a list of old keys from a stream
111141 Args:
112142 stream : A stream to iterate for keys.
@@ -124,6 +154,7 @@ def read_old_signing_keys(stream):
124154
125155
126156def write_signing_keys (stream , keys ):
157+ # type: (TextIO, Iterable[SigningKey]) -> None
127158 """Writes a list of keys to a stream.
128159 Args:
129160 stream: Stream to write keys to.
0 commit comments