Skip to content

Commit

Permalink
fix: use json 5 for tor identity (regression) (tari-project#3624)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sdbondi authored Dec 2, 2021
1 parent c53be9b commit 7d49fa4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion applications/tari_app_utilities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 6 additions & 8 deletions applications/tari_app_utilities/src/identity_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@

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},
exit_codes::ExitCodes,
};
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";

Expand Down Expand Up @@ -194,7 +192,7 @@ pub fn recover_node_identity<P: AsRef<Path>>(
///
/// ## Returns
/// Result containing an object on success, string will indicate reason on error
pub fn load_from_json<P: AsRef<Path>, T: MessageFormat>(path: P) -> Result<T, String> {
pub fn load_from_json<P: AsRef<Path>, T: DeserializeOwned>(path: P) -> Result<T, String> {
if !path.as_ref().exists() {
return Err(format!(
"Identity file, {}, does not exist.",
Expand All @@ -203,7 +201,7 @@ pub fn load_from_json<P: AsRef<Path>, T: MessageFormat>(path: P) -> Result<T, St
}

let contents = fs::read_to_string(path).map_err(|err| err.to_string())?;
let object = T::from_json(&contents).map_err(|err| err.to_string())?;
let object = json5::from_str(&contents).map_err(|err| err.to_string())?;
Ok(object)
}

Expand All @@ -214,8 +212,8 @@ pub fn load_from_json<P: AsRef<Path>, T: MessageFormat>(path: P) -> Result<T, St
///
/// ## Returns
/// Result to check if successful or not, string will indicate reason on error
pub fn save_as_json<P: AsRef<Path>, T: MessageFormat>(path: P, object: &T) -> Result<(), String> {
let json = object.to_json().unwrap();
pub fn save_as_json<P: AsRef<Path>, 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()))?;
Expand Down

0 comments on commit 7d49fa4

Please sign in to comment.