Skip to content

Commit

Permalink
use bridge type for signature
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo committed Jul 9, 2024
1 parent d3f06f8 commit bf49020
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
7 changes: 1 addition & 6 deletions crates/storage/codecs/derive/src/compact/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ fn generate_from_compact(fields: &FieldList, ident: &Ident, is_zstd: bool) -> To
// it's hard to figure out with derive_macro which types have Bytes fields.
//
// This removes the requirement of the field to be placed last in the struct.
known_types.extend_from_slice(&[
"TxKind",
"AccessList",
"Signature",
"CheckpointBlockRange",
]);
known_types.extend_from_slice(&["TxKind", "AccessList", "Signature", "CheckpointBlockRange"]);

// let mut handle = FieldListHandler::new(fields);
let is_enum = fields.iter().any(|field| matches!(field, FieldTypes::EnumVariant(_)));
Expand Down
37 changes: 24 additions & 13 deletions crates/storage/codecs/src/alloy/authorization_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use core::ops::Deref;
use crate::Compact;
use alloy_eips::eip7702::{Authorization as AlloyAuthorization, SignedAuthorization};
use alloy_primitives::{Address, ChainId, U256};
use bytes::Buf;
use reth_codecs_derive::main_codec;

/// Authorization acts as bridge which simplifies Compact implementation for AlloyAuthorization.
Expand All @@ -17,6 +16,20 @@ struct Authorization {
nonce: Option<u64>,
}

/// Signature acts as bridge which simplifies Compact implementation for AlloySignature.
///
/// Notice: Make sure this struct is 1:1 with `alloy_primitives::Signature`
#[main_codec]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
struct Signature {
/// The R field of the signature; the point on the curve.
pub r: U256,
/// The S field of the signature; the point on the curve.
pub s: U256,
/// yParity: Signature Y parity; formally Ty
pub odd_y_parity: bool,
}

impl Compact for AlloyAuthorization {
fn to_compact<B>(self, buf: &mut B) -> usize
where
Expand Down Expand Up @@ -47,9 +60,8 @@ impl Compact for SignedAuthorization<alloy_primitives::Signature> {
// todo(onbjerg): add `SignedAuthorization::into_parts(self) -> (Auth, Signature)`
let (auth, signature) = (self.deref().clone(), self.signature());
let (v, r, s) = (signature.v(), signature.r(), signature.s());
buf.put_u8(v.y_parity_byte());
buf.put_slice(r.as_le_slice());
buf.put_slice(s.as_le_slice());

Signature { r, s, odd_y_parity: v.y_parity() }.to_compact(&mut buffer);

// to_compact doesn't write the len to buffer.
// By placing it as last, we don't need to store it either.
Expand All @@ -60,15 +72,14 @@ impl Compact for SignedAuthorization<alloy_primitives::Signature> {
total_len
}

fn from_compact(mut buf: &[u8], len: usize) -> (Self, &[u8]) {
let y = alloy_primitives::Parity::Parity(buf.get_u8() == 1);
let r = U256::from_le_slice(&buf[0..32]);
buf.advance(32);
let s = U256::from_le_slice(&buf[0..32]);
buf.advance(32);

let signature = alloy_primitives::Signature::from_rs_and_parity(r, s, y)
.expect("invalid authorization signature");
fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
let (signature, buf) = Signature::from_compact(buf, len);
let signature = alloy_primitives::Signature::from_rs_and_parity(
signature.r,
signature.s,
alloy_primitives::Parity::Parity(signature.odd_y_parity),
)
.expect("invalid authorization signature");
let (auth, buf) = AlloyAuthorization::from_compact(buf, len);

(auth.into_signed(signature), buf)
Expand Down

0 comments on commit bf49020

Please sign in to comment.