11import os
2- from typing import TypeVar
32from ruamel .yaml import YAML
4- from koi_net .protocol .node import NodeProfile
5- from rid_lib .types import KoiNetNode
63from pydantic import BaseModel , Field , PrivateAttr
74from dotenv import load_dotenv
5+ from rid_lib .ext .utils import sha256_hash
6+ from rid_lib .types import KoiNetNode
7+ from .protocol .secure import PrivateKey
8+ from .protocol .node import NodeProfile , NodeType
89
910
1011class ServerConfig (BaseModel ):
11- host : str | None = "127.0.0.1"
12- port : int | None = 8000
12+ host : str = "127.0.0.1"
13+ port : int = 8000
1314 path : str | None = "/koi-net"
1415
1516 @property
1617 def url (self ) -> str :
1718 return f"http://{ self .host } :{ self .port } { self .path or '' } "
1819
20+ class NodeContact (BaseModel ):
21+ rid : KoiNetNode | None = None
22+ url : str | None = None
23+
1924class KoiNetConfig (BaseModel ):
2025 node_name : str
2126 node_rid : KoiNetNode | None = None
2227 node_profile : NodeProfile
2328
24- cache_directory_path : str | None = ".rid_cache"
25- event_queues_path : str | None = "event_queues.json"
26-
27- first_contact : str | None = None
29+ cache_directory_path : str = ".rid_cache"
30+ event_queues_path : str = "event_queues.json"
31+ private_key_pem_path : str = "priv_key.pem"
32+
33+ first_contact : NodeContact = Field (default_factory = NodeContact )
34+
35+ _priv_key : PrivateKey | None = PrivateAttr (default = None )
2836
2937class EnvConfig (BaseModel ):
38+ priv_key_password : str | None = "PRIV_KEY_PASSWORD"
39+
3040 def __init__ (self , ** kwargs ):
3141 super ().__init__ (** kwargs )
3242 load_dotenv ()
@@ -41,8 +51,10 @@ def __getattribute__(self, name):
4151 return value
4252
4353class NodeConfig (BaseModel ):
44- server : ServerConfig | None = Field (default_factory = ServerConfig )
54+ server : ServerConfig = Field (default_factory = ServerConfig )
4555 koi_net : KoiNetConfig
56+ env : EnvConfig = Field (default_factory = EnvConfig )
57+
4658 _file_path : str = PrivateAttr (default = "config.yaml" )
4759 _file_content : str | None = PrivateAttr (default = None )
4860
@@ -72,13 +84,27 @@ def load_from_yaml(
7284
7385 config ._file_path = file_path
7486
75- if generate_missing :
76- config .koi_net .node_rid = (
77- config .koi_net .node_rid or KoiNetNode .generate (config .koi_net .node_name )
78- )
79- config .koi_net .node_profile .base_url = (
80- config .koi_net .node_profile .base_url or config .server .url
81- )
87+ if generate_missing :
88+ if not config .koi_net .node_rid :
89+ priv_key = PrivateKey .generate ()
90+ pub_key = priv_key .public_key ()
91+
92+ config .koi_net .node_rid = KoiNetNode (
93+ config .koi_net .node_name ,
94+ sha256_hash (pub_key .to_der ())
95+ )
96+
97+ with open (config .koi_net .private_key_pem_path , "w" ) as f :
98+ f .write (
99+ priv_key .to_pem (config .env .priv_key_password )
100+ )
101+
102+ config .koi_net .node_profile .public_key = pub_key .to_der ()
103+
104+ if config .koi_net .node_profile .node_type == NodeType .FULL :
105+ config .koi_net .node_profile .base_url = (
106+ config .koi_net .node_profile .base_url or config .server .url
107+ )
82108
83109 config .save_to_yaml ()
84110
@@ -98,4 +124,3 @@ def save_to_yaml(self):
98124 f .write (self ._file_content )
99125 raise e
100126
101- ConfigType = TypeVar ("ConfigType" , bound = NodeConfig )
0 commit comments