sui_sdk_types/crypto/
mod.rs1mod bls12381;
2mod ed25519;
3mod intent;
4mod multisig;
5mod passkey;
6mod secp256k1;
7mod secp256r1;
8mod signature;
9mod validator;
10mod zklogin;
11
12pub use bls12381::Bls12381PublicKey;
13pub use bls12381::Bls12381Signature;
14pub use ed25519::Ed25519PublicKey;
15pub use ed25519::Ed25519Signature;
16pub use intent::Intent;
17pub use intent::IntentAppId;
18pub use intent::IntentScope;
19pub use intent::IntentVersion;
20pub use multisig::MultisigAggregatedSignature;
21pub use multisig::MultisigCommittee;
22pub use multisig::MultisigMember;
23pub use multisig::MultisigMemberPublicKey;
24pub use multisig::MultisigMemberSignature;
25pub use passkey::PasskeyAuthenticator;
26pub use passkey::PasskeyPublicKey;
27pub use secp256k1::Secp256k1PublicKey;
28pub use secp256k1::Secp256k1Signature;
29pub use secp256r1::Secp256r1PublicKey;
30pub use secp256r1::Secp256r1Signature;
31pub use signature::SignatureScheme;
32pub use signature::SimpleSignature;
33pub use signature::UserSignature;
34pub use validator::ValidatorAggregatedSignature;
35pub use validator::ValidatorCommittee;
36pub use validator::ValidatorCommitteeMember;
37pub use validator::ValidatorSignature;
38pub use zklogin::Bn254FieldElement;
39pub use zklogin::CircomG1;
40pub use zklogin::CircomG2;
41pub use zklogin::InvalidZkLoginAuthenticatorError;
42pub use zklogin::Jwk;
43pub use zklogin::JwkId;
44pub use zklogin::ZkLoginAuthenticator;
45pub use zklogin::ZkLoginClaim;
46pub use zklogin::ZkLoginInputs;
47pub use zklogin::ZkLoginProof;
48pub use zklogin::ZkLoginPublicIdentifier;
49
50const fn base64_encoded_length(len: usize) -> usize {
61 ((4 * len / 3) + 3) & !3
62}
63
64macro_rules! impl_base64_helper {
65 ($base:ident, $display:ident, $fromstr:ident, $test_module:ident, $array_length:literal) => {
66 #[allow(unused)]
67 struct $base;
68
69 impl $base {
70 const LENGTH: usize = $array_length;
71 #[allow(unused)]
72 const ENCODED_LENGTH: usize = base64_encoded_length(Self::LENGTH);
73 }
74
75 #[allow(unused)]
76 struct $display<'a>(&'a [u8; $base::LENGTH]);
77
78 impl<'a> std::fmt::Display for $display<'a> {
79 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80 let mut buf = [0; $base::ENCODED_LENGTH];
81 let encoded =
82 <base64ct::Base64 as base64ct::Encoding>::encode(self.0, &mut buf).unwrap();
83 f.write_str(encoded)
84 }
85 }
86
87 #[allow(unused)]
88 #[derive(Debug, PartialEq)]
89 #[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
90 struct $fromstr([u8; $base::LENGTH]);
91
92 impl std::str::FromStr for $fromstr {
93 type Err = base64ct::Error;
94
95 fn from_str(s: &str) -> Result<Self, Self::Err> {
96 let mut buf = [0; $base::LENGTH];
97 let decoded = <base64ct::Base64 as base64ct::Encoding>::decode(s, &mut buf)?;
98 if decoded.len() != $base::LENGTH {
99 return Err(base64ct::Error::InvalidLength);
100 }
101 Ok(Self(buf))
102 }
103 }
104
105 #[cfg(feature = "serde")]
106 #[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
107 impl serde_with::SerializeAs<[u8; Self::LENGTH]> for $base {
108 fn serialize_as<S>(
109 source: &[u8; Self::LENGTH],
110 serializer: S,
111 ) -> Result<S::Ok, S::Error>
112 where
113 S: serde::Serializer,
114 {
115 let display = $display(source);
116 serde_with::DisplayFromStr::serialize_as(&display, serializer)
117 }
118 }
119
120 #[cfg(feature = "serde")]
121 #[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
122 impl<'de> serde_with::DeserializeAs<'de, [u8; Self::LENGTH]> for $base {
123 fn deserialize_as<D>(deserializer: D) -> Result<[u8; Self::LENGTH], D::Error>
124 where
125 D: serde::Deserializer<'de>,
126 {
127 let array: $fromstr = serde_with::DisplayFromStr::deserialize_as(deserializer)?;
128 Ok(array.0)
129 }
130 }
131
132 #[cfg(test)]
133 mod $test_module {
134 use super::$display;
135 use super::$fromstr;
136 use test_strategy::proptest;
137
138 #[cfg(target_arch = "wasm32")]
139 use wasm_bindgen_test::wasm_bindgen_test as test;
140
141 #[proptest]
142 fn roundtrip_display_fromstr(array: $fromstr) {
143 let s = $display(&array.0).to_string();
144 let a = s.parse::<$fromstr>().unwrap();
145 assert_eq!(array, a);
146 }
147
148 #[proptest]
154 fn fromstr_does_not_panic(#[strategy("[A-Za-z0-9+/=]{0,128}")] s: String) {
155 let _ = s.parse::<$fromstr>();
156 }
157
158 #[test]
159 fn short_decode_errors() {
160 "AAAA".parse::<$fromstr>().unwrap_err();
161 }
162 }
163 };
164}
165
166impl_base64_helper!(Base64Array32, Base64Display32, Base64FromStr32, test32, 32);
167impl_base64_helper!(Base64Array33, Base64Display33, Base64FromStr33, test33, 33);
168impl_base64_helper!(Base64Array34, Base64Display34, Base64FromStr34, test34, 34);
169impl_base64_helper!(Base64Array48, Base64Display48, Base64FromStr48, test48, 48);
170impl_base64_helper!(Base64Array64, Base64Display64, Base64FromStr64, test64, 64);
171impl_base64_helper!(Base64Array96, Base64Display96, Base64FromStr96, test96, 96);