Skip to content

Commit

Permalink
signature: add zklogin to UserSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Feb 25, 2024
1 parent b205a54 commit f23e1a2
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/types/crypto/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::Secp256k1PublicKey;
use super::Secp256k1Signature;
use super::Secp256r1PublicKey;
use super::Secp256r1Signature;
use super::ZkLoginAuthenticator;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
Expand Down Expand Up @@ -298,14 +299,16 @@ impl std::fmt::Display for InvalidSignatureScheme {
pub enum UserSignature {
Simple(SimpleSignature),
Multisig(MultisigAggregatedSignature),
// ZkLoginAuthenticator,
#[cfg_attr(test, proptest(skip))]
ZkLogin(ZkLoginAuthenticator),
}

impl UserSignature {
pub fn scheme(&self) -> SignatureScheme {
match self {
UserSignature::Simple(simple) => simple.scheme(),
UserSignature::Multisig(_) => SignatureScheme::Multisig,
UserSignature::ZkLogin(_) => SignatureScheme::ZkLogin,
}
}
}
Expand All @@ -331,6 +334,7 @@ mod serialization {
public_key: &'a Secp256r1PublicKey,
},
Multisig(&'a MultisigAggregatedSignature),
ZkLogin(&'a ZkLoginAuthenticator),
}

#[derive(serde_derive::Deserialize)]
Expand All @@ -349,6 +353,7 @@ mod serialization {
public_key: Secp256r1PublicKey,
},
Multisig(MultisigAggregatedSignature),
ZkLogin(ZkLoginAuthenticator),
}

impl serde::Serialize for UserSignature {
Expand Down Expand Up @@ -382,12 +387,14 @@ mod serialization {
UserSignature::Multisig(multisig) => {
ReadableUserSignatureRef::Multisig(multisig)
}
UserSignature::ZkLogin(zklogin) => ReadableUserSignatureRef::ZkLogin(zklogin),
};
readable.serialize(serializer)
} else {
match self {
UserSignature::Simple(simple) => simple.serialize(serializer),
UserSignature::Multisig(multisig) => multisig.serialize(serializer),
UserSignature::ZkLogin(zklogin) => zklogin.serialize(serializer),
}
}
}
Expand Down Expand Up @@ -423,6 +430,7 @@ mod serialization {
public_key,
}),
ReadableUserSignature::Multisig(multisig) => Self::Multisig(multisig),
ReadableUserSignature::ZkLogin(zklogin) => Self::ZkLogin(zklogin),
})
} else {
use serde_with::DeserializeAs;
Expand All @@ -442,8 +450,12 @@ mod serialization {
let multisig = MultisigAggregatedSignature::from_serialized_bytes(bytes)?;
Ok(Self::Multisig(multisig))
}
SignatureScheme::Bls12381 | SignatureScheme::ZkLogin => {
Err(serde::de::Error::custom("invalid signature scheme"))
SignatureScheme::Bls12381 => Err(serde::de::Error::custom(
"bls not supported for user signatures",
)),
SignatureScheme::ZkLogin => {
let multisig = ZkLoginAuthenticator::from_serialized_bytes(bytes)?;
Ok(Self::ZkLogin(multisig))
}
}
}
Expand Down Expand Up @@ -554,5 +566,29 @@ mod serialization {
println!("{json}");
assert_eq!(sig, serde_json::from_str(&json).unwrap());
}

#[test]
fn zklogin_fixtures() {
const FIXTURES: &[&str] = &[
"mAcFA00yMTM0MzA3MTg2NDQ3ODc4NTU1OTU1OTY2Njg3NDQ3Njg0MzYyODQxNjA4OTQ4ODEyMTk4MjQ0OTY0ODk4OTg3OTkxMTI1MTY2OTA2N0w1MzYyMzAzOTQxMzk3NzQ1MTk2MTQxNjgxNjA5MDk0MDI4MTg3NzgxMzY2ODc3ODA5NTA2NTU0NzA3MjQ4MzcwNzM4OTcwOTI5MzYwATEDAk0xOTAzMjkyNDMyMDAxODEyNjcyNzEyMDYzMjYzMzM2OTE1NTg2MDc4NDA0NjY2MDcyMzIzMjU0MTAwMjQyODAxODA4ODQ4MTI3MzA5N0sxOTM0MDEzODQwOTcyNjc5OTM0MzgxMTI2ODg3OTQ2MTk1NDk5NTczMjY3NTE5ODAxNDA4MzQ2MzA3NDA2NzI3NjIxNzI0MTA4ODUCTDQxMTc0OTU3NjIwNzc2NjE4OTk2Njk5ODU1MTUzMzc2MDcwMTkzNTgwMjc2MjUxNTc4M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c1MTI2NTI0TDUxNTE0MzIwNjYxMzg3NDcyMDkxMjA2ODc1MjIyMDg1NTQwNDE1NjQ4MjcwODA3OTcwNTA3MTY5Mjc3Njk2NDM0NDc0MjIxMjMyNzABMTF3aWFYTnpJam9pYUhSMGNITTZMeTlwWkM1MGQybDBZMmd1ZEhZdmIyRjFkR2d5SWl3AjJleUpoYkdjaU9pSlNVekkxTmlJc0luUjVjQ0k2SWtwWFZDSXNJbXRwWkNJNklqRWlmUU0yMDY4NzY0MjUxNzYzMDczMzI3MzY4Nzc5NTQ3NjI0NzQzNzc0MzgzNDYwMTEwMTU2NjY4NDk2Nzk2NzcwNTgwOTk4NTI0MTA2NTUzOQoAAAAAAAAAYQBn1v6x7RD9EyaiubLQ8qQkJSNI2Mr1GFHXZyOUJ+eCphFkwjYKBo44TMAbryd405BY+MHYTFLZOD06UTycKHgKucbuFjDvPnERRKZI2wa7sihPcnTPvuU//O5QPMGkkgA=",
];

for fixture in FIXTURES {
let bcs = Base64::decode_vec(fixture).unwrap();

let sig: UserSignature = bcs::from_bytes(&bcs).unwrap();
assert_eq!(SignatureScheme::ZkLogin, sig.scheme());
let bytes = bcs::to_bytes(&sig).unwrap();
assert_eq!(bcs, bytes);

let json = serde_json::to_string_pretty(&sig).unwrap();
println!("{json}");
assert_eq!(sig, serde_json::from_str(&json).unwrap());
}
}
}
}

0 comments on commit f23e1a2

Please sign in to comment.