Skip to content

Commit 6f2b16c

Browse files
authored
Migrate EcdsaSignature internal types to alloy (#3935)
# Description Migrate EcdsaSignature internal types to alloy # Changes <!-- List of detailed changes (how the change is accomplished) --> - [ ] Replaces the ethcontract::H256 with alloy::primitives::B256 - [ ] Refactors where applicable ## How to test Existing tests <!-- ## Related Issues Fixes # -->
1 parent 777d75c commit 6f2b16c

10 files changed

Lines changed: 44 additions & 42 deletions

File tree

crates/autopilot/src/database/competition.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use {
2-
crate::domain::{competition::Score, eth},
2+
crate::domain::competition::Score,
3+
alloy::primitives::Address,
34
anyhow::Context,
45
database::{
5-
Address,
66
auction::AuctionId,
77
auction_prices::AuctionPrice,
88
byte_array::ByteArray,
@@ -18,7 +18,7 @@ use {
1818
#[derive(Clone, Default, Debug)]
1919
pub struct Competition {
2020
pub auction_id: AuctionId,
21-
pub reference_scores: HashMap<eth::Address, Score>,
21+
pub reference_scores: HashMap<Address, Score>,
2222
/// Addresses to which the CIP20 participation rewards will be payed out.
2323
/// Usually the same as the solver addresses.
2424
pub participants: HashSet<H160>,
@@ -91,7 +91,7 @@ impl super::Postgres {
9191
pub async fn save_surplus_capturing_jit_order_owners(
9292
&self,
9393
auction_id: AuctionId,
94-
surplus_capturing_jit_order_owners: &[Address],
94+
surplus_capturing_jit_order_owners: &[database::Address],
9595
) -> anyhow::Result<()> {
9696
let mut ex = self.pool.acquire().await.context("acquire")?;
9797

crates/autopilot/src/domain/auction/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use {
22
super::{Order, eth},
3+
alloy::primitives::Address,
34
std::collections::HashMap,
45
};
56

@@ -15,7 +16,7 @@ pub struct RawAuctionData {
1516
pub block: u64,
1617
pub orders: Vec<Order>,
1718
pub prices: Prices,
18-
pub surplus_capturing_jit_order_owners: Vec<eth::Address>,
19+
pub surplus_capturing_jit_order_owners: Vec<Address>,
1920
}
2021

2122
pub type Id = i64;
@@ -26,7 +27,7 @@ pub struct Auction {
2627
pub block: u64,
2728
pub orders: Vec<Order>,
2829
pub prices: Prices,
29-
pub surplus_capturing_jit_order_owners: Vec<eth::Address>,
30+
pub surplus_capturing_jit_order_owners: Vec<Address>,
3031
}
3132

3233
impl PartialEq for Auction {

crates/autopilot/src/domain/competition/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use {
22
super::auction::order,
33
crate::domain::{self, auction, eth},
4+
alloy::primitives::Address,
45
derive_more::Display,
56
num::Saturating,
67
std::collections::HashMap,
@@ -21,7 +22,7 @@ type SolutionId = u64;
2122
pub struct Solution {
2223
/// A solution ID provided by the solver.
2324
id: SolutionId,
24-
solver: eth::Address,
25+
solver: Address,
2526
/// Score reported by the solver in their response.
2627
score: Score,
2728
orders: HashMap<domain::OrderUid, TradedOrder>,
@@ -35,7 +36,7 @@ pub struct Solution {
3536
impl Solution {
3637
pub fn new(
3738
id: SolutionId,
38-
solver: eth::Address,
39+
solver: Address,
3940
score: Score,
4041
orders: HashMap<domain::OrderUid, TradedOrder>,
4142
prices: auction::Prices,
@@ -54,7 +55,7 @@ impl Solution {
5455
self.id
5556
}
5657

57-
pub fn solver(&self) -> eth::Address {
58+
pub fn solver(&self) -> Address {
5859
self.solver
5960
}
6061

crates/autopilot/src/infra/persistence/dto/order.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ impl From<boundary::Signature> for domain::auction::order::Signature {
240240
impl From<domain::auction::order::EcdsaSignature> for boundary::EcdsaSignature {
241241
fn from(signature: domain::auction::order::EcdsaSignature) -> Self {
242242
Self {
243-
r: signature.r,
244-
s: signature.s,
243+
r: signature.r.0.into(),
244+
s: signature.s.0.into(),
245245
v: signature.v,
246246
}
247247
}
@@ -250,8 +250,8 @@ impl From<domain::auction::order::EcdsaSignature> for boundary::EcdsaSignature {
250250
impl From<boundary::EcdsaSignature> for domain::auction::order::EcdsaSignature {
251251
fn from(signature: boundary::EcdsaSignature) -> Self {
252252
Self {
253-
r: signature.r,
254-
s: signature.s,
253+
r: signature.r.0.into(),
254+
s: signature.s.0.into(),
255255
v: signature.v,
256256
}
257257
}

crates/e2e/src/api/zeroex.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use {
22
crate::setup::TestAccount,
33
alloy::primitives::{Address, B256, U256},
44
chrono::{DateTime, NaiveDateTime, Utc},
5-
ethrpc::alloy::conversions::{IntoAlloy, IntoLegacy},
5+
ethrpc::alloy::conversions::IntoLegacy,
66
hex_literal::hex,
77
model::DomainSeparator,
88
shared::zeroex_api::{self, Order, OrderMetadata, OrderRecord, ZeroExSignature},
@@ -114,8 +114,8 @@ impl Eip712TypedZeroExOrder {
114114
) -> ZeroExSignature {
115115
let signature = signer.sign_typed_data(domain_separator, &hash);
116116
ZeroExSignature {
117-
r: signature.r.into_alloy(),
118-
s: signature.s.into_alloy(),
117+
r: signature.r,
118+
s: signature.s,
119119
v: signature.v,
120120
// See <https://github.com/0xProject/protocol/blob/%400x/protocol-utils%4011.24.2/packages/protocol-utils/src/signature_utils.ts#L13>
121121
signature_type: 2,

crates/e2e/src/setup/onchain_components/safe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ impl Safe {
275275
// Signature format specified here:
276276
// <https://etherscan.io/address/0xd9Db270c1B5E3Bd161E8c8503c55cEABeE709552#code#F11#L20>
277277
[
278-
signature.r.as_bytes(),
279-
signature.s.as_bytes(),
278+
signature.r.as_slice(),
279+
signature.s.as_slice(),
280280
&[signature.v],
281281
]
282282
.concat()

crates/model/src/order.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,6 @@ mod tests {
10751075
chrono::TimeZone,
10761076
hex_literal::hex,
10771077
maplit::hashset,
1078-
primitive_types::H256,
10791078
secp256k1::{PublicKey, Secp256k1, SecretKey},
10801079
serde_json::json,
10811080
testlib::assert_json_matches,
@@ -1159,11 +1158,11 @@ mod tests {
11591158
},
11601159
signature: EcdsaSignature {
11611160
v: 1,
1162-
r: H256::from_str(
1161+
r: B256::from_str(
11631162
"0200000000000000000000000000000000000000000000000000000000000003",
11641163
)
11651164
.unwrap(),
1166-
s: H256::from_str(
1165+
s: B256::from_str(
11671166
"0400000000000000000000000000000000000000000000000000000000000005",
11681167
)
11691168
.unwrap(),

crates/model/src/signature.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use {
22
crate::{DomainSeparator, quote::QuoteSigningScheme},
3+
alloy::primitives::B256,
34
anyhow::{Context as _, Result, ensure},
45
primitive_types::{H160, H256},
56
serde::{Deserialize, Serialize, de},
@@ -120,8 +121,8 @@ impl Signature {
120121
.try_into()
121122
.context("ECDSA signature must be 65 bytes long")?;
122123
EcdsaSignature {
123-
r: H256::from_slice(&bytes[..32]),
124-
s: H256::from_slice(&bytes[32..64]),
124+
r: B256::from_slice(&bytes[..32]),
125+
s: B256::from_slice(&bytes[32..64]),
125126
v: bytes[64],
126127
}
127128
.to_signature(
@@ -258,8 +259,8 @@ impl SigningScheme {
258259

259260
#[derive(Eq, PartialEq, Clone, Copy, Debug, Default, Hash)]
260261
pub struct EcdsaSignature {
261-
pub r: H256,
262-
pub s: H256,
262+
pub r: B256,
263+
pub s: B256,
263264
pub v: u8,
264265
}
265266

@@ -306,16 +307,16 @@ impl EcdsaSignature {
306307
/// r + s + v
307308
pub fn to_bytes(self) -> [u8; 65] {
308309
let mut bytes = [0u8; 65];
309-
bytes[..32].copy_from_slice(self.r.as_bytes());
310-
bytes[32..64].copy_from_slice(self.s.as_bytes());
310+
bytes[..32].copy_from_slice(self.r.as_slice());
311+
bytes[32..64].copy_from_slice(self.s.as_slice());
311312
bytes[64] = self.v;
312313
bytes
313314
}
314315

315316
pub fn from_bytes(bytes: &[u8; 65]) -> Self {
316317
EcdsaSignature {
317-
r: H256::from_slice(&bytes[..32]),
318-
s: H256::from_slice(&bytes[32..64]),
318+
r: B256::from_slice(&bytes[..32]),
319+
s: B256::from_slice(&bytes[32..64]),
319320
v: bytes[64],
320321
}
321322
}
@@ -327,7 +328,7 @@ impl EcdsaSignature {
327328
struct_hash: &[u8; 32],
328329
) -> Result<Recovered> {
329330
let message = hashed_signing_message(signing_scheme, domain_separator, struct_hash);
330-
let recovery = Recovery::new(message, self.v as u64, self.r, self.s);
331+
let recovery = Recovery::new(message, self.v as u64, self.r.0.into(), self.s.0.into());
331332
let (signature, recovery_id) = recovery
332333
.as_signature()
333334
.context("unexpectedly invalid signature")?;
@@ -350,17 +351,17 @@ impl EcdsaSignature {
350351
let signature = key.sign(&message, None).unwrap();
351352
Self {
352353
v: signature.v as u8,
353-
r: signature.r,
354-
s: signature.s,
354+
r: signature.r.0.into(),
355+
s: signature.s.0.into(),
355356
}
356357
}
357358

358359
/// Returns an arbitrary non-zero signature that can be used for recovery
359360
/// when you don't actually care about the owner.
360361
pub fn non_zero() -> Self {
361362
Self {
362-
r: H256([1; 32]),
363-
s: H256([2; 32]),
363+
r: B256::repeat_byte(1),
364+
s: B256::repeat_byte(2),
364365
v: 27,
365366
}
366367
}
@@ -517,8 +518,8 @@ mod tests {
517518
),
518519
(
519520
Signature::EthSign(EcdsaSignature {
520-
r: H256([1; 32]),
521-
s: H256([2; 32]),
521+
r: B256::repeat_byte(1),
522+
s: B256::repeat_byte(2),
522523
v: 3,
523524
}),
524525
json!({

crates/orderbook/src/api/cancel_order.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn cancel_order(
8383
mod tests {
8484
use {
8585
super::*,
86-
ethcontract::H256,
86+
alloy::primitives::B256,
8787
hex_literal::hex,
8888
model::signature::{EcdsaSignature, EcdsaSigningScheme},
8989
serde_json::json,
@@ -103,10 +103,10 @@ mod tests {
103103
.unwrap(),
104104
CancellationPayload {
105105
signature: EcdsaSignature {
106-
r: H256(hex!(
106+
r: B256::new(hex!(
107107
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
108108
)),
109-
s: H256(hex!(
109+
s: B256::new(hex!(
110110
"202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"
111111
)),
112112
v: 27,

crates/shared/src/encoded_settlement.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub struct EncodedSettlement {
8888

8989
#[cfg(test)]
9090
mod tests {
91-
use {super::*, ethcontract::H256, hex_literal::hex, model::signature::EcdsaSignature};
91+
use {super::*, alloy::primitives::B256, hex_literal::hex, model::signature::EcdsaSignature};
9292

9393
#[test]
9494
fn order_flag_permutations() {
@@ -188,8 +188,8 @@ mod tests {
188188
(Signature::Eip712(Default::default()), vec![0; 65]),
189189
(
190190
Signature::EthSign(EcdsaSignature {
191-
r: H256([1; 32]),
192-
s: H256([1; 32]),
191+
r: B256::repeat_byte(1),
192+
s: B256::repeat_byte(1),
193193
v: 1,
194194
}),
195195
vec![1; 65],

0 commit comments

Comments
 (0)