Rust and WASM did-method-plc tools and structures

Fix DID derivation and signature generation. #1

merged opened by zicklag.dev targeting main from zicklag.dev/atproto-plc: fix/did-generation-and-signatures

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.

Note that there is still an issue with using p256 keys. For some reason the did:key serialization for p256 keys is the wrong length.

Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:ulg2bzgrgs7ddjjlmhtegk3v/sh.tangled.repo.pull/3maaaonimsw22
+3 -7
Diff #1
+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 }

History

2 rounds 0 comments
sign up or login to add to the discussion
1 commit
expand
fix: fix did derivation and signature generation.
expand 0 comments
pull request successfully merged
1 commit
expand
fix: fix did derivation and signature generation.
expand 0 comments