Skip to main content

alloy_primitives/signature/
sig.rs

1#![allow(clippy::missing_const_for_fn)] // On purpose for forward compatibility.
2
3use crate::{B256, U256, hex, normalize_v, signature::SignatureError, uint};
4use alloc::vec::Vec;
5use core::{fmt::Display, str::FromStr};
6
7#[cfg(any(feature = "k256", feature = "secp256k1"))]
8use crate::Address;
9
10/// The order of the [Secp256k1](https://en.bitcoin.it/wiki/Secp256k1) curve.
11const SECP256K1N_ORDER: U256 =
12    uint!(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141_U256);
13
14/// An Ethereum ECDSA signature.
15#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
16#[cfg_attr(feature = "diesel", derive(diesel::AsExpression, diesel::FromSqlRow))]
17#[cfg_attr(feature = "diesel", diesel(sql_type = diesel::sql_types::Binary))]
18pub struct Signature {
19    y_parity: bool,
20    r: U256,
21    s: U256,
22}
23
24impl Display for Signature {
25    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26        write!(f, "0x{}", hex::encode(self.as_bytes()))
27    }
28}
29
30impl TryFrom<&[u8]> for Signature {
31    type Error = SignatureError;
32
33    /// Parses a 65-byte long raw signature.
34    ///
35    /// See [`from_raw`](Self::from_raw).
36    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
37        Self::from_raw(bytes)
38    }
39}
40
41impl FromStr for Signature {
42    type Err = SignatureError;
43
44    fn from_str(s: &str) -> Result<Self, Self::Err> {
45        Self::from_raw_array(&hex::decode_to_array(s)?)
46    }
47}
48
49impl From<&Signature> for [u8; 65] {
50    #[inline]
51    fn from(value: &Signature) -> [u8; 65] {
52        value.as_bytes()
53    }
54}
55
56impl From<Signature> for [u8; 65] {
57    #[inline]
58    fn from(value: Signature) -> [u8; 65] {
59        value.as_bytes()
60    }
61}
62
63impl From<&Signature> for Vec<u8> {
64    #[inline]
65    fn from(value: &Signature) -> Self {
66        value.as_bytes().to_vec()
67    }
68}
69
70impl From<Signature> for Vec<u8> {
71    #[inline]
72    fn from(value: Signature) -> Self {
73        value.as_bytes().to_vec()
74    }
75}
76
77#[cfg(feature = "k256")]
78impl From<(k256::ecdsa::Signature, k256::ecdsa::RecoveryId)> for Signature {
79    fn from(value: (k256::ecdsa::Signature, k256::ecdsa::RecoveryId)) -> Self {
80        Self::from_signature_and_parity(value.0, value.1.is_y_odd())
81    }
82}
83
84#[cfg(feature = "secp256k1")]
85impl From<secp256k1::ecdsa::RecoverableSignature> for Signature {
86    fn from(value: secp256k1::ecdsa::RecoverableSignature) -> Self {
87        let (recid, bytes) = value.serialize_compact();
88        let r = U256::from_be_slice(&bytes[..32]);
89        let s = U256::from_be_slice(&bytes[32..]);
90        let v = matches!(
91            recid,
92            secp256k1::ecdsa::RecoveryId::One | secp256k1::ecdsa::RecoveryId::Three
93        );
94        Self { y_parity: v, r, s }
95    }
96}
97
98#[cfg(feature = "k256")]
99impl TryFrom<Signature> for k256::ecdsa::Signature {
100    type Error = k256::ecdsa::Error;
101
102    fn try_from(value: Signature) -> Result<Self, Self::Error> {
103        value.to_k256()
104    }
105}
106
107#[cfg(feature = "rlp")]
108impl Signature {
109    /// Decode an RLP-encoded VRS signature. Accepts `decode_parity` closure which allows to
110    /// customize parity decoding and possibly extract additional data from it (e.g chain_id for
111    /// legacy signature).
112    pub fn decode_rlp_vrs(
113        buf: &mut &[u8],
114        decode_parity: impl FnOnce(&mut &[u8]) -> alloy_rlp::Result<bool>,
115    ) -> Result<Self, alloy_rlp::Error> {
116        use alloy_rlp::Decodable;
117
118        let parity = decode_parity(buf)?;
119        let r = Decodable::decode(buf)?;
120        let s = Decodable::decode(buf)?;
121
122        Ok(Self::new(r, s, parity))
123    }
124}
125
126impl Signature {
127    /// Instantiate a new signature from `r`, `s`, and `v` values.
128    #[inline]
129    pub const fn new(r: U256, s: U256, y_parity: bool) -> Self {
130        Self { r, s, y_parity }
131    }
132
133    /// Parses a 65-byte long raw signature.
134    ///
135    /// The first 32 bytes is the `r` value, the second 32 bytes the `s` value, and the final byte
136    /// is the `v` value in 'Electrum' notation.
137    #[inline]
138    pub fn from_raw(bytes: &[u8]) -> Result<Self, SignatureError> {
139        Self::from_raw_array(
140            bytes.try_into().map_err(|_| SignatureError::FromBytes("expected exactly 65 bytes"))?,
141        )
142    }
143
144    /// Parses a 65-byte long raw signature.
145    ///
146    /// See [`from_raw`](Self::from_raw).
147    #[inline]
148    pub fn from_raw_array(bytes: &[u8; 65]) -> Result<Self, SignatureError> {
149        let [bytes @ .., v] = bytes;
150        let v = *v as u64;
151        let Some(parity) = normalize_v(v) else { return Err(SignatureError::InvalidParity(v)) };
152        Ok(Self::from_bytes_and_parity(bytes, parity))
153    }
154
155    /// Parses a signature from a byte slice, with a v value
156    ///
157    /// # Panics
158    ///
159    /// If the slice is not at least 64 bytes long.
160    #[inline]
161    #[track_caller]
162    pub fn from_bytes_and_parity(bytes: &[u8], parity: bool) -> Self {
163        let (r_bytes, s_bytes) = bytes[..64].split_at(32);
164        let r = U256::from_be_slice(r_bytes);
165        let s = U256::from_be_slice(s_bytes);
166        Self::new(r, s, parity)
167    }
168
169    /// Returns the byte-array representation of this signature.
170    ///
171    /// The first 32 bytes are the `r` value, the second 32 bytes the `s` value
172    /// and the final byte is the `v` value in 'Electrum' notation.
173    #[inline]
174    pub fn as_bytes(&self) -> [u8; 65] {
175        let mut sig = [0u8; 65];
176        sig[..32].copy_from_slice(&self.r.to_be_bytes::<32>());
177        sig[32..64].copy_from_slice(&self.s.to_be_bytes::<32>());
178        sig[64] = 27 + self.y_parity as u8;
179        sig
180    }
181
182    /// Returns the byte-array representation of this signature as (r, s, y_parity).
183    ///
184    /// The first 32 bytes are the `r` value, the second 32 bytes the `s` value,
185    /// and the final byte is the `y_parity` value as-is (0 or 1).
186    ///
187    /// See [`as_erc2098`](Self::as_erc2098) for the compact ERC-2098 representation.
188    #[inline]
189    pub fn as_rsy(&self) -> [u8; 65] {
190        let mut sig = [0u8; 65];
191        sig[..32].copy_from_slice(&self.r.to_be_bytes::<32>());
192        sig[32..64].copy_from_slice(&self.s.to_be_bytes::<32>());
193        sig[64] = self.y_parity as u8;
194        sig
195    }
196
197    /// Decode the signature from the ERC-2098 compact representation.
198    ///
199    /// The first 32 bytes are the `r` value, and the next 32 bytes are the `s` value with `yParity`
200    /// in the top bit of the `s` value, as described in ERC-2098.
201    ///
202    /// See <https://eips.ethereum.org/EIPS/eip-2098>
203    ///
204    /// # Panics
205    ///
206    /// If the slice is not at least 64 bytes long.
207    pub fn from_erc2098(bytes: &[u8]) -> Self {
208        let (r_bytes, y_and_s_bytes) = bytes[..64].split_at(32);
209        let r = U256::from_be_slice(r_bytes);
210        let y_and_s = U256::from_be_slice(y_and_s_bytes);
211        let y_parity = y_and_s.bit(255);
212        let mut s = y_and_s;
213        s.set_bit(255, false);
214        Self { y_parity, r, s }
215    }
216
217    /// Returns the ERC-2098 compact representation of this signature.
218    ///
219    /// The first 32 bytes are the `r` value, and the next 32 bytes are the `s` value with `yParity`
220    /// in the top bit of the `s` value, as described in ERC-2098.
221    ///
222    /// See <https://eips.ethereum.org/EIPS/eip-2098>
223    pub fn as_erc2098(&self) -> [u8; 64] {
224        let normalized = self.normalized_s();
225        // The top bit of the `s` parameters is always 0, due to the use of canonical
226        // signatures which flip the solution parity to prevent negative values, which was
227        // introduced as a constraint in Homestead.
228        let mut sig = [0u8; 64];
229        sig[..32].copy_from_slice(&normalized.r().to_be_bytes::<32>());
230        sig[32..64].copy_from_slice(&normalized.s().to_be_bytes::<32>());
231        debug_assert_eq!(sig[32] >> 7, 0, "top bit of s should be 0");
232        sig[32] |= (normalized.y_parity as u8) << 7;
233        sig
234    }
235
236    /// Sets the recovery ID by normalizing a `v` value.
237    #[inline]
238    pub fn with_parity(mut self, v: bool) -> Self {
239        self.y_parity = v;
240        self
241    }
242
243    /// Returns the inner ECDSA signature.
244    #[cfg(feature = "k256")]
245    #[inline]
246    pub fn to_k256(self) -> Result<k256::ecdsa::Signature, k256::ecdsa::Error> {
247        k256::ecdsa::Signature::from_scalars(self.r.to_be_bytes(), self.s.to_be_bytes())
248    }
249
250    /// Returns the inner ECDSA recoverable signature using the `secp256k1` backend.
251    #[cfg(feature = "secp256k1")]
252    #[inline]
253    pub fn to_secp256k1(&self) -> Result<secp256k1::ecdsa::RecoverableSignature, secp256k1::Error> {
254        let mut bytes = [0u8; 64];
255        bytes[..32].copy_from_slice(&self.r.to_be_bytes::<32>());
256        bytes[32..].copy_from_slice(&self.s.to_be_bytes::<32>());
257        secp256k1::ecdsa::RecoverableSignature::from_compact(&bytes, self.secp256k1_recid())
258    }
259
260    /// Instantiate from a signature and recovery id
261    #[cfg(feature = "k256")]
262    pub fn from_signature_and_parity(sig: k256::ecdsa::Signature, v: bool) -> Self {
263        let r = U256::from_be_slice(sig.r().to_bytes().as_ref());
264        let s = U256::from_be_slice(sig.s().to_bytes().as_ref());
265        Self { y_parity: v, r, s }
266    }
267
268    /// Creates a [`Signature`] from the serialized `r` and `s` scalar values, which
269    /// comprise the ECDSA signature, alongside a `v` value, used to determine the recovery ID.
270    #[inline]
271    pub fn from_scalars_and_parity(r: B256, s: B256, parity: bool) -> Self {
272        Self::new(U256::from_be_bytes(r.0), U256::from_be_bytes(s.0), parity)
273    }
274
275    /// Normalizes the signature into "low S" form as described in
276    /// [BIP 0062: Dealing with Malleability][1].
277    ///
278    /// If `s` is already normalized, returns `None`.
279    ///
280    /// [1]: https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki
281    #[inline]
282    pub fn normalize_s(&self) -> Option<Self> {
283        let s = self.s();
284        if s > SECP256K1N_ORDER >> 1 {
285            Some(Self { y_parity: !self.y_parity, r: self.r, s: SECP256K1N_ORDER - s })
286        } else {
287            None
288        }
289    }
290
291    /// Normalizes the signature into "low S" form as described in
292    /// [BIP 0062: Dealing with Malleability][1].
293    ///
294    /// If `s` is already normalized, returns `self`.
295    ///
296    /// [1]: https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki
297    #[inline]
298    pub fn normalized_s(self) -> Self {
299        self.normalize_s().unwrap_or(self)
300    }
301
302    /// Returns the recovery ID.
303    #[cfg(feature = "k256")]
304    #[inline]
305    pub fn recid(&self) -> k256::ecdsa::RecoveryId {
306        k256::ecdsa::RecoveryId::new(self.y_parity, false)
307    }
308
309    /// Returns the recovery ID as a [`secp256k1::ecdsa::RecoveryId`].
310    #[cfg(feature = "secp256k1")]
311    #[inline]
312    pub fn secp256k1_recid(&self) -> secp256k1::ecdsa::RecoveryId {
313        if self.y_parity {
314            secp256k1::ecdsa::RecoveryId::One
315        } else {
316            secp256k1::ecdsa::RecoveryId::Zero
317        }
318    }
319
320    /// Recovers an [`Address`] from this signature and the given message by first prefixing and
321    /// hashing the message according to [EIP-191](crate::eip191_hash_message).
322    #[cfg(any(feature = "k256", feature = "secp256k1"))]
323    #[inline]
324    pub fn recover_address_from_msg<T: AsRef<[u8]>>(
325        &self,
326        msg: T,
327    ) -> Result<Address, SignatureError> {
328        self.recover_address_from_prehash(&crate::eip191_hash_message(msg))
329    }
330
331    /// Recovers an [`Address`] from this signature and the given prehashed message.
332    #[cfg(any(feature = "k256", feature = "secp256k1"))]
333    #[inline]
334    pub fn recover_address_from_prehash(&self, prehash: &B256) -> Result<Address, SignatureError> {
335        cfg_if::cfg_if! {
336            if #[cfg(feature = "secp256k1")] {
337                let pubkey = self.recover_from_prehash_secp256k1(prehash)?;
338                Ok(Address::from_raw_public_key(&pubkey.serialize_uncompressed()[1..]))
339            } else {
340                let vk = self.recover_from_prehash_k256(prehash)?;
341                Ok(Address::from_public_key(&vk))
342            }
343        }
344    }
345
346    /// Recovers a [`VerifyingKey`] from this signature and the given message by first prefixing and
347    /// hashing the message according to [EIP-191](crate::eip191_hash_message).
348    ///
349    /// [`VerifyingKey`]: k256::ecdsa::VerifyingKey
350    #[cfg(feature = "k256")]
351    #[inline]
352    pub fn recover_from_msg<T: AsRef<[u8]>>(
353        &self,
354        msg: T,
355    ) -> Result<k256::ecdsa::VerifyingKey, SignatureError> {
356        self.recover_from_prehash(&crate::eip191_hash_message(msg))
357    }
358
359    /// Recovers a [`VerifyingKey`] from this signature and the given prehashed message.
360    ///
361    /// [`VerifyingKey`]: k256::ecdsa::VerifyingKey
362    #[cfg(feature = "k256")]
363    #[inline]
364    pub fn recover_from_prehash(
365        &self,
366        prehash: &B256,
367    ) -> Result<k256::ecdsa::VerifyingKey, SignatureError> {
368        self.recover_from_prehash_k256(prehash)
369    }
370
371    /// Recovers a [`VerifyingKey`] using the `k256` backend.
372    ///
373    /// [`VerifyingKey`]: k256::ecdsa::VerifyingKey
374    #[cfg(feature = "k256")]
375    #[inline]
376    fn recover_from_prehash_k256(
377        &self,
378        prehash: &B256,
379    ) -> Result<k256::ecdsa::VerifyingKey, SignatureError> {
380        let this = self.normalized_s();
381        k256::ecdsa::VerifyingKey::recover_from_prehash(
382            prehash.as_slice(),
383            &this.to_k256()?,
384            this.recid(),
385        )
386        .map_err(Into::into)
387    }
388
389    /// Recovers a [`secp256k1::PublicKey`] from this signature and the given message by first
390    /// prefixing and hashing the message according to [EIP-191](crate::eip191_hash_message).
391    #[cfg(feature = "secp256k1")]
392    #[inline]
393    pub fn recover_from_msg_secp256k1<T: AsRef<[u8]>>(
394        &self,
395        msg: T,
396    ) -> Result<secp256k1::PublicKey, SignatureError> {
397        self.recover_from_prehash_secp256k1(&crate::eip191_hash_message(msg))
398    }
399
400    /// Recovers a [`secp256k1::PublicKey`] from this signature and the given prehashed message.
401    #[cfg(feature = "secp256k1")]
402    #[inline]
403    pub fn recover_from_prehash_secp256k1(
404        &self,
405        prehash: &B256,
406    ) -> Result<secp256k1::PublicKey, SignatureError> {
407        let this = self.normalized_s();
408        let sig = this.to_secp256k1()?;
409        let msg = secp256k1::Message::from_digest(prehash.0);
410        let secp = secp256k1::Secp256k1::verification_only();
411        secp.recover_ecdsa(msg, &sig).map_err(Into::into)
412    }
413
414    /// Returns the `r` component of this signature.
415    #[inline]
416    pub fn r(&self) -> U256 {
417        self.r
418    }
419
420    /// Returns the `s` component of this signature.
421    #[inline]
422    pub fn s(&self) -> U256 {
423        self.s
424    }
425
426    /// Returns the recovery ID as a `bool`.
427    #[inline]
428    pub fn v(&self) -> bool {
429        self.y_parity
430    }
431
432    /// Length of RLP RS field encoding
433    #[cfg(feature = "rlp")]
434    pub fn rlp_rs_len(&self) -> usize {
435        alloy_rlp::Encodable::length(&self.r) + alloy_rlp::Encodable::length(&self.s)
436    }
437
438    /// Write R and S to an RLP buffer in progress.
439    #[cfg(feature = "rlp")]
440    pub fn write_rlp_rs(&self, out: &mut dyn alloy_rlp::BufMut) {
441        alloy_rlp::Encodable::encode(&self.r, out);
442        alloy_rlp::Encodable::encode(&self.s, out);
443    }
444
445    /// Write the VRS to the output.
446    #[cfg(feature = "rlp")]
447    pub fn write_rlp_vrs(&self, out: &mut dyn alloy_rlp::BufMut, v: impl alloy_rlp::Encodable) {
448        v.encode(out);
449        self.write_rlp_rs(out);
450    }
451
452    #[doc(hidden)]
453    #[inline(always)]
454    pub fn test_signature() -> Self {
455        Self::from_scalars_and_parity(
456            b256!("0x840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"),
457            b256!("0x25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"),
458            false,
459        )
460    }
461}
462
463#[cfg(feature = "arbitrary")]
464impl<'a> arbitrary::Arbitrary<'a> for Signature {
465    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
466        Ok(Self::new(u.arbitrary()?, u.arbitrary()?, u.arbitrary()?))
467    }
468}
469
470#[cfg(feature = "arbitrary")]
471impl proptest::arbitrary::Arbitrary for Signature {
472    type Parameters = ();
473    type Strategy = proptest::strategy::Map<
474        <(U256, U256, bool) as proptest::arbitrary::Arbitrary>::Strategy,
475        fn((U256, U256, bool)) -> Self,
476    >;
477
478    fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
479        use proptest::strategy::Strategy;
480        proptest::arbitrary::any::<(U256, U256, bool)>()
481            .prop_map(|(r, s, y_parity)| Self::new(r, s, y_parity))
482    }
483}
484
485#[cfg(feature = "serde")]
486mod signature_serde {
487    use super::Signature;
488    use crate::{U64, U256, normalize_v};
489    use serde::{Deserialize, Deserializer, Serialize};
490
491    #[derive(Serialize, Deserialize)]
492    struct HumanReadableRepr {
493        r: U256,
494        s: U256,
495        #[serde(rename = "yParity")]
496        y_parity: Option<U64>,
497        #[serde(default, skip_serializing_if = "Option::is_none")]
498        v: Option<U64>,
499    }
500
501    type NonHumanReadableRepr = (U256, U256, U64);
502
503    impl Serialize for Signature {
504        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
505        where
506            S: serde::Serializer,
507        {
508            // if the serializer is human readable, serialize as a map, otherwise as a tuple
509            if serializer.is_human_readable() {
510                HumanReadableRepr {
511                    y_parity: Some(U64::from(self.y_parity as u64)),
512                    v: Some(U64::from(self.y_parity as u64)),
513                    r: self.r,
514                    s: self.s,
515                }
516                .serialize(serializer)
517            } else {
518                let repr: NonHumanReadableRepr = (self.r, self.s, U64::from(self.y_parity as u64));
519                repr.serialize(serializer)
520            }
521        }
522    }
523
524    impl<'de> Deserialize<'de> for Signature {
525        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
526        where
527            D: Deserializer<'de>,
528        {
529            let (y_parity, v, r, s) = if deserializer.is_human_readable() {
530                let HumanReadableRepr { y_parity, v, r, s } = <_>::deserialize(deserializer)?;
531                (y_parity, v, r, s)
532            } else {
533                let (r, s, y_parity) = NonHumanReadableRepr::deserialize(deserializer)?;
534                (Some(y_parity), None, r, s)
535            };
536
537            // Attempt to extract `y_parity` bit from either `yParity` key or `v` value.
538            let y_parity = if let Some(y_parity) = y_parity {
539                match y_parity.to::<u64>() {
540                    0 => false,
541                    1 => true,
542                    _ => return Err(serde::de::Error::custom("invalid yParity")),
543                }
544            } else if let Some(v) = v {
545                normalize_v(v.to()).ok_or(serde::de::Error::custom("invalid v"))?
546            } else {
547                return Err(serde::de::Error::custom("missing `yParity` or `v`"));
548            };
549
550            Ok(Self::new(r, s, y_parity))
551        }
552    }
553}
554
555#[cfg(test)]
556mod tests {
557    use super::*;
558    use core::str::FromStr;
559
560    #[cfg(feature = "rlp")]
561    use alloy_rlp::{Decodable, Encodable};
562
563    #[test]
564    #[cfg(feature = "k256")]
565    #[cfg_attr(miri, ignore = "k256 is too slow under Miri")]
566    fn can_recover_tx_sender_not_normalized() {
567        let sig = Signature::from_str("48b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c8041b").unwrap();
568        let hash = b256!("0x5eb4f5a33c621f32a8622d5f943b6b102994dfe4e5aebbefe69bb1b2aa0fc93e");
569        let expected = address!("0x0f65fe9276bc9a24ae7083ae28e2660ef72df99e");
570        assert_eq!(sig.recover_address_from_prehash(&hash).unwrap(), expected);
571    }
572
573    #[test]
574    #[cfg(feature = "k256")]
575    #[cfg_attr(miri, ignore = "k256 is too slow under Miri")]
576    fn recover_web3_signature() {
577        // test vector taken from:
578        // https://web3js.readthedocs.io/en/v1.2.2/web3-eth-accounts.html#sign
579        let sig = Signature::from_str(
580            "b91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c"
581        ).expect("could not parse signature");
582        let expected = address!("0x2c7536E3605D9C16a7a3D7b1898e529396a65c23");
583        assert_eq!(sig.recover_address_from_msg("Some data").unwrap(), expected);
584    }
585
586    #[test]
587    fn signature_from_str() {
588        let s1 = Signature::from_str(
589            "0xaa231fbe0ed2b5418e6ba7c19bee2522852955ec50996c02a2fe3e71d30ddaf1645baf4823fea7cb4fcc7150842493847cfb6a6d63ab93e8ee928ee3f61f503500"
590        ).expect("could not parse 0x-prefixed signature");
591
592        let s2 = Signature::from_str(
593            "aa231fbe0ed2b5418e6ba7c19bee2522852955ec50996c02a2fe3e71d30ddaf1645baf4823fea7cb4fcc7150842493847cfb6a6d63ab93e8ee928ee3f61f503500"
594        ).expect("could not parse non-prefixed signature");
595
596        assert_eq!(s1, s2);
597    }
598
599    #[cfg(feature = "serde")]
600    #[test]
601    fn deserialize_with_parity() {
602        let raw_signature_with_y_parity = serde_json::json!({
603            "r": "0xc569c92f176a3be1a6352dd5005bfc751dcb32f57623dd2a23693e64bf4447b0",
604            "s": "0x1a891b566d369e79b7a66eecab1e008831e22daa15f91a0a0cf4f9f28f47ee05",
605            "v": "0x1",
606            "yParity": "0x1"
607        });
608
609        let signature: Signature = serde_json::from_value(raw_signature_with_y_parity).unwrap();
610
611        let expected = Signature::new(
612            U256::from_str("0xc569c92f176a3be1a6352dd5005bfc751dcb32f57623dd2a23693e64bf4447b0")
613                .unwrap(),
614            U256::from_str("0x1a891b566d369e79b7a66eecab1e008831e22daa15f91a0a0cf4f9f28f47ee05")
615                .unwrap(),
616            true,
617        );
618
619        assert_eq!(signature, expected);
620    }
621
622    #[cfg(feature = "serde")]
623    #[test]
624    fn serialize_both_parity() {
625        // this test should be removed if the struct moves to an enum based on tx type
626        let signature = Signature::new(
627            U256::from_str("0xc569c92f176a3be1a6352dd5005bfc751dcb32f57623dd2a23693e64bf4447b0")
628                .unwrap(),
629            U256::from_str("0x1a891b566d369e79b7a66eecab1e008831e22daa15f91a0a0cf4f9f28f47ee05")
630                .unwrap(),
631            true,
632        );
633
634        let serialized = serde_json::to_string(&signature).unwrap();
635        assert_eq!(
636            serialized,
637            r#"{"r":"0xc569c92f176a3be1a6352dd5005bfc751dcb32f57623dd2a23693e64bf4447b0","s":"0x1a891b566d369e79b7a66eecab1e008831e22daa15f91a0a0cf4f9f28f47ee05","yParity":"0x1","v":"0x1"}"#
638        );
639    }
640
641    #[cfg(feature = "serde")]
642    #[test]
643    fn serialize_v_only() {
644        // this test should be removed if the struct moves to an enum based on tx type
645        let signature = Signature::new(
646            U256::from_str("0xc569c92f176a3be1a6352dd5005bfc751dcb32f57623dd2a23693e64bf4447b0")
647                .unwrap(),
648            U256::from_str("0x1a891b566d369e79b7a66eecab1e008831e22daa15f91a0a0cf4f9f28f47ee05")
649                .unwrap(),
650            true,
651        );
652
653        let expected = r#"{"r":"0xc569c92f176a3be1a6352dd5005bfc751dcb32f57623dd2a23693e64bf4447b0","s":"0x1a891b566d369e79b7a66eecab1e008831e22daa15f91a0a0cf4f9f28f47ee05","yParity":"0x1","v":"0x1"}"#;
654
655        let serialized = serde_json::to_string(&signature).unwrap();
656        assert_eq!(serialized, expected);
657    }
658
659    #[cfg(feature = "serde")]
660    #[test]
661    fn bincode_roundtrip() {
662        let signature = Signature::new(
663            U256::from_str("0xc569c92f176a3be1a6352dd5005bfc751dcb32f57623dd2a23693e64bf4447b0")
664                .unwrap(),
665            U256::from_str("0x1a891b566d369e79b7a66eecab1e008831e22daa15f91a0a0cf4f9f28f47ee05")
666                .unwrap(),
667            true,
668        );
669
670        let bin = bincode::serialize(&signature).unwrap();
671        assert_eq!(bincode::deserialize::<Signature>(&bin).unwrap(), signature);
672    }
673
674    #[cfg(feature = "rlp")]
675    #[test]
676    fn signature_rlp_encode() {
677        // Given a Signature instance
678        let sig = Signature::from_str("48b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c8041b").unwrap();
679
680        // Initialize an empty buffer
681        let mut buf = vec![];
682
683        // Encode the Signature into the buffer
684        sig.write_rlp_vrs(&mut buf, sig.v());
685
686        // Define the expected hex-encoded string
687        let expected = "80a048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804";
688
689        // Assert that the encoded buffer matches the expected hex-encoded string
690        assert_eq!(hex::encode(&buf), expected);
691    }
692
693    #[cfg(feature = "rlp")]
694    #[test]
695    fn signature_rlp_length() {
696        // Given a Signature instance
697        let sig = Signature::from_str("48b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c8041b").unwrap();
698
699        // Assert that the length of the Signature matches the expected length
700        assert_eq!(sig.rlp_rs_len() + sig.v().length(), 67);
701    }
702
703    #[cfg(feature = "rlp")]
704    #[test]
705    fn rlp_vrs_len() {
706        let signature = Signature::test_signature();
707        assert_eq!(67, signature.rlp_rs_len() + 1);
708    }
709
710    #[cfg(feature = "rlp")]
711    #[test]
712    fn encode_and_decode() {
713        let signature = Signature::test_signature();
714
715        let mut encoded = Vec::new();
716        signature.write_rlp_vrs(&mut encoded, signature.v());
717        assert_eq!(encoded.len(), signature.rlp_rs_len() + signature.v().length());
718        let decoded = Signature::decode_rlp_vrs(&mut &*encoded, bool::decode).unwrap();
719        assert_eq!(signature, decoded);
720    }
721
722    #[test]
723    fn as_bytes() {
724        let signature = Signature::new(
725            U256::from_str(
726                "18515461264373351373200002665853028612451056578545711640558177340181847433846",
727            )
728            .unwrap(),
729            U256::from_str(
730                "46948507304638947509940763649030358759909902576025900602547168820602576006531",
731            )
732            .unwrap(),
733            false,
734        );
735
736        let expected = hex!(
737            "0x28ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa63627667cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d831b"
738        );
739        assert_eq!(signature.as_bytes(), expected);
740    }
741
742    #[test]
743    fn as_rsy() {
744        let signature = Signature::new(
745            U256::from_str(
746                "18515461264373351373200002665853028612451056578545711640558177340181847433846",
747            )
748            .unwrap(),
749            U256::from_str(
750                "46948507304638947509940763649030358759909902576025900602547168820602576006531",
751            )
752            .unwrap(),
753            false,
754        );
755
756        let expected = hex!(
757            "0x28ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa63627667cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d8300"
758        );
759        assert_eq!(signature.as_rsy(), expected);
760    }
761
762    #[test]
763    fn as_rsy_y_parity_true() {
764        let signature = Signature::new(
765            U256::from_str(
766                "18515461264373351373200002665853028612451056578545711640558177340181847433846",
767            )
768            .unwrap(),
769            U256::from_str(
770                "46948507304638947509940763649030358759909902576025900602547168820602576006531",
771            )
772            .unwrap(),
773            true,
774        );
775
776        let expected = hex!(
777            "0x28ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa63627667cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d8301"
778        );
779        assert_eq!(signature.as_rsy(), expected);
780    }
781
782    #[test]
783    fn as_erc2098_y_false() {
784        let signature = Signature::new(
785            U256::from_str(
786                "47323457007453657207889730243826965761922296599680473886588287015755652701072",
787            )
788            .unwrap(),
789            U256::from_str(
790                "57228803202727131502949358313456071280488184270258293674242124340113824882788",
791            )
792            .unwrap(),
793            false,
794        );
795
796        let expected = hex!(
797            "0x68a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b907e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064"
798        );
799        assert_eq!(signature.as_erc2098(), expected);
800    }
801
802    #[test]
803    fn as_erc2098_y_true() {
804        let signature = Signature::new(
805            U256::from_str("0x9328da16089fcba9bececa81663203989f2df5fe1faa6291a45381c81bd17f76")
806                .unwrap(),
807            U256::from_str("0x139c6d6b623b42da56557e5e734a43dc83345ddfadec52cbe24d0cc64f550793")
808                .unwrap(),
809            true,
810        );
811
812        let expected = hex!(
813            "0x9328da16089fcba9bececa81663203989f2df5fe1faa6291a45381c81bd17f76939c6d6b623b42da56557e5e734a43dc83345ddfadec52cbe24d0cc64f550793"
814        );
815        assert_eq!(signature.as_erc2098(), expected);
816    }
817
818    #[test]
819    fn from_erc2098_y_false() {
820        let expected = Signature::new(
821            U256::from_str(
822                "47323457007453657207889730243826965761922296599680473886588287015755652701072",
823            )
824            .unwrap(),
825            U256::from_str(
826                "57228803202727131502949358313456071280488184270258293674242124340113824882788",
827            )
828            .unwrap(),
829            false,
830        );
831
832        assert_eq!(
833            Signature::from_erc2098(&hex!(
834                "0x68a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b907e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064"
835            )),
836            expected
837        );
838    }
839
840    #[test]
841    fn from_erc2098_y_true() {
842        let expected = Signature::new(
843            U256::from_str("0x9328da16089fcba9bececa81663203989f2df5fe1faa6291a45381c81bd17f76")
844                .unwrap(),
845            U256::from_str("0x139c6d6b623b42da56557e5e734a43dc83345ddfadec52cbe24d0cc64f550793")
846                .unwrap(),
847            true,
848        );
849
850        assert_eq!(
851            Signature::from_erc2098(&hex!(
852                "0x9328da16089fcba9bececa81663203989f2df5fe1faa6291a45381c81bd17f76939c6d6b623b42da56557e5e734a43dc83345ddfadec52cbe24d0cc64f550793"
853            )),
854            expected
855        );
856    }
857
858    #[test]
859    fn display_impl() {
860        let sig = Signature::new(
861            U256::from_str("0x9328da16089fcba9bececa81663203989f2df5fe1faa6291a45381c81bd17f76")
862                .unwrap(),
863            U256::from_str("0x139c6d6b623b42da56557e5e734a43dc83345ddfadec52cbe24d0cc64f550793")
864                .unwrap(),
865            true,
866        );
867
868        assert_eq!(
869            format!("{sig}"),
870            "0x9328da16089fcba9bececa81663203989f2df5fe1faa6291a45381c81bd17f76139c6d6b623b42da56557e5e734a43dc83345ddfadec52cbe24d0cc64f5507931c"
871        );
872    }
873}