CLI app for developers prototyping atproto functionality
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(identity): move to_jws_bytes onto AnySignature

Move signature_to_jws_bytes from AnySigningKey to AnySignature::to_jws_bytes
as an instance method. This places the method on the type it actually operates
on, improving semantic clarity. Update all call sites in jwt.rs and tests in
identity.rs. Rename the test from any_signing_key_signature_to_jws_bytes to
any_signature_to_jws_bytes to match the moved method.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>

authored by

Jack Grigg
Claude Haiku 4.5
and committed by
Tangled
289038f2 9cfd4eda

+18 -16
+17 -15
src/common/identity.rs
··· 211 211 } 212 212 } 213 213 } 214 - 215 - /// Serializes the signature bytes for JWS compact form: raw `r || s` 216 - /// big-endian concatenation (NOT DER). 217 - /// 218 - /// For both ES256 and ES256K this is a 64-byte fixed-length array. 219 - pub fn signature_to_jws_bytes(sig: &AnySignature) -> [u8; 64] { 220 - match sig { 221 - AnySignature::K256(s) => s.to_bytes().into(), 222 - AnySignature::P256(s) => s.to_bytes().into(), 223 - } 224 - } 225 214 } 226 215 227 216 /// A signature that may be one of several supported curves. ··· 231 220 K256(k256::ecdsa::Signature), 232 221 /// P-256 signature. 233 222 P256(p256::ecdsa::Signature), 223 + } 224 + 225 + impl AnySignature { 226 + /// Serializes the signature bytes for JWS compact form: raw `r || s` 227 + /// big-endian concatenation (NOT DER). 228 + /// 229 + /// For both ES256 and ES256K this is a 64-byte fixed-length array. 230 + pub fn to_jws_bytes(&self) -> [u8; 64] { 231 + match self { 232 + AnySignature::K256(s) => s.to_bytes().into(), 233 + AnySignature::P256(s) => s.to_bytes().into(), 234 + } 235 + } 234 236 } 235 237 236 238 /// Error from signature verification across multiple curves. ··· 1540 1542 } 1541 1543 1542 1544 #[test] 1543 - fn any_signing_key_signature_to_jws_bytes() { 1545 + fn any_signature_to_jws_bytes() { 1544 1546 let key = AnySigningKey::K256(K256SigningKey::from_slice(&[1u8; 32]).expect("valid seed")); 1545 1547 let msg = b"test"; 1546 1548 let sig = key.sign(msg); 1547 - let jws_bytes = AnySigningKey::signature_to_jws_bytes(&sig); 1549 + let jws_bytes = sig.to_jws_bytes(); 1548 1550 assert_eq!(jws_bytes.len(), 64); 1549 1551 } 1550 1552 ··· 1576 1578 "P256 signature should verify after normalization" 1577 1579 ); 1578 1580 1579 - // Also verify that signature_to_jws_bytes produces a 64-byte result. 1580 - let sig_bytes = AnySigningKey::signature_to_jws_bytes(&sig); 1581 + // Also verify that to_jws_bytes produces a 64-byte result. 1582 + let sig_bytes = sig.to_jws_bytes(); 1581 1583 assert_eq!( 1582 1584 sig_bytes.len(), 1583 1585 64,
+1 -1
src/common/jwt.rs
··· 137 137 let claims_b64 = URL_SAFE_NO_PAD.encode(&claims_json); 138 138 let signing_input = format!("{header_b64}.{claims_b64}"); 139 139 let sig = signer.sign(signing_input.as_bytes()); 140 - let sig_bytes = AnySigningKey::signature_to_jws_bytes(&sig); 140 + let sig_bytes = sig.to_jws_bytes(); 141 141 let sig_b64 = URL_SAFE_NO_PAD.encode(sig_bytes); 142 142 Ok(format!("{header_b64}.{claims_b64}.{sig_b64}")) 143 143 }