Rust and WASM did-method-plc tools and structures

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.

authored by zicklag.dev and committed by tangled.org 86466c3e 32d3e814

Changed files
+3 -7
src
+3 -3
src/builder.rs
··· 3 3 use crate::crypto::SigningKey; 4 4 use crate::did::Did; 5 5 use crate::document::ServiceEndpoint; 6 - use crate::encoding::{base32_encode, sha256}; 6 + use crate::encoding::{base32_encode, dag_cbor_encode, sha256}; 7 7 use crate::error::{PlcError, Result}; 8 8 use crate::operations::{Operation, UnsignedOperation}; 9 9 use crate::validation::{ ··· 229 229 230 230 // The DID is derived from the CID by taking the hash portion 231 231 // For simplicity, we'll hash the entire serialized operation 232 - let serialized = serde_json::to_vec(operation) 233 - .map_err(|e| PlcError::DagCborError(e.to_string()))?; 232 + let serialized = 233 + dag_cbor_encode(operation).map_err(|e| PlcError::DagCborError(e.to_string()))?; 234 234 235 235 let hash = sha256(&serialized); 236 236 let encoded = base32_encode(&hash);
-4
src/operations.rs
··· 30 30 services: HashMap<String, ServiceEndpoint>, 31 31 32 32 /// Previous operation CID (null for genesis) 33 - #[serde(skip_serializing_if = "Option::is_none")] 34 33 prev: Option<String>, 35 34 36 35 /// Base64url-encoded signature ··· 65 64 service: String, 66 65 67 66 /// Previous operation CID 68 - #[serde(skip_serializing_if = "Option::is_none")] 69 67 prev: Option<String>, 70 68 71 69 /// Base64url-encoded signature ··· 247 245 services: HashMap<String, ServiceEndpoint>, 248 246 249 247 /// CID of previous operation (None for genesis) 250 - #[serde(skip_serializing_if = "Option::is_none")] 251 248 prev: Option<String>, 252 249 }, 253 250 ··· 276 273 service: String, 277 274 278 275 /// CID of previous operation (None for genesis) 279 - #[serde(skip_serializing_if = "Option::is_none")] 280 276 prev: Option<String>, 281 277 }, 282 278 }