Skip to content

Commit 6894e23

Browse files
committed
Persist static key for Template Provider
1 parent 1802b49 commit 6894e23

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

src/node/sv2_template_provider.cpp

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <common/args.h>
77
#include <common/sv2_noise.h>
88
#include <logging.h>
9+
#include <util/readwritefile.h>
910
#include <util/strencodings.h>
1011
#include <util/thread.h>
1112
#include <validation.h>
@@ -14,10 +15,46 @@ Sv2TemplateProvider::Sv2TemplateProvider(interfaces::Mining& mining) : m_mining{
1415
{
1516
// TODO: persist static key
1617
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+
}
2158
// SRI uses base58 encoded x-only pubkeys in its configuration files
2259
std::array<unsigned char, 34> version_pubkey_bytes;
2360
version_pubkey_bytes[0] = 1;
@@ -35,11 +72,19 @@ Sv2TemplateProvider::Sv2TemplateProvider(interfaces::Mining& mining) : m_mining{
3572
uint32_t valid_to = std::numeric_limits<unsigned int>::max(); // 2106
3673
Sv2SignatureNoiseMessage certificate = Sv2SignatureNoiseMessage(version, valid_from, valid_to, XOnlyPubKey(static_key.GetPubKey()), authority_key);
3774

38-
// TODO: persist certificate
39-
4075
m_connman = std::make_unique<Sv2Connman>(TP_SUBPROTOCOL, static_key, m_authority_pubkey, certificate);
4176
}
4277

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+
4388
bool Sv2TemplateProvider::Start(const Sv2TemplateProviderOptions& options)
4489
{
4590
m_options = options;

src/node/sv2_template_provider.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class Sv2TemplateProvider : public Sv2EventsInterface
5959

6060
std::unique_ptr<Sv2Connman> m_connman;
6161

62+
/** Get name of file to store static key */
63+
fs::path GetStaticKeyFile();
64+
65+
/** Get name of file to store authority key */
66+
fs::path GetAuthorityKeyFile();
67+
6268
/**
6369
* Configuration
6470
*/

0 commit comments

Comments
 (0)