quantus-cli 1.3.4

Command line interface and library for interacting with the Quantus Network
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
use crate::{
	chain::quantus_subxt, cli::common::resolve_address, log_error, log_print, log_success,
};
use clap::Subcommand;
// no colored output needed here
use sp_core::crypto::{AccountId32 as SpAccountId32, Ss58Codec};

// Base unit (QUAN) decimals for amount conversions
const QUAN_DECIMALS: u128 = 1_000_000_000_000; // 10^12

/// Recovery-related commands
#[derive(Subcommand, Debug)]
pub enum RecoveryCommands {
	/// Initiate recovery (rescuer starts)
	Initiate {
		/// Rescuer wallet name
		#[arg(long)]
		rescuer: String,
		/// Lost account (SS58 or wallet name)
		#[arg(long)]
		lost: String,
		/// Password for rescuer wallet
		#[arg(short, long)]
		password: Option<String>,
		/// Read password from file (for scripting)
		#[arg(long)]
		password_file: Option<String>,
	},

	/// Vouch for a recovery attempt (friend)
	Vouch {
		/// Friend wallet name (who vouches)
		#[arg(long)]
		friend: String,
		/// Lost account (SS58 or wallet name)
		#[arg(long)]
		lost: String,
		/// Rescuer account (SS58 or wallet name)
		#[arg(long)]
		rescuer: String,
		/// Password for friend wallet
		#[arg(short, long)]
		password: Option<String>,
		/// Read password from file
		#[arg(long)]
		password_file: Option<String>,
	},

	/// Claim recovery (rescuer claims after threshold and delay)
	Claim {
		/// Rescuer wallet name
		#[arg(long)]
		rescuer: String,
		/// Lost account (SS58 or wallet name)
		#[arg(long)]
		lost: String,
		/// Password for rescuer wallet
		#[arg(short, long)]
		password: Option<String>,
		/// Read password from file
		#[arg(long)]
		password_file: Option<String>,
	},

	/// Close an active recovery (lost account stops a malicious attempt)
	Close {
		/// Lost wallet name (the recoverable account)
		#[arg(long)]
		lost: String,
		/// Rescuer account (SS58 or wallet name)
		#[arg(long)]
		rescuer: String,
		/// Password for lost wallet
		#[arg(short, long)]
		password: Option<String>,
		/// Read password from file
		#[arg(long)]
		password_file: Option<String>,
	},

	/// Cancel recovered proxy (rescuer disables their own proxy)
	CancelProxy {
		/// Rescuer wallet name
		#[arg(long)]
		rescuer: String,
		/// Lost account (SS58 or wallet name)
		#[arg(long)]
		lost: String,
		/// Password for rescuer wallet
		#[arg(short, long)]
		password: Option<String>,
		/// Read password from file
		#[arg(long)]
		password_file: Option<String>,
	},

	/// Query: active recovery info
	Active {
		/// Lost account (SS58 or wallet name)
		#[arg(long)]
		lost: String,
		/// Rescuer account (SS58 or wallet name)
		#[arg(long)]
		rescuer: String,
	},

	/// Query: proxy-of (rescuer -> lost)
	ProxyOf {
		/// Rescuer account (SS58 or wallet name)
		#[arg(long)]
		rescuer: String,
	},

	/// Query: recovery config (recoverable)
	Config {
		/// Account to query (SS58 or wallet name)
		#[arg(long)]
		account: String,
	},

	/// Recover all funds from the lost account to a destination
	RecoverAll {
		/// Rescuer wallet name
		#[arg(long)]
		rescuer: String,
		/// Lost account (SS58 or wallet name)
		#[arg(long)]
		lost: String,
		/// Destination to receive the recovered funds
		#[arg(long)]
		dest: String,
		/// Keep the lost account alive
		#[arg(long, default_value_t = true)]
		keep_alive: bool,
		/// Password for rescuer wallet
		#[arg(short, long)]
		password: Option<String>,
		/// Read password from file
		#[arg(long)]
		password_file: Option<String>,
	},

	/// Recover a specific amount (in QUAN units) from the lost account to destination
	RecoverAmount {
		/// Rescuer wallet name
		#[arg(long)]
		rescuer: String,
		/// Lost account (SS58 or wallet name)
		#[arg(long)]
		lost: String,
		/// Destination to receive the recovered funds
		#[arg(long)]
		dest: String,
		/// Amount in QUAN (human units) - multiplied by chain decimals
		#[arg(long, value_name = "AMOUNT_QUAN")]
		amount_quan: u128,
		/// Keep the lost account alive
		#[arg(long, default_value_t = true)]
		keep_alive: bool,
		/// Password for rescuer wallet
		#[arg(short, long)]
		password: Option<String>,
		/// Read password from file
		#[arg(long)]
		password_file: Option<String>,
	},
}

pub async fn handle_recovery_command(
	command: RecoveryCommands,
	node_url: &str,
	execution_mode: crate::cli::common::ExecutionMode,
) -> crate::error::Result<()> {
	let quantus_client = crate::chain::client::QuantusClient::new(node_url).await?;

	match command {
		RecoveryCommands::Initiate { rescuer, lost, password, password_file } => {
			let rescuer_key =
				crate::wallet::load_keypair_from_wallet(&rescuer, password, password_file)?;
			let rescuer_addr = rescuer_key.to_account_id_ss58check();
			log_print!("🔑 Rescuer: {}", rescuer);
			log_print!("🔑 Rescuer address: {}", rescuer_addr);
			let lost_resolved = resolve_address(&lost)?;
			let lost_id_sp = SpAccountId32::from_ss58check(&lost_resolved).map_err(|e| {
				crate::error::QuantusError::Generic(format!("Invalid lost address: {e:?}"))
			})?;
			let lost_id_bytes: [u8; 32] = *lost_id_sp.as_ref();
			let lost_id = subxt::ext::subxt_core::utils::AccountId32::from(lost_id_bytes);
			let call = quantus_subxt::api::tx()
				.recovery()
				.initiate_recovery(subxt::ext::subxt_core::utils::MultiAddress::Id(lost_id));

			let tx_hash = crate::cli::common::submit_transaction(
				&quantus_client,
				&rescuer_key,
				call,
				None,
				execution_mode,
			)
			.await
			.map_err(|e| {
				crate::error::QuantusError::NetworkError(format!(
					"Failed to submit initiate_recovery transaction: {e}"
				))
			})?;
			log_success!("✅ Initiate recovery submitted successfully {:?}", tx_hash);
		},

		RecoveryCommands::Vouch { friend, lost, rescuer, password, password_file } => {
			let friend_key =
				crate::wallet::load_keypair_from_wallet(&friend, password, password_file)?;
			let lost_resolved = resolve_address(&lost)?;
			let rescuer_resolved = resolve_address(&rescuer)?;
			let lost_sp = SpAccountId32::from_ss58check(&lost_resolved).map_err(|e| {
				crate::error::QuantusError::Generic(format!("Invalid lost address: {e:?}"))
			})?;
			let lost_bytes: [u8; 32] = *lost_sp.as_ref();
			let lost_id = subxt::ext::subxt_core::utils::AccountId32::from(lost_bytes);
			let rescuer_sp = SpAccountId32::from_ss58check(&rescuer_resolved).map_err(|e| {
				crate::error::QuantusError::Generic(format!("Invalid rescuer address: {e:?}"))
			})?;
			let rescuer_bytes: [u8; 32] = *rescuer_sp.as_ref();
			let rescuer_id = subxt::ext::subxt_core::utils::AccountId32::from(rescuer_bytes);
			let call = quantus_subxt::api::tx().recovery().vouch_recovery(
				subxt::ext::subxt_core::utils::MultiAddress::Id(lost_id),
				subxt::ext::subxt_core::utils::MultiAddress::Id(rescuer_id),
			);
			let tx_hash = crate::cli::common::submit_transaction(
				&quantus_client,
				&friend_key,
				call,
				None,
				execution_mode,
			)
			.await
			.map_err(|e| {
				crate::error::QuantusError::NetworkError(format!(
					"Failed to submit vouch_recovery transaction: {e}"
				))
			})?;
			log_success!("✅ Vouch submitted successfully {:?}", tx_hash);
		},

		RecoveryCommands::Claim { rescuer, lost, password, password_file } => {
			let rescuer_key =
				crate::wallet::load_keypair_from_wallet(&rescuer, password, password_file)?;
			let lost_resolved = resolve_address(&lost)?;
			let lost_sp = SpAccountId32::from_ss58check(&lost_resolved).map_err(|e| {
				crate::error::QuantusError::Generic(format!("Invalid lost address: {e:?}"))
			})?;
			let lost_bytes: [u8; 32] = *lost_sp.as_ref();
			let lost_id = subxt::ext::subxt_core::utils::AccountId32::from(lost_bytes);
			let call = quantus_subxt::api::tx()
				.recovery()
				.claim_recovery(subxt::ext::subxt_core::utils::MultiAddress::Id(lost_id));
			let tx_hash = crate::cli::common::submit_transaction(
				&quantus_client,
				&rescuer_key,
				call,
				None,
				execution_mode,
			)
			.await
			.map_err(|e| {
				crate::error::QuantusError::NetworkError(format!(
					"Failed to submit claim_recovery transaction: {e}"
				))
			})?;

			log_success!("✅ Claim submitted successfully {:?}", tx_hash);
		},

		RecoveryCommands::RecoverAll {
			rescuer,
			lost,
			dest,
			keep_alive,
			password,
			password_file,
		} => {
			use quantus_subxt::api::runtime_types::pallet_balances::pallet::Call as BalancesCall;

			let rescuer_key =
				crate::wallet::load_keypair_from_wallet(&rescuer, password, password_file)?;
			let rescuer_addr = rescuer_key.to_account_id_ss58check();
			log_print!("🔑 Rescuer: {}", rescuer);
			log_print!("🔑 Rescuer address: {}", rescuer_addr);

			let lost_resolved = resolve_address(&lost)?;
			let dest_resolved = resolve_address(&dest)?;
			log_print!("🆘 Lost input: {} -> {}", lost, lost_resolved);
			log_print!("🎯 Dest input: {} -> {}", dest, dest_resolved);
			log_print!("🛟 keep_alive: {}", keep_alive);

			let lost_sp = SpAccountId32::from_ss58check(&lost_resolved).map_err(|e| {
				crate::error::QuantusError::Generic(format!("Invalid lost address: {e:?}"))
			})?;
			let dest_sp = SpAccountId32::from_ss58check(&dest_resolved).map_err(|e| {
				crate::error::QuantusError::Generic(format!("Invalid dest address: {e:?}"))
			})?;

			let lost_id_bytes: [u8; 32] = *lost_sp.as_ref();
			let dest_id_bytes: [u8; 32] = *dest_sp.as_ref();
			let lost_id = subxt::ext::subxt_core::utils::AccountId32::from(lost_id_bytes);
			let dest_id = subxt::ext::subxt_core::utils::AccountId32::from(dest_id_bytes);

			// Check proxy mapping for rescuer
			let rescuer_sp = SpAccountId32::from_ss58check(&rescuer_addr).map_err(|e| {
				crate::error::QuantusError::Generic(format!(
					"Invalid rescuer address from wallet: {e:?}"
				))
			})?;
			let rescuer_id_bytes: [u8; 32] = *rescuer_sp.as_ref();
			let rescuer_id = subxt::ext::subxt_core::utils::AccountId32::from(rescuer_id_bytes);
			let proxy_storage = quantus_subxt::api::storage().recovery().proxy(rescuer_id);
			let latest = quantus_client.get_latest_block().await?;
			let proxy_result =
				quantus_client.client().storage().at(latest).fetch(&proxy_storage).await;
			let proxy_of = match proxy_result {
				Ok(Some(proxy)) => {
					let proxy_bytes: &[u8; 32] = proxy.as_ref();
					let proxy_sp = SpAccountId32::from(*proxy_bytes);
					log_print!("🧩 Proxy mapping: rescuer proxies -> {}", proxy_sp.to_ss58check());
					Some(proxy)
				},
				Ok(None) => {
					log_error!(
						"❌ No proxy mapping found for rescuer - recovery not set up properly"
					);
					return Err(crate::error::QuantusError::Generic(
						"Rescuer has no proxy mapping. Recovery process may not be properly set up."
							.to_string(),
					));
				},
				Err(e) => {
					log_error!("❌ Proxy mapping fetch error: {:?}", e);
					return Err(crate::error::QuantusError::NetworkError(format!(
						"Failed to check proxy mapping: {e:?}"
					)));
				},
			};

			// Validate that the proxy points to the correct lost account
			if let Some(proxy) = proxy_of {
				let proxy_bytes: &[u8; 32] = proxy.as_ref();
				let proxy_sp = SpAccountId32::from(*proxy_bytes);
				let proxy_addr = proxy_sp.to_ss58check();
				if proxy_addr != lost_resolved {
					log_error!(
						"❌ Proxy mismatch! Rescuer proxies {} but we're trying to recover {}",
						proxy_addr,
						lost_resolved
					);
					return Err(crate::error::QuantusError::Generic(format!(
						"Proxy mismatch: rescuer proxies {proxy_addr} but target is {lost_resolved}"
					)));
				}
				log_print!("✅ Proxy validation successful");
			}

			let inner_call = quantus_subxt::api::Call::Balances(BalancesCall::transfer_all {
				dest: subxt::ext::subxt_core::utils::MultiAddress::Id(dest_id),
				keep_alive,
			});
			log_print!("🧱 Inner call: Balances.transfer_all(keep_alive={})", keep_alive);

			let call = quantus_subxt::api::tx()
				.recovery()
				.as_recovered(subxt::ext::subxt_core::utils::MultiAddress::Id(lost_id), inner_call);

			let tx_hash = match crate::cli::common::submit_transaction(
				&quantus_client,
				&rescuer_key,
				call,
				None,
				execution_mode,
			)
			.await
			{
				Ok(h) => h,
				Err(e) => {
					log_error!("❌ Submit error (recover_all): {:?}", e);
					return Err(e);
				},
			};
			log_success!("✅ recover_all submitted successfully {:?}", tx_hash);
		},

		RecoveryCommands::RecoverAmount {
			rescuer,
			lost,
			dest,
			amount_quan,
			keep_alive,
			password,
			password_file,
		} => {
			use quantus_subxt::api::runtime_types::pallet_balances::pallet::Call as BalancesCall;

			let rescuer_key =
				crate::wallet::load_keypair_from_wallet(&rescuer, password, password_file)?;

			let rescuer_addr = rescuer_key.to_account_id_ss58check();
			log_print!("🔑 Rescuer: {}", rescuer);
			log_print!("🔑 Rescuer address: {}", rescuer_addr);

			let lost_resolved = resolve_address(&lost)?;
			let dest_resolved = resolve_address(&dest)?;
			log_print!("🆘 Lost input: {} -> {}", lost, lost_resolved);
			log_print!("🎯 Dest input: {} -> {}", dest, dest_resolved);
			log_print!("💵 amount_quan: {} (QUAN_DECIMALS={})", amount_quan, QUAN_DECIMALS);
			log_print!("🛟 keep_alive: {}", keep_alive);

			let lost_sp = SpAccountId32::from_ss58check(&lost_resolved).map_err(|e| {
				crate::error::QuantusError::Generic(format!("Invalid lost address: {e:?}"))
			})?;
			let dest_sp = SpAccountId32::from_ss58check(&dest_resolved).map_err(|e| {
				crate::error::QuantusError::Generic(format!("Invalid dest address: {e:?}"))
			})?;

			let lost_id_bytes: [u8; 32] = *lost_sp.as_ref();
			let dest_id_bytes: [u8; 32] = *dest_sp.as_ref();
			let lost_id = subxt::ext::subxt_core::utils::AccountId32::from(lost_id_bytes);
			let dest_id = subxt::ext::subxt_core::utils::AccountId32::from(dest_id_bytes);

			let amount_plancks = amount_quan.saturating_mul(QUAN_DECIMALS);
			log_print!("💵 amount_plancks: {}", amount_plancks);

			let latest = quantus_client.get_latest_block().await?;

			// Check account balance before attempting transfer
			log_print!("💰 Checking lost account balance...");
			let balance_result = quantus_client
				.client()
				.storage()
				.at(latest)
				.fetch(&quantus_subxt::api::storage().system().account(lost_id.clone()))
				.await;

			let account_info = match balance_result {
				Ok(Some(info)) => info,
				Ok(None) => {
					log_error!("❌ Lost account not found in storage");
					return Err(crate::error::QuantusError::Generic(
						"Lost account not found in storage".to_string(),
					));
				},
				Err(e) => {
					log_error!("❌ Failed to fetch account balance: {:?}", e);
					return Err(crate::error::QuantusError::NetworkError(format!(
						"Failed to fetch account balance: {e:?}"
					)));
				},
			};

			let available_balance = account_info.data.free;
			log_print!("💰 Available balance: {} plancks", available_balance);

			if available_balance < amount_plancks {
				log_error!(
					"❌ Insufficient funds! Account has {} plancks but needs {} plancks",
					available_balance,
					amount_plancks
				);
				return Err(crate::error::QuantusError::Generic(format!(
					"Insufficient funds: account has {available_balance} plancks but transfer requires {amount_plancks} plancks"
				)));
			}

			log_print!("✅ Balance validation successful - sufficient funds available");

			let inner_call =
				quantus_subxt::api::Call::Balances(BalancesCall::transfer_keep_alive {
					dest: subxt::ext::subxt_core::utils::MultiAddress::Id(dest_id),
					value: amount_plancks,
				});

			let call = quantus_subxt::api::tx()
				.recovery()
				.as_recovered(subxt::ext::subxt_core::utils::MultiAddress::Id(lost_id), inner_call);

			let tx_hash = match crate::cli::common::submit_transaction(
				&quantus_client,
				&rescuer_key,
				call,
				None,
				execution_mode,
			)
			.await
			{
				Ok(h) => h,
				Err(e) => {
					log_error!("❌ Submit error (recover_amount): {:?}", e);
					return Err(e);
				},
			};
			log_success!("✅ recover_amount submitted successfully {:?}", tx_hash);
		},

		RecoveryCommands::Close { lost, rescuer, password, password_file } => {
			let lost_key = crate::wallet::load_keypair_from_wallet(&lost, password, password_file)?;
			let rescuer_resolved = resolve_address(&rescuer)?;
			let rescuer_sp = SpAccountId32::from_ss58check(&rescuer_resolved).map_err(|e| {
				crate::error::QuantusError::Generic(format!("Invalid rescuer address: {e:?}"))
			})?;
			let rescuer_bytes: [u8; 32] = *rescuer_sp.as_ref();
			let rescuer_id = subxt::ext::subxt_core::utils::AccountId32::from(rescuer_bytes);
			let call = quantus_subxt::api::tx()
				.recovery()
				.close_recovery(subxt::ext::subxt_core::utils::MultiAddress::Id(rescuer_id));
			let tx_hash = crate::cli::common::submit_transaction(
				&quantus_client,
				&lost_key,
				call,
				None,
				execution_mode,
			)
			.await
			.map_err(|e| {
				crate::error::QuantusError::NetworkError(format!(
					"Failed to submit close_recovery transaction: {e}"
				))
			})?;

			log_print!("📋 Transaction submitted: 0x{}", hex::encode(tx_hash.as_ref()));
			log_success!("✅ close_recovery submitted successfully");
		},

		RecoveryCommands::CancelProxy { rescuer, lost, password, password_file } => {
			let rescuer_key =
				crate::wallet::load_keypair_from_wallet(&rescuer, password, password_file)?;
			let lost_resolved = resolve_address(&lost)?;
			let lost_sp = SpAccountId32::from_ss58check(&lost_resolved).map_err(|e| {
				crate::error::QuantusError::Generic(format!("Invalid lost address: {e:?}"))
			})?;
			let lost_bytes: [u8; 32] = *lost_sp.as_ref();
			let lost_id = subxt::ext::subxt_core::utils::AccountId32::from(lost_bytes);
			let call = quantus_subxt::api::tx()
				.recovery()
				.cancel_recovered(subxt::ext::subxt_core::utils::MultiAddress::Id(lost_id));
			let tx_hash = crate::cli::common::submit_transaction(
				&quantus_client,
				&rescuer_key,
				call,
				None,
				execution_mode,
			)
			.await
			.map_err(|e| {
				crate::error::QuantusError::NetworkError(format!(
					"Failed to submit cancel_recovered transaction: {e}"
				))
			})?;

			log_success!("✅ cancel_recovered submitted successfully {:?}", tx_hash);
		},

		RecoveryCommands::Active { lost, rescuer } => {
			let lost_resolved = resolve_address(&lost)?;
			let rescuer_resolved = resolve_address(&rescuer)?;
			let lost_sp = SpAccountId32::from_ss58check(&lost_resolved).map_err(|e| {
				crate::error::QuantusError::Generic(format!("Invalid lost address: {e:?}"))
			})?;
			let lost_bytes: [u8; 32] = *lost_sp.as_ref();
			let lost_id = subxt::ext::subxt_core::utils::AccountId32::from(lost_bytes);
			let rescuer_sp = SpAccountId32::from_ss58check(&rescuer_resolved).map_err(|e| {
				crate::error::QuantusError::Generic(format!("Invalid rescuer address: {e:?}"))
			})?;
			let rescuer_bytes: [u8; 32] = *rescuer_sp.as_ref();
			let rescuer_id = subxt::ext::subxt_core::utils::AccountId32::from(rescuer_bytes);
			let storage_addr =
				quantus_subxt::api::storage().recovery().active_recoveries(lost_id, rescuer_id);
			let latest = quantus_client.get_latest_block().await?;
			let value = quantus_client
				.client()
				.storage()
				.at(latest)
				.fetch(&storage_addr)
				.await
				.map_err(|e| {
					crate::error::QuantusError::NetworkError(format!("Fetch error: {e:?}"))
				})?;
			if let Some(active) = value {
				log_print!(
					"{}",
					serde_json::json!({
						"created": active.created,
						"deposit": active.deposit,
						"friends_vouched": active.friends.0.len(),
					})
				);
			} else {
				log_print!("{}", serde_json::json!({"active": false}));
			}
		},

		RecoveryCommands::ProxyOf { rescuer } => {
			let rescuer_resolved = resolve_address(&rescuer)?;
			let rescuer_sp = SpAccountId32::from_ss58check(&rescuer_resolved).map_err(|e| {
				crate::error::QuantusError::Generic(format!("Invalid rescuer address: {e:?}"))
			})?;
			let rescuer_bytes: [u8; 32] = *rescuer_sp.as_ref();
			let rescuer_id = subxt::ext::subxt_core::utils::AccountId32::from(rescuer_bytes);
			let storage_addr = quantus_subxt::api::storage().recovery().proxy(rescuer_id);
			let latest = quantus_client.get_latest_block().await?;
			let value = quantus_client
				.client()
				.storage()
				.at(latest)
				.fetch(&storage_addr)
				.await
				.map_err(|e| {
					crate::error::QuantusError::NetworkError(format!("Fetch error: {e:?}"))
				})?;
			if let Some(lost_id) = value {
				log_print!("{}", serde_json::json!({"lost": format!("{}", lost_id)}));
			} else {
				log_print!("{}", serde_json::json!({"lost": null}));
			}
		},

		RecoveryCommands::Config { account } => {
			let account_resolved = resolve_address(&account)?;
			let account_sp = SpAccountId32::from_ss58check(&account_resolved).map_err(|e| {
				crate::error::QuantusError::Generic(format!("Invalid account address: {e:?}"))
			})?;
			let account_bytes: [u8; 32] = *account_sp.as_ref();
			let account_id = subxt::ext::subxt_core::utils::AccountId32::from(account_bytes);
			let storage_addr = quantus_subxt::api::storage().recovery().recoverable(account_id);
			let latest = quantus_client.get_latest_block().await?;
			let value = quantus_client
				.client()
				.storage()
				.at(latest)
				.fetch(&storage_addr)
				.await
				.map_err(|e| {
					crate::error::QuantusError::NetworkError(format!("Fetch error: {e:?}"))
				})?;
			if let Some(cfg) = value {
				log_print!(
					"{}",
					serde_json::json!({
						"delay_period": cfg.delay_period,
						"deposit": cfg.deposit,
						"friends": cfg.friends.0.iter().map(|f| format!("{f}")).collect::<Vec<_>>(),
						"threshold": cfg.threshold,
					})
				);
			} else {
				log_print!("{}", serde_json::json!({"recoverable": false}));
			}
		},
	};

	Ok(())
}