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
//! #EnumDescriptor

use super::contract_info::OracleIndexAndPrefixLength;
use super::utils::{get_majority_combination, unordered_equal};
use super::AdaptorInfo;
use crate::error::Error;
use bitcoin::{Script, Transaction};
use dlc::OracleInfo;
use dlc::{EnumerationPayout, Payout};
use dlc_messages::oracle_msgs::EnumEventDescriptor;
use dlc_trie::{combination_iterator::CombinationIterator, RangeInfo};
use secp256k1_zkp::{
    All, EcdsaAdaptorSignature, Message, PublicKey, Secp256k1, SecretKey, Verification,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// A descriptor for a contract whose outcomes are represented as an enumeration.
#[derive(Clone, Debug)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(rename_all = "camelCase")
)]
pub struct EnumDescriptor {
    /// The set of outcomes.
    pub outcome_payouts: Vec<EnumerationPayout>,
}

impl EnumDescriptor {
    /// Returns the set of payouts.
    pub fn get_payouts(&self) -> Vec<Payout> {
        self.outcome_payouts
            .iter()
            .map(|x| x.payout.clone())
            .collect()
    }

    /// Validate that the descriptor covers all possible outcomes of the given
    /// enum event descriptor.
    pub fn validate(&self, enum_event_descriptor: &EnumEventDescriptor) -> Result<(), Error> {
        if unordered_equal(
            &enum_event_descriptor.outcomes.iter().collect::<Vec<_>>(),
            &self
                .outcome_payouts
                .iter()
                .map(|x| &x.outcome)
                .collect::<Vec<_>>(),
        ) {
            Ok(())
        } else {
            Err(Error::InvalidParameters(
                "Oracle outcomes do not each have a single associated payout.".to_string(),
            ))
        }
    }

    /// Returns the `RangeInfo` that matches the given set of outcomes if any.
    pub fn get_range_info_for_outcome(
        &self,
        nb_oracles: usize,
        threshold: usize,
        outcomes: &[(usize, &Vec<String>)],
        adaptor_sig_start: usize,
    ) -> Option<(OracleIndexAndPrefixLength, RangeInfo)> {
        if outcomes.len() < threshold {
            return None;
        }

        let filtered_outcomes: Vec<(usize, &Vec<String>)> = outcomes
            .iter()
            .filter(|x| x.1.len() == 1)
            .cloned()
            .collect();
        let (mut outcome, mut actual_combination) = get_majority_combination(&filtered_outcomes)?;
        let outcome = outcome.remove(0);

        if actual_combination.len() < threshold {
            return None;
        }

        actual_combination.truncate(threshold);

        let pos = self
            .outcome_payouts
            .iter()
            .position(|x| x.outcome == outcome)?;

        let combinator = CombinationIterator::new(nb_oracles, threshold);
        let mut comb_pos = 0;
        let mut comb_count = 0;

        for (i, combination) in combinator.enumerate() {
            if combination == actual_combination {
                comb_pos = i;
            }
            comb_count += 1;
        }

        let range_info = RangeInfo {
            cet_index: pos,
            adaptor_index: comb_count * pos + comb_pos + adaptor_sig_start,
        };

        Some((
            actual_combination.iter().map(|x| (*x, 1)).collect(),
            range_info,
        ))
    }

    /// Verify the given set adaptor signatures.
    pub fn verify_adaptor_info(
        &self,
        secp: &Secp256k1<All>,
        oracle_infos: &[OracleInfo],
        threshold: usize,
        fund_pubkey: &PublicKey,
        funding_script_pubkey: &Script,
        fund_output_value: u64,
        cets: &[Transaction],
        adaptor_sigs: &[EcdsaAdaptorSignature],
        adaptor_sig_start: usize,
    ) -> Result<usize, dlc::Error> {
        let mut adaptor_sig_index = adaptor_sig_start;
        let mut callback =
            |adaptor_point: &PublicKey, cet_index: usize| -> Result<(), dlc::Error> {
                let sig = adaptor_sigs[adaptor_sig_index];
                adaptor_sig_index += 1;
                dlc::verify_cet_adaptor_sig_from_point(
                    secp,
                    &sig,
                    &cets[cet_index],
                    adaptor_point,
                    fund_pubkey,
                    funding_script_pubkey,
                    fund_output_value,
                )?;
                Ok(())
            };

        self.iter_outcomes(secp, oracle_infos, threshold, &mut callback)?;

        Ok(adaptor_sig_index)
    }

    /// Verify the given set of adaptor signature and generates the adaptor info.
    pub fn verify_and_get_adaptor_info(
        &self,
        secp: &Secp256k1<All>,
        oracle_infos: &[OracleInfo],
        threshold: usize,
        fund_pubkey: &PublicKey,
        funding_script_pubkey: &Script,
        fund_output_value: u64,
        cets: &[Transaction],
        adaptor_sigs: &[EcdsaAdaptorSignature],
        adaptor_sig_start: usize,
    ) -> Result<(AdaptorInfo, usize), dlc::Error> {
        let adaptor_sig_index = self.verify_adaptor_info(
            secp,
            oracle_infos,
            threshold,
            fund_pubkey,
            funding_script_pubkey,
            fund_output_value,
            cets,
            adaptor_sigs,
            adaptor_sig_start,
        )?;

        Ok((AdaptorInfo::Enum, adaptor_sig_index))
    }

    /// Generate the set of adaptor signatures and return the adaptor info.
    pub fn get_adaptor_info(
        &self,
        secp: &Secp256k1<All>,
        oracle_infos: &[OracleInfo],
        threshold: usize,
        fund_privkey: &SecretKey,
        funding_script_pubkey: &Script,
        fund_output_value: u64,
        cets: &[Transaction],
    ) -> Result<(AdaptorInfo, Vec<EcdsaAdaptorSignature>), Error> {
        let adaptor_sigs = self.get_adaptor_signatures(
            secp,
            oracle_infos,
            threshold,
            cets,
            fund_privkey,
            funding_script_pubkey,
            fund_output_value,
        )?;

        Ok((AdaptorInfo::Enum, adaptor_sigs))
    }

    /// Generate the set of adaptor signatures.
    pub fn get_adaptor_signatures(
        &self,
        secp: &Secp256k1<All>,
        oracle_infos: &[OracleInfo],
        threshold: usize,
        cets: &[Transaction],
        fund_privkey: &SecretKey,
        funding_script_pubkey: &Script,
        fund_output_value: u64,
    ) -> Result<Vec<EcdsaAdaptorSignature>, Error> {
        let mut adaptor_sigs = Vec::new();
        let mut callback =
            |adaptor_point: &PublicKey, cet_index: usize| -> Result<(), dlc::Error> {
                let sig = dlc::create_cet_adaptor_sig_from_point(
                    secp,
                    &cets[cet_index],
                    adaptor_point,
                    fund_privkey,
                    funding_script_pubkey,
                    fund_output_value,
                )?;
                adaptor_sigs.push(sig);
                Ok(())
            };

        self.iter_outcomes(secp, oracle_infos, threshold, &mut callback)?;

        Ok(adaptor_sigs)
    }

    fn iter_outcomes<C: Verification, F>(
        &self,
        secp: &Secp256k1<C>,
        oracle_infos: &[OracleInfo],
        threshold: usize,
        callback: &mut F,
    ) -> Result<(), dlc::Error>
    where
        F: FnMut(&PublicKey, usize) -> Result<(), dlc::Error>,
    {
        let messages: Vec<Vec<Vec<Message>>> = self
            .outcome_payouts
            .iter()
            .map(|x| {
                let message = vec![Message::from_hashed_data::<
                    secp256k1_zkp::hashes::sha256::Hash,
                >(x.outcome.as_bytes())];
                std::iter::repeat(message).take(threshold).collect()
            })
            .collect();
        let combination_iter = CombinationIterator::new(oracle_infos.len(), threshold);
        let combinations: Vec<Vec<usize>> = combination_iter.collect();

        for (i, outcome_messages) in messages.iter().enumerate() {
            for selector in &combinations {
                let cur_oracle_infos: Vec<_> = oracle_infos
                    .iter()
                    .enumerate()
                    .filter_map(|(i, x)| {
                        if selector.contains(&i) {
                            Some(x.clone())
                        } else {
                            None
                        }
                    })
                    .collect();
                let adaptor_point = dlc::get_adaptor_point_from_oracle_info(
                    secp,
                    &cur_oracle_infos,
                    outcome_messages,
                )?;
                callback(&adaptor_point, i)?;
            }
        }

        Ok(())
    }
}