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
46
47
48
49
50
51
52
use alloy_primitives::U256;
use reth_primitives::{Signature as PrimitiveSignature, TxType};
use reth_rpc_types::{Parity, Signature};

/// Creates a new rpc signature from a legacy [primitive
/// signature](reth_primitives::Signature), using the give chain id to compute the signature's
/// recovery id.
///
/// If the chain id is `Some`, the recovery id is computed according to [EIP-155](https://eips.ethereum.org/EIPS/eip-155).
pub(crate) fn from_legacy_primitive_signature(
    signature: PrimitiveSignature,
    chain_id: Option<u64>,
) -> Signature {
    Signature {
        r: signature.r,
        s: signature.s,
        v: U256::from(signature.legacy_parity(chain_id).to_u64()),
        y_parity: None,
    }
}

/// Creates a new rpc signature from a non-legacy [primitive
/// signature](reth_primitives::Signature). This sets the `v` value to `0` or `1` depending on
/// the signature's `odd_y_parity`.
pub(crate) fn from_typed_primitive_signature(signature: PrimitiveSignature) -> Signature {
    Signature {
        r: signature.r,
        s: signature.s,
        v: U256::from(signature.odd_y_parity as u8),
        y_parity: Some(Parity(signature.odd_y_parity)),
    }
}

/// Creates a new rpc signature from a legacy [primitive
/// signature](reth_primitives::Signature).
///
/// The tx type is used to determine whether or not to use the `chain_id` to compute the
/// signature's recovery id.
///
/// If the transaction is a legacy transaction, it will use the `chain_id` to compute the
/// signature's recovery id. If the transaction is a typed transaction, it will set the `v`
/// value to `0` or `1` depending on the signature's `odd_y_parity`.
pub(crate) fn from_primitive_signature(
    signature: PrimitiveSignature,
    tx_type: TxType,
    chain_id: Option<u64>,
) -> Signature {
    match tx_type {
        TxType::Legacy => from_legacy_primitive_signature(signature, chain_id),
        _ => from_typed_primitive_signature(signature),
    }
}