1+ mod basic;
12mod commands;
3+ mod helper;
24
35use std:: path:: PathBuf ;
46
57use clap:: Parser ;
68
79use crate :: config:: { Config , default_config_path} ;
810use crate :: error:: Error ;
9- use crate :: wallet:: Wallet ;
1011
11- pub use commands:: { Command , HelperCommand , MakerCommand , TakerCommand } ;
12+ use crate :: wallet:: Wallet ;
13+ pub use commands:: { BasicCommand , Command , HelperCommand , MakerCommand , TakerCommand } ;
1214
1315#[ derive( Debug , Parser ) ]
1416#[ command( name = "simplicity-dex" ) ]
@@ -43,11 +45,18 @@ impl Cli {
4345 . map_err ( |_| Error :: Config ( "Seed must be exactly 32 bytes (64 hex chars)" . to_string ( ) ) )
4446 }
4547
48+ async fn get_wallet ( & self , config : & Config ) -> Result < Wallet , Error > {
49+ let seed = self . parse_seed ( ) ?;
50+ let db_path = config. database_path ( ) ;
51+
52+ Wallet :: open ( & seed, & db_path, config. address_params ( ) ) . await
53+ }
54+
4655 pub async fn run ( & self ) -> Result < ( ) , Error > {
4756 let config = self . load_config ( ) ;
4857
4958 match & self . command {
50- Command :: Basic { command : _ } => todo ! ( ) ,
59+ Command :: Basic { command } => self . run_basic ( config , command ) . await ,
5160 Command :: Maker { command : _ } => todo ! ( ) ,
5261 Command :: Taker { command : _ } => todo ! ( ) ,
5362 Command :: Helper { command } => self . run_helper ( config, command) . await ,
@@ -57,113 +66,4 @@ impl Cli {
5766 }
5867 }
5968 }
60-
61- async fn run_helper ( & self , config : Config , command : & HelperCommand ) -> Result < ( ) , Error > {
62- match command {
63- HelperCommand :: Init => {
64- let seed = self . parse_seed ( ) ?;
65- let db_path = config. database_path ( ) ;
66-
67- std:: fs:: create_dir_all ( & config. storage . data_dir ) ?;
68- Wallet :: create ( & seed, & db_path, config. address_params ( ) ) . await ?;
69-
70- println ! ( "Wallet initialized at {}" , db_path. display( ) ) ;
71- Ok ( ( ) )
72- }
73- HelperCommand :: Address => {
74- let seed = self . parse_seed ( ) ?;
75- let db_path = config. database_path ( ) ;
76- let wallet = Wallet :: open ( & seed, & db_path, config. address_params ( ) ) . await ?;
77-
78- wallet. signer ( ) . print_details ( ) ?;
79-
80- Ok ( ( ) )
81- }
82- HelperCommand :: Balance => {
83- let seed = self . parse_seed ( ) ?;
84- let db_path = config. database_path ( ) ;
85- let wallet = Wallet :: open ( & seed, & db_path, config. address_params ( ) ) . await ?;
86-
87- let filter = coin_store:: Filter :: new ( )
88- . script_pubkey ( wallet. signer ( ) . p2pk_address ( config. address_params ( ) ) ?. script_pubkey ( ) ) ;
89- let results = wallet. store ( ) . query ( & [ filter] ) . await ?;
90-
91- let mut balances: std:: collections:: HashMap < simplicityhl:: elements:: AssetId , u64 > =
92- std:: collections:: HashMap :: new ( ) ;
93-
94- if let Some ( coin_store:: QueryResult :: Found ( entries) ) = results. into_iter ( ) . next ( ) {
95- for entry in entries {
96- let ( asset, value) = match entry {
97- coin_store:: UtxoEntry :: Confidential { secrets, .. } => ( secrets. asset , secrets. value ) ,
98- coin_store:: UtxoEntry :: Explicit { txout, .. } => {
99- let asset = txout. asset . explicit ( ) . unwrap ( ) ;
100- let value = txout. value . explicit ( ) . unwrap ( ) ;
101- ( asset, value)
102- }
103- } ;
104- * balances. entry ( asset) . or_insert ( 0 ) += value;
105- }
106- }
107-
108- if balances. is_empty ( ) {
109- println ! ( "No UTXOs found" ) ;
110- } else {
111- for ( asset, value) in & balances {
112- println ! ( "{asset}: {value}" ) ;
113- }
114- }
115- Ok ( ( ) )
116- }
117- HelperCommand :: Utxos => {
118- let seed = self . parse_seed ( ) ?;
119- let db_path = config. database_path ( ) ;
120- let wallet = Wallet :: open ( & seed, & db_path, config. address_params ( ) ) . await ?;
121-
122- let filter = coin_store:: Filter :: new ( ) ;
123- let results = wallet. store ( ) . query ( & [ filter] ) . await ?;
124-
125- if let Some ( coin_store:: QueryResult :: Found ( entries) ) = results. into_iter ( ) . next ( ) {
126- for entry in & entries {
127- let outpoint = entry. outpoint ( ) ;
128- let ( asset, value) = match entry {
129- coin_store:: UtxoEntry :: Confidential { secrets, .. } => ( secrets. asset , secrets. value ) ,
130- coin_store:: UtxoEntry :: Explicit { txout, .. } => {
131- let asset = txout. asset . explicit ( ) . unwrap ( ) ;
132- let value = txout. value . explicit ( ) . unwrap ( ) ;
133- ( asset, value)
134- }
135- } ;
136- println ! ( "{outpoint} | {asset} | {value}" ) ;
137- }
138- println ! ( "Total: {} UTXOs" , entries. len( ) ) ;
139- } else {
140- println ! ( "No UTXOs found" ) ;
141- }
142- Ok ( ( ) )
143- }
144- HelperCommand :: Import { outpoint, blinding_key } => {
145- let seed = self . parse_seed ( ) ?;
146- let db_path = config. database_path ( ) ;
147- let wallet = Wallet :: open ( & seed, & db_path, config. address_params ( ) ) . await ?;
148-
149- let txout = cli_helper:: explorer:: fetch_utxo ( * outpoint) . await ?;
150-
151- let blinder = match blinding_key {
152- Some ( key_hex) => {
153- let bytes: [ u8 ; 32 ] = hex:: decode ( key_hex)
154- . map_err ( |e| Error :: Config ( format ! ( "Invalid blinding key hex: {e}" ) ) ) ?
155- . try_into ( )
156- . map_err ( |_| Error :: Config ( "Blinding key must be 32 bytes" . to_string ( ) ) ) ?;
157- Some ( bytes)
158- }
159- None => None ,
160- } ;
161-
162- wallet. store ( ) . insert ( * outpoint, txout, blinder) . await ?;
163-
164- println ! ( "Imported {outpoint}" ) ;
165- Ok ( ( ) )
166- }
167- }
168- }
16969}
0 commit comments