@@ -3,80 +3,77 @@ use k256::{
33 elliptic_curve:: PublicKey , elliptic_curve:: ScalarPrimitive , ProjectivePoint , Scalar , SecretKey ,
44} ;
55use serde:: { de, Deserialize , Deserializer , Serialize , Serializer } ;
6- use serde:: de:: { SeqAccess } ;
76
7+ use serde_bytes:: ByteArray ;
88use sha3:: { Digest , Keccak256 } ;
9- use std:: marker:: PhantomData ;
10- use std:: fmt;
11- use serde:: de:: Visitor ;
129
1310#[ derive( Clone , Debug , Deserialize , Serialize ) ]
1411#[ cfg_attr( feature = "nif" , serde( rename = "Elixir.Anoma.Arm.DeltaProof" ) ) ]
1512pub struct DeltaProof {
16- #[ serde( deserialize_with = "deserialize_signature" , serialize_with = "serialize_signature" ) ]
13+ #[ serde(
14+ deserialize_with = "deserialize_signature" ,
15+ serialize_with = "serialize_signature"
16+ ) ]
1717 pub signature : Signature ,
18- #[ serde( deserialize_with = "deserialize_recovery_id" , serialize_with = "serialize_recovery_id" ) ]
18+ #[ serde(
19+ deserialize_with = "deserialize_recovery_id" ,
20+ serialize_with = "serialize_recovery_id"
21+ ) ]
1922 pub recid : RecoveryId ,
2023}
2124
22- fn serialize_signature < S > ( t : & Signature , s : S ) -> Result < S :: Ok , S :: Error > where S : Serializer {
23- s. serialize_bytes ( & t. to_bytes ( ) )
25+ fn serialize_signature < S > ( t : & Signature , s : S ) -> Result < S :: Ok , S :: Error >
26+ where
27+ S : Serializer ,
28+ {
29+ ByteArray :: < 64 > :: new ( t. to_bytes ( ) . into ( ) ) . serialize ( s)
2430}
2531
26- fn deserialize_signature < ' de , D > ( deserializer : D ) -> Result < Signature , D :: Error > where D : Deserializer < ' de > {
27- const LEN : usize = 64 ;
28- struct ArrayVisitor < T > {
29- element : PhantomData < T > ,
30- }
31-
32- impl < ' de , T > Visitor < ' de > for ArrayVisitor < T >
33- where T : Default + Copy + Deserialize < ' de >
34- {
35- type Value = [ T ; LEN ] ;
36-
37- fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
38- write ! ( formatter, "an array of length {}" , LEN )
39- }
40-
41- fn visit_seq < A > ( self , mut seq : A ) -> Result < [ T ; LEN ] , A :: Error >
42- where A : SeqAccess < ' de >
43- {
44- let mut arr = [ T :: default ( ) ; LEN ] ;
45- for i in 0 ..LEN {
46- arr[ i] = seq. next_element ( ) ?
47- . ok_or_else ( || de:: Error :: invalid_length ( i, & self ) ) ?;
48- }
49- Ok ( arr)
50- }
51- }
52-
53- let visitor = ArrayVisitor { element : PhantomData } ;
54- let b: [ u8 ; LEN ] = deserializer. deserialize_tuple ( LEN , visitor) ?;
32+ fn deserialize_signature < ' de , D > ( deserializer : D ) -> Result < Signature , D :: Error >
33+ where
34+ D : Deserializer < ' de > ,
35+ {
36+ let b: [ u8 ; 64 ] = ByteArray :: deserialize ( deserializer) ?. into_array ( ) ;
5537 Signature :: from_bytes ( & b. into ( ) ) . map_err ( de:: Error :: custom)
5638}
5739
58- fn serialize_recovery_id < S > ( t : & RecoveryId , s : S ) -> Result < S :: Ok , S :: Error > where S : Serializer {
40+ fn serialize_recovery_id < S > ( t : & RecoveryId , s : S ) -> Result < S :: Ok , S :: Error >
41+ where
42+ S : Serializer ,
43+ {
5944 s. serialize_u8 ( t. to_byte ( ) )
6045}
6146
62- fn deserialize_recovery_id < ' de , D > ( d : D ) -> Result < RecoveryId , D :: Error > where D : Deserializer < ' de > {
47+ fn deserialize_recovery_id < ' de , D > ( d : D ) -> Result < RecoveryId , D :: Error >
48+ where
49+ D : Deserializer < ' de > ,
50+ {
6351 let s: u8 = de:: Deserialize :: deserialize ( d) ?;
6452 RecoveryId :: try_from ( s) . map_err ( de:: Error :: custom)
6553}
6654
6755#[ derive( Clone , Debug , Deserialize , Serialize ) ]
6856#[ cfg_attr( feature = "nif" , serde( rename = "Elixir.Anoma.Arm.DeltaWitness" ) ) ]
6957pub struct DeltaWitness {
70- #[ serde( deserialize_with = "deserialize_signing_key" , serialize_with = "serialize_signing_key" ) ]
58+ #[ serde(
59+ deserialize_with = "deserialize_signing_key" ,
60+ serialize_with = "serialize_signing_key"
61+ ) ]
7162 pub signing_key : SigningKey ,
7263}
7364
74- fn serialize_signing_key < S > ( t : & SigningKey , s : S ) -> Result < S :: Ok , S :: Error > where S : Serializer {
75- s. serialize_bytes ( & t. to_bytes ( ) )
65+ fn serialize_signing_key < S > ( t : & SigningKey , s : S ) -> Result < S :: Ok , S :: Error >
66+ where
67+ S : Serializer ,
68+ {
69+ ByteArray :: < 32 > :: new ( t. to_bytes ( ) . into ( ) ) . serialize ( s)
7670}
7771
78- fn deserialize_signing_key < ' de , D > ( deserializer : D ) -> Result < SigningKey , D :: Error > where D : Deserializer < ' de > {
79- let b: [ u8 ; 32 ] = Deserialize :: deserialize ( deserializer) ?;
72+ fn deserialize_signing_key < ' de , D > ( deserializer : D ) -> Result < SigningKey , D :: Error >
73+ where
74+ D : Deserializer < ' de > ,
75+ {
76+ let b: [ u8 ; 32 ] = ByteArray :: deserialize ( deserializer) ?. into_array ( ) ;
8077 SigningKey :: from_bytes ( & b. into ( ) ) . map_err ( de:: Error :: custom)
8178}
8279
0 commit comments