From a1c6d209437341ba0293699a31008ab90a5d8877 Mon Sep 17 00:00:00 2001 From: Zicklag Date: Wed, 17 Dec 2025 21:27:18 -0600 Subject: [PATCH] fix: fix did derivation and signature generation. This fixes two bugs that were affecting did derivation and signature generation. - The `UnsignedOperation` and `Operation` types were not serializing prev as `null` and were instead skipping serialization when it was `None`. - The DID was being calculated from the hash of the JSON encoding instead of the CBOR encoding. --- src/builder.rs | 6 +++--- src/operations.rs | 8 +++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index b25d7d9..6bf7908 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -3,7 +3,7 @@ use crate::crypto::SigningKey; use crate::did::Did; use crate::document::ServiceEndpoint; -use crate::encoding::{base32_encode, sha256}; +use crate::encoding::{base32_encode, dag_cbor_encode, sha256}; use crate::error::{PlcError, Result}; use crate::operations::{Operation, UnsignedOperation}; use crate::validation::{ @@ -229,8 +229,8 @@ impl DidBuilder { // The DID is derived from the CID by taking the hash portion // For simplicity, we'll hash the entire serialized operation - let serialized = serde_json::to_vec(operation) - .map_err(|e| PlcError::DagCborError(e.to_string()))?; + let serialized = + dag_cbor_encode(operation).map_err(|e| PlcError::DagCborError(e.to_string()))?; let hash = sha256(&serialized); let encoded = base32_encode(&hash); diff --git a/src/operations.rs b/src/operations.rs index 33e27ae..3ee14d6 100644 --- a/src/operations.rs +++ b/src/operations.rs @@ -30,7 +30,6 @@ pub enum Operation { services: HashMap, /// Previous operation CID (null for genesis) - #[serde(skip_serializing_if = "Option::is_none")] prev: Option, /// Base64url-encoded signature @@ -193,9 +192,9 @@ impl Operation { services: services.clone(), prev: prev.clone(), }, - Operation::PlcTombstone { prev, .. } => UnsignedOperation::PlcTombstone { - prev: prev.clone(), - }, + Operation::PlcTombstone { prev, .. } => { + UnsignedOperation::PlcTombstone { prev: prev.clone() } + } Operation::LegacyCreate { signing_key, recovery_key, @@ -247,7 +246,6 @@ pub enum UnsignedOperation { services: HashMap, /// CID of previous operation (None for genesis) - #[serde(skip_serializing_if = "Option::is_none")] prev: Option, }, -- 2.43.0