@@ -58,7 +58,6 @@ use crate::utils::BlockchainClient::Electrum;
5858#[ cfg( feature = "electrum" ) ]
5959use crate :: utils:: BlockchainClient :: Electrum ;
6060use bdk_wallet:: bitcoin:: base64:: prelude:: * ;
61- use serde_json:: Value ;
6261use std:: collections:: BTreeMap ;
6362#[ cfg( any( feature = "electrum" , feature = "esplora" ) ) ]
6463use std:: collections:: HashSet ;
@@ -1288,9 +1287,8 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
12881287 subcommand : descriptor_subcommand,
12891288 } => {
12901289 let network = cli_opts. network ;
1291- let descriptor = handle_descriptor_subcommand ( network, descriptor_subcommand) ?;
1292- let json = serde_json:: to_string_pretty ( & descriptor) ?;
1293- Ok ( json)
1290+ let descriptor = handle_descriptor_subcommand ( network, descriptor_subcommand, pretty) ?;
1291+ Ok ( descriptor)
12941292 }
12951293 } ;
12961294 result
@@ -1368,43 +1366,45 @@ fn readline() -> Result<String, Error> {
13681366pub fn handle_descriptor_subcommand (
13691367 network : Network ,
13701368 subcommand : DescriptorSubCommand ,
1371- ) -> Result < Value , Error > {
1372- match subcommand {
1369+ pretty : bool ,
1370+ ) -> Result < String , Error > {
1371+ let result = match subcommand {
13731372 DescriptorSubCommand :: Generate {
13741373 r#type,
13751374 multipath,
13761375 key,
13771376 } => {
1378- let descriptor_type = match r#type {
1379- 44 => DescriptorType :: Bip44 ,
1380- 49 => DescriptorType :: Bip49 ,
1381- 84 => DescriptorType :: Bip84 ,
1382- 86 => DescriptorType :: Bip86 ,
1383- _ => {
1384- return Err ( Error :: Generic (
1385- "Unsupported script type: {r#type}" . to_string ( ) ,
1386- ) ) ;
1377+ let descriptor_type = DescriptorType :: from_bip32_num ( r#type)
1378+ . ok_or_else ( || Error :: Generic ( format ! ( "Unsupported script type: {type}" ) ) ) ?;
1379+
1380+ match ( multipath, key) {
1381+ // generate multipath descriptors with a key
1382+ ( true , Some ( key) ) => {
1383+ if is_mnemonic ( & key) {
1384+ return Err ( Error :: Generic (
1385+ "Mnemonic not supported for multipath descriptors" . to_string ( ) ,
1386+ ) ) ;
1387+ }
1388+ generate_descriptors ( descriptor_type, & key, true )
13871389 }
1388- } ;
1389-
1390- match ( multipath, key. as_ref ( ) ) {
1391- // generate multipath descriptors
1392- ( true , Some ( k) ) => generate_descriptors ( & network, descriptor_type, k, true ) ,
1393- ( false , Some ( k) ) => {
1394- if is_mnemonic ( k) {
1395- // generate descriptors from given mnemonic string
1396- generate_descriptor_from_mnemonic_string ( k, network, descriptor_type)
1390+ // generate descriptors with a key or mnemonic
1391+ ( false , Some ( key) ) => {
1392+ if is_mnemonic ( & key) {
1393+ generate_descriptor_from_mnemonic_string ( & key, network, descriptor_type)
13971394 } else {
1398- // generate descriptors from key
1399- generate_descriptors ( & network, descriptor_type, k, false )
1395+ generate_descriptors ( descriptor_type, & key, false )
14001396 }
14011397 }
1402- // generate mnemonic and descriptors
1398+ // Generate new mnemonic and descriptors
14031399 ( false , None ) => generate_new_descriptor_with_mnemonic ( network, descriptor_type) ,
1404- _ => Err ( Error :: Generic ( "Provide a key or string" . to_string ( ) ) ) ,
1400+ // Invalid case
1401+ ( true , None ) => Err ( Error :: Generic (
1402+ "A key is required for multipath descriptors" . to_string ( ) ,
1403+ ) ) ,
14051404 }
14061405 }
1407- }
1406+ } ?;
1407+ format_descriptor_output ( & result, pretty)
14081408}
14091409
14101410#[ cfg( any(
0 commit comments