node: Use CJSON for identities

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier 2022-09-07 14:40:28 +02:00
parent 46ee08aaf3
commit c80ba01ed9
No known key found for this signature in database
3 changed files with 18 additions and 10 deletions

View File

@ -19,6 +19,7 @@ git-url = { version = "0.3.5", features = ["serde1"] }
multibase = { version = "0.9.1" } multibase = { version = "0.9.1" }
log = { version = "0.4.17", features = ["std"] } log = { version = "0.4.17", features = ["std"] }
once_cell = { version = "1.13" } once_cell = { version = "1.13" }
olpc-cjson = { version = "0.1.1" }
sha2 = { version = "0.10.2" } sha2 = { version = "0.10.2" }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["preserve_order"] } serde_json = { version = "1", features = ["preserve_order"] }
@ -28,7 +29,6 @@ nonempty = { version = "0.8.0", features = ["serialize"] }
nakamoto-net = { version = "0.3.0" } nakamoto-net = { version = "0.3.0" }
nakamoto-net-poll = { version = "0.3.0" } nakamoto-net-poll = { version = "0.3.0" }
thiserror = { version = "1" } thiserror = { version = "1" }
toml = { version = "0.5.9" }
[dev-dependencies] [dev-dependencies]
quickcheck = { version = "1", default-features = false } quickcheck = { version = "1", default-features = false }

View File

@ -12,7 +12,7 @@ use crate::hash;
use crate::serde_ext; use crate::serde_ext;
use crate::storage::Remotes; use crate::storage::Remotes;
pub static IDENTITY_PATH: Lazy<&Path> = Lazy::new(|| Path::new("Radicle.toml")); pub static IDENTITY_PATH: Lazy<&Path> = Lazy::new(|| Path::new("radicle.json"));
/// A user's identifier is simply their public key. /// A user's identifier is simply their public key.
pub type UserId = crypto::PublicKey; pub type UserId = crypto::PublicKey;
@ -154,8 +154,8 @@ pub struct Project {
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum DocError { pub enum DocError {
#[error("toml: {0}")] #[error("json: {0}")]
Toml(#[from] toml::ser::Error), Json(#[from] serde_json::Error),
#[error("i/o: {0}")] #[error("i/o: {0}")]
Io(#[from] io::Error), Io(#[from] io::Error),
} }
@ -178,17 +178,25 @@ pub struct Doc {
impl Doc { impl Doc {
pub fn write<W: io::Write>(&self, mut writer: W) -> Result<ProjId, DocError> { pub fn write<W: io::Write>(&self, mut writer: W) -> Result<ProjId, DocError> {
let buf = toml::to_string_pretty(self)?; let mut buf = Vec::new();
let digest = hash::Digest::new(buf.as_bytes()); let mut ser =
serde_json::Serializer::with_formatter(&mut buf, olpc_cjson::CanonicalFormatter::new());
self.serialize(&mut ser)?;
let digest = hash::Digest::new(&buf);
let id = ProjId::from(digest); let id = ProjId::from(digest);
writer.write_all(buf.as_bytes())?; // TODO: Currently, we serialize the document in canonical JSON. This isn't strictly
// necessary, as we could use CJSON just to get the hash, but then write a prettified
// version to disk to make it easier for users to edit.
writer.write_all(&buf)?;
Ok(id) Ok(id)
} }
pub fn from_toml(bytes: &[u8]) -> Result<Self, toml::de::Error> { pub fn from_json(bytes: &[u8]) -> Result<Self, serde_json::Error> {
toml::from_slice(bytes) serde_json::from_slice(bytes)
} }
} }

View File

@ -216,7 +216,7 @@ impl Repository {
Err(e) => return Err(e.into()), Err(e) => return Err(e.into()),
Ok(doc) => doc, Ok(doc) => doc,
}; };
let doc = identity::Doc::from_toml(doc.content()).unwrap(); let doc = identity::Doc::from_json(doc.content()).unwrap();
Ok(Some(doc)) Ok(Some(doc))
} }