11use super :: {
2+ zklogin:: { ZkLoginAuthenticator , ZkLoginPublicIdentifier } ,
23 Ed25519PublicKey , Ed25519Signature , Secp256k1PublicKey , Secp256k1Signature , Secp256r1PublicKey ,
34 Secp256r1Signature , SignatureScheme ,
4- zklogin:: { ZkLoginAuthenticator , ZkLoginPublicIdentifier } ,
55} ;
66
77pub type WeightUnit = u8 ;
@@ -122,10 +122,6 @@ pub struct MultisigAggregatedSignature {
122122 /// A bitmap that indicates the position of which public key the signature
123123 /// should be authenticated with.
124124 bitmap : BitmapUnit ,
125- /// Legacy encoding for the bitmap.
126- // TODO implement a strategy for legacy bitmap
127- #[ cfg_attr( feature = "proptest" , strategy( proptest:: strategy:: Just ( None ) ) ) ]
128- legacy_bitmap : Option < roaring:: RoaringBitmap > ,
129125 /// The public key encoded with each public key with its signature scheme
130126 /// used along with the corresponding weight.
131127 committee : MultisigCommittee ,
@@ -140,7 +136,6 @@ impl MultisigAggregatedSignature {
140136 Self {
141137 signatures,
142138 bitmap,
143- legacy_bitmap : None ,
144139 committee,
145140 }
146141 }
@@ -153,23 +148,13 @@ impl MultisigAggregatedSignature {
153148 self . bitmap
154149 }
155150
156- pub fn legacy_bitmap ( & self ) -> Option < & roaring:: RoaringBitmap > {
157- self . legacy_bitmap . as_ref ( )
158- }
159-
160- pub fn with_legacy_bitmap ( & mut self , legacy_bitmap : roaring:: RoaringBitmap ) {
161- self . legacy_bitmap = Some ( legacy_bitmap) ;
162- }
163-
164151 pub fn committee ( & self ) -> & MultisigCommittee {
165152 & self . committee
166153 }
167154}
168155
169156impl PartialEq for MultisigAggregatedSignature {
170157 fn eq ( & self , other : & Self ) -> bool {
171- // Skip comparing the legacy bitmap since we always convert to the new bitmap
172- // form
173158 self . bitmap == other. bitmap
174159 && self . signatures == other. signatures
175160 && self . committee == other. committee
@@ -178,19 +163,6 @@ impl PartialEq for MultisigAggregatedSignature {
178163
179164impl Eq for MultisigAggregatedSignature { }
180165
181- /// Convert a roaring bitmap to plain bitmap.
182- #[ cfg( feature = "serde" ) ]
183- fn roaring_bitmap_to_u16 ( roaring : & roaring:: RoaringBitmap ) -> Result < BitmapUnit , & ' static str > {
184- let mut val = 0 ;
185- for i in roaring. iter ( ) {
186- if i >= MAX_COMMITTEE_SIZE as u32 {
187- return Err ( "invalid bitmap" ) ;
188- }
189- val |= 1 << i as u8 ;
190- }
191- Ok ( val)
192- }
193-
194166#[ derive( Debug , Clone , PartialEq , Eq ) ]
195167#[ cfg_attr( feature = "proptest" , derive( test_strategy:: Arbitrary ) ) ]
196168pub enum MultisigMemberSignature {
@@ -205,135 +177,11 @@ pub enum MultisigMemberSignature {
205177mod serialization {
206178 use std:: borrow:: Cow ;
207179
208- use base64ct:: { Base64 , Encoding } ;
209180 use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
210- use serde_with:: { Bytes , DeserializeAs , SerializeAs } ;
181+ use serde_with:: { Bytes , DeserializeAs } ;
211182
212183 use super :: * ;
213- use crate :: {
214- Ed25519PublicKey , Secp256k1PublicKey , Secp256r1PublicKey , SignatureScheme ,
215- crypto:: { Base64Array33 , Base64Array34 } ,
216- } ;
217-
218- pub struct Base64MultisigMemberPublicKey ;
219-
220- impl SerializeAs < MultisigMemberPublicKey > for Base64MultisigMemberPublicKey {
221- fn serialize_as < S > (
222- source : & MultisigMemberPublicKey ,
223- serializer : S ,
224- ) -> Result < S :: Ok , S :: Error >
225- where
226- S : Serializer ,
227- {
228- match source {
229- MultisigMemberPublicKey :: Ed25519 ( public_key) => {
230- let mut buf = [ 0 ; 1 + Ed25519PublicKey :: LENGTH ] ;
231- buf[ 0 ] = SignatureScheme :: Ed25519 as u8 ;
232- buf[ 1 ..] . copy_from_slice ( public_key. as_ref ( ) ) ;
233- Base64Array33 :: serialize_as ( & buf, serializer)
234- }
235- MultisigMemberPublicKey :: Secp256k1 ( public_key) => {
236- let mut buf = [ 0 ; 1 + Secp256k1PublicKey :: LENGTH ] ;
237- buf[ 0 ] = SignatureScheme :: Secp256k1 as u8 ;
238- buf[ 1 ..] . copy_from_slice ( public_key. as_ref ( ) ) ;
239- Base64Array34 :: serialize_as ( & buf, serializer)
240- }
241- MultisigMemberPublicKey :: Secp256r1 ( public_key) => {
242- let mut buf = [ 0 ; 1 + Secp256r1PublicKey :: LENGTH ] ;
243- buf[ 0 ] = SignatureScheme :: Secp256r1 as u8 ;
244- buf[ 1 ..] . copy_from_slice ( public_key. as_ref ( ) ) ;
245- Base64Array34 :: serialize_as ( & buf, serializer)
246- }
247- MultisigMemberPublicKey :: ZkLogin ( _) => Err ( serde:: ser:: Error :: custom (
248- "zklogin not supported in legacy multisig" ,
249- ) ) ,
250- }
251- }
252- }
253-
254- impl < ' de > DeserializeAs < ' de , MultisigMemberPublicKey > for Base64MultisigMemberPublicKey {
255- fn deserialize_as < D > ( deserializer : D ) -> Result < MultisigMemberPublicKey , D :: Error >
256- where
257- D : Deserializer < ' de > ,
258- {
259- let b64: Cow < ' de , str > = Deserialize :: deserialize ( deserializer) ?;
260- let bytes = Base64 :: decode_vec ( & b64) . map_err ( serde:: de:: Error :: custom) ?;
261- let flag = SignatureScheme :: from_byte (
262- * bytes
263- . first ( )
264- . ok_or_else ( || serde:: de:: Error :: custom ( "missing signature scheme falg" ) ) ?,
265- )
266- . map_err ( serde:: de:: Error :: custom) ?;
267- let public_key_bytes = & bytes[ 1 ..] ;
268- match flag {
269- SignatureScheme :: Ed25519 => {
270- let public_key = Ed25519PublicKey :: from_bytes ( public_key_bytes)
271- . map_err ( serde:: de:: Error :: custom) ?;
272- Ok ( MultisigMemberPublicKey :: Ed25519 ( public_key) )
273- }
274- SignatureScheme :: Secp256k1 => {
275- let public_key = Secp256k1PublicKey :: from_bytes ( public_key_bytes)
276- . map_err ( serde:: de:: Error :: custom) ?;
277- Ok ( MultisigMemberPublicKey :: Secp256k1 ( public_key) )
278- }
279- SignatureScheme :: Secp256r1 => {
280- let public_key = Secp256r1PublicKey :: from_bytes ( public_key_bytes)
281- . map_err ( serde:: de:: Error :: custom) ?;
282- Ok ( MultisigMemberPublicKey :: Secp256r1 ( public_key) )
283- }
284- SignatureScheme :: Multisig
285- | SignatureScheme :: Bls12381
286- | SignatureScheme :: ZkLogin
287- | SignatureScheme :: Passkey => {
288- Err ( serde:: de:: Error :: custom ( "invalid public key type" ) )
289- }
290- }
291- }
292- }
293-
294- pub struct LegacyMultisigMember ;
295-
296- impl SerializeAs < MultisigMember > for LegacyMultisigMember {
297- fn serialize_as < S > ( source : & MultisigMember , serializer : S ) -> Result < S :: Ok , S :: Error >
298- where
299- S : Serializer ,
300- {
301- #[ derive( serde_derive:: Serialize ) ]
302- struct LegacyMember < ' a > {
303- #[ serde( with = "::serde_with::As::<Base64MultisigMemberPublicKey>" ) ]
304- public_key : & ' a MultisigMemberPublicKey ,
305- weight : WeightUnit ,
306- }
307-
308- let legacy = LegacyMember {
309- public_key : & source. public_key ,
310- weight : source. weight ,
311- } ;
312-
313- legacy. serialize ( serializer)
314- }
315- }
316-
317- impl < ' de > DeserializeAs < ' de , MultisigMember > for LegacyMultisigMember {
318- fn deserialize_as < D > ( deserializer : D ) -> Result < MultisigMember , D :: Error >
319- where
320- D : Deserializer < ' de > ,
321- {
322- #[ derive( serde_derive:: Deserialize ) ]
323- struct LegacyMember {
324- #[ serde( with = "::serde_with::As::<Base64MultisigMemberPublicKey>" ) ]
325- public_key : MultisigMemberPublicKey ,
326- weight : WeightUnit ,
327- }
328-
329- let legacy = LegacyMember :: deserialize ( deserializer) ?;
330-
331- Ok ( MultisigMember {
332- public_key : legacy. public_key ,
333- weight : legacy. weight ,
334- } )
335- }
336- }
184+ use crate :: { Ed25519PublicKey , Secp256k1PublicKey , Secp256r1PublicKey , SignatureScheme } ;
337185
338186 #[ derive( serde_derive:: Deserialize ) ]
339187 pub struct Multisig {
@@ -349,53 +197,17 @@ mod serialization {
349197 committee : & ' a MultisigCommittee ,
350198 }
351199
352- #[ derive( serde_derive:: Deserialize ) ]
353- pub struct LegacyMultisig {
354- signatures : Vec < MultisigMemberSignature > ,
355- #[ serde( with = "::serde_with::As::<crate::_serde::BinaryRoaringBitmap>" ) ]
356- bitmap : roaring:: RoaringBitmap ,
357- committee : LegacyMultisigCommittee ,
358- }
359-
360- #[ derive( serde_derive:: Serialize ) ]
361- pub struct LegacyMultisigRef < ' a > {
362- signatures : & ' a [ MultisigMemberSignature ] ,
363- #[ serde( with = "::serde_with::As::<crate::_serde::BinaryRoaringBitmap>" ) ]
364- bitmap : & ' a roaring:: RoaringBitmap ,
365- committee : LegacyMultisigCommitteeRef < ' a > ,
366- }
367-
368- #[ derive( serde_derive:: Deserialize ) ]
369- struct LegacyMultisigCommittee {
370- #[ serde( with = "::serde_with::As::<Vec<LegacyMultisigMember>>" ) ]
371- members : Vec < MultisigMember > ,
372- threshold : ThresholdUnit ,
373- }
374-
375- #[ derive( serde_derive:: Serialize ) ]
376- struct LegacyMultisigCommitteeRef < ' a > {
377- #[ serde( with = "::serde_with::As::<&[LegacyMultisigMember]>" ) ]
378- members : & ' a [ MultisigMember ] ,
379- threshold : ThresholdUnit ,
380- }
381-
382200 #[ derive( serde_derive:: Deserialize ) ]
383201 struct ReadableMultisigAggregatedSignature {
384202 signatures : Vec < MultisigMemberSignature > ,
385203 bitmap : BitmapUnit ,
386- #[ serde( default ) ]
387- #[ serde( with = "::serde_with::As::<Option<crate::_serde::Base64RoaringBitmap>>" ) ]
388- legacy_bitmap : Option < roaring:: RoaringBitmap > ,
389204 committee : MultisigCommittee ,
390205 }
391206
392207 #[ derive( serde_derive:: Serialize ) ]
393208 struct ReadableMultisigAggregatedSignatureRef < ' a > {
394209 signatures : & ' a [ MultisigMemberSignature ] ,
395210 bitmap : BitmapUnit ,
396- #[ serde( skip_serializing_if = "Option::is_none" ) ]
397- #[ serde( with = "::serde_with::As::<Option<crate::_serde::Base64RoaringBitmap>>" ) ]
398- legacy_bitmap : & ' a Option < roaring:: RoaringBitmap > ,
399211 committee : & ' a MultisigCommittee ,
400212 }
401213
@@ -408,13 +220,20 @@ mod serialization {
408220 let readable = ReadableMultisigAggregatedSignatureRef {
409221 signatures : & self . signatures ,
410222 bitmap : self . bitmap ,
411- legacy_bitmap : & self . legacy_bitmap ,
412223 committee : & self . committee ,
413224 } ;
414225 readable. serialize ( serializer)
415226 } else {
416- let bytes = self . to_bytes ( ) ;
417- serializer. serialize_bytes ( & bytes)
227+ let mut buf = Vec :: new ( ) ;
228+ buf. push ( SignatureScheme :: Multisig as u8 ) ;
229+
230+ let multisig = MultisigRef {
231+ signatures : & self . signatures ,
232+ bitmap : self . bitmap ,
233+ committee : & self . committee ,
234+ } ;
235+ bcs:: serialize_into ( & mut buf, & multisig) . expect ( "serialization cannot fail" ) ;
236+ serializer. serialize_bytes ( & buf)
418237 }
419238 }
420239 }
@@ -429,7 +248,6 @@ mod serialization {
429248 Ok ( Self {
430249 signatures : readable. signatures ,
431250 bitmap : readable. bitmap ,
432- legacy_bitmap : readable. legacy_bitmap ,
433251 committee : readable. committee ,
434252 } )
435253 } else {
@@ -444,25 +262,12 @@ mod serialization {
444262 let mut buf = Vec :: new ( ) ;
445263 buf. push ( SignatureScheme :: Multisig as u8 ) ;
446264
447- if let Some ( bitmap) = & self . legacy_bitmap {
448- let legacy = LegacyMultisigRef {
449- signatures : & self . signatures ,
450- bitmap,
451- committee : LegacyMultisigCommitteeRef {
452- members : & self . committee . members ,
453- threshold : self . committee . threshold ,
454- } ,
455- } ;
456-
457- bcs:: serialize_into ( & mut buf, & legacy) . expect ( "serialization cannot fail" ) ;
458- } else {
459- let multisig = MultisigRef {
460- signatures : & self . signatures ,
461- bitmap : self . bitmap ,
462- committee : & self . committee ,
463- } ;
464- bcs:: serialize_into ( & mut buf, & multisig) . expect ( "serialization cannot fail" ) ;
465- }
265+ let multisig = MultisigRef {
266+ signatures : & self . signatures ,
267+ bitmap : self . bitmap ,
268+ committee : & self . committee ,
269+ } ;
270+ bcs:: serialize_into ( & mut buf, & multisig) . expect ( "serialization cannot fail" ) ;
466271 buf
467272 }
468273
@@ -481,29 +286,12 @@ mod serialization {
481286 }
482287 let bcs_bytes = & bytes[ 1 ..] ;
483288
484- // Unfortunately we have no information in the serialized form of a Multisig to
485- // be able to determine if its a Legacy format or the new standard
486- // format so we just need to try each.
487- //
488- // We'll start with the newer format as that should be more prevalent.
489289 if let Ok ( multisig) = bcs:: from_bytes :: < Multisig > ( bcs_bytes) {
490290 Ok ( Self {
491291 signatures : multisig. signatures ,
492292 bitmap : multisig. bitmap ,
493- legacy_bitmap : None ,
494293 committee : multisig. committee ,
495294 } )
496- } else if let Ok ( legacy) = bcs:: from_bytes :: < LegacyMultisig > ( bcs_bytes) {
497- Ok ( Self {
498- signatures : legacy. signatures ,
499- bitmap : roaring_bitmap_to_u16 ( & legacy. bitmap )
500- . map_err ( serde:: de:: Error :: custom) ?,
501- legacy_bitmap : Some ( legacy. bitmap ) ,
502- committee : MultisigCommittee {
503- members : legacy. committee . members ,
504- threshold : legacy. committee . threshold ,
505- } ,
506- } )
507295 } else {
508296 Err ( serde:: de:: Error :: custom ( "invalid multisig" ) )
509297 }
0 commit comments