@@ -40,10 +40,10 @@ const INITIAL_PUBLISH_DELAY: Duration = Duration::from_millis(500);
4040/// and publishes them to the bittorrent mainline DHT.
4141///
4242/// Calling publish will start a background task that periodically publishes the node address.
43- #[ derive( Debug , Clone ) ]
43+ #[ derive( Debug , Clone , Default ) ]
4444pub struct PkarrNodeDiscovery ( Arc < Inner > ) ;
4545
46- #[ derive( Debug ) ]
46+ #[ derive( Debug , Default ) ]
4747struct Inner {
4848 /// Pkarr client for interacting with the DHT.
4949 pkarr : PkarrClient ,
@@ -56,12 +56,45 @@ struct Inner {
5656 keypair : Option < pkarr:: Keypair > ,
5757}
5858
59+ /// Builder for PkarrNodeDiscovery.
60+ #[ derive( Debug , Default ) ]
61+ pub struct Builder < ' a > {
62+ client : Option < PkarrClient > ,
63+ secret_key : Option < & ' a SecretKey > ,
64+ }
65+
66+ impl < ' a > Builder < ' a > {
67+ /// Explicitly set the pkarr client to use.
68+ pub fn client ( mut self , client : PkarrClient ) -> Self {
69+ self . client = Some ( client) ;
70+ self
71+ }
72+
73+ /// Set the secret key to use for signing the DNS packets.
74+ ///
75+ /// Without a secret key, the node will not publish its address to the DHT.
76+ pub fn secret_key ( mut self , secret_key : & ' a SecretKey ) -> Self {
77+ self . secret_key = Some ( secret_key) ;
78+ self
79+ }
80+
81+ /// Build the discovery mechanism.
82+ pub fn build ( self ) -> PkarrNodeDiscovery {
83+ let client = self . client . unwrap_or_else ( PkarrClient :: new) ;
84+ PkarrNodeDiscovery :: new ( client, self . secret_key )
85+ }
86+ }
87+
5988impl PkarrNodeDiscovery {
89+ pub fn builder ( ) -> Builder < ' static > {
90+ Builder :: default ( )
91+ }
92+
6093 /// Create a new discovery mechanism.
6194 ///
6295 /// If a secret key is provided, the node will publish its address to the DHT.
6396 /// If no secret key is provided, publish will be a no-op, but resolving other nodes will still work.
64- pub fn new ( pkarr : PkarrClient , secret_key : Option < & SecretKey > ) -> Self {
97+ fn new ( pkarr : PkarrClient , secret_key : Option < & SecretKey > ) -> Self {
6598 let keypair =
6699 secret_key. map ( |secret_key| pkarr:: Keypair :: from_secret_key ( & secret_key. to_bytes ( ) ) ) ;
67100 Self ( Arc :: new ( Inner {
0 commit comments