@@ -8,21 +8,21 @@ use std::{
88use alloy:: {
99 primitives:: { address, Address , U256 } ,
1010 providers:: ProviderBuilder ,
11- rpc:: { client:: RpcClient , types:: beacon:: BlsPublicKey } ,
11+ rpc:: { client:: RpcClient , types:: beacon:: constants :: BLS_PUBLIC_KEY_BYTES_LEN } ,
1212 sol,
1313 transports:: http:: Http ,
1414} ;
1515use eyre:: { bail, ensure, Context } ;
1616use reqwest:: Client ;
17- use serde:: { Deserialize , Serialize } ;
17+ use serde:: { Deserialize , Deserializer , Serialize } ;
1818use tracing:: { debug, info, warn} ;
1919use url:: Url ;
2020
2121use super :: { load_optional_env_var, PbsConfig , RelayConfig , MUX_PATH_ENV } ;
2222use crate :: {
2323 config:: { remove_duplicate_keys, safe_read_http_response} ,
2424 pbs:: RelayClient ,
25- types:: Chain ,
25+ types:: { BlsPublicKey , Chain } ,
2626} ;
2727
2828#[ derive( Debug , Deserialize , Serialize ) ]
@@ -103,8 +103,8 @@ impl PbsMuxes {
103103 let config = Arc :: new ( config) ;
104104
105105 let runtime_config = RuntimeMuxConfig { id : mux. id , config, relays : relay_clients } ;
106- for pubkey in mux. validator_pubkeys . iter ( ) {
107- configs. insert ( * pubkey, runtime_config. clone ( ) ) ;
106+ for pubkey in mux. validator_pubkeys . into_iter ( ) {
107+ configs. insert ( pubkey, runtime_config. clone ( ) ) ;
108108 }
109109 }
110110
@@ -296,7 +296,6 @@ async fn fetch_lido_registry_keys(
296296 debug ! ( "fetching {total_keys} total keys" ) ;
297297
298298 const CALL_BATCH_SIZE : u64 = 250u64 ;
299- const BLS_PK_LEN : usize = BlsPublicKey :: len_bytes ( ) ;
300299
301300 let mut keys = vec ! [ ] ;
302301 let mut offset = 0 ;
@@ -311,13 +310,16 @@ async fn fetch_lido_registry_keys(
311310 . pubkeys ;
312311
313312 ensure ! (
314- pubkeys. len( ) % BLS_PK_LEN == 0 ,
315- "unexpected number of keys in batch, expected multiple of {BLS_PK_LEN }, got {}" ,
313+ pubkeys. len( ) % BLS_PUBLIC_KEY_BYTES_LEN == 0 ,
314+ "unexpected number of keys in batch, expected multiple of {BLS_PUBLIC_KEY_BYTES_LEN }, got {}" ,
316315 pubkeys. len( )
317316 ) ;
318317
319- for chunk in pubkeys. chunks ( BLS_PK_LEN ) {
320- keys. push ( BlsPublicKey :: try_from ( chunk) ?) ;
318+ for chunk in pubkeys. chunks ( BLS_PUBLIC_KEY_BYTES_LEN ) {
319+ keys. push (
320+ BlsPublicKey :: deserialize ( chunk)
321+ . map_err ( |_| eyre:: eyre!( "invalid BLS public key" ) ) ?,
322+ ) ;
321323 }
322324
323325 offset += limit;
@@ -356,10 +358,13 @@ async fn fetch_ssv_pubkeys(
356358 ) ;
357359
358360 let response = fetch_ssv_pubkeys_from_url ( & url, http_timeout) . await ?;
359- pubkeys. extend ( response. validators . iter ( ) . map ( |v| v. pubkey ) . collect :: < Vec < BlsPublicKey > > ( ) ) ;
361+ let fetched = response. validators . len ( ) ;
362+ pubkeys. extend (
363+ response. validators . into_iter ( ) . map ( |v| v. pubkey ) . collect :: < Vec < BlsPublicKey > > ( ) ,
364+ ) ;
360365 page += 1 ;
361366
362- if response . validators . len ( ) < MAX_PER_PAGE {
367+ if fetched < MAX_PER_PAGE {
363368 ensure ! (
364369 pubkeys. len( ) == response. pagination. total,
365370 "expected {} keys, got {}" ,
@@ -397,12 +402,29 @@ struct SSVResponse {
397402 pagination : SSVPagination ,
398403}
399404
400- #[ derive( Deserialize ) ]
401405struct SSVValidator {
402- #[ serde( rename = "public_key" ) ]
403406 pubkey : BlsPublicKey ,
404407}
405408
409+ impl < ' de > Deserialize < ' de > for SSVValidator {
410+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
411+ where
412+ D : Deserializer < ' de > ,
413+ {
414+ #[ derive( Deserialize ) ]
415+ struct SSVValidator {
416+ public_key : String ,
417+ }
418+
419+ let s = SSVValidator :: deserialize ( deserializer) ?;
420+ let bytes = alloy:: hex:: decode ( & s. public_key ) . map_err ( serde:: de:: Error :: custom) ?;
421+ let pubkey = BlsPublicKey :: deserialize ( & bytes)
422+ . map_err ( |e| serde:: de:: Error :: custom ( format ! ( "invalid BLS public key: {e:?}" ) ) ) ?;
423+
424+ Ok ( Self { pubkey } )
425+ }
426+ }
427+
406428#[ derive( Deserialize ) ]
407429struct SSVPagination {
408430 total : usize ,
@@ -412,15 +434,15 @@ struct SSVPagination {
412434mod tests {
413435 use std:: net:: SocketAddr ;
414436
415- use alloy:: { hex :: FromHex , primitives:: U256 , providers:: ProviderBuilder } ;
437+ use alloy:: { primitives:: U256 , providers:: ProviderBuilder } ;
416438 use axum:: { response:: Response , routing:: get} ;
417439 use tokio:: { net:: TcpListener , task:: JoinHandle } ;
418440 use url:: Url ;
419441
420442 use super :: * ;
421443 use crate :: {
422444 config:: { HTTP_TIMEOUT_SECONDS_DEFAULT , MUXER_HTTP_MAX_LENGTH } ,
423- utils:: { set_ignore_content_length, ResponseReadError } ,
445+ utils:: { bls_pubkey_from_hex_unchecked , set_ignore_content_length, ResponseReadError } ,
424446 } ;
425447
426448 const TEST_HTTP_TIMEOUT : u64 = 2 ;
@@ -448,8 +470,11 @@ mod tests {
448470 . pubkeys ;
449471
450472 let mut vec = vec ! [ ] ;
451- for chunk in pubkeys. chunks ( BlsPublicKey :: len_bytes ( ) ) {
452- vec. push ( BlsPublicKey :: try_from ( chunk) ?) ;
473+ for chunk in pubkeys. chunks ( BLS_PUBLIC_KEY_BYTES_LEN ) {
474+ vec. push (
475+ BlsPublicKey :: deserialize ( chunk)
476+ . map_err ( |_| eyre:: eyre!( "invalid BLS public key" ) ) ?,
477+ ) ;
453478 }
454479
455480 assert_eq ! ( vec. len( ) , LIMIT ) ;
@@ -472,15 +497,9 @@ mod tests {
472497 // NOTE: requires that ssv_data.json dpesn't change
473498 assert_eq ! ( response. validators. len( ) , 3 ) ;
474499 let expected_pubkeys = [
475- BlsPublicKey :: from_hex (
476- "0x967ba17a3e7f82a25aa5350ec34d6923e28ad8237b5a41efe2c5e325240d74d87a015bf04634f21900963539c8229b2a" ,
477- ) ?,
478- BlsPublicKey :: from_hex (
479- "0xac769e8cec802e8ffee34de3253be8f438a0c17ee84bdff0b6730280d24b5ecb77ebc9c985281b41ee3bda8663b6658c" ,
480- ) ?,
481- BlsPublicKey :: from_hex (
482- "0x8c866a5a05f3d45c49b457e29365259021a509c5daa82e124f9701a960ee87b8902e87175315ab638a3d8b1115b23639" ,
483- ) ?,
500+ bls_pubkey_from_hex_unchecked ( "967ba17a3e7f82a25aa5350ec34d6923e28ad8237b5a41efe2c5e325240d74d87a015bf04634f21900963539c8229b2a" ) ,
501+ bls_pubkey_from_hex_unchecked ( "ac769e8cec802e8ffee34de3253be8f438a0c17ee84bdff0b6730280d24b5ecb77ebc9c985281b41ee3bda8663b6658c" ) ,
502+ bls_pubkey_from_hex_unchecked ( "8c866a5a05f3d45c49b457e29365259021a509c5daa82e124f9701a960ee87b8902e87175315ab638a3d8b1115b23639" ) ,
484503 ] ;
485504 for ( i, validator) in response. validators . iter ( ) . enumerate ( ) {
486505 assert_eq ! ( validator. pubkey, expected_pubkeys[ i] ) ;
0 commit comments