// We need this because of the need to control maximum number of nested
// miniscript code
We need this because of the need to control maximum number of nested
miniscript code
We need this because of the need to control maximum number of nested miniscript code
fn encode_policy_inner<Pk>(
            policy: &Policy<Pk>,
            e: &mut impl io::Write,
            mut depth: u8,
        ) -> Result<usize, Error>
        where
            Pk: MiniscriptKey + StrictEncode,
            <Pk as MiniscriptKey>::Hash: StrictEncode,
        {
            if depth > MINISCRIPT_DEPTH_LIMIT {
                return Err(Error::ExceedMaxItems(
                    MINISCRIPT_DEPTH_LIMIT as usize,
                ));
            }
            depth += 1;
            Ok(match policy {
                Policy::Unsatisfiable => MS_FALSE.strict_encode(e)?,
                Policy::Trivial => MS_TRUE.strict_encode(e)?,
                Policy::Key(pk) => strict_encode_tuple!(e; MS_KEY, pk),
                Policy::After(tl) => strict_encode_tuple!(e; MS_AFTER, tl),
                Policy::Older(tl) => strict_encode_tuple!(e; MS_OLDER, tl),
                Policy::Sha256(hash) => {
                    strict_encode_tuple!(e; MS_SHA256, hash)
                }
                Policy::Hash256(hash) => {
                    strict_encode_tuple!(e; MS_HASH256, hash)
                }
                Policy::Ripemd160(hash) => {
                    strict_encode_tuple!(e; MS_RIPEMD160, hash)
                }
                Policy::Hash160(hash) => {
                    strict_encode_tuple!(e; MS_HASH160, hash)
                }
                Policy::And(vec) => {
                    let mut len = 1usize;
                    e.write_all(&[MS_AND_B])?;
                    len += strict_encode_usize!(e; vec.len());
                    for p in vec {
                        len += encode_policy_inner(p, e, depth)?;
                    }
                    len
                }
                Policy::Or(vec) => {
                    let mut len = 1usize;
                    e.write_all(&[MS_OR_B])?;
                    len += strict_encode_usize!(e; vec.len());
                    for (x, p) in vec {
                        len += strict_encode_usize!(e; *x);
                        len += encode_policy_inner(p, e, depth)?;
                    }
                    len
                }
                Policy::Threshold(thresh, vec) => {
                    let mut len = 1usize;
                    e.write_all(&[MS_THRESH])?;
                    len += strict_encode_usize!(e; *thresh);
                    len += strict_encode_usize!(e; vec.len());
                    for p in vec {
                        len += encode_policy_inner(p, e, depth)?;
                    }
                    len
                }
            })
        }
fn encode_policy_inner < Pk > ( policy : & Policy < Pk > , e : & mut impl io :: Write , mut depth : u8 , ) -> Result < usize , Error > where Pk : MiniscriptKey + StrictEncode , < Pk as MiniscriptKey > :: Hash : StrictEncode , { if depth > MINISCRIPT_DEPTH_LIMIT { return Err ( Error :: ExceedMaxItems ( MINISCRIPT_DEPTH_LIMIT as usize , ) ) ; } depth += 1 ; Ok ( match policy { Policy :: Unsatisfiable => MS_FALSE . strict_encode ( e ) ? , Policy :: Trivial => MS_TRUE . strict_encode ( e ) ? , Policy :: Key ( pk ) => strict_encode_tuple ! ( e MS_KEY pk ) , Policy :: After ( tl ) => strict_encode_tuple ! ( e MS_AFTER tl ) , Policy :: Older ( tl ) => strict_encode_tuple ! ( e MS_OLDER tl ) , Policy :: Sha256 ( hash ) => { strict_encode_tuple ! ( e MS_SHA256 hash ) } Policy :: Hash256 ( hash ) => { strict_encode_tuple ! ( e MS_HASH256 hash ) } Policy :: Ripemd160 ( hash ) => { strict_encode_tuple ! ( e MS_RIPEMD160 hash ) } Policy :: Hash160 ( hash ) => { strict_encode_tuple ! ( e MS_HASH160 hash ) } Policy :: And ( vec ) => { let mut len = 1usize ; e . write_all ( & [ MS_AND_B ] ) ? ; len += strict_encode_usize ! ( e vec len ( ) ) ; for p in vec { len += encode_policy_inner ( p , e , depth ) ? ; } len } Policy :: Or ( vec ) => { let mut len = 1usize ; e . write_all ( & [ MS_OR_B ] ) ? ; len += strict_encode_usize ! ( e vec len ( ) ) ; for ( x , p ) in vec { len += strict_encode_usize ! ( e x ) ; len += encode_policy_inner ( p , e , depth ) ? ; } len } Policy :: Threshold ( thresh , vec ) => { let mut len = 1usize ; e . write_all ( & [ MS_THRESH ] ) ? ; len += strict_encode_usize ! ( e thresh ) ; len += strict_encode_usize ! ( e vec len ( ) ) ; for p in vec { len += encode_policy_inner ( p , e , depth ) ? ; } len } } ) }
We need this because of the need to control maximum number of nested
miniscript code
We need this because of the need to control maximum number of nested miniscript code
policy
&Policy<Pk>
e
&mut impl io::Write
depth
u8