3
3
config:: Command ,
4
4
signer:: { GuardianKey , Signer , GUARDIAN_KEY_ARMORED_BLOCK , STANDARD_ARMOR_LINE_HEADER } ,
5
5
} ,
6
+ anyhow:: Context ,
6
7
api_client:: { ApiClient , Observation } ,
7
8
borsh:: BorshDeserialize ,
8
9
clap:: Parser ,
@@ -215,27 +216,27 @@ async fn get_signer(run_options: config::RunOptions) -> anyhow::Result<Arc<dyn S
215
216
}
216
217
}
217
218
218
- async fn run ( run_options : config:: RunOptions ) {
219
+ async fn run ( run_options : config:: RunOptions ) -> anyhow :: Result < ( ) > {
219
220
let signer = get_signer ( run_options. clone ( ) )
220
221
. await
221
- . expect ( "Failed to create signer" ) ;
222
+ . context ( "Failed to create signer" ) ? ;
222
223
let client = PubsubClient :: new ( & run_options. pythnet_url )
223
224
. await
224
- . expect ( "Invalid WebSocket URL" ) ;
225
+ . context ( "Invalid WebSocket URL" ) ? ;
225
226
drop ( client) ; // Drop the client to avoid holding the connection open
226
227
let accumulator_address = Pubkey :: from_str ( "G9LV2mp9ua1znRAfYwZz5cPiJMAbo1T6mbjdQsDZuMJg" )
227
- . expect ( "Invalid accumulator address" ) ;
228
+ . context ( "Invalid accumulator address" ) ? ;
228
229
let wormhole_pid =
229
- Pubkey :: from_str ( & run_options. wormhole_pid ) . expect ( "Invalid Wormhole program ID" ) ;
230
+ Pubkey :: from_str ( & run_options. wormhole_pid ) . context ( "Invalid Wormhole program ID" ) ? ;
230
231
let api_clients: Vec < ApiClient > = run_options
231
232
. server_urls
232
233
. into_iter ( )
233
- . map ( |server_url| {
234
- ApiClient :: try_new ( server_url, None ) . expect ( "Failed to create API client" )
235
- } )
236
- . collect ( ) ;
234
+ . map ( |server_url| ApiClient :: try_new ( server_url, None ) )
235
+ . collect :: < anyhow:: Result < Vec < _ > > > ( ) ?;
237
236
238
- let ( pubkey, pubkey_evm) = signer. get_public_key ( ) . expect ( "Failed to get public key" ) ;
237
+ let ( pubkey, pubkey_evm) = signer
238
+ . get_public_key ( )
239
+ . context ( "Failed to get public key" ) ?;
239
240
let evm_encded_public_key = format ! ( "0x{}" , hex:: encode( pubkey_evm) ) ;
240
241
tracing:: info!(
241
242
public_key = ?pubkey,
@@ -260,7 +261,7 @@ async fn run(run_options: config::RunOptions) {
260
261
}
261
262
262
263
#[ tokio:: main]
263
- async fn main ( ) {
264
+ async fn main ( ) -> anyhow :: Result < ( ) > {
264
265
// Initialize a Tracing Subscriber
265
266
let fmt_builder = tracing_subscriber:: fmt ( )
266
267
. with_file ( false )
@@ -272,24 +273,26 @@ async fn main() {
272
273
// Use the compact formatter if we're in a terminal, otherwise use the JSON formatter.
273
274
if std:: io:: stderr ( ) . is_terminal ( ) {
274
275
tracing:: subscriber:: set_global_default ( fmt_builder. compact ( ) . finish ( ) )
275
- . expect ( "Failed to set global default subscriber" ) ;
276
+ . context ( "Failed to set global default subscriber" ) ? ;
276
277
} else {
277
278
tracing:: subscriber:: set_global_default ( fmt_builder. json ( ) . finish ( ) )
278
- . expect ( "Failed to set global default subscriber" ) ;
279
+ . context ( "Failed to set global default subscriber" ) ? ;
279
280
}
280
281
281
282
// Parse the command line arguments with StructOpt, will exit automatically on `--help` or
282
283
// with invalid arguments.
283
284
match Command :: parse ( ) {
284
- Command :: Run ( run_options) => run ( run_options) . await ,
285
+ Command :: Run ( run_options) => run ( run_options) . await ? ,
285
286
Command :: GenerateKey ( opts) => {
286
287
let secp = Secp256k1 :: new ( ) ;
287
288
let mut rng = OsRng ;
288
289
289
290
// Generate keypair (secret + public key)
290
291
let ( secret_key, _) = secp. generate_keypair ( & mut rng) ;
291
292
let signer = signer:: FileSigner { secret_key } ;
292
- let ( pubkey, pubkey_evm) = signer. get_public_key ( ) . expect ( "Failed to get public key" ) ;
293
+ let ( pubkey, pubkey_evm) = signer
294
+ . get_public_key ( )
295
+ . context ( "Failed to get public key" ) ?;
293
296
294
297
let guardian_key = GuardianKey {
295
298
data : secret_key. secret_bytes ( ) . to_vec ( ) ,
@@ -300,24 +303,25 @@ async fn main() {
300
303
Kind :: SecretKey ,
301
304
vec ! [ ( "PublicKey" , format!( "0x{}" , hex:: encode( pubkey_evm) ) ) ] ,
302
305
)
303
- . expect ( "Failed to create writer" ) ;
306
+ . context ( "Failed to create writer" ) ? ;
304
307
writer
305
308
. write_all ( guardian_key. encode_to_vec ( ) . as_slice ( ) )
306
- . expect ( "Failed to write GuardianKey to writer" ) ;
307
- let buffer = writer. finalize ( ) . expect ( "Failed to finalize writer" ) ;
309
+ . context ( "Failed to write GuardianKey to writer" ) ? ;
310
+ let buffer = writer. finalize ( ) . context ( "Failed to finalize writer" ) ? ;
308
311
let armored_string =
309
- String :: from_utf8 ( buffer) . expect ( "Failed to convert buffer to string" ) ;
312
+ String :: from_utf8 ( buffer) . context ( "Failed to convert buffer to string" ) ? ;
310
313
let armored_string =
311
314
armored_string. replace ( STANDARD_ARMOR_LINE_HEADER , GUARDIAN_KEY_ARMORED_BLOCK ) ;
312
315
313
- fs:: write ( & opts. output_path , armored_string)
314
- . expect ( "Failed to write GuardianKey to file" ) ;
316
+ fs:: write ( & opts. output_path , armored_string) . context ( "Failed to write key to file" ) ?;
315
317
316
318
tracing:: info!( "Generated secret key at: {}" , opts. output_path) ;
317
319
tracing:: info!( "Public key: {}" , pubkey) ;
318
320
tracing:: info!( "EVM encoded public key: 0x{}" , hex:: encode( pubkey_evm) ) ;
319
321
}
320
322
}
323
+
324
+ Ok ( ( ) )
321
325
}
322
326
323
327
#[ cfg( test) ]
0 commit comments