ddk-messages 1.0.11

Structs and serialization for the Discreet Log Contract (DLC) protocol.
Documentation
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! Contains messages used for the establishment and update of DLC channels.

use bitcoin::{Amount, ScriptBuf};
use ddk_dlc::Error;
use lightning::ln::msgs::DecodeError;
use lightning::util::ser::{Readable, Writeable, Writer};
use secp256k1_zkp::{
    ecdsa::Signature, EcdsaAdaptorSignature, PublicKey, Secp256k1, SecretKey, Verification,
};

use crate::FundingSignatures;
use crate::{
    contract_msgs::ContractInfo,
    ser_impls::{read_ecdsa_adaptor_signature, write_ecdsa_adaptor_signature},
    types::*,
    CetAdaptorSignatures, FundingInput, NegotiationFields,
};

/// Contains information about a party wishing to enter into a DLC with
/// another party. The contained information is sufficient for any other party
/// to create a set of transactions representing the contract and its terms.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
    feature = "use-serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
pub struct OfferChannel {
    /// The version of the protocol used by the sending peer.
    pub protocol_version: u32,
    /// Indicates options and features selected for the offered contract.
    pub contract_flags: u8,
    /// The identifier of the chain on which the contract takes place.
    #[cfg_attr(
        feature = "use-serde",
        serde(
            serialize_with = "crate::serde_utils::serialize_hex",
            deserialize_with = "crate::serde_utils::deserialize_hex_array"
        )
    )]
    pub chain_hash: [u8; 32],
    /// A random nonce identifying the contract until the fund transaction
    /// is created.
    pub temporary_contract_id: [u8; 32],
    /// A random nonce identifying the channel until the fund transaction is
    /// created.
    pub temporary_channel_id: [u8; 32],
    /// Information about the contract established during channel creation.
    pub contract_info: ContractInfo,
    /// The public key used by the offer party in the 2 of 2 funding output.
    pub funding_pubkey: PublicKey,
    /// The base point that will be used by the offer party for revocation.
    pub revocation_basepoint: PublicKey,
    /// The base point that will be used by the offer party for generating
    /// adaptor signatures to revocable transactions.
    pub publish_basepoint: PublicKey,
    /// The base point that will be used by the offer party in the 2 of 2 output
    /// of buffer transactions.
    pub own_basepoint: PublicKey,
    /// The first per update point of the offer party.
    pub first_per_update_point: PublicKey,
    /// Script used by the offer party to receive their payout on channel close.
    pub payout_spk: ScriptBuf,
    /// Serial id used to order outputs.
    pub payout_serial_id: u64,
    /// The collateral input by the offer party in the channel.
    pub offer_collateral: Amount,
    /// The inputs that the offer party will use to fund the channel.
    pub funding_inputs: Vec<FundingInput>,
    /// The script that the offer party to receive their change.
    pub change_spk: ScriptBuf,
    /// Serial id used to order outputs.
    pub change_serial_id: u64,
    /// Serial id used to order outputs.
    pub fund_output_serial_id: u64,
    /// The fee rate proposed by the offer party for the channel transactions.
    pub fee_rate_per_vb: u64,
    /// Lock time for the CETs.
    pub cet_locktime: u32,
    /// Lock time for the refund transaction.
    pub refund_locktime: u32,
    /// The nSequence value to use for the CETs.
    pub cet_nsequence: u32,
}

impl_dlc_writeable!(OfferChannel, OFFER_CHANNEL_TYPE, {
        (protocol_version, writeable),
        (contract_flags, writeable),
        (chain_hash, writeable),
        (temporary_contract_id, writeable),
        (temporary_channel_id, writeable),
        (contract_info, writeable),
        (funding_pubkey, writeable),
        (revocation_basepoint, writeable),
        (publish_basepoint, writeable),
        (own_basepoint, writeable),
        (first_per_update_point, writeable),
        (payout_spk, writeable),
        (payout_serial_id, writeable),
        (offer_collateral, writeable),
        (funding_inputs, vec),
        (change_spk, writeable),
        (change_serial_id, writeable),
        (fund_output_serial_id, writeable),
        (fee_rate_per_vb, writeable),
        (cet_locktime, writeable),
        (refund_locktime, writeable),
        (cet_nsequence, writeable)
});

impl OfferChannel {
    /// Returns whether the message satisfies validity requirements.
    pub fn validate<C: Verification>(
        &self,
        secp: &Secp256k1<C>,
        min_timeout_interval: u32,
        max_timeout_interval: u32,
        min_cet_nsequence: u32,
        max_cet_nsequence: u32,
    ) -> Result<(), Error> {
        let closest_maturity_date = self.contract_info.get_closest_maturity_date();
        let valid_dates = self.cet_locktime <= closest_maturity_date
            && closest_maturity_date + min_timeout_interval <= self.refund_locktime
            && self.refund_locktime <= closest_maturity_date + max_timeout_interval
            && self.cet_nsequence >= min_cet_nsequence
            && self.cet_nsequence <= max_cet_nsequence;
        if !valid_dates {
            return Err(Error::InvalidArgument(
                "Locktime is less than closest maturity date".to_string(),
            ));
        }

        match &self.contract_info {
            ContractInfo::SingleContractInfo(s) => s.contract_info.oracle_info.validate(secp)?,
            ContractInfo::DisjointContractInfo(d) => {
                for c in &d.contract_infos {
                    c.oracle_info.validate(secp)?;
                }
            }
        }

        Ok(())
    }
}

/// Contains information about a party wishing to accept a DLC offer. The contained
/// information is sufficient for the offering party to re-build the set of
/// transactions representing the contract and its terms, and guarantees the offering
/// party that they can safely provide signatures for their funding input.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "use-serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
pub struct AcceptChannel {
    #[cfg_attr(
        feature = "use-serde",
        serde(
            serialize_with = "crate::serde_utils::serialize_hex",
            deserialize_with = "crate::serde_utils::deserialize_hex_array"
        )
    )]
    /// The temporary id of the channel.
    pub temporary_channel_id: [u8; 32],
    /// The collateral input by the accept party.
    pub accept_collateral: Amount,
    /// The [`PublicKey`] used for the fund output by the accept party.
    pub funding_pubkey: PublicKey,
    /// The [`PublicKey`] used for deriving revocation points by the accept party.
    pub revocation_basepoint: PublicKey,
    /// The [`PublicKey`] used for deriving publish points by the accept party.
    pub publish_basepoint: PublicKey,
    /// The [`PublicKey`] used for deriving own points by the accept party.
    pub own_basepoint: PublicKey,
    /// The initial per update point used by the accept party.
    pub first_per_update_point: PublicKey,
    /// The script pubkey for the accept party to receive their payout.
    pub payout_spk: ScriptBuf,
    /// The serial id of the payout output used to order transaction outputs.
    pub payout_serial_id: u64,
    /// The set of inputs used by the accept party to fund the channel.
    pub funding_inputs: Vec<FundingInput>,
    /// The script pubkey used by the accept party to receive back their change.
    pub change_spk: ScriptBuf,
    /// The serial id of the change output used to order transaction outputs.
    pub change_serial_id: u64,
    /// The adaptor signatures for all CETs generated by the accept party.
    pub cet_adaptor_signatures: CetAdaptorSignatures,
    /// The adaptor signature for the buffer transaction generated by the accept
    /// party.
    pub buffer_adaptor_signature: EcdsaAdaptorSignature,
    /// The refund signature generated by the accept party.
    pub refund_signature: Signature,
    /// Fields used to negotiate parameters with the counter party.
    pub negotiation_fields: Option<NegotiationFields>,
}

impl_dlc_writeable!(AcceptChannel, ACCEPT_CHANNEL_TYPE, {
    (temporary_channel_id, writeable),
    (accept_collateral, writeable),
    (funding_pubkey, writeable),
    (revocation_basepoint, writeable),
    (publish_basepoint, writeable),
    (own_basepoint, writeable),
    (first_per_update_point, writeable),
    (payout_spk, writeable),
    (payout_serial_id, writeable),
    (funding_inputs, vec),
    (change_spk, writeable),
    (change_serial_id, writeable),
    (cet_adaptor_signatures, writeable),
    (buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}),
    (refund_signature, writeable),
    (negotiation_fields, option)
});

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "use-serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
/// Message used to finalize the setup of a DLC channel.
pub struct SignChannel {
    #[cfg_attr(
        feature = "use-serde",
        serde(
            serialize_with = "crate::serde_utils::serialize_hex",
            deserialize_with = "crate::serde_utils::deserialize_hex_array"
        )
    )]
    /// The id of the channel referred to by the message.
    pub channel_id: [u8; 32],
    /// The adaptor signatures for all CETs generated by the offer party.
    pub cet_adaptor_signatures: CetAdaptorSignatures,
    /// The adaptor signature for the buffer transaction generated by the offer
    /// party.
    pub buffer_adaptor_signature: EcdsaAdaptorSignature,
    /// The refund signature generated by the offer party.
    pub refund_signature: Signature,
    /// The signatures for the offer party's inputs.
    pub funding_signatures: FundingSignatures,
}

impl_dlc_writeable!(SignChannel, SIGN_CHANNEL_TYPE, {
    (channel_id, writeable),
    (cet_adaptor_signatures, writeable),
    (buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}),
    (refund_signature, writeable),
    (funding_signatures, writeable)
});

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "use-serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
/// Message used to offer a settlement of the channel by on of the parties.
pub struct SettleOffer {
    #[cfg_attr(
        feature = "use-serde",
        serde(
            serialize_with = "crate::serde_utils::serialize_hex",
            deserialize_with = "crate::serde_utils::deserialize_hex_array"
        )
    )]
    /// The id of the channel referred to by the message.
    pub channel_id: [u8; 32],
    /// The payout offered to the receiving party.
    pub counter_payout: Amount,
    /// The per update point to be used by the sending party to setup the next
    /// channel state.
    pub next_per_update_point: PublicKey,
}

impl_dlc_writeable!(SettleOffer, SETTLE_CHANNEL_OFFER_TYPE, {
    (channel_id, writeable),
    (counter_payout, writeable),
    (next_per_update_point, writeable)
});

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "use-serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
/// Message used to accept a previously received settlement offer.
pub struct SettleAccept {
    #[cfg_attr(
        feature = "use-serde",
        serde(
            serialize_with = "crate::serde_utils::serialize_hex",
            deserialize_with = "crate::serde_utils::deserialize_hex_array"
        )
    )]
    /// The id of the channel referred to by the message.
    pub channel_id: [u8; 32],
    /// The per update point to be used by the sending party to setup the next
    /// channel state.
    pub next_per_update_point: PublicKey,
    /// The adaptor signature for the settle transaction generated by the sending
    /// party.
    pub settle_adaptor_signature: EcdsaAdaptorSignature,
}

impl_dlc_writeable!(SettleAccept, SETTLE_CHANNEL_ACCEPT_TYPE, {
    (channel_id, writeable),
    (next_per_update_point, writeable),
    (settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature})
});

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "use-serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
/// Message used to confirm the settlement of a channel.
pub struct SettleConfirm {
    #[cfg_attr(
        feature = "use-serde",
        serde(
            serialize_with = "crate::serde_utils::serialize_hex",
            deserialize_with = "crate::serde_utils::deserialize_hex_array"
        )
    )]
    /// The id of the channel referred to by the message.
    pub channel_id: [u8; 32],
    /// The pre-image of the per update point used by the sending party during
    /// the establishment of the previous channel state.
    pub prev_per_update_secret: SecretKey,
    /// The adaptor signature for the settlement transaction generated by the
    /// sending party.
    pub settle_adaptor_signature: EcdsaAdaptorSignature,
}

impl_dlc_writeable!(SettleConfirm, SETTLE_CHANNEL_CONFIRM_TYPE, {
    (channel_id, writeable),
    (prev_per_update_secret, writeable),
    (settle_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature})
});

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "use-serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
/// Message used to finalize the settlement of a channel.
pub struct SettleFinalize {
    #[cfg_attr(
        feature = "use-serde",
        serde(
            serialize_with = "crate::serde_utils::serialize_hex",
            deserialize_with = "crate::serde_utils::deserialize_hex_array"
        )
    )]
    /// The id of the channel referred to by the message.
    pub channel_id: [u8; 32],
    /// The pre-image of the per update point used by the sending party during
    /// the establishment of the previous channel state.
    pub prev_per_update_secret: SecretKey,
}

impl_dlc_writeable!(SettleFinalize, SETTLE_CHANNEL_FINALIZE_TYPE, {
    (channel_id, writeable),
    (prev_per_update_secret, writeable)
});

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
    feature = "use-serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
/// Message used to offer to establish a new contract within the channel.
pub struct RenewOffer {
    #[cfg_attr(
        feature = "use-serde",
        serde(
            serialize_with = "crate::serde_utils::serialize_hex",
            deserialize_with = "crate::serde_utils::deserialize_hex_array"
        )
    )]
    /// The id of the channel referred to by the message.
    pub channel_id: [u8; 32],
    /// The temporary id of the offered contract.
    pub temporary_contract_id: [u8; 32],
    /// The proposed payout for the receiving party for the previous channel
    /// state.
    pub counter_payout: Amount,
    /// The per update point to be used by the sending party to setup the next
    /// channel state.
    pub next_per_update_point: PublicKey,
    /// Information about the offered contract.
    pub contract_info: ContractInfo,
    /// Lock time for the CETs.
    pub cet_locktime: u32,
    /// Lock time for the refund transaction.
    pub refund_locktime: u32,
    /// The nSequence value to use for the CETs.
    pub cet_nsequence: u32,
}

impl_dlc_writeable!(RenewOffer, RENEW_CHANNEL_OFFER_TYPE, {
    (channel_id, writeable),
    (temporary_contract_id, writeable),
    (counter_payout, writeable),
    (next_per_update_point, writeable),
    (contract_info, writeable),
    (cet_locktime, writeable),
    (refund_locktime, writeable),
    (cet_nsequence, writeable)
});

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "use-serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
/// Message used to accept the establishment of a new contract within a channel.
pub struct RenewAccept {
    #[cfg_attr(
        feature = "use-serde",
        serde(
            serialize_with = "crate::serde_utils::serialize_hex",
            deserialize_with = "crate::serde_utils::deserialize_hex_array"
        )
    )]
    /// The id of the channel referred to by the message.
    pub channel_id: [u8; 32],
    /// The per update point to be used by the sending party to setup the next
    /// channel state.
    pub next_per_update_point: PublicKey,
    /// The adaptor signatures for all CETs generated by the offer party.
    pub cet_adaptor_signatures: CetAdaptorSignatures,
    /// The refund signature generated by the offer party.
    pub refund_signature: Signature,
}

impl_dlc_writeable!(RenewAccept, RENEW_CHANNEL_ACCEPT_TYPE, {
    (channel_id, writeable),
    (next_per_update_point, writeable),
    (cet_adaptor_signatures, writeable),
    (refund_signature, writeable)
});

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "use-serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
/// Message used to confirm the establishment of a new contract within a channel.
pub struct RenewConfirm {
    #[cfg_attr(
        feature = "use-serde",
        serde(
            serialize_with = "crate::serde_utils::serialize_hex",
            deserialize_with = "crate::serde_utils::deserialize_hex_array"
        )
    )]
    /// The id of the channel referred to by the message.
    pub channel_id: [u8; 32],
    /// The adaptor signature for the buffer transaction generated by the offer
    /// party.
    pub buffer_adaptor_signature: EcdsaAdaptorSignature,
    /// The adaptor signatures for all CETs generated by the offer party.
    pub cet_adaptor_signatures: CetAdaptorSignatures,
    /// The refund signature generated by the offer party.
    pub refund_signature: Signature,
}

impl_dlc_writeable!(RenewConfirm, RENEW_CHANNEL_CONFIRM_TYPE, {
    (channel_id, writeable),
    (buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature}),
    (cet_adaptor_signatures, writeable),
    (refund_signature, writeable)
});

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "use-serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
/// Message used to finalize the establishment of a new contract within a channel.
pub struct RenewFinalize {
    #[cfg_attr(
        feature = "use-serde",
        serde(
            serialize_with = "crate::serde_utils::serialize_hex",
            deserialize_with = "crate::serde_utils::deserialize_hex_array"
        )
    )]
    /// The id of the channel referred to by the message.
    pub channel_id: [u8; 32],
    /// The pre image of the per update point used by the sending party to setup
    /// the previous channel state.
    pub per_update_secret: SecretKey,
    /// The adaptor signature for the buffer transaction generated by the accept
    /// party.
    pub buffer_adaptor_signature: EcdsaAdaptorSignature,
}

impl_dlc_writeable!(RenewFinalize, RENEW_CHANNEL_FINALIZE_TYPE, {
    (channel_id, writeable),
    (per_update_secret, writeable),
    (buffer_adaptor_signature, {cb_writeable, write_ecdsa_adaptor_signature, read_ecdsa_adaptor_signature})

});

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
/// Message used to finalize the establishment of a new contract within a channel.
pub struct RenewRevoke {
    #[cfg_attr(
        feature = "serde",
        serde(
            serialize_with = "crate::serde_utils::serialize_hex",
            deserialize_with = "crate::serde_utils::deserialize_hex_array"
        )
    )]
    /// The id of the channel referred to by the message.
    pub channel_id: [u8; 32],
    /// The pre image of the per update point used by the sending party to setup
    /// the previous channel state.
    pub per_update_secret: SecretKey,
}

impl_dlc_writeable!(RenewRevoke, RENEW_CHANNEL_REVOKE_TYPE, {
    (channel_id, writeable),
    (per_update_secret, writeable)
});

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "use-serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
/// Message used to offer to collaboratively close a channel.
pub struct CollaborativeCloseOffer {
    #[cfg_attr(
        feature = "use-serde",
        serde(
            serialize_with = "crate::serde_utils::serialize_hex",
            deserialize_with = "crate::serde_utils::deserialize_hex_array"
        )
    )]
    /// The id of the channel referred to by the message.
    pub channel_id: [u8; 32],
    /// The proposed payout for the receiving party to close the channel with.
    pub counter_payout: Amount,
    /// The signature of the sending party for the closing transaction.
    pub close_signature: Signature,
}

impl_dlc_writeable!(CollaborativeCloseOffer, COLLABORATIVE_CLOSE_OFFER_TYPE, {
    (channel_id, writeable),
    (counter_payout, writeable),
    (close_signature, writeable)
});

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "use-serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
/// Message used to reject an received offer.
pub struct Reject {
    #[cfg_attr(
        feature = "use-serde",
        serde(
            serialize_with = "crate::serde_utils::serialize_hex",
            deserialize_with = "crate::serde_utils::deserialize_hex_array"
        )
    )]
    /// The id of the channel referred to by the message.
    pub channel_id: [u8; 32],
}

impl_dlc_writeable!(Reject, REJECT, { (channel_id, writeable) });