stratus/eth/primitives/
signature_component.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use alloy_primitives::Uint;
use display_json::DebugAsJson;
use ethereum_types::U256;
use fake::Dummy;
use fake::Faker;
use rand::Rng;

use crate::alias::AlloyUint256;
use crate::gen_newtype_from;

/// A signature component (r or s value)
#[derive(DebugAsJson, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SignatureComponent(pub U256);

impl Dummy<Faker> for SignatureComponent {
    fn dummy_with_rng<R: Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
        Self(U256::from(rng.gen::<u64>()))
    }
}

// -----------------------------------------------------------------------------
// Conversions: Other -> Self
// -----------------------------------------------------------------------------
gen_newtype_from!(self = SignatureComponent, other = U256);

impl From<Uint<256, 4>> for SignatureComponent {
    fn from(value: Uint<256, 4>) -> Self {
        Self(U256::from(value.to_be_bytes::<32>()))
    }
}

// -----------------------------------------------------------------------------
// Conversions: Self -> Other
// -----------------------------------------------------------------------------
impl From<SignatureComponent> for AlloyUint256 {
    fn from(value: SignatureComponent) -> Self {
        Self::from_limbs(value.0 .0)
    }
}

impl From<SignatureComponent> for U256 {
    fn from(value: SignatureComponent) -> Self {
        value.0
    }
}