@@ -60,8 +60,11 @@ use lightning::{
60
60
} ;
61
61
use lightning_invoice:: Bolt11Invoice ;
62
62
use serde:: { de:: DeserializeOwned , Deserialize , Serialize } ;
63
- use std:: sync:: atomic:: { AtomicU32 , Ordering } ;
64
63
use std:: { collections:: HashMap , fmt:: Debug , sync:: Arc } ;
64
+ use std:: {
65
+ str:: FromStr ,
66
+ sync:: atomic:: { AtomicU32 , Ordering } ,
67
+ } ;
65
68
66
69
// The amount of time in milliseconds to wait for
67
70
// checking the status of a fedimint payment. This
@@ -223,6 +226,75 @@ impl<S: MutinyStorage> FederationClient<S> {
223
226
return Err ( MutinyError :: NetworkMismatch ) ;
224
227
}
225
228
229
+ // Set active gateway preference
230
+ let lightning_module = fedimint_client. get_first_module :: < LightningClientModule > ( ) ;
231
+ let gateways = lightning_module
232
+ . fetch_registered_gateways ( )
233
+ . await
234
+ . map_err ( |e| {
235
+ log_warn ! (
236
+ logger,
237
+ "Could not fetch gateways from federation {}: {e}" ,
238
+ federation_info. federation_id( )
239
+ )
240
+ } ) ;
241
+
242
+ if let Ok ( gateways) = gateways {
243
+ let mut active_choice: Option < bitcoin:: secp256k1:: PublicKey > = None ;
244
+ for g in gateways {
245
+ let g_id = g. info . gateway_id ;
246
+
247
+ // if the gateway node ID matches what we expect for our signet/mainnet
248
+ // these take the highest priority
249
+ if g_id
250
+ == bitcoin:: secp256k1:: PublicKey :: from_str (
251
+ "0256f5ef1d986e9abf559651b7167de28bfd954683cd0f14703be12d1421aedc55" ,
252
+ )
253
+ . expect ( "should be valid pubkey" )
254
+ && fedimint_client. federation_id ( ) // our signet federation
255
+ == FederationId :: from_str (
256
+ "c8d423964c7ad944d30f57359b6e5b260e211dcfdb945140e28d4df51fd572d2" ,
257
+ )
258
+ . expect ( "should be a valid federation id" )
259
+ {
260
+ active_choice = Some ( g_id) ;
261
+ break ;
262
+ } else if g. info . gateway_id
263
+ == bitcoin:: secp256k1:: PublicKey :: from_str (
264
+ "025b9f090d3daab012346701f27d1c220d6d290f6b498255cddc492c255532a09d" ,
265
+ )
266
+ . expect ( "should be valid pubkey" )
267
+ && fedimint_client. federation_id ( ) // our mainnet federation
268
+ == FederationId :: from_str (
269
+ "c36038cce5a97e3467f03336fa8e7e3410960b81d1865cda2a609f70a8f51efb" ,
270
+ )
271
+ . expect ( "should be a valid federation id" )
272
+ {
273
+ active_choice = Some ( g_id) ;
274
+ break ;
275
+ }
276
+
277
+ // TODO if vetted, set assigned
278
+ // TODO if not vetted, make sure fee is high enough
279
+ }
280
+
281
+ if let Some ( a) = active_choice {
282
+ log_info ! (
283
+ logger,
284
+ "Setting active gateway for federation {}: {:?}" ,
285
+ federation_info. federation_id( ) ,
286
+ a
287
+ ) ;
288
+ let _ = lightning_module. set_active_gateway ( & a) . await . map_err ( |e| {
289
+ log_warn ! (
290
+ logger,
291
+ "Could not set gateway for federation {}: {e}" ,
292
+ federation_info. federation_id( )
293
+ )
294
+ } ) ;
295
+ }
296
+ }
297
+
226
298
log_debug ! ( logger, "Built fedimint client" ) ;
227
299
Ok ( FederationClient {
228
300
uuid,
0 commit comments