@@ -3,37 +3,39 @@ use clap::Subcommand;
33use wasm_utxo:: bitcoin:: Script ;
44use wasm_utxo:: { from_output_script_with_network, to_output_script_with_network, Network } ;
55
6+ use crate :: network:: NetworkArg ;
7+
68#[ derive( Subcommand ) ]
79pub enum AddressCommand {
810 /// Decode an address to its output script (hex)
911 Decode {
1012 /// The address to decode
1113 address : String ,
12- /// Network (bitcoin, testnet, litecoin, zcash , etc.)
13- #[ arg( short, long, default_value = "bitcoin" ) ]
14- network : String ,
14+ /// Network (btc, tbtc, ltc, bch, zec , etc.)
15+ #[ arg( short, long, value_enum ) ]
16+ network : NetworkArg ,
1517 } ,
1618 /// Encode an output script (hex) to an address
1719 Encode {
1820 /// Output script as hex
1921 script : String ,
20- /// Network (bitcoin, testnet, litecoin, zcash , etc.)
21- #[ arg( short, long, default_value = "bitcoin" ) ]
22- network : String ,
22+ /// Network (btc, tbtc, ltc, bch, zec , etc.)
23+ #[ arg( short, long, value_enum ) ]
24+ network : NetworkArg ,
2325 } ,
2426}
2527
2628pub fn handle_command ( command : AddressCommand ) -> Result < ( ) > {
2729 match command {
2830 AddressCommand :: Decode { address, network } => {
29- let network = parse_network ( & network) ? ;
31+ let network: Network = network. into ( ) ;
3032 let script = to_output_script_with_network ( & address, network)
3133 . context ( "Failed to decode address" ) ?;
3234 println ! ( "{}" , hex:: encode( script. as_bytes( ) ) ) ;
3335 Ok ( ( ) )
3436 }
3537 AddressCommand :: Encode { script, network } => {
36- let network = parse_network ( & network) ? ;
38+ let network: Network = network. into ( ) ;
3739 let script_bytes =
3840 hex:: decode ( & script) . context ( "Invalid hex string for output script" ) ?;
3941 let script_obj = Script :: from_bytes ( & script_bytes) ;
@@ -44,32 +46,3 @@ pub fn handle_command(command: AddressCommand) -> Result<()> {
4446 }
4547 }
4648}
47-
48- fn parse_network ( network : & str ) -> Result < Network > {
49- // Try utxolib name first (e.g., "bitcoin", "testnet", "bitcoincash")
50- if let Some ( net) = Network :: from_utxolib_name ( network) {
51- return Ok ( net) ;
52- }
53-
54- // Try coin name (e.g., "btc", "ltc", "bch")
55- if let Some ( net) = Network :: from_coin_name ( network) {
56- return Ok ( net) ;
57- }
58-
59- // Try common aliases
60- let normalized = network. to_lowercase ( ) ;
61- match normalized. as_str ( ) {
62- "test" | "testnet3" => Ok ( Network :: BitcoinTestnet3 ) ,
63- "signet" => Ok ( Network :: BitcoinPublicSignet ) ,
64- "ltctest" => Ok ( Network :: LitecoinTestnet ) ,
65- "bchtest" => Ok ( Network :: BitcoinCashTestnet ) ,
66- "bsvtest" => Ok ( Network :: BitcoinSVTestnet ) ,
67- "btgtest" => Ok ( Network :: BitcoinGoldTestnet ) ,
68- "dashtest" => Ok ( Network :: DashTestnet ) ,
69- "zectest" => Ok ( Network :: ZcashTestnet ) ,
70- "dogetest" => Ok ( Network :: DogecoinTestnet ) ,
71- "xec" => Ok ( Network :: Ecash ) ,
72- "xectest" => Ok ( Network :: EcashTestnet ) ,
73- _ => anyhow:: bail!( "Unknown network: {}" , network) ,
74- }
75- }
0 commit comments