Skip to content

Commit 6bd3fbf

Browse files
committed
bft signature format
1 parent e5109d4 commit 6bd3fbf

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

src/types/bft.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,9 @@ impl UnicityCertificate {
407407
for (node_id, signature) in &self.unicity_seal.signatures {
408408
// Find the node in trust base
409409
if let Some(node) = trust_base.root_nodes.iter().find(|n| &n.node_id == node_id) {
410-
// Verify signature (remove recovery byte if present)
410+
// Remove the last byte (recovery ID) from 65-byte signature
411411
let sig_bytes = if signature.len() == 65 {
412-
&signature[..64]
412+
&signature[..signature.len() - 1]
413413
} else {
414414
signature
415415
};
@@ -436,7 +436,7 @@ impl UnicityCertificate {
436436
fn compute_seal_hash(&self) -> Result<Vec<u8>> {
437437
use ciborium::Value;
438438

439-
// Create seal without signatures for hashing
439+
// Create seal array without signatures (signatures = null in CBOR)
440440
let seal_array = vec![
441441
Value::Integer(self.unicity_seal.version.into()),
442442
Value::Integer(self.unicity_seal.network_id.into()),
@@ -447,13 +447,18 @@ impl UnicityCertificate {
447447
.map(|h| Value::Bytes(h.clone()))
448448
.unwrap_or(Value::Null),
449449
Value::Bytes(self.unicity_seal.hash.clone()),
450+
Value::Null, // signatures field set to null
450451
];
451452

453+
// Tag the array with CBOR tag 1001 (UnicitySeal)
454+
let tagged_seal = Value::Tag(1001, Box::new(Value::Array(seal_array)));
455+
456+
// Encode to CBOR
452457
let mut cbor_bytes = Vec::new();
453-
ciborium::into_writer(&Value::Array(seal_array), &mut cbor_bytes)
458+
ciborium::into_writer(&tagged_seal, &mut cbor_bytes)
454459
.map_err(|e| SdkError::Cbor(format!("Failed to encode seal for hashing: {}", e)))?;
455460

456-
// Return just the hash bytes without the algorithm prefix
461+
// Hash the CBOR bytes with SHA256
457462
Ok(sha256(&cbor_bytes).data().to_vec())
458463
}
459464

@@ -470,11 +475,11 @@ impl UnicityCertificate {
470475
let message = Message::from_digest(hash.try_into()
471476
.map_err(|_| SdkError::Crypto("Hash must be exactly 32 bytes".into()))?);
472477

473-
// Parse signature
478+
// Parse signature in compact format (r||s)
474479
let sig = Signature::from_compact(signature)
475480
.map_err(|e| SdkError::Crypto(format!("Invalid signature: {}", e)))?;
476481

477-
// Verify
482+
// Verify the signature
478483
Ok(secp.verify_ecdsa(message, &sig, &pk).is_ok())
479484
}
480485
}

0 commit comments

Comments
 (0)