2023-11-01 04:57:08 +00:00
|
|
|
"""Utility functions"""
|
|
|
|
|
2017-08-05 16:21:14 +00:00
|
|
|
from base64 import b64decode, b64encode
|
2023-11-01 04:57:08 +00:00
|
|
|
import os
|
2023-11-01 06:22:29 +00:00
|
|
|
from typing import TypedDict
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2023-11-01 06:22:29 +00:00
|
|
|
from nacl.signing import SigningKey, VerifyKey
|
2023-11-01 06:16:17 +00:00
|
|
|
import yaml
|
2017-05-25 10:47:01 +00:00
|
|
|
|
|
|
|
|
2023-11-01 06:22:29 +00:00
|
|
|
class SSBSecret(TypedDict):
|
2023-11-01 06:22:29 +00:00
|
|
|
"""Dictionary type to hold an SSB secret identity"""
|
2023-11-01 06:22:29 +00:00
|
|
|
|
|
|
|
keypair: SigningKey
|
|
|
|
id: str
|
|
|
|
|
|
|
|
|
2017-08-05 10:24:46 +00:00
|
|
|
class ConfigException(Exception):
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Exception to raise if there is a problem with the configuration data"""
|
2017-05-25 10:47:01 +00:00
|
|
|
|
|
|
|
|
2023-11-01 06:22:29 +00:00
|
|
|
def tag(key: VerifyKey) -> bytes:
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Create tag from public key."""
|
2023-11-01 04:04:43 +00:00
|
|
|
|
|
|
|
return b"@" + b64encode(bytes(key)) + b".ed25519"
|
2017-08-05 16:21:14 +00:00
|
|
|
|
|
|
|
|
2023-11-01 06:22:29 +00:00
|
|
|
def load_ssb_secret() -> SSBSecret:
|
2017-08-05 10:24:46 +00:00
|
|
|
"""Load SSB keys from ~/.ssb"""
|
2023-11-01 04:04:43 +00:00
|
|
|
|
2023-11-01 04:57:08 +00:00
|
|
|
with open(os.path.expanduser("~/.ssb/secret"), encoding="utf-8") as f:
|
2019-06-12 19:09:48 +00:00
|
|
|
config = yaml.load(f, Loader=yaml.SafeLoader)
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
if config["curve"] != "ed25519":
|
|
|
|
raise ConfigException("Algorithm not known: " + config["curve"])
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
server_prv_key = b64decode(config["private"][:-8])
|
2023-11-01 06:22:29 +00:00
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
return {"keypair": SigningKey(server_prv_key[:32]), "id": config["id"]}
|