1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
crate::ix!();

/**
  | Value for the first BIP 32 hardened derivation.
  | Can be used as a bit mask and as a value.
  | See BIP 32 for more details.
  |
  */
pub const BIP32_HARDENED_KEY_LIMIT: u32 = 0x80000000;

pub type ValType = Vec<u8>;

/**
  | This is an enum that tracks the execution
  | context of a script, similar to
  | 
  | SigVersion in script/interpreter.
  | It is separate however because we want
  | to distinguish between top-level scriptPubKey
  | execution and P2SH redeemScript execution
  | (a distinction that has no impact on
  | consensus rules).
  |
  */
pub enum IsMineSigVersion
{

    /**
      | scriptPubKey execution
      |
      */
    TOP = 0,        

    /**
      | P2SH redeemScript
      |
      */
    P2SH = 1,       

    /**
      | P2WSH witness script execution
      |
      */
    WITNESS_V0 = 2, 
}

/**
  | This is an internal representation
  | of isminetype + invalidity.
  | 
  | Its order is significant, as we return
  | the max of all explored possibilities.
  |
  */
pub enum IsMineResult
{
    /**
      | Not ours
      |
      */
    NO = 0,         

    /**
      | Included in watch-only balance
      |
      */
    WATCH_ONLY = 1, 

    /**
      | Included in all balances
      |
      */
    SPENDABLE = 2,  

    /**
      | Not spendable by anyone (uncompressed
      | pubkey in segwit, P2SH inside P2SH or
      | witness, witness inside witness)
      |
      */
    INVALID = 3,    
}

pub fn permits_uncompressed(sigversion: IsMineSigVersion) -> bool {
    
    todo!();
        /*
            return sigversion == IsMineSigVersion::TOP || sigversion == IsMineSigVersion::P2SH;
        */
}

/**
  | Recursively solve script and return
  | spendable/watchonly/invalid status.
  | 
  | -----------
  | @param keystore
  | 
  | legacy key and script store
  | ----------
  | @param scriptPubKey
  | 
  | script to solve
  | ----------
  | @param sigversion
  | 
  | script type (top-level / redeemscript
  | / witnessscript)
  | ----------
  | @param recurse_scripthash
  | 
  | whether to recurse into nested p2sh
  | and p2wsh scripts or simply treat any
  | script that has been stored in the keystore
  | as spendable
  |
  */
pub fn is_mine_inner(
        keystore:           &LegacyScriptPubKeyMan,
        script_pub_key:     &Script,
        sigversion:         IsMineSigVersion,
        recurse_scripthash: Option<bool>) -> IsMineResult {
    let recurse_scripthash: bool = recurse_scripthash.unwrap_or(true);

    todo!();
        /*
            IsMineResult ret = IsMineResult::NO;

        std::vector<valtype> vSolutions;
        TxoutType whichType = Solver(scriptPubKey, vSolutions);

        CKeyID keyID;
        switch (whichType) {
        case TxoutType::NONSTANDARD:
        case TxoutType::NULL_DATA:
        case TxoutType::WITNESS_UNKNOWN:
        case TxoutType::WITNESS_V1_TAPROOT:
            break;
        case TxoutType::PUBKEY:
            keyID = CPubKey(vSolutions[0]).GetID();
            if (!PermitsUncompressed(sigversion) && vSolutions[0].size() != 33) {
                return IsMineResult::INVALID;
            }
            if (keystore.HaveKey(keyID)) {
                ret = std::max(ret, IsMineResult::SPENDABLE);
            }
            break;
        case TxoutType::WITNESS_V0_KEYHASH:
        {
            if (sigversion == IsMineSigVersion::WITNESS_V0) {
                // P2WPKH inside P2WSH is invalid.
                return IsMineResult::INVALID;
            }
            if (sigversion == IsMineSigVersion::TOP && !keystore.HaveCScript(CScriptID(CScript() << OP_0 << vSolutions[0]))) {
                // We do not support bare witness outputs unless the P2SH version of it would be
                // acceptable as well. This protects against matching before segwit activates.
                // This also applies to the P2WSH case.
                break;
            }
            ret = std::max(ret, IsMineInner(keystore, GetScriptForDestination(PKHash(u160(vSolutions[0]))), IsMineSigVersion::WITNESS_V0));
            break;
        }
        case TxoutType::PUBKEYHASH:
            keyID = CKeyID(u160(vSolutions[0]));
            if (!PermitsUncompressed(sigversion)) {
                CPubKey pubkey;
                if (keystore.GetPubKey(keyID, pubkey) && !pubkey.IsCompressed()) {
                    return IsMineResult::INVALID;
                }
            }
            if (keystore.HaveKey(keyID)) {
                ret = std::max(ret, IsMineResult::SPENDABLE);
            }
            break;
        case TxoutType::SCRIPTHASH:
        {
            if (sigversion != IsMineSigVersion::TOP) {
                // P2SH inside P2WSH or P2SH is invalid.
                return IsMineResult::INVALID;
            }
            CScriptID scriptID = CScriptID(u160(vSolutions[0]));
            CScript subscript;
            if (keystore.GetCScript(scriptID, subscript)) {
                ret = std::max(ret, recurse_scripthash ? IsMineInner(keystore, subscript, IsMineSigVersion::P2SH) : IsMineResult::SPENDABLE);
            }
            break;
        }
        case TxoutType::WITNESS_V0_SCRIPTHASH:
        {
            if (sigversion == IsMineSigVersion::WITNESS_V0) {
                // P2WSH inside P2WSH is invalid.
                return IsMineResult::INVALID;
            }
            if (sigversion == IsMineSigVersion::TOP && !keystore.HaveCScript(CScriptID(CScript() << OP_0 << vSolutions[0]))) {
                break;
            }
            u160 hash;
            CRIPEMD160().Write(vSolutions[0].data(), vSolutions[0].size()).Finalize(hash.begin());
            CScriptID scriptID = CScriptID(hash);
            CScript subscript;
            if (keystore.GetCScript(scriptID, subscript)) {
                ret = std::max(ret, recurse_scripthash ? IsMineInner(keystore, subscript, IsMineSigVersion::WITNESS_V0) : IsMineResult::SPENDABLE);
            }
            break;
        }

        case TxoutType::MULTISIG:
        {
            // Never treat bare multisig outputs as ours (they can still be made watchonly-though)
            if (sigversion == IsMineSigVersion::TOP) {
                break;
            }

            // Only consider transactions "mine" if we own ALL the
            // keys involved. Multi-signature transactions that are
            // partially owned (somebody else has a key that can spend
            // them) enable spend-out-from-under-you attacks, especially
            // in shared-wallet situations.
            std::vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1);
            if (!PermitsUncompressed(sigversion)) {
                for (size_t i = 0; i < keys.size(); i++) {
                    if (keys[i].size() != 33) {
                        return IsMineResult::INVALID;
                    }
                }
            }
            if (HaveKeys(keys, keystore)) {
                ret = std::max(ret, IsMineResult::SPENDABLE);
            }
            break;
        }
        } // no default case, so the compiler can warn about missing cases

        if (ret == IsMineResult::NO && keystore.HaveWatchOnly(scriptPubKey)) {
            ret = std::max(ret, IsMineResult::WATCH_ONLY);
        }
        return ret;
        */
}