@@ -3,19 +3,17 @@ use crate::models::node::OrchestratorNode;
33use crate :: p2p:: InviteRequest as InviteRequestWithMetadata ;
44use crate :: store:: core:: StoreContext ;
55use crate :: utils:: loop_heartbeats:: LoopHeartbeats ;
6- use alloy:: primitives:: utils:: keccak256 as keccak;
7- use alloy:: primitives:: U256 ;
8- use alloy:: signers:: Signer ;
96use anyhow:: { bail, Result } ;
107use futures:: stream;
118use futures:: StreamExt ;
129use log:: { debug, error, info, warn} ;
13- use p2p:: InviteRequest ;
1410use p2p:: InviteRequestUrl ;
11+ use prime_core:: invite:: {
12+ admin:: { generate_invite_expiration, generate_invite_nonce, generate_invite_signature} ,
13+ common:: InviteBuilder ,
14+ } ;
1515use shared:: web3:: wallet:: Wallet ;
1616use std:: sync:: Arc ;
17- use std:: time:: SystemTime ;
18- use std:: time:: UNIX_EPOCH ;
1917use tokio:: sync:: mpsc:: Sender ;
2018use tokio:: time:: { interval, Duration } ;
2119
@@ -89,29 +87,15 @@ impl NodeInviter {
8987 nonce : [ u8 ; 32 ] ,
9088 expiration : [ u8 ; 32 ] ,
9189 ) -> Result < [ u8 ; 65 ] > {
92- let domain_id: [ u8 ; 32 ] = U256 :: from ( self . domain_id ) . to_be_bytes ( ) ;
93- let pool_id: [ u8 ; 32 ] = U256 :: from ( self . pool_id ) . to_be_bytes ( ) ;
94-
95- let digest = keccak (
96- [
97- & domain_id,
98- & pool_id,
99- node. address . as_slice ( ) ,
100- & nonce,
101- & expiration,
102- ]
103- . concat ( ) ,
104- ) ;
105-
106- let signature = self
107- . wallet
108- . signer
109- . sign_message ( digest. as_slice ( ) )
110- . await ?
111- . as_bytes ( )
112- . to_owned ( ) ;
113-
114- Ok ( signature)
90+ generate_invite_signature (
91+ & self . wallet ,
92+ self . domain_id ,
93+ self . pool_id ,
94+ node. address ,
95+ nonce,
96+ expiration,
97+ )
98+ . await
11599 }
116100
117101 async fn send_invite ( & self , node : & OrchestratorNode ) -> Result < ( ) , anyhow:: Error > {
@@ -122,29 +106,21 @@ impl NodeInviter {
122106 let p2p_addresses = node. worker_p2p_addresses . as_ref ( ) . unwrap ( ) ;
123107
124108 // Generate random nonce and expiration
125- let nonce: [ u8 ; 32 ] = rand:: random ( ) ;
126- let expiration: [ u8 ; 32 ] = U256 :: from (
127- SystemTime :: now ( )
128- . duration_since ( UNIX_EPOCH )
129- . map_err ( |e| anyhow:: anyhow!( "System time error: {}" , e) ) ?
130- . as_secs ( )
131- + 1000 ,
132- )
133- . to_be_bytes ( ) ;
109+ let nonce = generate_invite_nonce ( ) ;
110+ let expiration = generate_invite_expiration ( Some ( 1000 ) ) ?;
134111
135112 let invite_signature = self . generate_invite ( node, nonce, expiration) . await ?;
136- let payload = InviteRequest {
137- invite : hex:: encode ( invite_signature) ,
138- pool_id : self . pool_id ,
139- url : self . url . clone ( ) ,
140- timestamp : SystemTime :: now ( )
141- . duration_since ( UNIX_EPOCH )
142- . map_err ( |e| anyhow:: anyhow!( "System time error: {}" , e) ) ?
143- . as_secs ( ) ,
144- expiration,
145- nonce,
113+
114+ // Build the invite request using the builder
115+ let builder = match & self . url {
116+ InviteRequestUrl :: MasterUrl ( url) => InviteBuilder :: with_url ( self . pool_id , url. clone ( ) ) ,
117+ InviteRequestUrl :: MasterIpPort ( ip, port) => {
118+ InviteBuilder :: with_ip_port ( self . pool_id , ip. clone ( ) , * port)
119+ }
146120 } ;
147121
122+ let payload = builder. build ( invite_signature, nonce, expiration) ?;
123+
148124 info ! ( "Sending invite to node: {p2p_id}" ) ;
149125
150126 let ( response_tx, response_rx) = tokio:: sync:: oneshot:: channel ( ) ;
0 commit comments