@@ -18,6 +18,7 @@ use miniscript::types::extra_props::{
1818 MAX_STANDARD_P2WSH_SCRIPT_SIZE , MAX_STANDARD_P2WSH_STACK_ITEMS ,
1919} ;
2020use std:: fmt;
21+ use util:: { witness_size, witness_to_scriptsig} ;
2122use Error ;
2223use { Miniscript , MiniscriptKey , Terminal } ;
2324/// Error for Script Context
@@ -111,6 +112,18 @@ pub trait ScriptContext:
111112 _frag : & Terminal < Pk , Ctx > ,
112113 ) -> Result < ( ) , ScriptContextError > ;
113114
115+ /// Check whether the given satisfaction is valid under the ScriptContext
116+ /// For example, segwit satisfactions may fail if the witness len is more
117+ /// 3600 or number of stack elements are more than 100.
118+ fn check_witness < Pk : MiniscriptKey , Ctx : ScriptContext > (
119+ _witness : & [ Vec < u8 > ] ,
120+ ) -> Result < ( ) , ScriptContextError > {
121+ // Only really need to do this for segwitv0 and legacy
122+ // Bare is already restrcited by standardness rules
123+ // and would reach these limits.
124+ Ok ( ( ) )
125+ }
126+
114127 /// Depending on script context, the size of a satifaction witness may slightly differ.
115128 fn max_satisfaction_size < Pk : MiniscriptKey , Ctx : ScriptContext > (
116129 ms : & Miniscript < Pk , Ctx > ,
@@ -241,6 +254,17 @@ impl ScriptContext for Legacy {
241254 }
242255 }
243256
257+ fn check_witness < Pk : MiniscriptKey , Ctx : ScriptContext > (
258+ witness : & [ Vec < u8 > ] ,
259+ ) -> Result < ( ) , ScriptContextError > {
260+ // In future, we could avoid by having a function to count only
261+ // len of script instead of converting it.
262+ if witness_to_scriptsig ( witness) . len ( ) > MAX_SCRIPTSIG_SIZE {
263+ return Err ( ScriptContextError :: MaxScriptSigSizeExceeded ) ;
264+ }
265+ Ok ( ( ) )
266+ }
267+
244268 fn check_global_consensus_validity < Pk : MiniscriptKey , Ctx : ScriptContext > (
245269 ms : & Miniscript < Pk , Ctx > ,
246270 ) -> Result < ( ) , ScriptContextError > {
@@ -294,6 +318,17 @@ impl ScriptContext for Segwitv0 {
294318 Ok ( ( ) )
295319 }
296320
321+ fn check_witness < Pk : MiniscriptKey , Ctx : ScriptContext > (
322+ witness : & [ Vec < u8 > ] ,
323+ ) -> Result < ( ) , ScriptContextError > {
324+ if witness_size ( witness) > MAX_STANDARD_P2WSH_SCRIPT_SIZE {
325+ return Err ( ScriptContextError :: MaxScriptSigSizeExceeded ) ;
326+ } else if witness. len ( ) > MAX_STANDARD_P2WSH_STACK_ITEMS {
327+ return Err ( ScriptContextError :: MaxWitnessItemssExceeded ) ;
328+ }
329+ Ok ( ( ) )
330+ }
331+
297332 fn check_global_consensus_validity < Pk : MiniscriptKey , Ctx : ScriptContext > (
298333 ms : & Miniscript < Pk , Ctx > ,
299334 ) -> Result < ( ) , ScriptContextError > {
0 commit comments