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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
crate::ix!();

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

/**
  | A signature creator that just produces
  | 71-byte empty signatures.
  |
  */
lazy_static!{
    /*
    extern const BaseSignatureCreator& DUMMY_SIGNATURE_CREATOR;
    */
}

/**
  | A signature creator that just produces
  | 72-byte empty signatures.
  |
  */
lazy_static!{
    /*
    extern const BaseSignatureCreator& DUMMY_MAXIMUM_SIGNATURE_CREATOR;
    */
}

pub type SigPair = (PubKey,Vec<u8>);

/**
  | This struct contains information from
  | a transaction input and also contains
  | signatures for that input.
  |
  | The information contained here can be used to
  | create a signature and is also filled by
  | ProduceSignature in order to construct final
  | scriptSigs and scriptWitnesses.
  */
#[derive(Default)]
pub struct SignatureData {

    /**
      | Stores whether the scriptSig and scriptWitness
      | are complete
      |
      */
    pub complete:               bool, // default = false

    /**
      | Stores whether the input this SigData
      | corresponds to is a witness input
      |
      */
    pub witness:                bool, // default = false

    /**
      | The scriptSig of an input. Contains
      | complete signatures or the traditional
      | partial signatures format
      |
      */
    pub script_sig:             Script,

    /**
      | The redeemScript (if any) for the input
      |
      */
    pub redeem_script:          Script,

    /**
      | The witnessScript (if any) for the input.
      | witnessScripts are used in P2WSH outputs.
      |
      */
    pub witness_script:         Script,

    /**
      | The scriptWitness of an input. Contains
      | complete signatures or the traditional
      | partial signatures format. scriptWitness
      | is part of a transaction input per BIP
      | 144.
      |
      */
    pub script_witness:         ScriptWitness,

    /**
      | Taproot spending data.
      |
      */
    pub tr_spenddata:           TaprootSpendData,

    /**
      | BIP 174 style partial signatures for
      | the input. May contain all signatures
      | necessary for producing a final scriptSig
      | or scriptWitness.
      |
      */
    pub signatures:             HashMap<KeyID,SigPair>,

    pub misc_pubkeys:           HashMap<KeyID,(PubKey,KeyOriginInfo)>,

    /**
      | Schnorr signature for key path spending
      |
      */
    pub taproot_key_path_sig:   Vec<u8>,

    /**
      | (Partial) schnorr signatures, indexed
      | by XOnlyPubKey and leaf_hash.
      |
      */
    pub taproot_script_sigs:    HashMap<(XOnlyPubKey,u256),Vec<u8>>,

    /**
      | KeyIDs of pubkeys which could not be
      | found
      |
      */
    pub missing_pubkeys:        Vec<KeyID>,

    /**
      | KeyIDs of pubkeys for signatures which
      | could not be found
      |
      */
    pub missing_sigs:           Vec<KeyID>,

    /**
      | ScriptID of the missing redeemScript
      | (if any)
      |
      */
    pub missing_redeem_script:  u160,

    /**
      | SHA256 of the missing witnessScript
      | (if any)
      |
      */
    pub missing_witness_script: u256,
}

impl From<&Script> for SignatureData {
    fn from(script: &Script) -> Self {
    
        todo!();
        /*
        : script_sig(script),
        */
    }
}

impl SignatureData {

    pub fn merge_signature_data(&mut self, sigdata: SignatureData)  {
        
        todo!();
        /*
            if (complete) return;
        if (sigdata.complete) {
            *this = std::move(sigdata);
            return;
        }
        if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
            redeem_script = sigdata.redeem_script;
        }
        if (witness_script.empty() && !sigdata.witness_script.empty()) {
            witness_script = sigdata.witness_script;
        }
        signatures.insert(std::make_move_iterator(sigdata.signatures.begin()), std::make_move_iterator(sigdata.signatures.end()));
        */
    }
}

/**
  | Takes a stream and multiple arguments and
  | serializes them as if first serialized into
  | a vector and then into the stream
  |
  | The resulting output into the stream has the
  | total serialized length of all of the objects
  | followed by all objects concatenated with each
  | other.
  */
pub fn serialize_to_vector<Stream, X>(
        s:    &mut Stream,
        args: &X)  {

    todo!();
        /*
            WriteCompactSize(s, GetSerializeSizeMany(s.GetVersion(), args...));
        SerializeMany(s, args...);
        */
}

/**
  | Takes a stream and multiple arguments and
  | unserializes them first as a vector then each
  | object individually in the order provided in
  | the arguments
  */
pub fn unserialize_from_vector<Stream, X>(
        s:    &mut Stream,
        args: &mut X)  {

    todo!();
        /*
            size_t expected_size = ReadCompactSize(s);
        size_t remaining_before = s.size();
        UnserializeMany(s, args...);
        size_t remaining_after = s.size();
        if (remaining_after + expected_size != remaining_before) {
            throw std::ios_base::failure("Size of value was not the stated size");
        }
        */
}

/**
  | Deserialize HD keypaths into a map
  |
  */
pub fn deserialize_hd_keypaths<Stream>(
        s:           &mut Stream,
        key:         &Vec<u8>,
        hd_keypaths: &mut HashMap<PubKey,KeyOriginInfo>)  {

    todo!();
        /*
            // Make sure that the key is the size of pubkey + 1
        if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) {
            throw std::ios_base::failure("Size of key was not the expected size for the type BIP32 keypath");
        }
        // Read in the pubkey from key
        CPubKey pubkey(key.begin() + 1, key.end());
        if (!pubkey.IsFullyValid()) {
           throw std::ios_base::failure("Invalid pubkey");
        }
        if (hd_keypaths.count(pubkey) > 0) {
            throw std::ios_base::failure("Duplicate Key, pubkey derivation path already provided");
        }

        // Read in key path
        uint64_t value_len = ReadCompactSize(s);
        if (value_len % 4 || value_len == 0) {
            throw std::ios_base::failure("Invalid length for HD key path");
        }

        KeyOriginInfo keypath;
        s >> keypath.fingerprint;
        for (unsigned int i = 4; i < value_len; i += sizeof(uint32_t)) {
            uint32_t index;
            s >> index;
            keypath.path.push_back(index);
        }

        // Add to map
        hd_keypaths.emplace(pubkey, std::move(keypath));
        */
}

/**
  | Serialize HD keypaths to a stream from
  | a map
  |
  */
pub fn serialize_hd_keypaths<Stream>(
        s:           &mut Stream,
        hd_keypaths: &HashMap<PubKey,KeyOriginInfo>,
        ty:          u8)  {

    todo!();
        /*
            for (auto keypath_pair : hd_keypaths) {
            if (!keypath_pair.first.IsValid()) {
                throw std::ios_base::failure("Invalid CPubKey being serialized");
            }
            SerializeToVector(s, type, MakeSpan(keypath_pair.first));
            WriteCompactSize(s, (keypath_pair.second.path.size() + 1) * sizeof(uint32_t));
            s << keypath_pair.second.fingerprint;
            for (const auto& path : keypath_pair.second.path) {
                s << path;
            }
        }
        */
}

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

pub type ValType = Vec<u8>;

pub fn push_all(values: &Vec<ValType>) -> Script {
    
    todo!();
        /*
            CScript result;
        for (const valtype& v : values) {
            if (v.size() == 0) {
                result << OP_0;
            } else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) {
                result << CScript::EncodeOP_N(v[0]);
            } else if (v.size() == 1 && v[0] == 0x81) {
                result << OP_1NEGATE;
            } else {
                result << v;
            }
        }
        return result;
        */
}

///-------------------------
pub struct Stacks {
    script:  Vec<ValType>,
    witness: Vec<ValType>,
}

impl Stacks {

    pub fn new(data: &SignatureData) -> Self {
    
        todo!();
        /*
        : witness(data.scriptWitness.stack),

            EvalScript(script, data.scriptSig, SCRIPT_VERIFY_STRICTENC, BaseSignatureChecker(), SigVersion::BASE);
        */
    }
}