* tidy ups of PRIVATE_KEY_IMPORT/_EXPORT stuff

This commit is contained in:
Scott Powell
2025-02-21 13:51:26 +11:00
parent 0bb202327a
commit f434123e99
2 changed files with 31 additions and 31 deletions

View File

@@ -46,14 +46,6 @@
#define OFFLINE_QUEUE_SIZE 16 #define OFFLINE_QUEUE_SIZE 16
#endif #endif
#ifndef ENABLE_PRIVATE_KEY_EXPORT
#define ENABLE_PRIVATE_KEY_EXPORT 0
#endif
#ifndef ENABLE_PRIVATE_KEY_IMPORT
#define ENABLE_PRIVATE_KEY_IMPORT 0
#endif
#include <helpers/BaseChatMesh.h> #include <helpers/BaseChatMesh.h>
#define SEND_TIMEOUT_BASE_MILLIS 500 #define SEND_TIMEOUT_BASE_MILLIS 500
@@ -196,23 +188,14 @@ class MyMesh : public BaseChatMesh {
int offline_queue_len; int offline_queue_len;
Frame offline_queue[OFFLINE_QUEUE_SIZE]; Frame offline_queue[OFFLINE_QUEUE_SIZE];
void initIdentityStore(FILESYSTEM& fs) {
#if defined(NRF52_PLATFORM)
_identity_store = new IdentityStore(fs, "");
#else
_identity_store = new IdentityStore(fs, "/identity");
#endif
}
void loadMainIdentity(mesh::RNG& trng) { void loadMainIdentity(mesh::RNG& trng) {
if(!_identity_store->load("_main", self_id)){ if (!_identity_store->load("_main", self_id)) {
self_id = mesh::LocalIdentity(&trng); // create new random identity self_id = mesh::LocalIdentity(&trng); // create new random identity
saveMainIdentity(self_id); saveMainIdentity(self_id);
} }
} }
bool saveMainIdentity(mesh::LocalIdentity identity) { bool saveMainIdentity(const mesh::LocalIdentity& identity) {
self_id = identity;
return _identity_store->save("_main", identity); return _identity_store->save("_main", identity);
} }
@@ -516,6 +499,7 @@ public:
_iter_started = false; _iter_started = false;
offline_queue_len = 0; offline_queue_len = 0;
app_target_ver = 0; app_target_ver = 0;
_identity_store = NULL;
// defaults // defaults
memset(&_prefs, 0, sizeof(_prefs)); memset(&_prefs, 0, sizeof(_prefs));
@@ -534,7 +518,12 @@ public:
BaseChatMesh::begin(); BaseChatMesh::begin();
initIdentityStore(fs); #if defined(NRF52_PLATFORM)
_identity_store = new IdentityStore(fs, "");
#else
_identity_store = new IdentityStore(fs, "/identity");
#endif
loadMainIdentity(trng); loadMainIdentity(trng);
// load persisted prefs // load persisted prefs
@@ -877,28 +866,27 @@ public:
memcpy(&reply[1], &battery_millivolts, 2); memcpy(&reply[1], &battery_millivolts, 2);
_serial->writeFrame(reply, 3); _serial->writeFrame(reply, 3);
} else if (cmd_frame[0] == CMD_EXPORT_PRIVATE_KEY) { } else if (cmd_frame[0] == CMD_EXPORT_PRIVATE_KEY) {
if(ENABLE_PRIVATE_KEY_EXPORT == 1){ #if ENABLE_PRIVATE_KEY_EXPORT
uint8_t reply[65]; uint8_t reply[65];
reply[0] = RESP_CODE_PRIVATE_KEY; reply[0] = RESP_CODE_PRIVATE_KEY;
uint8_t private_key[64]; self_id.writeTo(&reply[1], 64);
self_id.writeTo(private_key, 64);
memcpy(&reply[1], &private_key, 64);
_serial->writeFrame(reply, 65); _serial->writeFrame(reply, 65);
} else { #else
writeDisabledFrame(); writeDisabledFrame();
} #endif
} else if (cmd_frame[0] == CMD_IMPORT_PRIVATE_KEY && len >= 65) { } else if (cmd_frame[0] == CMD_IMPORT_PRIVATE_KEY && len >= 65) {
if(ENABLE_PRIVATE_KEY_IMPORT == 1){ #if ENABLE_PRIVATE_KEY_IMPORT
mesh::LocalIdentity identity = mesh::LocalIdentity(); mesh::LocalIdentity identity;
identity.readFrom(&cmd_frame[1], 64); identity.readFrom(&cmd_frame[1], 64);
if(saveMainIdentity(identity)){ if (saveMainIdentity(identity)) {
self_id = identity;
writeOKFrame(); writeOKFrame();
} else { } else {
writeErrFrame(); writeErrFrame();
} }
} else { #else
writeDisabledFrame(); writeDisabledFrame();
} #endif
} else { } else {
writeErrFrame(); writeErrFrame();
MESH_DEBUG_PRINTLN("ERROR: unknown command: %02X", cmd_frame[0]); MESH_DEBUG_PRINTLN("ERROR: unknown command: %02X", cmd_frame[0]);

View File

@@ -161,6 +161,8 @@ build_flags =
${Heltec_lora32_v3.build_flags} ${Heltec_lora32_v3.build_flags}
-D MAX_CONTACTS=100 -D MAX_CONTACTS=100
-D MAX_GROUP_CHANNELS=1 -D MAX_GROUP_CHANNELS=1
; -D ENABLE_PRIVATE_KEY_IMPORT=1
; -D ENABLE_PRIVATE_KEY_EXPORT=1
; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1 ; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1
; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1 ; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1
build_src_filter = ${Heltec_lora32_v3.build_src_filter} +<../examples/companion_radio/main.cpp> build_src_filter = ${Heltec_lora32_v3.build_src_filter} +<../examples/companion_radio/main.cpp>
@@ -177,6 +179,8 @@ build_flags =
-D MAX_GROUP_CHANNELS=1 -D MAX_GROUP_CHANNELS=1
-D BLE_PIN_CODE=123456 -D BLE_PIN_CODE=123456
-D BLE_DEBUG_LOGGING=1 -D BLE_DEBUG_LOGGING=1
; -D ENABLE_PRIVATE_KEY_IMPORT=1
; -D ENABLE_PRIVATE_KEY_EXPORT=1
; -D MESH_PACKET_LOGGING=1 ; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1 ; -D MESH_DEBUG=1
build_src_filter = ${Heltec_lora32_v3.build_src_filter} +<helpers/esp32/*.cpp> +<../examples/companion_radio/main.cpp> build_src_filter = ${Heltec_lora32_v3.build_src_filter} +<helpers/esp32/*.cpp> +<../examples/companion_radio/main.cpp>
@@ -352,6 +356,8 @@ build_flags =
${LilyGo_T3S3_sx1262.build_flags} ${LilyGo_T3S3_sx1262.build_flags}
-D MAX_CONTACTS=100 -D MAX_CONTACTS=100
-D MAX_GROUP_CHANNELS=1 -D MAX_GROUP_CHANNELS=1
; -D ENABLE_PRIVATE_KEY_IMPORT=1
; -D ENABLE_PRIVATE_KEY_EXPORT=1
; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1 ; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1
; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1 ; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1
build_src_filter = ${LilyGo_T3S3_sx1262.build_src_filter} +<../examples/companion_radio/main.cpp> build_src_filter = ${LilyGo_T3S3_sx1262.build_src_filter} +<../examples/companion_radio/main.cpp>
@@ -368,6 +374,8 @@ build_flags =
-D MAX_GROUP_CHANNELS=1 -D MAX_GROUP_CHANNELS=1
-D BLE_PIN_CODE=123456 -D BLE_PIN_CODE=123456
-D BLE_DEBUG_LOGGING=1 -D BLE_DEBUG_LOGGING=1
; -D ENABLE_PRIVATE_KEY_IMPORT=1
; -D ENABLE_PRIVATE_KEY_EXPORT=1
; -D MESH_PACKET_LOGGING=1 ; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1 ; -D MESH_DEBUG=1
build_src_filter = ${LilyGo_T3S3_sx1262.build_src_filter} +<helpers/esp32/*.cpp> +<../examples/companion_radio/main.cpp> build_src_filter = ${LilyGo_T3S3_sx1262.build_src_filter} +<helpers/esp32/*.cpp> +<../examples/companion_radio/main.cpp>
@@ -455,6 +463,8 @@ build_flags =
${rak4631.build_flags} ${rak4631.build_flags}
-D MAX_CONTACTS=100 -D MAX_CONTACTS=100
-D MAX_GROUP_CHANNELS=1 -D MAX_GROUP_CHANNELS=1
; -D ENABLE_PRIVATE_KEY_IMPORT=1
; -D ENABLE_PRIVATE_KEY_EXPORT=1
; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1 ; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1
; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1 ; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1
build_src_filter = ${rak4631.build_src_filter} +<../examples/companion_radio/main.cpp> build_src_filter = ${rak4631.build_src_filter} +<../examples/companion_radio/main.cpp>
@@ -471,6 +481,8 @@ build_flags =
-D MAX_GROUP_CHANNELS=1 -D MAX_GROUP_CHANNELS=1
-D BLE_PIN_CODE=123456 -D BLE_PIN_CODE=123456
-D BLE_DEBUG_LOGGING=1 -D BLE_DEBUG_LOGGING=1
; -D ENABLE_PRIVATE_KEY_IMPORT=1
; -D ENABLE_PRIVATE_KEY_EXPORT=1
; -D MESH_PACKET_LOGGING=1 ; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1 ; -D MESH_DEBUG=1
build_src_filter = ${rak4631.build_src_filter} +<helpers/nrf52/*.cpp> +<../examples/companion_radio/main.cpp> build_src_filter = ${rak4631.build_src_filter} +<helpers/nrf52/*.cpp> +<../examples/companion_radio/main.cpp>