Coverage for sfkit/encryption/generate_personal_keys.py: 100%
20 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-07 15:11 -0400
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-07 15:11 -0400
1import os
3from nacl.encoding import HexEncoder
4from nacl.public import PrivateKey
6from sfkit.api import update_firestore
7from sfkit.utils import constants
8from sfkit.utils.helper_functions import authenticate_user
11def generate_personal_keys() -> None:
12 """
13 Generate and save a new keypair (public and private keys) for the user.
14 """
15 authenticate_user()
17 private_key: PrivateKey = PrivateKey.generate()
18 public_key: str = private_key.public_key.encode(encoder=HexEncoder).decode()
20 public_key_path: str = os.path.join(constants.SFKIT_DIR, "my_public_key.txt")
21 os.makedirs(constants.SFKIT_DIR, exist_ok=True)
22 with open(public_key_path, "w") as f:
23 f.write(public_key + "\n")
25 private_key_path = os.path.join(constants.SFKIT_DIR, "my_private_key.txt")
26 with open(private_key_path, "w") as f:
27 f.write(private_key.encode(encoder=HexEncoder).decode() + "\n")
28 print(f"Your public and private keys have been generated and saved to {constants.SFKIT_DIR}.")
30 update_firestore(f"update_firestore::PUBLIC_KEY={public_key}")
31 print("Your public key has been uploaded to the website and is available for all participants in your study.")