ark-core 0.10.1

Core types and utilities for Ark
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
use crate::boarding_output::BoardingOutput;
use crate::vhtlc::VhtlcOptions;
use crate::vtxo::Vtxo;
use crate::Error;
use bitcoin::absolute;
use bitcoin::key::Secp256k1;
use bitcoin::secp256k1::All;
use bitcoin::taproot::ControlBlock;
use bitcoin::Network;
use bitcoin::ScriptBuf;
use bitcoin::Sequence;
use bitcoin::XOnlyPublicKey;
use serde::Deserialize;
use serde::Serialize;
use std::fmt;

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ContractType(String);

impl ContractType {
    pub fn new(value: impl Into<String>) -> Result<Self, Error> {
        let value = value.into();
        if value.is_empty() {
            return Err(Error::ad_hoc("contract type cannot be empty"));
        }
        Ok(Self(value))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }

    pub fn default_vtxo() -> Self {
        Self("default".to_string())
    }

    pub fn delegate_vtxo() -> Self {
        Self("delegate".to_string())
    }

    pub fn boarding() -> Self {
        Self("boarding".to_string())
    }

    pub fn vhtlc() -> Self {
        Self("vhtlc".to_string())
    }
}

impl fmt::Display for ContractType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl From<&'static str> for ContractType {
    fn from(value: &'static str) -> Self {
        Self(value.to_string())
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContractState {
    Active,
    Inactive,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct StoredContract {
    pub contract_type: ContractType,
    pub contract_version: u32,
    pub script_pubkey: ScriptBuf,
    pub state: ContractState,
    pub created_at: u64,
    /// HD derivation index for contracts generated by an index-based key provider.
    ///
    /// Static/custom providers may not expose an index, and imported contracts may not have one.
    pub key_index: Option<u32>,
    pub data: serde_json::Value,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SpendPathKind {
    Forfeit,
    Exit,
    Delegate,
    VhtlcClaim,
    VhtlcRefund,
    VhtlcRefundWithoutReceiver,
    VhtlcUnilateralClaim,
    VhtlcUnilateralRefund,
    VhtlcUnilateralRefundWithoutReceiver,
    Custom(String),
}

impl SpendPathKind {
    pub fn from_vhtlc_name(name: String) -> Self {
        match name.as_str() {
            "claim" => Self::VhtlcClaim,
            "refund" => Self::VhtlcRefund,
            "refund_without_receiver" => Self::VhtlcRefundWithoutReceiver,
            "unilateral_claim" => Self::VhtlcUnilateralClaim,
            "unilateral_refund" => Self::VhtlcUnilateralRefund,
            "unilateral_refund_without_receiver" => Self::VhtlcUnilateralRefundWithoutReceiver,
            _ => Self::Custom(name),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SpendPath {
    pub kind: SpendPathKind,
    pub script: ScriptBuf,
    pub control_block: ControlBlock,
}

impl SpendPath {
    pub fn new(kind: SpendPathKind, script: ScriptBuf, control_block: ControlBlock) -> Self {
        Self {
            kind,
            script,
            control_block,
        }
    }

    pub fn select(self) -> SpendSelection {
        SpendSelection::new(self)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SpendSelection {
    pub path: SpendPath,
    pub sequence: Option<Sequence>,
    pub locktime: Option<absolute::LockTime>,
    pub extra_witness: Vec<Vec<u8>>,
}

impl SpendSelection {
    pub fn new(path: SpendPath) -> Self {
        Self {
            path,
            sequence: None,
            locktime: None,
            extra_witness: Vec::new(),
        }
    }

    pub fn with_sequence(mut self, sequence: Sequence) -> Self {
        self.sequence = Some(sequence);
        self
    }

    pub fn with_locktime(mut self, locktime: absolute::LockTime) -> Self {
        self.locktime = Some(locktime);
        self
    }

    pub fn with_extra_witness(mut self, extra_witness: Vec<Vec<u8>>) -> Self {
        self.extra_witness = extra_witness;
        self
    }

    pub fn resolved_sequence(&self, default_sequence: Sequence) -> Sequence {
        self.sequence.unwrap_or(default_sequence)
    }

    pub fn resolved_spend_info(
        &self,
        default_sequence: Sequence,
    ) -> (Sequence, (ScriptBuf, ControlBlock)) {
        (self.resolved_sequence(default_sequence), self.spend_info())
    }

    pub fn spend_info(&self) -> (ScriptBuf, ControlBlock) {
        (self.path.script.clone(), self.path.control_block.clone())
    }
}

#[derive(Clone)]
pub struct ContractContext {
    network: Network,
    secp: Secp256k1<All>,
}

impl ContractContext {
    pub fn new(network: Network) -> Self {
        Self {
            network,
            secp: Secp256k1::new(),
        }
    }

    pub fn network(&self) -> Network {
        self.network
    }

    pub fn secp(&self) -> &Secp256k1<All> {
        &self.secp
    }
}

pub trait ContractSpec:
    Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static
{
    const VERSION: u32;

    fn contract_type() -> ContractType;
    fn script_pubkey(&self, ctx: &ContractContext) -> Result<ScriptBuf, Error>;
    fn spendable_paths(&self, ctx: &ContractContext) -> Result<Vec<SpendPath>, Error>;

    fn spendable_selections(&self, ctx: &ContractContext) -> Result<Vec<SpendSelection>, Error> {
        Ok(self
            .spendable_paths(ctx)?
            .into_iter()
            .map(SpendPath::select)
            .collect())
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DefaultVtxoContract {
    pub server: XOnlyPublicKey,
    pub owner: XOnlyPublicKey,
    pub exit_delay: Sequence,
}

impl ContractSpec for DefaultVtxoContract {
    const VERSION: u32 = 1;

    fn contract_type() -> ContractType {
        ContractType::default_vtxo()
    }

    fn script_pubkey(&self, ctx: &ContractContext) -> Result<ScriptBuf, Error> {
        Ok(self.vtxo(ctx)?.script_pubkey())
    }

    fn spendable_paths(&self, ctx: &ContractContext) -> Result<Vec<SpendPath>, Error> {
        let vtxo = self.vtxo(ctx)?;
        let (forfeit_script, forfeit_control_block) = vtxo.forfeit_spend_info()?;
        let (exit_script, exit_control_block) = vtxo.exit_spend_info()?;
        Ok(vec![
            SpendPath::new(
                SpendPathKind::Forfeit,
                forfeit_script,
                forfeit_control_block,
            ),
            SpendPath::new(SpendPathKind::Exit, exit_script, exit_control_block),
        ])
    }

    fn spendable_selections(&self, ctx: &ContractContext) -> Result<Vec<SpendSelection>, Error> {
        Ok(self
            .spendable_paths(ctx)?
            .into_iter()
            .map(|path| {
                if path.kind == SpendPathKind::Exit {
                    path.select().with_sequence(self.exit_delay)
                } else {
                    path.select()
                }
            })
            .collect())
    }
}

impl DefaultVtxoContract {
    pub fn vtxo(&self, ctx: &ContractContext) -> Result<Vtxo, Error> {
        Vtxo::new_default(
            ctx.secp(),
            self.server,
            self.owner,
            self.exit_delay,
            ctx.network(),
        )
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DelegateVtxoContract {
    pub server: XOnlyPublicKey,
    pub owner: XOnlyPublicKey,
    pub delegator: XOnlyPublicKey,
    pub exit_delay: Sequence,
}

impl ContractSpec for DelegateVtxoContract {
    const VERSION: u32 = 1;

    fn contract_type() -> ContractType {
        ContractType::delegate_vtxo()
    }

    fn script_pubkey(&self, ctx: &ContractContext) -> Result<ScriptBuf, Error> {
        Ok(self.vtxo(ctx)?.script_pubkey())
    }

    fn spendable_paths(&self, ctx: &ContractContext) -> Result<Vec<SpendPath>, Error> {
        let vtxo = self.vtxo(ctx)?;
        let (forfeit_script, forfeit_control_block) = vtxo.forfeit_spend_info()?;
        let (exit_script, exit_control_block) = vtxo.exit_spend_info()?;
        let (delegate_script, delegate_control_block) = vtxo.delegate_spend_info()?;
        Ok(vec![
            SpendPath::new(
                SpendPathKind::Forfeit,
                forfeit_script,
                forfeit_control_block,
            ),
            SpendPath::new(SpendPathKind::Exit, exit_script, exit_control_block),
            SpendPath::new(
                SpendPathKind::Delegate,
                delegate_script,
                delegate_control_block,
            ),
        ])
    }

    fn spendable_selections(&self, ctx: &ContractContext) -> Result<Vec<SpendSelection>, Error> {
        Ok(self
            .spendable_paths(ctx)?
            .into_iter()
            .map(|path| {
                if path.kind == SpendPathKind::Exit {
                    path.select().with_sequence(self.exit_delay)
                } else {
                    path.select()
                }
            })
            .collect())
    }
}

impl DelegateVtxoContract {
    pub fn vtxo(&self, ctx: &ContractContext) -> Result<Vtxo, Error> {
        Vtxo::new_with_delegator(
            ctx.secp(),
            self.server,
            self.owner,
            self.delegator,
            self.exit_delay,
            ctx.network(),
        )
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct BoardingContract {
    pub server: XOnlyPublicKey,
    pub owner: XOnlyPublicKey,
    pub exit_delay: Sequence,
}

impl ContractSpec for BoardingContract {
    const VERSION: u32 = 1;

    fn contract_type() -> ContractType {
        ContractType::boarding()
    }

    fn script_pubkey(&self, ctx: &ContractContext) -> Result<ScriptBuf, Error> {
        Ok(self.boarding_output(ctx)?.script_pubkey())
    }

    fn spendable_paths(&self, ctx: &ContractContext) -> Result<Vec<SpendPath>, Error> {
        let boarding_output = self.boarding_output(ctx)?;
        let (forfeit_script, forfeit_control_block) = boarding_output.forfeit_spend_info();
        let (exit_script, exit_control_block) = boarding_output.exit_spend_info();
        Ok(vec![
            SpendPath::new(
                SpendPathKind::Forfeit,
                forfeit_script,
                forfeit_control_block,
            ),
            SpendPath::new(SpendPathKind::Exit, exit_script, exit_control_block),
        ])
    }

    fn spendable_selections(&self, ctx: &ContractContext) -> Result<Vec<SpendSelection>, Error> {
        Ok(self
            .spendable_paths(ctx)?
            .into_iter()
            .map(|path| {
                if path.kind == SpendPathKind::Exit {
                    path.select().with_sequence(self.exit_delay)
                } else {
                    path.select()
                }
            })
            .collect())
    }
}

impl BoardingContract {
    pub fn boarding_output(&self, ctx: &ContractContext) -> Result<BoardingOutput, Error> {
        BoardingOutput::new(
            ctx.secp(),
            self.server,
            self.owner,
            self.exit_delay,
            ctx.network(),
        )
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct VhtlcContract {
    pub options: VhtlcOptions,
}

impl ContractSpec for VhtlcContract {
    const VERSION: u32 = 1;

    fn contract_type() -> ContractType {
        ContractType::vhtlc()
    }

    fn script_pubkey(&self, ctx: &ContractContext) -> Result<ScriptBuf, Error> {
        let script = crate::vhtlc::VhtlcScript::new(self.options.clone(), ctx.network())
            .map_err(|e| Error::ad_hoc(format!("failed to build vhtlc: {e}")))?;
        Ok(script.script_pubkey())
    }

    fn spendable_paths(&self, ctx: &ContractContext) -> Result<Vec<SpendPath>, Error> {
        let script = crate::vhtlc::VhtlcScript::new(self.options.clone(), ctx.network())
            .map_err(|e| Error::ad_hoc(format!("failed to build vhtlc: {e}")))?;
        script
            .get_script_map()
            .into_iter()
            .map(|(name, tapscript)| {
                let control_block = script
                    .taproot_spend_info()
                    .control_block(&(tapscript.clone(), bitcoin::taproot::LeafVersion::TapScript))
                    .ok_or_else(|| Error::ad_hoc("missing vhtlc control block"))?;
                Ok(SpendPath {
                    kind: SpendPathKind::from_vhtlc_name(name),
                    script: tapscript,
                    control_block,
                })
            })
            .collect()
    }

    fn spendable_selections(&self, ctx: &ContractContext) -> Result<Vec<SpendSelection>, Error> {
        Ok(self
            .spendable_paths(ctx)?
            .into_iter()
            .filter_map(|path| match path.kind {
                SpendPathKind::VhtlcClaim | SpendPathKind::VhtlcUnilateralClaim => None,
                SpendPathKind::VhtlcRefundWithoutReceiver => Some(path.select().with_locktime(
                    absolute::LockTime::from_consensus(self.options.refund_locktime),
                )),
                SpendPathKind::VhtlcUnilateralRefund => Some(
                    path.select()
                        .with_sequence(self.options.unilateral_refund_delay),
                ),
                SpendPathKind::VhtlcUnilateralRefundWithoutReceiver => Some(
                    path.select()
                        .with_sequence(self.options.unilateral_refund_without_receiver_delay),
                ),
                SpendPathKind::Forfeit
                | SpendPathKind::Exit
                | SpendPathKind::Delegate
                | SpendPathKind::VhtlcRefund
                | SpendPathKind::Custom(_) => Some(path.select()),
            })
            .collect())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bitcoin::hashes::ripemd160;
    use bitcoin::hashes::Hash;
    use std::str::FromStr;

    fn test_key(hex: &str) -> XOnlyPublicKey {
        XOnlyPublicKey::from_str(hex).unwrap()
    }

    fn vhtlc_contract() -> VhtlcContract {
        VhtlcContract {
            options: VhtlcOptions {
                sender: test_key(
                    "f874c4fd782a63a2b078b424f9f4e5edae622880a29fe572bd71bee45ab55ea0",
                ),
                receiver: test_key(
                    "b02b1a956cd0ed91d1a06b65ae976470ffd4b8c9215f446f266bb74fedd153b6",
                ),
                server: test_key(
                    "e35799157be4b37565bb5afe4d04e6a0fa0a4b6a4f4e48b0d904685d253cdbdb",
                ),
                preimage_hash: ripemd160::Hash::hash(b"preimage"),
                refund_locktime: 500,
                unilateral_claim_delay: Sequence::from_height(10),
                unilateral_refund_delay: Sequence::from_height(20),
                unilateral_refund_without_receiver_delay: Sequence::from_height(30),
            },
        }
    }

    #[test]
    fn vhtlc_spendable_selections_include_required_constraints() {
        let ctx = ContractContext::new(Network::Regtest);
        let selections = vhtlc_contract().spendable_selections(&ctx).unwrap();

        assert!(!selections
            .iter()
            .any(|selection| selection.path.kind == SpendPathKind::VhtlcClaim));
        assert!(!selections
            .iter()
            .any(|selection| selection.path.kind == SpendPathKind::VhtlcUnilateralClaim));

        let refund_without_receiver = selections
            .iter()
            .find(|selection| selection.path.kind == SpendPathKind::VhtlcRefundWithoutReceiver)
            .unwrap();
        assert_eq!(
            refund_without_receiver.locktime,
            Some(absolute::LockTime::from_consensus(500))
        );

        let unilateral_refund = selections
            .iter()
            .find(|selection| selection.path.kind == SpendPathKind::VhtlcUnilateralRefund)
            .unwrap();
        assert_eq!(unilateral_refund.sequence, Some(Sequence::from_height(20)));

        let unilateral_refund_without_receiver = selections
            .iter()
            .find(|selection| {
                selection.path.kind == SpendPathKind::VhtlcUnilateralRefundWithoutReceiver
            })
            .unwrap();
        assert_eq!(
            unilateral_refund_without_receiver.sequence,
            Some(Sequence::from_height(30))
        );
    }
}