node: Check the id matches the root

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-09-18 01:08:04 +02:00
parent 6353d6115b
commit fa4ac94829
No known key found for this signature in database
1 changed files with 17 additions and 11 deletions

View File

@ -279,9 +279,9 @@ pub struct Identity<V> {
/// The head of the identity branch. This points to a commit that /// The head of the identity branch. This points to a commit that
/// contains the current document blob. /// contains the current document blob.
pub head: Oid, pub head: Oid,
/// The object id of the initial document blob. /// The canonical identifier for this identity.
/// This is the canonical identifier for this identity. /// This is the object id of the initial document blob.
pub root: Oid, pub root: Id,
/// The object id of the current document blob. /// The object id of the current document blob.
pub current: Oid, pub current: Oid,
/// The current document. /// The current document.
@ -294,7 +294,7 @@ pub struct Identity<V> {
impl Identity<Untrusted> { impl Identity<Untrusted> {
pub fn load<'r, R: ReadRepository<'r>>( pub fn load<'r, R: ReadRepository<'r>>(
_id: &Id, id: &Id,
remote: &RemoteId, remote: &RemoteId,
repo: &R, repo: &R,
) -> Result<Option<Self>, Error> { ) -> Result<Option<Self>, Error> {
@ -302,15 +302,21 @@ impl Identity<Untrusted> {
let mut history = repo.revwalk(head)?.collect::<Vec<_>>(); let mut history = repo.revwalk(head)?.collect::<Vec<_>>();
// Retrieve root document. // Retrieve root document.
let root = history.pop().unwrap()?.into(); let root_oid = history.pop().unwrap()?.into();
let root = Doc::blob_at(root, repo)?.unwrap(); let root_blob = Doc::blob_at(root_oid, repo)?.unwrap();
let trusted = Doc::from_json(root.content()).unwrap(); let root = Id::from(root_blob.id());
// TODO: Check the root document matches ID. let trusted = Doc::from_json(root_blob.content()).unwrap();
// The root hash must be equal to the id.
if root != *id {
todo!();
}
let mut trusted = trusted.verified()?; let mut trusted = trusted.verified()?;
let mut current = root.id().into(); let mut current = *root;
let mut signatures = Vec::new(); let mut signatures = Vec::new();
// Traverse the history chronologically.
for oid in history.into_iter().rev() { for oid in history.into_iter().rev() {
let oid = oid?; let oid = oid?;
let blob = Doc::blob_at(head, repo)?.unwrap(); let blob = Doc::blob_at(head, repo)?.unwrap();
@ -327,7 +333,7 @@ impl Identity<Untrusted> {
} }
} }
// Check that enough delegates from the previous version signed this version. // Check that enough delegates signed this next version.
let quorum = signatures let quorum = signatures
.iter() .iter()
.filter(|(key, _)| trusted.delegates.iter().any(|d| d.matches(key))) .filter(|(key, _)| trusted.delegates.iter().any(|d| d.matches(key)))
@ -342,7 +348,7 @@ impl Identity<Untrusted> {
} }
return Ok(Some(Self { return Ok(Some(Self {
root: root.id().into(), root,
head, head,
current, current,
doc: trusted, doc: trusted,