6
6
#include < common/args.h>
7
7
#include < common/sv2_noise.h>
8
8
#include < logging.h>
9
+ #include < util/readwritefile.h>
9
10
#include < util/strencodings.h>
10
11
#include < util/thread.h>
11
12
#include < validation.h>
@@ -14,10 +15,46 @@ Sv2TemplateProvider::Sv2TemplateProvider(interfaces::Mining& mining) : m_mining{
14
15
{
15
16
// TODO: persist static key
16
17
CKey static_key;
17
- static_key.MakeNewKey (true );
18
-
19
- auto authority_key{GenerateRandomKey ()};
20
-
18
+ try {
19
+ AutoFile{fsbridge::fopen (GetStaticKeyFile (), " rb" )} >> static_key;
20
+ LogPrintLevel (BCLog::SV2, BCLog::Level::Debug, " Reading cached static key from %s\n " , fs::PathToString (GetStaticKeyFile ()));
21
+ } catch (const std::ios_base::failure&) {
22
+ // File is not expected to exist the first time.
23
+ // In the unlikely event that loading an existing key fails, create a new one.
24
+ }
25
+ if (!static_key.IsValid ()) {
26
+ static_key = GenerateRandomKey ();
27
+ try {
28
+ AutoFile{fsbridge::fopen (GetStaticKeyFile (), " wb" )} << static_key;
29
+ } catch (const std::ios_base::failure&) {
30
+ LogPrintLevel (BCLog::SV2, BCLog::Level::Error, " Error writing static key to %s\n " , fs::PathToString (GetStaticKeyFile ()));
31
+ // Continue, because this is not a critical failure.
32
+ }
33
+ LogPrintLevel (BCLog::SV2, BCLog::Level::Debug, " Generated static key, saved to %s\n " , fs::PathToString (GetStaticKeyFile ()));
34
+ }
35
+ LogPrintLevel (BCLog::SV2, BCLog::Level::Info, " Static key: %s\n " , HexStr (static_key.GetPubKey ()));
36
+
37
+ // Generate self signed certificate using (cached) authority key
38
+ // TODO: skip loading authoritity key if -sv2cert is used
39
+
40
+ // Load authority key if cached
41
+ CKey authority_key;
42
+ try {
43
+ AutoFile{fsbridge::fopen (GetAuthorityKeyFile (), " rb" )} >> authority_key;
44
+ } catch (const std::ios_base::failure&) {
45
+ // File is not expected to exist the first time.
46
+ // In the unlikely event that loading an existing key fails, create a new one.
47
+ }
48
+ if (!authority_key.IsValid ()) {
49
+ authority_key = GenerateRandomKey ();
50
+ try {
51
+ AutoFile{fsbridge::fopen (GetAuthorityKeyFile (), " wb" )} << authority_key;
52
+ } catch (const std::ios_base::failure&) {
53
+ LogPrintLevel (BCLog::SV2, BCLog::Level::Error, " Error writing authority key to %s\n " , fs::PathToString (GetAuthorityKeyFile ()));
54
+ // Continue, because this is not a critical failure.
55
+ }
56
+ LogPrintLevel (BCLog::SV2, BCLog::Level::Debug, " Generated authority key, saved to %s\n " , fs::PathToString (GetAuthorityKeyFile ()));
57
+ }
21
58
// SRI uses base58 encoded x-only pubkeys in its configuration files
22
59
std::array<unsigned char , 34 > version_pubkey_bytes;
23
60
version_pubkey_bytes[0 ] = 1 ;
@@ -35,11 +72,19 @@ Sv2TemplateProvider::Sv2TemplateProvider(interfaces::Mining& mining) : m_mining{
35
72
uint32_t valid_to = std::numeric_limits<unsigned int >::max (); // 2106
36
73
Sv2SignatureNoiseMessage certificate = Sv2SignatureNoiseMessage (version, valid_from, valid_to, XOnlyPubKey (static_key.GetPubKey ()), authority_key);
37
74
38
- // TODO: persist certificate
39
-
40
75
m_connman = std::make_unique<Sv2Connman>(TP_SUBPROTOCOL, static_key, m_authority_pubkey, certificate);
41
76
}
42
77
78
+ fs::path Sv2TemplateProvider::GetStaticKeyFile ()
79
+ {
80
+ return gArgs .GetDataDirNet () / " sv2_static_key" ;
81
+ }
82
+
83
+ fs::path Sv2TemplateProvider::GetAuthorityKeyFile ()
84
+ {
85
+ return gArgs .GetDataDirNet () / " sv2_authority_key" ;
86
+ }
87
+
43
88
bool Sv2TemplateProvider::Start (const Sv2TemplateProviderOptions& options)
44
89
{
45
90
m_options = options;
0 commit comments