1- use crate :: { cli:: SpellProveParams , spell, spell:: Spell , tx, tx:: txs_by_txid, utils} ;
1+ use crate :: {
2+ app, cli,
3+ cli:: { SpellCheckParams , SpellProveParams } ,
4+ spell,
5+ spell:: Spell ,
6+ tx,
7+ tx:: txs_by_txid,
8+ utils, SPELL_VK ,
9+ } ;
210use anyhow:: { ensure, Result } ;
311use bitcoin:: {
412 consensus:: encode:: { deserialize_hex, serialize_hex} ,
@@ -20,7 +28,7 @@ pub fn prove(
2028 utils:: logger:: setup_logger ( ) ;
2129
2230 // Parse funding UTXO early: to fail fast
23- let funding_utxo = crate :: cli:: tx:: parse_outpoint ( & funding_utxo_id) ?;
31+ let funding_utxo = cli:: tx:: parse_outpoint ( & funding_utxo_id) ?;
2432
2533 ensure ! ( fee_rate >= 1.0 , "fee rate must be >= 1.0" ) ;
2634
@@ -30,16 +38,23 @@ pub fn prove(
3038 Some ( tx) => deserialize_hex :: < Transaction > ( & tx) ?,
3139 None => tx:: from_spell ( & spell) ,
3240 } ;
41+ let prev_txs = prev_txs
42+ . into_iter ( )
43+ . map ( |tx| Ok ( deserialize_hex :: < Transaction > ( & tx) ?) )
44+ . collect :: < Result < _ > > ( ) ?;
3345 let prev_txs = txs_by_txid ( prev_txs) ?;
3446 ensure ! ( tx
3547 . input
3648 . iter( )
3749 . all( |input| prev_txs. contains_key( & input. previous_output. txid) ) ) ;
3850
51+ let app_prover = app:: Prover :: new ( ) ;
52+ let binaries = cli:: app:: binaries_by_vk ( & app_prover, app_bins) ?;
53+
3954 let transactions = spell:: prove_spell_tx (
4055 spell,
4156 tx,
42- app_bins ,
57+ binaries ,
4358 prev_txs,
4459 funding_utxo,
4560 funding_utxo_value,
@@ -55,3 +70,52 @@ pub fn prove(
5570
5671 Ok ( ( ) )
5772}
73+
74+ pub fn check ( SpellCheckParams { spell, app_bins } : SpellCheckParams ) -> Result < ( ) > {
75+ utils:: logger:: setup_logger ( ) ;
76+
77+ let mut spell: Spell = serde_yaml:: from_slice ( & std:: fs:: read ( spell) ?) ?;
78+ for u in spell. outs . iter_mut ( ) {
79+ u. sats . get_or_insert ( crate :: cli:: wallet:: MIN_SATS ) ;
80+ }
81+
82+ // make sure spell inputs all have utxo_id
83+ ensure ! (
84+ spell. ins. iter( ) . all( |u| u. utxo_id. is_some( ) ) ,
85+ "all spell inputs must have utxo_id"
86+ ) ;
87+
88+ let tx = tx:: from_spell ( & spell) ;
89+
90+ let prev_txs = cli:: tx:: get_prev_txs ( & tx) ?;
91+
92+ eprintln ! ( "checking prev_txs" ) ;
93+ let prev_spells = charms_client:: prev_spells ( & prev_txs, & SPELL_VK ) ;
94+ eprintln ! ( "checking prev_txs... done!" ) ;
95+
96+ let ( norm_spell, app_private_inputs) = spell. normalized ( ) ?;
97+ let norm_spell = spell:: align_spell_to_tx ( norm_spell, & tx) ?;
98+
99+ eprintln ! ( "checking spell is well-formed" ) ;
100+ ensure ! (
101+ charms_client:: well_formed( & norm_spell, & prev_spells) ,
102+ "spell is not well-formed"
103+ ) ;
104+ eprintln ! ( "checking spell is well-formed... done!" ) ;
105+
106+ eprintln ! ( "checking spell is correct" ) ;
107+ let app_prover = app:: Prover :: new ( ) ;
108+
109+ let binaries = cli:: app:: binaries_by_vk ( & app_prover, app_bins) ?;
110+
111+ let charms_tx = spell. to_tx ( ) ?;
112+ app_prover. run_all (
113+ & binaries,
114+ & charms_tx,
115+ & norm_spell. app_public_inputs ,
116+ app_private_inputs,
117+ ) ?;
118+ eprintln ! ( "checking spell is correct... done!" ) ;
119+
120+ Ok ( ( ) )
121+ }
0 commit comments