From 7d49fa4c092f5d7e0a373f3f0c91d9007534e575 Mon Sep 17 00:00:00 2001 From: Stan Bondi Date: Thu, 2 Dec 2021 13:26:26 +0200 Subject: [PATCH] fix: use json 5 for tor identity (regression) (#3624) Description --- Use json 5 for save/load identity functions Motivation and Context --- Save and load identity were using older version of the json parser which coul not parse the json comments. This causes a new tor identity and onion address to be created on each base node startup. How Has This Been Tested? --- Onion address does not change on base node restart --- Cargo.lock | 2 +- applications/tari_app_utilities/Cargo.toml | 2 +- .../tari_app_utilities/src/identity_management.rs | 14 ++++++-------- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 730b9ef7f5..caa7719ce2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4539,7 +4539,7 @@ dependencies = [ "log", "qrcode", "rand 0.8.4", - "serde_json", + "serde 1.0.130", "structopt", "strum 0.19.5", "strum_macros 0.19.4", diff --git a/applications/tari_app_utilities/Cargo.toml b/applications/tari_app_utilities/Cargo.toml index ae6d9c9d1f..d4f64bc4c7 100644 --- a/applications/tari_app_utilities/Cargo.toml +++ b/applications/tari_app_utilities/Cargo.toml @@ -15,7 +15,7 @@ config = { version = "0.9.3" } futures = { version = "^0.3.16", default-features = false, features = ["alloc"] } qrcode = { version = "0.12" } dirs-next = "1.0.2" -serde_json = "1.0" +serde = "1.0.126" json5 = "0.2.2" log = { version = "0.4.8", features = ["std"] } rand = "0.8" diff --git a/applications/tari_app_utilities/src/identity_management.rs b/applications/tari_app_utilities/src/identity_management.rs index 7dc6458b5f..086d35c672 100644 --- a/applications/tari_app_utilities/src/identity_management.rs +++ b/applications/tari_app_utilities/src/identity_management.rs @@ -22,6 +22,7 @@ use log::*; use rand::rngs::OsRng; +use serde::{de::DeserializeOwned, Serialize}; use std::{clone::Clone, fs, path::Path, str::FromStr, string::ToString, sync::Arc}; use tari_common::{ configuration::{bootstrap::prompt, utils::get_local_ip}, @@ -29,10 +30,7 @@ use tari_common::{ }; use tari_common_types::types::PrivateKey; use tari_comms::{multiaddr::Multiaddr, peer_manager::PeerFeatures, NodeIdentity}; -use tari_crypto::{ - keys::SecretKey, - tari_utilities::{hex::Hex, message_format::MessageFormat}, -}; +use tari_crypto::{keys::SecretKey, tari_utilities::hex::Hex}; pub const LOG_TARGET: &str = "tari_application"; @@ -194,7 +192,7 @@ pub fn recover_node_identity>( /// /// ## Returns /// Result containing an object on success, string will indicate reason on error -pub fn load_from_json, T: MessageFormat>(path: P) -> Result { +pub fn load_from_json, T: DeserializeOwned>(path: P) -> Result { if !path.as_ref().exists() { return Err(format!( "Identity file, {}, does not exist.", @@ -203,7 +201,7 @@ pub fn load_from_json, T: MessageFormat>(path: P) -> Result, T: MessageFormat>(path: P) -> Result, T: MessageFormat>(path: P, object: &T) -> Result<(), String> { - let json = object.to_json().unwrap(); +pub fn save_as_json, T: Serialize>(path: P, object: &T) -> Result<(), String> { + let json = json5::to_string(object).unwrap(); if let Some(p) = path.as_ref().parent() { if !p.exists() { fs::create_dir_all(p).map_err(|e| format!("Could not save json to data folder. {}", e.to_string()))?;