neo-cli 1.0.0

Command-line interface for the NeoRust SDK
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
#![allow(dead_code)]
use crate::{
	errors::CliError,
	utils_core::{
		create_table, display_key_value, print_info, print_section_header, print_success,
		print_warning, prompt_input, prompt_password, prompt_yes_no, status_indicator,
		with_loading,
	},
};
use bip39::Language;
use clap::{Args, Subcommand};
use comfy_table::{Cell, Color};
use hex;
use neo3::{
	neo_clients::{APITrait, HttpProvider, RpcClient},
	neo_protocol::{Account, AccountTrait},
	neo_types::AddressExtension,
	neo_wallets::{Wallet, WalletBackup, WalletTrait},
	NeoVMStateType,
};
use std::path::PathBuf;

pub struct CliState {
	pub wallet: Option<Wallet>,
	pub wallet_path: Option<PathBuf>,
	// Cached password for the current CLI session only.
	pub wallet_password: Option<String>,
	pub rpc_client: Option<RpcClient<HttpProvider>>,
	pub network_type: Option<String>,
	pub current_network: Option<crate::commands::network::NetworkConfig>,
	pub networks: Vec<crate::commands::network::NetworkConfig>,
}

impl Default for CliState {
	fn default() -> Self {
		Self {
			wallet: None,
			wallet_path: None,
			wallet_password: None,
			rpc_client: None,
			network_type: None,
			current_network: None,
			networks: Vec::new(),
		}
	}
}

impl CliState {
	pub fn get_network_type_string(&self) -> String {
		self.network_type.clone().unwrap_or_else(|| "testnet".to_string())
	}

	pub fn set_network_type(&mut self, network: String) {
		self.network_type = Some(network);
	}

	pub fn get_rpc_client(&self) -> Result<&RpcClient<HttpProvider>, CliError> {
		self.rpc_client.as_ref().ok_or_else(|| {
			CliError::Config("No RPC client configured. Use 'network connect' first.".to_string())
		})
	}

	pub fn get_account(&self) -> Result<Account, CliError> {
		let wallet = self
			.wallet
			.as_ref()
			.ok_or_else(|| CliError::Wallet("No wallet open. Open a wallet first.".to_string()))?;

		if let Some(default_account) = wallet.default_account() {
			return Ok(default_account.clone());
		}

		wallet
			.accounts()
			.into_iter()
			.next()
			.ok_or_else(|| CliError::Wallet("Wallet has no accounts.".to_string()))
	}
}

#[derive(Args, Debug)]
pub struct WalletArgs {
	#[command(subcommand)]
	pub command: WalletCommands,
}

#[derive(Subcommand, Debug)]
pub enum WalletCommands {
	/// Create a new wallet
	#[command(about = "Create a new Neo wallet")]
	Create {
		/// Path to save the wallet
		#[arg(short, long, help = "Path to save the wallet file")]
		path: Option<PathBuf>,

		/// Wallet name
		#[arg(short, long, help = "Name for the wallet")]
		name: Option<String>,

		/// Password for the wallet (if not provided, will prompt)
		#[arg(long, help = "Password for the wallet")]
		password: Option<String>,
	},

	/// Open an existing wallet
	#[command(about = "Open an existing wallet")]
	Open {
		/// Path to the wallet file
		#[arg(short, long, help = "Path to the wallet file")]
		path: PathBuf,

		/// Password for the wallet (if not provided, will prompt)
		#[arg(long, help = "Password for the wallet")]
		password: Option<String>,
	},

	/// Close the current wallet
	#[command(about = "Close the currently open wallet")]
	Close,

	/// List addresses in the wallet
	#[command(about = "List all addresses in the wallet")]
	List,

	/// Show wallet information
	#[command(about = "Show detailed wallet information")]
	Info,

	/// Create a new address in the wallet
	#[command(about = "Create new addresses in the wallet")]
	CreateAddress {
		/// Number of addresses to create
		#[arg(short, long, default_value = "1", help = "Number of addresses to create")]
		count: u16,

		/// Label for the address
		#[arg(short, long, help = "Label for the new address")]
		label: Option<String>,
	},

	/// Import a private key
	#[command(about = "Import a private key into the wallet")]
	Import {
		/// WIF string or path to a file containing WIF keys
		#[arg(short, long, help = "WIF private key or file path")]
		wif_or_file: String,

		/// Label for the imported account
		#[arg(short, long, help = "Label for the imported account")]
		label: Option<String>,
	},

	/// Export private keys
	#[command(about = "Export private keys from the wallet")]
	Export {
		/// Path to save the exported keys
		#[arg(short, long, help = "Path to save exported keys")]
		path: Option<PathBuf>,

		/// Address to export (if not specified, exports all)
		#[arg(short, long, help = "Specific address to export")]
		address: Option<String>,

		/// Export format (wif, json, csv)
		#[arg(short, long, default_value = "wif", help = "Export format")]
		format: String,
	},

	/// Show unclaimed GAS
	#[command(about = "Show unclaimed GAS for wallet addresses")]
	Gas {
		/// Address to check (if not provided, checks all addresses)
		#[arg(short, long, help = "Specific address to check")]
		address: Option<String>,
	},

	/// Change wallet password
	#[command(about = "Change the wallet password")]
	Password,

	/// Transfer assets to another address
	#[command(about = "Transfer assets to another address")]
	Send {
		/// Asset ID (NEO, GAS, or script hash)
		#[arg(short, long, help = "Asset to transfer (NEO, GAS, or token hash)")]
		asset: String,

		/// Recipient address
		#[arg(short, long, help = "Recipient address")]
		to: String,

		/// Amount to transfer
		#[arg(short, long, help = "Amount to transfer")]
		amount: String,

		/// Sender address (if not specified, uses the first account)
		#[arg(short, long, help = "Sender address")]
		from: Option<String>,

		/// Transaction fee
		#[arg(short, long, help = "Network fee for the transaction")]
		fee: Option<String>,
	},

	/// Show wallet balance
	#[command(about = "Show wallet balance")]
	Balance {
		/// Address to show balance for (if not provided, shows all addresses)
		#[arg(short, long, help = "Specific address to check")]
		address: Option<String>,

		/// Only show this token (NEO, GAS, or script hash)
		#[arg(short, long, help = "Specific token to display")]
		token: Option<String>,

		/// Show detailed balance information
		#[arg(short, long, help = "Show detailed balance information")]
		detailed: bool,
	},

	/// Backup wallet
	#[command(about = "Create a backup of the wallet")]
	Backup {
		/// Path to save the backup
		#[arg(short, long, help = "Path to save the backup")]
		path: PathBuf,
	},

	/// Restore wallet from backup
	#[command(about = "Restore wallet from backup")]
	Restore {
		/// Path to the backup file
		#[arg(short, long, help = "Path to the backup file")]
		path: PathBuf,
	},

	/// Create HD wallet with mnemonic phrase
	#[command(about = "Create or restore HD wallet with BIP-39/44 support")]
	HDWallet {
		/// Create new HD wallet or restore from mnemonic
		#[arg(short, long, help = "Create new HD wallet")]
		create: bool,

		/// Mnemonic phrase for restoration (12/24 words)
		#[arg(short, long, help = "Mnemonic phrase for restoration")]
		mnemonic: Option<String>,

		/// Number of accounts to derive
		#[arg(short, long, default_value = "1", help = "Number of accounts to derive")]
		accounts: u32,

		/// Custom derivation path (BIP-44)
		#[arg(short, long, help = "Custom derivation path")]
		path: Option<String>,

		/// Save wallet to file
		#[arg(short, long, help = "Save HD wallet to file")]
		save: Option<PathBuf>,
	},

	/// Subscribe to blockchain events via WebSocket
	#[command(about = "Subscribe to real-time blockchain events")]
	Subscribe {
		/// WebSocket URL
		#[arg(short, long, help = "WebSocket endpoint URL")]
		url: Option<String>,

		/// Event types to subscribe (block, transaction, notification, etc.)
		#[arg(short, long, help = "Event types to subscribe")]
		events: Vec<String>,

		/// Filter by contract hash
		#[arg(short, long, help = "Filter by contract hash")]
		contract: Option<String>,
	},

	/// Simulate transaction before sending
	#[command(about = "Simulate transaction for gas estimation")]
	Simulate {
		/// Transaction script (base64 or hex)
		#[arg(short, long, help = "Transaction script to simulate")]
		script: String,

		/// Signers for the transaction
		#[arg(short, long, help = "Transaction signers")]
		signers: Vec<String>,

		/// Show detailed simulation results
		#[arg(short, long, help = "Show detailed results")]
		detailed: bool,
	},
}

/// Handle wallet command with comprehensive functionality
pub async fn handle_wallet_command(args: WalletArgs, state: &mut CliState) -> Result<(), CliError> {
	match args.command {
		WalletCommands::Create { path, name, password } => {
			handle_create_wallet(path, name, password, state).await
		},
		WalletCommands::Open { path, password } => handle_open_wallet(path, password, state).await,
		WalletCommands::Close => handle_close_wallet(state).await,
		WalletCommands::List => handle_list_addresses(state).await,
		WalletCommands::Info => handle_wallet_info(state).await,
		WalletCommands::CreateAddress { count, label } => {
			handle_create_address(count, label, state).await
		},
		WalletCommands::Import { wif_or_file, label } => {
			handle_import_key(wif_or_file, label, state).await
		},
		WalletCommands::Export { path, address, format } => {
			handle_export_key(path, address, format, state).await
		},
		WalletCommands::Gas { address } => handle_show_gas(address, state).await,
		WalletCommands::Password => handle_change_password(state).await,
		WalletCommands::Send { asset, to, amount, from, fee } => {
			handle_transfer(asset, to, amount, from, fee, state).await
		},
		WalletCommands::Balance { address, token, detailed } => {
			handle_balance(address, token, detailed, state).await
		},
		WalletCommands::Backup { path } => handle_backup_wallet(path, state).await,
		WalletCommands::Restore { path } => handle_restore_wallet(path, state).await,
		WalletCommands::HDWallet { create, mnemonic, accounts, path, save } => {
			handle_hd_wallet(create, mnemonic, accounts, path, save, state).await
		},
		WalletCommands::Subscribe { url, events, contract } => {
			handle_websocket_subscribe(url, events, contract, state).await
		},
		WalletCommands::Simulate { script, signers, detailed } => {
			handle_transaction_simulation(script, signers, detailed, state).await
		},
	}
}

/// Create a new wallet
async fn handle_create_wallet(
	path: Option<PathBuf>,
	name: Option<String>,
	password: Option<String>,
	state: &mut CliState,
) -> Result<(), CliError> {
	print_section_header("Creating New Wallet");

	let wallet_name = name.unwrap_or_else(|| {
		prompt_input("Enter wallet name").unwrap_or_else(|_| "MyWallet".to_string())
	});

	let wallet_path = path.unwrap_or_else(|| {
		PathBuf::from(format!("{}.json", wallet_name.to_lowercase().replace(" ", "_")))
	});

	if wallet_path.exists() {
		let overwrite = prompt_yes_no(&format!(
			"Wallet file '{}' already exists. Overwrite?",
			wallet_path.display()
		))
		.map_err(|e| CliError::Io(e))?;

		if !overwrite {
			print_warning("Wallet creation cancelled");
			return Ok(());
		}
	}

	let password = match password {
		Some(pwd) => pwd,
		None => {
			let pwd = prompt_password("Enter password for the new wallet")
				.map_err(|e| CliError::Io(e))?;
			let confirm_password =
				prompt_password("Confirm password").map_err(|e| CliError::Io(e))?;

			if pwd != confirm_password {
				return Err(CliError::Wallet("Passwords do not match".to_string()));
			}
			pwd
		},
	};

	let wallet = with_loading("Creating wallet...", async {
		let mut wallet = Wallet::new();
		wallet.name = wallet_name.clone();
		wallet.encrypt_accounts(&password);
		wallet
			.save_to_file(wallet_path.clone())
			.map_err(|e| CliError::Wallet(format!("Failed to save wallet: {e}")))?;
		Ok::<Wallet, CliError>(wallet)
	})
	.await?;

	state.wallet = Some(wallet);
	state.wallet_path = Some(wallet_path.clone());
	state.wallet_password = Some(password);

	let mut table = create_table();
	table.add_row(vec![
		Cell::new("Wallet Name").fg(Color::Cyan),
		Cell::new(&wallet_name).fg(Color::Green),
	]);
	table.add_row(vec![
		Cell::new("File Path").fg(Color::Cyan),
		Cell::new(wallet_path.display().to_string()).fg(Color::Green),
	]);
	table.add_row(vec![
		Cell::new("Status").fg(Color::Cyan),
		Cell::new("Created Successfully").fg(Color::Green),
	]);

	println!("{table}");
	print_info("💡 Use 'neo-cli wallet create-address' to create your first address");

	Ok(())
}

/// Open an existing wallet
async fn handle_open_wallet(
	path: PathBuf,
	password: Option<String>,
	state: &mut CliState,
) -> Result<(), CliError> {
	print_section_header("Opening Wallet");

	if !path.exists() {
		return Err(CliError::Wallet(format!("Wallet file not found: {}", path.display())));
	}

	let password = match password {
		Some(pwd) => pwd,
		None => prompt_password("Enter wallet password").map_err(|e| CliError::Io(e))?,
	};

	let wallet = with_loading("Opening wallet...", async {
		Wallet::open_wallet(path.as_path(), &password)
			.map_err(|e| CliError::Wallet(format!("Failed to open wallet: {e}")))
	})
	.await?;

	state.wallet = Some(wallet);
	state.wallet_path = Some(path.clone());
	state.wallet_password = Some(password);

	display_key_value("Wallet Path", &path.display().to_string());
	display_key_value("Status", "Opened Successfully");

	if let Some(wallet) = &state.wallet {
		display_key_value("Wallet Name", wallet.name());
		display_key_value("Accounts", &wallet.accounts.len().to_string());
	}

	Ok(())
}

/// Close the current wallet
async fn handle_close_wallet(state: &mut CliState) -> Result<(), CliError> {
	if state.wallet.is_none() {
		print_warning("No wallet is currently open");
		return Ok(());
	}

	state.wallet = None;
	state.wallet_path = None;
	state.wallet_password = None;
	print_success("🔒 Wallet closed successfully");
	Ok(())
}

/// List all addresses in the wallet
async fn handle_list_addresses(state: &CliState) -> Result<(), CliError> {
	let wallet = state
		.wallet
		.as_ref()
		.ok_or_else(|| CliError::Wallet("No wallet open".to_string()))?;

	print_section_header("Wallet Addresses");

	if wallet.accounts.is_empty() {
		print_warning("No addresses found in wallet");
		print_info("💡 Use 'neo-cli wallet create-address' to create an address");
		return Ok(());
	}

	let mut table = create_table();
	table.set_header(vec![
		Cell::new("#").fg(Color::Cyan),
		Cell::new("Address").fg(Color::Cyan),
		Cell::new("Label").fg(Color::Cyan),
		Cell::new("Default").fg(Color::Cyan),
		Cell::new("Status").fg(Color::Cyan),
	]);

	let mut accounts: Vec<Account> = wallet.accounts();
	accounts.sort_by_key(|a| a.get_address());

	for (index, account) in accounts.iter().enumerate() {
		let label = account.label.clone().unwrap_or_else(|| "-".to_string());
		let default_mark = if account.is_default { "Yes" } else { "" };
		let status = if account.encrypted_private_key().is_some() {
			format!("{} Encrypted", status_indicator("success"))
		} else {
			format!("{} Unencrypted", status_indicator("warning"))
		};
		table.add_row(vec![
			Cell::new((index + 1).to_string()).fg(Color::Yellow),
			Cell::new(account.get_address()).fg(Color::Green),
			Cell::new(label).fg(Color::Blue),
			Cell::new(default_mark).fg(Color::Cyan),
			Cell::new(status).fg(Color::Green),
		]);
	}

	println!("{table}");
	print_info(&format!("Total addresses: {}", wallet.accounts.len()));

	Ok(())
}

/// Show detailed wallet information
async fn handle_wallet_info(state: &CliState) -> Result<(), CliError> {
	let wallet = state
		.wallet
		.as_ref()
		.ok_or_else(|| CliError::Wallet("No wallet open".to_string()))?;

	print_section_header("Wallet Information");

	let mut table = create_table();
	table.add_row(vec![
		Cell::new("File Path").fg(Color::Cyan),
		Cell::new(
			state
				.wallet_path
				.as_ref()
				.map(|p| p.display().to_string())
				.unwrap_or_else(|| "Not saved".to_string()),
		)
		.fg(Color::Green),
	]);
	table.add_row(vec![
		Cell::new("Total Accounts").fg(Color::Cyan),
		Cell::new(wallet.accounts.len().to_string()).fg(Color::Green),
	]);
	table.add_row(vec![
		Cell::new("Wallet Name").fg(Color::Cyan),
		Cell::new(wallet.name()).fg(Color::Green),
	]);
	table.add_row(vec![
		Cell::new("Version").fg(Color::Cyan),
		Cell::new(wallet.version()).fg(Color::Green),
	]);
	table.add_row(vec![
		Cell::new("Network").fg(Color::Cyan),
		Cell::new(state.get_network_type_string()).fg(Color::Green),
	]);
	table.add_row(vec![
		Cell::new("Status").fg(Color::Cyan),
		Cell::new(format!("{} Open", status_indicator("success"))).fg(Color::Green),
	]);

	println!("{table}");

	Ok(())
}

async fn handle_create_address(
	count: u16,
	label: Option<String>,
	state: &mut CliState,
) -> Result<(), CliError> {
	let wallet = state.wallet.as_mut().ok_or_else(|| {
		CliError::WalletNotLoaded("No wallet open. Use 'wallet open' first.".into())
	})?;
	let wallet_path = state.wallet_path.clone().ok_or_else(|| {
		CliError::Config("Wallet path unknown. Re-open the wallet with 'wallet open'.".into())
	})?;

	let password = match state.wallet_password.clone() {
		Some(pwd) => pwd,
		None => prompt_password("Enter wallet password")?,
	};

	print_section_header("Creating Address");

	let mut created_addresses = Vec::new();
	for _ in 0..count {
		let (script_hash, address) = {
			let account = wallet
				.create_new_account()
				.map_err(|e| CliError::WalletOperation(format!("Failed to create account: {e}")))?;
			(account.get_script_hash(), account.get_address())
		};

		if let Some(label) = label.as_ref() {
			if let Some(acc) = wallet.accounts.get_mut(&script_hash) {
				acc.label = Some(label.clone());
			}
		}

		created_addresses.push(address);
	}

	// Ensure all private keys are encrypted before saving.
	wallet.encrypt_accounts(&password);
	wallet
		.save_to_file(wallet_path)
		.map_err(|e| CliError::WalletOperation(format!("Failed to save wallet: {e}")))?;

	state.wallet_password = Some(password);

	let mut table = create_table();
	table.set_header(vec![Cell::new("#").fg(Color::Cyan), Cell::new("Address").fg(Color::Cyan)]);
	for (i, addr) in created_addresses.iter().enumerate() {
		table.add_row(vec![Cell::new((i + 1).to_string()).fg(Color::Yellow), Cell::new(addr)]);
	}
	println!("{table}");

	print_success(&format!("✅ Created {} address(es)", created_addresses.len()));
	Ok(())
}

async fn handle_import_key(
	wif_or_file: String,
	label: Option<String>,
	state: &mut CliState,
) -> Result<(), CliError> {
	let wallet = state.wallet.as_mut().ok_or_else(|| {
		CliError::WalletNotLoaded("No wallet open. Use 'wallet open' first.".into())
	})?;
	let wallet_path = state.wallet_path.clone().ok_or_else(|| {
		CliError::Config("Wallet path unknown. Re-open the wallet with 'wallet open'.".into())
	})?;

	let password = match state.wallet_password.clone() {
		Some(pwd) => pwd,
		None => prompt_password("Enter wallet password")?,
	};

	print_section_header("Import Private Key");

	let candidate_path = PathBuf::from(&wif_or_file);
	let wifs: Vec<String> = if candidate_path.exists() {
		let content = std::fs::read_to_string(&candidate_path)
			.map_err(|e| CliError::FileSystem(format!("Failed to read file: {e}")))?;
		content
			.lines()
			.map(|l| l.trim())
			.filter(|l| !l.is_empty() && !l.starts_with('#'))
			.map(|l| l.to_string())
			.collect()
	} else {
		vec![wif_or_file]
	};

	if wifs.is_empty() {
		return Err(CliError::InvalidInput("No WIF keys provided".to_string()));
	}

	let mut imported = Vec::new();
	for wif in wifs {
		let mut account =
			Account::from_wif(&wif).map_err(|e| CliError::Wallet(format!("Invalid WIF: {e}")))?;
		if let Some(label) = label.as_ref() {
			account.label = Some(label.clone());
		}

		let script_hash = account.get_script_hash();
		if wallet.accounts.contains_key(&script_hash) {
			print_warning(&format!("Account already exists: {}", account.get_address()));
			continue;
		}

		let address = account.get_address();
		wallet.add_account(account);
		imported.push(address);
	}

	if imported.is_empty() {
		print_warning("No new accounts imported");
		return Ok(());
	}

	// Encrypt imported private keys and persist the wallet.
	wallet.encrypt_accounts(&password);
	wallet
		.save_to_file(wallet_path)
		.map_err(|e| CliError::WalletOperation(format!("Failed to save wallet: {e}")))?;

	state.wallet_password = Some(password);

	let mut table = create_table();
	table.set_header(vec![Cell::new("#").fg(Color::Cyan), Cell::new("Address").fg(Color::Cyan)]);
	for (i, addr) in imported.iter().enumerate() {
		table.add_row(vec![Cell::new((i + 1).to_string()).fg(Color::Yellow), Cell::new(addr)]);
	}
	println!("{table}");
	print_success(&format!("✅ Imported {} account(s)", imported.len()));
	Ok(())
}

async fn handle_export_key(
	path: Option<PathBuf>,
	address: Option<String>,
	format: String,
	state: &CliState,
) -> Result<(), CliError> {
	let wallet = state.wallet.as_ref().ok_or_else(|| {
		CliError::WalletNotLoaded("No wallet open. Use 'wallet open' first.".into())
	})?;

	let format = format.to_lowercase();
	if !matches!(format.as_str(), "wif" | "json" | "csv") {
		return Err(CliError::InvalidArgument(
			"format".to_string(),
			"Supported formats: wif, json, csv".to_string(),
		));
	}

	let mut accounts: Vec<Account> = wallet.accounts();
	if let Some(target) = address.as_deref() {
		accounts.retain(|a| a.get_address() == target);
		if accounts.is_empty() {
			return Err(CliError::Wallet(format!("Account not found in wallet: {target}")));
		}
	}

	if accounts.is_empty() {
		return Err(CliError::Wallet("Wallet has no accounts.".to_string()));
	}

	print_warning("⚠️  Exporting private keys is sensitive and unsafe if mishandled.");
	print_warning("   Do not share exported files. Store them securely and offline.");
	if !prompt_yes_no("Proceed with exporting private keys?")? {
		return Err(CliError::UserCancelled("Export cancelled by user".to_string()));
	}

	let password = match state.wallet_password.clone() {
		Some(pwd) => pwd,
		None => prompt_password("Enter wallet password")?,
	};

	let mut exported: Vec<(String, String)> = Vec::new();
	for account in accounts {
		let mut account_clone = account.clone();
		if account_clone.key_pair().is_none() {
			if account_clone.encrypted_private_key().is_none() {
				print_warning(&format!(
					"Skipping watch-only account (no private key): {}",
					account_clone.get_address()
				));
				continue;
			}
			account_clone.decrypt_private_key(&password).map_err(|e| {
				CliError::WalletOperation(format!(
					"Failed to decrypt account {}: {e}",
					account_clone.get_address()
				))
			})?;
		}

		let key_pair = account_clone
			.key_pair()
			.as_ref()
			.ok_or_else(|| CliError::Wallet("No key pair available after decryption".to_string()))?
			.clone();
		let wif = key_pair.export_as_wif();
		exported.push((account_clone.get_address(), wif));
	}

	if exported.is_empty() {
		return Err(CliError::Wallet("No private keys available to export.".to_string()));
	}

	// Determine destination
	let output = match format.as_str() {
		"json" => serde_json::to_string_pretty(
			&exported
				.iter()
				.map(|(address, wif)| serde_json::json!({ "address": address, "wif": wif }))
				.collect::<Vec<_>>(),
		)?,
		"csv" => {
			let mut out = String::from("address,wif\n");
			for (address, wif) in &exported {
				out.push_str(&format!("{address},{wif}\n"));
			}
			out
		},
		_ => {
			// wif
			let mut out = String::new();
			for (address, wif) in &exported {
				out.push_str(&format!("{address}\t{wif}\n"));
			}
			out
		},
	};

	if let Some(path) = path {
		use std::fs::OpenOptions;
		use std::io::Write;

		let mut file = OpenOptions::new()
			.create(true)
			.truncate(true)
			.write(true)
			.open(&path)
			.map_err(|e| CliError::FileSystem(format!("Failed to create export file: {e}")))?;

		file.write_all(output.as_bytes())
			.map_err(|e| CliError::FileSystem(format!("Failed to write export file: {e}")))?;

		#[cfg(unix)]
		{
			use std::os::unix::fs::PermissionsExt;
			let perm = std::fs::Permissions::from_mode(0o600);
			let _ = std::fs::set_permissions(&path, perm);
		}

		print_success(&format!("✅ Exported {} key(s) to {}", exported.len(), path.display()));
		return Ok(());
	}

	// No file path provided: only allow printing when exporting a single account.
	if exported.len() != 1 {
		return Err(CliError::InvalidArgument(
			"path".to_string(),
			"Refusing to print multiple private keys to stdout. Use --path to export to a file."
				.to_string(),
		));
	}

	print_warning(
		"⚠️  Printing private keys to the terminal can leak secrets via shell history/logs.",
	);
	if !prompt_yes_no("Print the private key to stdout?")? {
		return Err(CliError::UserCancelled("Export cancelled by user".to_string()));
	}

	let (addr, wif) = &exported[0];
	println!("Address: {addr}");
	println!("WIF: {wif}");
	Ok(())
}

async fn handle_show_gas(address: Option<String>, state: &CliState) -> Result<(), CliError> {
	let wallet = state.wallet.as_ref().ok_or_else(|| {
		CliError::WalletNotLoaded("No wallet open. Use 'wallet open' first.".into())
	})?;
	let rpc_client = state.get_rpc_client()?;

	print_section_header("Unclaimed GAS");

	let mut accounts: Vec<Account> = wallet.accounts();
	if let Some(addr) = address.as_deref() {
		accounts.retain(|a| a.get_address() == addr);
		if accounts.is_empty() {
			return Err(CliError::Wallet(format!("Account not found in wallet: {addr}")));
		}
	}

	let mut table = create_table();
	table.set_header(vec![
		Cell::new("Address").fg(Color::Cyan),
		Cell::new("Unclaimed GAS").fg(Color::Cyan),
	]);

	for account in accounts {
		let script_hash = account.get_script_hash();
		let gas = rpc_client
			.get_unclaimed_gas(script_hash)
			.await
			.map_err(|e| CliError::Network(format!("Failed to query unclaimed GAS: {e}")))?;

		table.add_row(vec![Cell::new(account.get_address()), Cell::new(gas.unclaimed)]);
	}

	println!("{table}");
	Ok(())
}

async fn handle_change_password(state: &mut CliState) -> Result<(), CliError> {
	let wallet = state.wallet.as_mut().ok_or_else(|| {
		CliError::WalletNotLoaded("No wallet open. Use 'wallet open' first.".into())
	})?;
	let wallet_path = state.wallet_path.clone().ok_or_else(|| {
		CliError::Config("Wallet path unknown. Re-open the wallet with 'wallet open'.".into())
	})?;

	print_section_header("Change Wallet Password");

	let current_password = prompt_password("Enter current wallet password")?;
	if !wallet.verify_password(&current_password) {
		return Err(CliError::Authentication("Invalid current password".to_string()));
	}

	let new_password = prompt_password("Enter new wallet password")?;
	let confirm_password = prompt_password("Confirm new wallet password")?;
	if new_password != confirm_password {
		return Err(CliError::Wallet("Passwords do not match".to_string()));
	}

	wallet
		.change_password(&current_password, &new_password)
		.map_err(|e| CliError::WalletOperation(format!("Failed to change password: {e}")))?;
	wallet
		.save_to_file(wallet_path)
		.map_err(|e| CliError::WalletOperation(format!("Failed to save wallet: {e}")))?;

	state.wallet_password = Some(new_password);
	print_success("🔐 Password changed successfully");
	Ok(())
}

async fn handle_transfer(
	asset: String,
	to: String,
	amount: String,
	from: Option<String>,
	fee: Option<String>,
	state: &mut CliState,
) -> Result<(), CliError> {
	if fee.is_some() {
		return Err(CliError::NotImplemented(
			"Custom fees are not supported by this command yet.".to_string(),
		));
	}

	let original_default = state
		.wallet
		.as_ref()
		.and_then(|w| w.default_account().map(|a| a.get_script_hash()));

	if let Some(from_address) = from.as_deref() {
		let from_hash = from_address.address_to_script_hash().map_err(|e| {
			CliError::InvalidInput(format!("Invalid sender address '{from_address}': {e}"))
		})?;

		let wallet = state.wallet.as_mut().ok_or_else(|| {
			CliError::WalletNotLoaded("No wallet open. Use 'wallet open' first.".into())
		})?;
		if !wallet.accounts.contains_key(&from_hash) {
			return Err(CliError::Wallet(format!(
				"Sender address not found in wallet: {from_address}"
			)));
		}

		wallet.set_default_account(from_hash);
	}

	let result = crate::commands::defi::tokens::transfer_token(&asset, &to, &amount, state).await;

	if let Some(hash) = original_default {
		if let Some(wallet) = state.wallet.as_mut() {
			wallet.set_default_account(hash);
		}
	}

	result
}

async fn handle_balance(
	address: Option<String>,
	token: Option<String>,
	detailed: bool,
	state: &CliState,
) -> Result<(), CliError> {
	let rpc_client = state.get_rpc_client()?;

	print_section_header("Wallet Balances");

	let mut addresses: Vec<String> = if let Some(addr) = address {
		vec![addr]
	} else {
		let wallet = state.wallet.as_ref().ok_or_else(|| {
			CliError::WalletNotLoaded("No wallet open. Use 'wallet open' first.".into())
		})?;
		wallet.accounts().into_iter().map(|a| a.get_address()).collect()
	};

	if addresses.is_empty() {
		return Err(CliError::Wallet("Wallet has no accounts.".to_string()));
	}

	addresses.sort();
	addresses.dedup();

	for address in addresses {
		let script_hash = address
			.address_to_script_hash()
			.map_err(|e| CliError::InvalidInput(format!("Invalid address '{address}': {e}")))?;

		let balances = rpc_client
			.get_nep17_balances(script_hash)
			.await
			.map_err(|e| CliError::Network(format!("Failed to query balances: {e}")))?;

		println!();
		print_info(&format!("Address: {}", balances.address));

		let token_filter = token.as_deref().map(|t| t.trim().to_string());
		let token_filter_upper = token_filter.as_deref().map(|t| t.to_uppercase());
		let token_filter_hex =
			token_filter.as_deref().map(|t| t.trim_start_matches("0x").to_lowercase());

		let mut table = create_table();
		if detailed {
			table.set_header(vec![
				Cell::new("Symbol").fg(Color::Cyan),
				Cell::new("Amount").fg(Color::Cyan),
				Cell::new("Decimals").fg(Color::Cyan),
				Cell::new("Asset Hash").fg(Color::Cyan),
				Cell::new("Updated").fg(Color::Cyan),
			]);
		} else {
			table.set_header(vec![
				Cell::new("Symbol").fg(Color::Cyan),
				Cell::new("Amount").fg(Color::Cyan),
			]);
		}

		let mut rows = 0usize;
		for bal in balances.balances {
			let symbol = bal.symbol.clone().unwrap_or_else(|| "<unknown>".to_string());
			let decimals_str = bal.decimals.clone().unwrap_or_else(|| "0".to_string());
			let decimals = decimals_str.parse::<u8>().unwrap_or(0);
			let amount =
				neo3::sdk::DecimalAmount::from_raw(bal.amount.clone(), decimals).to_fixed_string();

			if let Some(filter_upper) = token_filter_upper.as_deref() {
				let asset_hex = format!("{:x}", bal.asset_hash);
				let filter_hex = token_filter_hex.as_deref().unwrap_or("");
				let matches = symbol.eq_ignore_ascii_case(filter_upper)
					|| asset_hex.eq_ignore_ascii_case(filter_hex);
				if !matches {
					continue;
				}
			}

			rows += 1;
			if detailed {
				table.add_row(vec![
					Cell::new(symbol),
					Cell::new(amount),
					Cell::new(decimals_str),
					Cell::new(format!("{:x}", bal.asset_hash)),
					Cell::new(bal.last_updated_block.to_string()),
				]);
			} else {
				table.add_row(vec![Cell::new(symbol), Cell::new(amount)]);
			}
		}

		if rows == 0 {
			print_warning("No balances found (or filtered out).");
		} else {
			println!("{table}");
		}
	}

	Ok(())
}

async fn handle_backup_wallet(path: PathBuf, state: &CliState) -> Result<(), CliError> {
	let wallet = state.wallet.as_ref().ok_or_else(|| {
		CliError::WalletNotLoaded(
			"No wallet is currently loaded. Use 'wallet open' first.".to_string(),
		)
	})?;

	// Create backup using WalletBackup
	match WalletBackup::backup(wallet, path.clone()) {
		Ok(_) => {
			println!("✅ Wallet backup created successfully!");
			println!("📁 Backup saved to: {}", path.display());
			println!("🔐 Backup contains {} accounts", wallet.accounts.len());
			println!("\n⚠️  Security reminders:");
			println!("   • Store this backup in a secure location");
			println!("   • Keep multiple copies in different locations");
			println!("   • Never share your backup file");
			println!("   • Remember your wallet password - it's required for recovery");
			Ok(())
		},
		Err(e) => Err(CliError::WalletOperation(format!("Failed to create backup: {}", e))),
	}
}

async fn handle_restore_wallet(path: PathBuf, state: &mut CliState) -> Result<(), CliError> {
	// Check if backup file exists
	if !path.exists() {
		return Err(CliError::InvalidOperation(format!(
			"Backup file not found: {}",
			path.display()
		)));
	}

	// Warn if a wallet is already loaded
	if state.wallet.is_some() {
		println!(
			"⚠️  Warning: A wallet is already loaded. Restoring will replace the current wallet."
		);
		print!("Continue? (y/N): ");
		use std::io::Write;
		std::io::stdout().flush().map_err(CliError::IoError)?;

		let mut input = String::new();
		std::io::stdin().read_line(&mut input).map_err(CliError::IoError)?;

		if !input.trim().to_lowercase().starts_with('y') {
			println!("Restore cancelled.");
			return Ok(());
		}
	}

	// Backups are valid NEP-6 wallets; restore by opening the backup file with a password.
	let password = prompt_password("Enter wallet password")?;
	let wallet = Wallet::open_wallet(path.as_path(), &password)
		.map_err(|e| CliError::WalletOperation(format!("Failed to open backup wallet: {e}")))?;

	println!("✅ Wallet restored successfully!");
	println!("📁 Loaded from: {}", path.display());
	println!("🏷️  Wallet name: {}", wallet.name());
	println!("🔐 Accounts loaded: {}", wallet.accounts.len());

	// Display account addresses
	println!("\n📋 Accounts:");
	let mut accounts: Vec<Account> = wallet.accounts();
	accounts.sort_by_key(|a| a.get_address());
	for (i, account) in accounts.iter().enumerate() {
		println!("   {}. {}", i + 1, account.get_address());
	}

	state.wallet = Some(wallet);
	state.wallet_path = Some(path);
	state.wallet_password = Some(password);

	Ok(())
}

// HD Wallet implementation
async fn handle_hd_wallet(
	create: bool,
	mnemonic: Option<String>,
	accounts: u32,
	derivation_path: Option<String>,
	save_path: Option<PathBuf>,
	_state: &mut CliState,
) -> Result<(), CliError> {
	print_section_header("HD Wallet Management");

	// Import HD wallet functionality from the SDK
	use neo3::sdk::hd_wallet::HDWallet;

	let mut hd_wallet = if create {
		print_info("🔑 Creating new HD wallet with BIP-39 mnemonic...");

		// Create new HD wallet
		let wallet = HDWallet::generate(24, None)
			.map_err(|e| CliError::WalletOperation(format!("Failed to create HD wallet: {}", e)))?;

		print_success(&format!("✅ HD Wallet created successfully!"));
		print_warning("⚠️ IMPORTANT: Save your mnemonic phrase securely!");
		println!();
		println!("Mnemonic phrase (24 words):");
		println!("{}", "".repeat(60));
		println!("{}", wallet.mnemonic_phrase());
		println!("{}", "".repeat(60));
		println!();

		wallet
	} else if let Some(phrase) = mnemonic {
		print_info("📥 Restoring HD wallet from mnemonic phrase...");

		HDWallet::from_phrase(&phrase, None, Language::English)
			.map_err(|e| CliError::WalletOperation(format!("Failed to restore HD wallet: {}", e)))?
	} else {
		return Err(CliError::WalletOperation(
			"Please specify --create or provide --mnemonic phrase".to_string(),
		));
	};

	// Derive accounts
	print_info(&format!("🔄 Deriving {} account(s)...", accounts));
	let mut table = create_table();

	for i in 0..accounts {
		let path = derivation_path.clone().unwrap_or_else(|| format!("m/44'/888'/0'/0/{}", i));
		match hd_wallet.derive_account(&path) {
			Ok(account) => {
				let pk_str = account
					.get_public_key()
					.map(|pk| pk.to_string())
					.unwrap_or_else(|| "n/a".to_string());
				table.add_row(vec![
					Cell::new(i),
					Cell::new(account.get_address()),
					Cell::new(format!("{}...", &pk_str[..pk_str.len().min(8)])),
				]);
			},
			Err(e) => {
				print_warning(&format!("Failed to derive account {}: {}", i, e));
			},
		}
	}

	println!("{}", table);

	// Save if requested
	if let Some(save_path) = save_path {
		print_info(&format!("💾 Saving HD wallet to {}...", save_path.display()));

		let wallet_data = serde_json::json!({
			"type": "HD",
			"mnemonic": hd_wallet.mnemonic_phrase(),
			"accounts": accounts,
			"derivation_path": derivation_path.unwrap_or_else(|| "m/44'/888'/0'/0/0".to_string()),
		});

		std::fs::write(&save_path, serde_json::to_string_pretty(&wallet_data)?)
			.map_err(|e| CliError::FileSystem(format!("Failed to save wallet: {}", e)))?;

		print_success(&format!("✅ HD wallet saved to {}", save_path.display()));
	}

	Ok(())
}

// WebSocket subscription implementation
async fn handle_websocket_subscribe(
	url: Option<String>,
	events: Vec<String>,
	contract: Option<String>,
	state: &mut CliState,
) -> Result<(), CliError> {
	print_section_header("WebSocket Event Subscription");

	use neo3::sdk::websocket::{SubscriptionType, WebSocketClient};

	let ws_url = url.unwrap_or_else(|| {
		if let Some(network) = &state.current_network {
			network.rpc_url.replace("http", "ws").replace("https", "wss")
		} else {
			"wss://testnet1.neo.coz.io:443/ws".to_string()
		}
	});

	print_info(&format!("🔌 Connecting to WebSocket: {}", ws_url));

	let mut client = WebSocketClient::new(&ws_url)
		.await
		.map_err(|e| CliError::Network(format!("Failed to connect to WebSocket: {}", e)))?;
	client
		.connect()
		.await
		.map_err(|e| CliError::Network(format!("WS connect error: {}", e)))?;

	// Subscribe to requested events
	if events.is_empty() || events.contains(&"block".to_string()) {
		client
			.subscribe(SubscriptionType::NewBlocks)
			.await
			.map_err(|e| CliError::Network(format!("Failed to subscribe to blocks: {}", e)))?;
		print_success("✅ Subscribed to block events");
	}

	if events.contains(&"transaction".to_string()) {
		client.subscribe(SubscriptionType::NewTransactions).await.map_err(|e| {
			CliError::Network(format!("Failed to subscribe to transactions: {}", e))
		})?;
		print_success("✅ Subscribed to transaction events");
	}

	if let Some(contract_hash) = contract {
		let hash = contract_hash
			.parse()
			.map_err(|e| CliError::InvalidInput(format!("Invalid contract hash: {}", e)))?;
		client
			.subscribe(SubscriptionType::Notifications { contract: Some(hash), name: None })
			.await
			.map_err(|e| CliError::Network(format!("Failed to subscribe to contract: {}", e)))?;
		print_success(&format!("✅ Subscribed to contract {} notifications", contract_hash));
	}

	print_info("📡 Listening for events (press Ctrl+C to stop)...");
	println!();

	// Listen for events
	if let Some(mut rx) = client.take_event_receiver() {
		while let Some((event_type, data)) = rx.recv().await {
			println!("📨 Event {:?}: {}", event_type, serde_json::to_string(&data)?);
		}
	}

	Ok(())
}

// Transaction simulation implementation
async fn handle_transaction_simulation(
	script: String,
	_signers: Vec<String>,
	_detailed: bool,
	state: &mut CliState,
) -> Result<(), CliError> {
	print_section_header("Transaction Simulation");

	use neo3::sdk::transaction_simulator::TransactionSimulator;

	let rpc_client = state.get_rpc_client()?;

	// Create simulator
	let mut simulator = TransactionSimulator::new(std::sync::Arc::new(rpc_client.clone()));

	// Parse script (hex or base64)
	let script_bytes = if script.starts_with("0x") {
		hex::decode(&script[2..])
			.map_err(|e| CliError::InvalidInput(format!("Invalid hex script: {}", e)))?
	} else {
		use base64::{engine::general_purpose::STANDARD, Engine as _};
		STANDARD
			.decode(script.as_bytes())
			.map_err(|e| CliError::InvalidInput(format!("Invalid base64 script: {}", e)))?
	};

	// Parse signers
	let signer_list = Vec::new();

	print_info("🔍 Simulating transaction...");

	// Run simulation
	match simulator.simulate_transaction(&script_bytes, signer_list).await {
		Ok(result) => {
			print_success("✅ Simulation completed successfully!");
			println!();

			// Display results
			let mut table = create_table();

			table.add_row(vec![
				Cell::new("Execution State"),
				Cell::new(if result.vm_state == NeoVMStateType::Halt {
					"SUCCESS"
				} else {
					"FAILED"
				})
				.fg(if result.vm_state == NeoVMStateType::Halt {
					Color::Green
				} else {
					Color::Red
				}),
			]);

			table.add_row(vec![
				Cell::new("GAS Consumed"),
				Cell::new(format!("{} GAS", result.gas_consumed)),
			]);

			table.add_row(vec![
				Cell::new("System Fee"),
				Cell::new(format!("{} GAS", result.system_fee)),
			]);

			table.add_row(vec![
				Cell::new("Network Fee"),
				Cell::new(format!("{} GAS", result.network_fee)),
			]);

			table.add_row(vec![
				Cell::new("Total Fee"),
				Cell::new(format!("{} GAS", result.total_fee)),
			]);

			if !result.notifications.is_empty() {
				table.add_row(vec![
					Cell::new("Notifications"),
					Cell::new(format!("{} events", result.notifications.len())),
				]);
			}

			println!("{}", table);

			// Detailed notifications are already printed above
		},
		Err(e) => {
			print_warning(&format!("❌ Simulation failed: {}", e));
			return Err(CliError::WalletOperation(format!("Transaction simulation failed: {}", e)));
		},
	}

	Ok(())
}