keyanchor 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- keyanchor/__init__.py +3 -0
- keyanchor/__main__.py +5 -0
- keyanchor/canon.py +62 -0
- keyanchor/cli.py +608 -0
- keyanchor/completions.py +117 -0
- keyanchor/github.py +65 -0
- keyanchor/gitmanifest.py +81 -0
- keyanchor/keys.py +111 -0
- keyanchor/ots.py +394 -0
- keyanchor/prompt.py +155 -0
- keyanchor/render.py +386 -0
- keyanchor/setup.py +317 -0
- keyanchor/ssh.py +120 -0
- keyanchor/store.py +224 -0
- keyanchor/verify.py +378 -0
- keyanchor-0.1.0.dist-info/METADATA +323 -0
- keyanchor-0.1.0.dist-info/RECORD +22 -0
- keyanchor-0.1.0.dist-info/WHEEL +5 -0
- keyanchor-0.1.0.dist-info/entry_points.txt +3 -0
- keyanchor-0.1.0.dist-info/licenses/LICENSE +202 -0
- keyanchor-0.1.0.dist-info/licenses/NOTICE +5 -0
- keyanchor-0.1.0.dist-info/top_level.txt +1 -0
keyanchor/__init__.py
ADDED
keyanchor/__main__.py
ADDED
keyanchor/canon.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Canonical statement bytes and content addresses.
|
|
2
|
+
|
|
3
|
+
Statements use only strings, integers, objects and arrays. For that subset, sorted keys and compact
|
|
4
|
+
separators give the RFC 8785 (JCS) form; floats, booleans and null are rejected so the subset holds.
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import hashlib
|
|
9
|
+
import json
|
|
10
|
+
|
|
11
|
+
MAX_INT = 2**53 - 1
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class CanonError(ValueError):
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _check(value: object, depth: int = 0) -> None:
|
|
19
|
+
if depth > 8:
|
|
20
|
+
raise CanonError("statement nested too deeply")
|
|
21
|
+
if isinstance(value, bool) or value is None or isinstance(value, float):
|
|
22
|
+
raise CanonError(f"unsupported JSON value: {value!r}")
|
|
23
|
+
if isinstance(value, int):
|
|
24
|
+
if abs(value) > MAX_INT:
|
|
25
|
+
raise CanonError("integer outside ±(2^53-1)")
|
|
26
|
+
elif isinstance(value, str):
|
|
27
|
+
pass
|
|
28
|
+
elif isinstance(value, list):
|
|
29
|
+
for item in value:
|
|
30
|
+
_check(item, depth + 1)
|
|
31
|
+
elif isinstance(value, dict):
|
|
32
|
+
for key, item in value.items():
|
|
33
|
+
if not isinstance(key, str) or not key.isascii():
|
|
34
|
+
raise CanonError(f"member names must be ASCII strings: {key!r}")
|
|
35
|
+
_check(item, depth + 1)
|
|
36
|
+
else:
|
|
37
|
+
raise CanonError(f"unsupported type: {type(value).__name__}")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def canonical(statement: dict) -> bytes:
|
|
41
|
+
if not isinstance(statement, dict):
|
|
42
|
+
raise CanonError("statement must be a JSON object")
|
|
43
|
+
_check(statement)
|
|
44
|
+
try:
|
|
45
|
+
return json.dumps(statement, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
|
|
46
|
+
except UnicodeEncodeError as exc: # lone surrogate from a "\ud800" escape
|
|
47
|
+
raise CanonError("string is not valid Unicode") from exc
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def content_address(raw: bytes) -> str:
|
|
51
|
+
return hashlib.sha256(raw).hexdigest()
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def parse_canonical(raw: bytes) -> dict:
|
|
55
|
+
"""Parse statement bytes; reject anything that is not byte-identical to its canonical form."""
|
|
56
|
+
try:
|
|
57
|
+
statement = json.loads(raw.decode("utf-8"))
|
|
58
|
+
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
|
|
59
|
+
raise CanonError(f"not JSON: {exc}") from exc
|
|
60
|
+
if canonical(statement) != raw:
|
|
61
|
+
raise CanonError("statement bytes are not canonical")
|
|
62
|
+
return statement
|