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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
crate::ix!();

//-------------------------------------------[.cpp/bitcoin/src/script/bitcoinconsensus.h]

pub const BITCOINCONSENSUS_API_VER: usize = 1;

pub enum BitcoinConsensusError
{
    Ok = 0,
    Txindex,
    TxSizeMismatch,
    TxDeserialize,
    AmountRequired,
    InvalidFlags,
}

bitflags! {
    pub struct BitcoinConsensusScriptVerificationFlags: u32 {
        const None                = 0b0000000000000000;

        /*
           | evaluate P2SH (BIP16) subscripts
           |
           */
        const P2sh                = 0b0000000000000001;

        /*
           | enforce strict DER (BIP66) compliance
           |
           */
        const Dersig              = 0b0000000000000100;

        /*
          | enforce NULLDUMMY (BIP147)
          |
          */
        const Nulldummy           = 0b0000000000010000;

        /*
          | enable CHECKLOCKTIMEVERIFY (BIP65)
          |
          */
        const Checklocktimeverify = 0b0000001000000000;

        /*
          | enable CHECKSEQUENCEVERIFY (BIP112)
          |
          */
        const Checksequenceverify = 0b0000010000000000;

        /*
          | enable WITNESS (BIP141)
          |
          */
        const Witness             = 0b0000100000000000;

        const ALL = 
            Self::P2sh.bits 
            | Self::Dersig.bits 
            | Self::Nulldummy.bits
            | Self::Checklocktimeverify.bits
            | Self::Checksequenceverify.bits
            | Self::Witness.bits;
    }
}

//-------------------------------------------[.cpp/bitcoin/src/script/bitcoinconsensus.cpp]

/**
  | A class that deserializes a single CTransaction
  | one time.
  |
  */
pub struct TxInputStream {
    version:   i32,
    data:      *const u8,
    remaining: usize,
}

impl<T> Shr<T> for TxInputStream {
    type Output = TxInputStream;

    #[inline] fn shr(self, rhs: T) -> Self::Output {
        todo!();
        /*
            ::Unserialize(*this, obj);
            return *this;
        */
    }
}

impl TxInputStream {

    pub fn new(
        n_version_in: i32,
        tx_to:        *const u8,
        tx_to_len:    usize) -> Self {
    
        todo!();
        /*
        : version(nVersionIn),
        : data(txTo),
        : remaining(txToLen),

        
        */
    }
    
    pub fn read(&mut self, 
        pch:    *mut u8,
        n_size: usize)  {
        
        todo!();
        /*
            if (nSize > m_remaining)
                throw std::ios_base::failure(std::string(__func__) + ": end of data");

            if (pch == nullptr)
                throw std::ios_base::failure(std::string(__func__) + ": bad destination buffer");

            if (m_data == nullptr)
                throw std::ios_base::failure(std::string(__func__) + ": bad source buffer");

            memcpy(pch, m_data, nSize);
            m_remaining -= nSize;
            m_data += nSize;
        */
    }
    
    pub fn get_version(&self) -> i32 {
        
        todo!();
        /*
            return m_version;
        */
    }
}

#[inline] pub fn set_error(
        ret:    *mut BitcoinConsensusError,
        serror: BitcoinConsensusError) -> i32 {
    
    todo!();
        /*
            if (ret)
            *ret = serror;
        return 0;
        */
}

pub struct ECCryptoClosure
{
    handle: ECCVerifyHandle,
}

lazy_static!{
    /*
    ECCryptoClosure instance_of_eccryptoclosure;
    */
}

/**
  | Check that all specified flags are part
  | of the libconsensus interface.
  |
  */
pub fn verify_flags(flags: u32) -> bool {
    
    todo!();
        /*
            return (flags & ~(bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL)) == 0;
        */
}

pub fn verify_script(
        script_pub_key:     *const u8,
        script_pub_key_len: u32,
        amount:             Amount,
        tx_to:              *const u8,
        tx_to_len:          u32,
        n_in:               u32,
        flags:              u32,
        err:                *mut BitcoinConsensusError) -> i32 {
    
    todo!();
        /*
            if (!verify_flags(flags)) {
            return set_error(err, bitcoinconsensus_ERR_INVALID_FLAGS);
        }
        try {
            TxInputStream stream(PROTOCOL_VERSION, txTo, txToLen);
            CTransaction tx(deserialize, stream);
            if (nIn >= tx.vin.size())
                return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
            if (GetSerializeSize(tx, PROTOCOL_VERSION) != txToLen)
                return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);

            // Regardless of the verification result, the tx did not error.
            set_error(err, bitcoinconsensus_ERR_OK);

            PrecomputedTransactionData txdata(tx);
            return VerifyScript(tx.vin[nIn].scriptSig, CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), &tx.vin[nIn].scriptWitness, flags, TransactionSignatureChecker(&tx, nIn, amount, txdata, MissingDataBehavior::FAIL), nullptr);
        } catch (const std::exception&) {
            return set_error(err, bitcoinconsensus_ERR_TX_DESERIALIZE); // Error deserializing
        }
        */
}

pub fn bitcoinconsensus_verify_script_with_amount(
        script_pub_key:     *const u8,
        script_pub_key_len: u32,
        amount:             i64,
        tx_to:              *const u8,
        tx_to_len:          u32,
        n_in:               u32,
        flags:              u32,
        err:                *mut BitcoinConsensusError) -> i32 {
    
    todo!();
        /*
            CAmount am(amount);
        return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
        */
}

/**
  | Returns 1 if the input nIn of the serialized
  | transaction pointed to by txTo correctly
  | spends the scriptPubKey pointed to by
  | scriptPubKey under the additional constraints
  | specified by flags.
  |
  | If not nullptr, err will contain an
  | error/success code for the operation
  */
pub fn bitcoinconsensus_verify_script(
        script_pub_key:     *const u8,
        script_pub_key_len: u32,
        tx_to:              *const u8,
        tx_to_len:          u32,
        n_in:               u32,
        flags:              u32,
        err:                *mut BitcoinConsensusError) -> i32 {
    
    todo!();
        /*
            if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) {
            return set_error(err, bitcoinconsensus_ERR_AMOUNT_REQUIRED);
        }

        CAmount am(0);
        return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
        */
}

pub fn bitcoinconsensus_version() -> u32 {
    
    todo!();
        /*
            // Just use the API version for now
        return BITCOINCONSENSUS_API_VER;
        */
}