Skip to content

Commit 56371c8

Browse files
authored
fix: construct v properly relative to upstream api changes (#47)
* fix: construct v properly relative to upstream api changes * fix: helper function, plus a test * fix: doc comments * chore: fmt
1 parent 3e95899 commit 56371c8

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pub mod config;
22
pub mod service;
33
pub mod signer;
44
pub mod tasks;
5+
pub mod utils;

src/tasks/submit.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use crate::{
22
config::{Provider, ZenithInstance},
33
signer::LocalOrAws,
44
tasks::block::InProgressBlock,
5+
utils::extract_signature_components,
56
};
67
use alloy::{
78
consensus::{constants::GWEI_TO_WEI, SimpleCoder},
89
eips::BlockNumberOrTag,
9-
network::TransactionBuilder,
10-
network::TransactionBuilder4844,
10+
network::{TransactionBuilder, TransactionBuilder4844},
1111
primitives::{FixedBytes, U256},
1212
providers::{Provider as _, SendableTx, WalletProvider},
1313
rpc::types::eth::TransactionRequest,
@@ -134,9 +134,7 @@ impl SubmitTask {
134134
resp: &SignResponse,
135135
in_progress: &InProgressBlock,
136136
) -> eyre::Result<ControlFlow> {
137-
let v = resp.sig.v().into();
138-
let r: FixedBytes<32> = resp.sig.r().into();
139-
let s: FixedBytes<32> = resp.sig.s().into();
137+
let (v, r, s) = extract_signature_components(&resp.sig);
140138

141139
let header = Zenith::BlockHeader {
142140
hostBlockNumber: resp.req.host_block_number,

src/utils.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use alloy::primitives::{PrimitiveSignature, B256};
2+
3+
/// Extracts the components of a signature.
4+
/// Currently alloy has no function for extracting the components of a signature.
5+
/// Returns a tuple of (v, r, s) where:
6+
/// - `v` is the recovery id
7+
/// - `r` is the r component of the signature
8+
/// - `s` is the s component of the signature
9+
pub fn extract_signature_components(sig: &PrimitiveSignature) -> (u8, B256, B256) {
10+
let v = sig.as_bytes()[64];
11+
let r = sig.r().into();
12+
let s = sig.s().into();
13+
(v, r, s)
14+
}
15+
16+
#[cfg(test)]
17+
mod tests {
18+
use super::*;
19+
use alloy::primitives::U256;
20+
21+
#[test]
22+
fn test_extract_signature_components() {
23+
let r = U256::from(123456789);
24+
let s = U256::from(987654321);
25+
let y_parity = true;
26+
let sig = PrimitiveSignature::new(r, s, y_parity);
27+
let (v, r_bytes, s_bytes) = extract_signature_components(&sig);
28+
assert_eq!(v, 28);
29+
assert_eq!(U256::from_be_bytes(r_bytes.0), r);
30+
assert_eq!(U256::from_be_bytes(s_bytes.0), s);
31+
}
32+
}

0 commit comments

Comments
 (0)