@@ -15,14 +15,13 @@ use libp2p::identity::Keypair;
1515use libp2p:: kad:: store:: MemoryStore ;
1616
1717use libp2p:: swarm:: SwarmEvent ;
18- use libp2p:: { gossipsub, identify, mdns, noise, tcp, yamux, Multiaddr , PeerId , Swarm } ;
18+ use libp2p:: { gossipsub, identify, kad , mdns, noise, tcp, yamux, Multiaddr , PeerId , Swarm } ;
1919use serde:: Serialize ;
2020
2121use crate :: app:: config:: { self , TASK_INTERVAL } ;
2222use crate :: app:: config:: Config ;
2323use crate :: helper:: bitcoin:: get_group_address_by_tweak;
24- use crate :: helper:: cipher:: random_bytes;
25- use crate :: helper:: encoding:: from_base64;
24+ use crate :: helper:: encoding:: identifier_to_peer_id;
2625use crate :: helper:: gossip:: { subscribe_gossip_topics, HeartBeatMessage , SubscribeTopic } ;
2726use crate :: helper:: mem_store;
2827use crate :: protocols:: sign:: { received_sign_message, SignMesage , SignTask } ;
@@ -66,19 +65,20 @@ pub struct Signer {
6665 db_dkg : sled:: Db ,
6766 db_dkg_variables : sled:: Db ,
6867 db_keypair : sled:: Db ,
68+
6969}
7070
7171impl Signer {
7272 pub fn new ( conf : Config ) -> Self {
7373
7474 // load private key from priv_validator_key_path
75- let local_key = match conf. get_validator_key ( ) {
76- Some ( validator_key ) => {
77- let b = from_base64 ( & validator_key . priv_key . value ) . expect ( "Decode private key failed" ) ;
78- SecretKey :: from_slice ( b . as_slice ( ) ) . expect ( "invalid secret key" )
79- } ,
80- None => SecretKey :: from_slice ( random_bytes ( SecretKey :: BYTES ) . as_slice ( ) ) . expect ( "invalid secret key" )
81- } ;
75+ let priv_validator_key = conf. load_validator_key ( ) ;
76+
77+ // let b = serde_json::to_vec(&priv_validator_key .priv_key).unwrap( );
78+ let mut b = priv_validator_key . priv_key . ed25519_signing_key ( ) . unwrap ( ) . as_bytes ( ) . to_vec ( ) ;
79+
80+ b . extend ( priv_validator_key . pub_key . to_bytes ( ) ) ;
81+ let local_key = SecretKey :: new ( b . as_slice ( ) . try_into ( ) . unwrap ( ) ) ;
8282
8383 let id = frost:: Secp256K1ScalarField :: deserialize ( & local_key. public_key ( ) . as_slice ( ) . try_into ( ) . unwrap ( ) ) . unwrap ( ) ;
8484 let identifier = frost_core:: Identifier :: new ( id) . unwrap ( ) ;
@@ -88,7 +88,7 @@ impl Signer {
8888 let bitcoin_client = Client :: new (
8989 & conf. bitcoin . rpc ,
9090 Auth :: UserPass ( conf. bitcoin . user . clone ( ) , conf. bitcoin . password . clone ( ) ) )
91- . expect ( "Could not initial bitcoin RPC client" ) ;
91+ . expect ( "Could not initial bitcoin RPC client" ) ;
9292
9393 let db_sign = sled:: open ( conf. get_database_with_name ( "sign-task" ) ) . expect ( "Counld not create database!" ) ;
9494 let db_sign_variables = sled:: open ( conf. get_database_with_name ( "sign-task-variables" ) ) . expect ( "Counld not create database!" ) ;
@@ -117,11 +117,38 @@ impl Signer {
117117 & self . identifier
118118 }
119119
120+ pub fn peer_id ( & self ) -> PeerId {
121+ identifier_to_peer_id ( & self . identifier )
122+ }
123+
124+ pub fn p2p_keypair ( & self ) -> Keypair {
125+ let raw = & self . identity_key . to_vec ( ) [ 0 ..32 ] . to_vec ( ) ;
126+ Keypair :: ed25519_from_bytes ( raw. clone ( ) ) . unwrap ( )
127+ }
128+
120129 pub fn validator_address ( & self ) -> String {
121- match & self . config ( ) . get_validator_key ( ) {
122- Some ( key) => key. address . clone ( ) ,
123- None => "" . to_string ( )
130+ self . config ( ) . load_validator_key ( ) . address . to_string ( )
131+ }
132+
133+ pub fn is_white_listed_peer ( & self , peer_id : PeerId ) -> bool {
134+
135+ if self . config . bootstrap_nodes . iter ( ) . any ( |addr| { addr. contains ( & peer_id. to_base58 ( ) ) } ) {
136+ return true
124137 }
138+ let keypairs = self . list_keypairs ( ) ;
139+ if keypairs. len ( ) == 0 {
140+ return true ;
141+ }
142+ for ( _, k) in keypairs {
143+ for identifier in k. pub_key . verifying_shares ( ) . keys ( ) {
144+ let local = identifier_to_peer_id ( identifier) ;
145+ // println!("{:?}={:?} {}", local, peer_id, local==peer_id);
146+ if local == peer_id {
147+ return true ;
148+ }
149+ }
150+ }
151+ false
125152 }
126153
127154 pub async fn get_relayer_account ( & self ) -> BaseAccount {
@@ -376,15 +403,18 @@ pub async fn run_signer_daemon(conf: Config, seed: bool) {
376403 info ! ( "Starting TSS Signer Daemon" ) ;
377404
378405 // load config
379- conf. load_validator_key ( ) ;
380406 let signer = Signer :: new ( conf. clone ( ) ) ;
381407
382408 for ( i, ( addr, vkp) ) in signer. list_keypairs ( ) . iter ( ) . enumerate ( ) {
383409 debug ! ( "Vault {i}. {addr}, ({}-of-{})" , vkp. priv_key. min_signers( ) , vkp. pub_key. verifying_shares( ) . len( ) ) ;
384410 }
385411
386- let libp2p_keypair = Keypair :: from_protobuf_encoding ( from_base64 ( & conf. p2p_keypair ) . unwrap ( ) . as_slice ( ) ) . unwrap ( ) ;
387- let mut swarm: libp2p:: Swarm < TSSBehaviour > = libp2p:: SwarmBuilder :: with_existing_identity ( libp2p_keypair)
412+ // let priv_validator_key = conf.load_validator_key();
413+ // let bytes = serde_json::to_vec(&priv_validator_key.priv_key).unwrap();
414+ // let libp2p_keypair = Keypair::from_protobuf_encoding(bytes.as_slice()).unwrap();
415+ // let mut raw = signer.identity_key.as_slice().to_owned();
416+ // let libp2p_keypair = Keypair::ed25519_from_bytes(&mut raw).unwrap();
417+ let mut swarm: libp2p:: Swarm < TSSBehaviour > = libp2p:: SwarmBuilder :: with_existing_identity ( signer. p2p_keypair ( ) )
388418 . with_tokio ( )
389419 . with_tcp (
390420 tcp:: Config :: default ( ) ,
@@ -446,9 +476,6 @@ pub async fn run_signer_daemon(conf: Config, seed: bool) {
446476 subscribe_gossip_topics ( & mut swarm) ;
447477
448478 let mut interval_free = tokio:: time:: interval ( TASK_INTERVAL ) ;
449- // let start = Instant::now() + (TASK_ROUND_WINDOW - tokio::time::Duration::from_secs(now() % TASK_ROUND_WINDOW.as_secs()));
450- // let mut interval_aligned = tokio::time::interval_at(start, TASK_ROUND_WINDOW);
451- // let mut alive_interval = tokio::time::interval(tokio::time::Duration::from_secs(5));
452479
453480 loop {
454481 select ! {
@@ -460,9 +487,14 @@ pub async fn run_signer_daemon(conf: Config, seed: bool) {
460487 info!( "Listening on {address}/p2p/{}" , swarm. local_peer_id( ) ) ;
461488 } ,
462489 SwarmEvent :: ConnectionEstablished { peer_id, endpoint, ..} => {
463- swarm. behaviour_mut( ) . gossip. add_explicit_peer( & peer_id) ;
464- let addr = endpoint. get_remote_address( ) ;
465- info!( "Connected to {:?}, " , addr) ;
490+ if signer. is_white_listed_peer( peer_id) {
491+ swarm. behaviour_mut( ) . gossip. add_explicit_peer( & peer_id) ;
492+ let addr = endpoint. get_remote_address( ) ;
493+ info!( "Connected to {:?}, " , addr) ;
494+ } else {
495+ let _ = swarm. disconnect_peer_id( peer_id) ;
496+ info!( "Disconnected (untrusted) {:?}" , peer_id) ;
497+ }
466498 } ,
467499 SwarmEvent :: ConnectionClosed { peer_id, cause, .. } => {
468500 info!( "Disconnected {peer_id}: {:?}" , cause) ;
@@ -540,11 +572,17 @@ async fn event_handler(event: TSSBehaviourEvent, swarm: &mut Swarm<TSSBehaviour>
540572 // info!(" @@(Received) Discovered new peer: {peer_id} with info: {connection_id} {:?}", info);
541573 info. listen_addrs . iter ( ) . for_each ( |addr| {
542574 if !addr. to_string ( ) . starts_with ( "/ip4/127.0.0.1" ) {
543- // tracing::debug!("Discovered: {addr}/p2p/{peer_id}");
575+ tracing:: debug!( "Discovered: {addr}/p2p/{peer_id}" ) ;
544576 swarm. behaviour_mut ( ) . kad . add_address ( & peer_id, addr. clone ( ) ) ;
545577 }
546578 } ) ;
547579 }
580+ TSSBehaviourEvent :: Kad ( kad:: Event :: RoutablePeer { peer, address } ) => {
581+ debug ! ( "Found Peer {:?}/{:?}" , address, peer)
582+ }
583+ TSSBehaviourEvent :: Kad ( kad:: Event :: RoutingUpdated { is_new_peer, addresses, .. } ) => {
584+ debug ! ( "Routing Peer {:?}/{:?}" , addresses, is_new_peer)
585+ }
548586 TSSBehaviourEvent :: Mdns ( mdns:: Event :: Discovered ( list) ) => {
549587 for ( peer_id, multiaddr) in list {
550588 swarm. behaviour_mut ( ) . gossip . add_explicit_peer ( & peer_id) ;
0 commit comments