revault_tx 0.5.0

Bitcoin Script descriptors and transactions creation routines for Revault
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
use super::{
    transaction_chain, CancelTransaction, CpfpTransaction, CpfpableTransaction, DepositTransaction,
    EmergencyAddress, EmergencyTransaction, RevaultPresignedTransaction, RevaultTransaction,
    SpendTransaction, UnvaultEmergencyTransaction, UnvaultTransaction, CPFP_MIN_CHANGE,
    DEPOSIT_MIN_SATS,
};

use crate::{error::*, scripts::*, txins::*, txouts::*};

use std::{iter::repeat_with, str::FromStr};

use miniscript::{
    bitcoin::{
        secp256k1,
        util::psbt::PartiallySignedTransaction as Psbt,
        util::{bip143::SigHashCache, bip32},
        Address, Amount, Network, OutPoint, SigHash, SigHashType, Transaction, TxIn, TxOut,
    },
    descriptor::{DescriptorPublicKey, DescriptorXKey, Wildcard},
    Descriptor, DescriptorTrait, MiniscriptKey,
};

fn get_random_privkey(rng: &mut fastrand::Rng) -> bip32::ExtendedPrivKey {
    let rand_bytes: Vec<u8> = repeat_with(|| rng.u8(..)).take(64).collect();

    bip32::ExtendedPrivKey::new_master(Network::Bitcoin, &rand_bytes)
        .unwrap_or_else(|_| get_random_privkey(rng))
}

// This generates the master private keys to derive directly from master, so it's
// [None]<xpub_goes_here>m/* descriptor pubkeys
fn get_participants_sets(
    n_stk: usize,
    n_man: usize,
    with_cosig_servers: bool,
    secp: &secp256k1::Secp256k1<secp256k1::All>,
) -> (
    (Vec<bip32::ExtendedPrivKey>, Vec<DescriptorPublicKey>),
    (Vec<bip32::ExtendedPrivKey>, Vec<DescriptorPublicKey>),
    (Vec<bip32::ExtendedPrivKey>, Vec<DescriptorPublicKey>),
    (Vec<bip32::ExtendedPrivKey>, Vec<DescriptorPublicKey>),
) {
    let mut rng = fastrand::Rng::new();

    let mut managers_priv = Vec::with_capacity(n_man);
    let mut managers = Vec::with_capacity(n_man);
    let mut mancpfp_priv = Vec::with_capacity(n_man);
    let mut mancpfp = Vec::with_capacity(n_man);
    for i in 0..n_man {
        managers_priv.push(get_random_privkey(&mut rng));
        managers.push(DescriptorPublicKey::XPub(DescriptorXKey {
            origin: None,
            xkey: bip32::ExtendedPubKey::from_private(&secp, &managers_priv[i]),
            derivation_path: bip32::DerivationPath::from(vec![]),
            wildcard: Wildcard::Unhardened,
        }));

        mancpfp_priv.push(get_random_privkey(&mut rng));
        mancpfp.push(DescriptorPublicKey::XPub(DescriptorXKey {
            origin: None,
            xkey: bip32::ExtendedPubKey::from_private(&secp, &mancpfp_priv[i]),
            derivation_path: bip32::DerivationPath::from(vec![]),
            wildcard: Wildcard::Unhardened,
        }));
    }

    let mut stakeholders_priv = Vec::with_capacity(n_stk);
    let mut stakeholders = Vec::with_capacity(n_stk);
    let mut cosigners_priv = Vec::with_capacity(n_stk);
    let mut cosigners = Vec::with_capacity(n_stk);
    for i in 0..n_stk {
        stakeholders_priv.push(get_random_privkey(&mut rng));
        stakeholders.push(DescriptorPublicKey::XPub(DescriptorXKey {
            origin: None,
            xkey: bip32::ExtendedPubKey::from_private(&secp, &stakeholders_priv[i]),
            derivation_path: bip32::DerivationPath::from(vec![]),
            wildcard: Wildcard::Unhardened,
        }));

        if with_cosig_servers {
            cosigners_priv.push(get_random_privkey(&mut rng));
            cosigners.push(DescriptorPublicKey::XPub(DescriptorXKey {
                origin: None,
                xkey: bip32::ExtendedPubKey::from_private(&secp, &cosigners_priv[i]),
                derivation_path: bip32::DerivationPath::from(vec![]),
                wildcard: Wildcard::Unhardened,
            }));
        }
    }

    (
        (managers_priv, managers),
        (mancpfp_priv, mancpfp),
        (stakeholders_priv, stakeholders),
        (cosigners_priv, cosigners),
    )
}

// Routine for ""signing"" a transaction
fn satisfy_transaction_input(
    secp: &secp256k1::Secp256k1<secp256k1::All>,
    tx: &mut impl RevaultTransaction,
    input_index: usize,
    tx_sighash: &SigHash,
    xprivs: &Vec<bip32::ExtendedPrivKey>,
    child_number: bip32::ChildNumber,
) -> Result<(), Error> {
    let derivation_path = bip32::DerivationPath::from(vec![child_number]);

    for xpriv in xprivs {
        let sig = secp.sign(
            &secp256k1::Message::from_slice(&tx_sighash).unwrap(),
            &xpriv
                .derive_priv(&secp, &derivation_path)
                .unwrap()
                .private_key
                .key,
        );

        let xpub = DescriptorPublicKey::XPub(DescriptorXKey {
            origin: None,
            xkey: bip32::ExtendedPubKey::from_private(&secp, xpriv),
            derivation_path: bip32::DerivationPath::from(vec![]),
            wildcard: Wildcard::Unhardened,
        });
        let key = xpub
            .derive(child_number.into())
            .derive_public_key(secp)
            .unwrap();

        tx.add_signature(input_index, key.key, sig, secp)?;
    }

    Ok(())
}

fn sign_psbt(
    secp: &secp256k1::Secp256k1<secp256k1::All>,
    psbt: &mut Psbt,
    input_index: usize,
    xprivs: &[bip32::ExtendedPrivKey],
    child_number: bip32::ChildNumber,
) {
    for xpriv in xprivs {
        let deriv_path = bip32::DerivationPath::from(vec![child_number]);
        let sig = {
            let prev_amount = psbt.inputs[input_index]
                .witness_utxo
                .as_ref()
                .unwrap()
                .value;
            let script_code = psbt.inputs[input_index].witness_script.as_ref().unwrap();
            let sighash = SigHashCache::new(&psbt.global.unsigned_tx).signature_hash(
                input_index,
                &script_code,
                prev_amount,
                SigHashType::All,
            );
            secp.sign(
                &secp256k1::Message::from_slice(&sighash).unwrap(),
                &xpriv
                    .derive_priv(&secp, &deriv_path)
                    .unwrap()
                    .private_key
                    .key,
            )
        };

        let xpub = bip32::ExtendedPubKey::from_private(&secp, xpriv);
        let pubkey = xpub.derive_pub(secp, &deriv_path).unwrap();
        let mut sig = sig.serialize_der().to_vec();
        sig.push(SigHashType::All.as_u32() as u8);
        psbt.inputs[input_index]
            .partial_sigs
            .insert(pubkey.public_key, sig);
    }
}

fn finalize_psbt(secp: &secp256k1::Secp256k1<impl secp256k1::Verification>, psbt: &mut Psbt) {
    miniscript::psbt::finalize(psbt, secp)
        .map_err(|e| Error::TransactionFinalisation(e.to_string()))
        .unwrap();
}

fn desc_san_check<P: MiniscriptKey>(desc: &Descriptor<P>) -> Result<(), ScriptCreationError> {
    match desc {
        Descriptor::Wsh(wsh) => wsh.sanity_check().map_err(|e| e.into()),
        _ => unreachable!(),
    }
}

macro_rules! roundtrip {
    ($tx:ident, $tx_type:ident) => {
        #[cfg(feature = "use-serde")]
        {
            let serialized_tx = serde_json::to_string(&$tx).unwrap();
            let deserialized_tx = serde_json::from_str(&serialized_tx).unwrap();
            assert_eq!($tx, deserialized_tx);
        }

        let serialized_tx = $tx.to_string();
        let deserialized_tx: $tx_type = FromStr::from_str(&serialized_tx).unwrap();
        assert_eq!($tx, deserialized_tx);
    };
}

/// Derive transactions for a given deployment configuration, asserting some invariants
pub fn derive_transactions(
    n_stk: usize,
    n_man: usize,
    csv: u32,
    deposit_prevout: OutPoint,
    deposit_value: u64,
    // Outpoint and amount of inputs of a Spend
    unvault_spends: Vec<(OutPoint, u64)>,
    with_cosig_servers: bool,
    secp: &secp256k1::Secp256k1<secp256k1::All>,
) -> Result<(), Error> {
    // Let's get the 10th key of each
    let child_number = bip32::ChildNumber::from(10);

    // Keys, keys, keys everywhere !
    let (
        (managers_priv, managers),
        (mancpfp_priv, mancpfp),
        (stakeholders_priv, stakeholders),
        (cosigners_priv, cosigners),
    ) = get_participants_sets(n_stk, n_man, with_cosig_servers, secp);

    // Get the script descriptors for the txos we're going to create
    let unvault_descriptor = UnvaultDescriptor::new(
        stakeholders.clone(),
        managers.clone(),
        managers.len(),
        cosigners.clone(),
        csv,
    )?;
    assert_eq!(unvault_descriptor.csv_value(), csv);
    let cpfp_descriptor = CpfpDescriptor::new(mancpfp)?;
    let deposit_descriptor = DepositDescriptor::new(stakeholders)?;

    desc_san_check(
        deposit_descriptor
            .derive(child_number.into(), &secp)
            .inner(),
    )?;
    desc_san_check(
        unvault_descriptor
            .derive(child_number.into(), &secp)
            .inner(),
    )?;
    desc_san_check(cpfp_descriptor.derive(child_number.into(), &secp).inner())?;

    // We reuse the deposit descriptor for the emergency address
    let emergency_address = EmergencyAddress::from(Address::p2wsh(
        &deposit_descriptor
            .derive(child_number, secp)
            .inner()
            .explicit_script(),
        Network::Bitcoin,
    ))
    .expect("It's a P2WSH");

    let der_deposit_descriptor = deposit_descriptor.derive(child_number, secp);
    let der_unvault_descriptor = unvault_descriptor.derive(child_number, secp);
    assert_eq!(
        der_unvault_descriptor.csv_value(),
        unvault_descriptor.csv_value()
    );
    let der_cpfp_descriptor = cpfp_descriptor.derive(child_number, secp);

    // The funding transaction does not matter (random txid from my mempool)
    let deposit_scriptpubkey = der_deposit_descriptor.inner().script_pubkey();
    let deposit_raw_tx = Transaction {
        version: 2,
        lock_time: 0,
        input: vec![TxIn {
            previous_output: deposit_prevout,
            ..TxIn::default()
        }],
        output: vec![TxOut {
            value: deposit_value,
            script_pubkey: deposit_scriptpubkey.clone(),
        }],
    };
    let deposit_txo = DepositTxOut::new(
        Amount::from_sat(deposit_raw_tx.output[0].value),
        &der_deposit_descriptor,
    );
    let deposit_tx = DepositTransaction(deposit_raw_tx);
    let deposit_outpoint = OutPoint {
        txid: deposit_tx.0.txid(),
        vout: 0,
    };
    let deposit_txin = DepositTxIn::new(deposit_outpoint, deposit_txo.clone());

    // Test that the transaction helper(s) derive the same transactions as we do
    let (h_unvault, h_cancel, h_emer, h_unemer) = transaction_chain(
        deposit_outpoint,
        Amount::from_sat(deposit_txo.txout().value),
        &deposit_descriptor,
        &unvault_descriptor,
        &cpfp_descriptor,
        child_number,
        emergency_address.clone(),
        secp,
    )?;

    // Create and sign the first (deposit) emergency transaction
    let mut emergency_tx =
        EmergencyTransaction::new(deposit_txin.clone(), emergency_address.clone())?;
    assert_eq!(h_emer, emergency_tx);
    assert_eq!(
        emergency_tx.emergency_outpoint(),
        OutPoint {
            txid: emergency_tx.txid(),
            vout: 0
        }
    );

    // 376 is the witstrip weight of an emer tx (1 segwit input, 1 P2WSH txout), 250 is the feerate is sat/WU
    assert_eq!(
        emergency_tx.fees().as_sat(),
        (376 + deposit_txin.txout().max_sat_weight() as u64) * 250,
    );
    // We cannot get a sighash for a non-existing input
    assert_eq!(
        emergency_tx.signature_hash(10),
        Err(InputSatisfactionError::OutOfBounds)
    );
    // But for an existing one, all good
    let emergency_tx_sighash_vault = emergency_tx.sig_hash().expect("Input exists");
    roundtrip!(emergency_tx, EmergencyTransaction);
    satisfy_transaction_input(
        &secp,
        &mut emergency_tx,
        0,
        &emergency_tx_sighash_vault,
        &stakeholders_priv,
        child_number,
    )?;
    roundtrip!(emergency_tx, EmergencyTransaction);
    emergency_tx.finalize(&secp)?;
    roundtrip!(emergency_tx, EmergencyTransaction);

    // Create but don't sign the unvaulting transaction until all revaulting transactions
    // are finalized
    let deposit_txin_sat_cost = deposit_txin.txout().max_sat_weight();
    let mut unvault_tx = UnvaultTransaction::new(
        deposit_txin.clone(),
        &der_unvault_descriptor,
        &der_cpfp_descriptor,
    )?;
    roundtrip!(unvault_tx, UnvaultTransaction);

    assert_eq!(h_unvault, unvault_tx);
    let unvault_value = unvault_tx.psbt().global.unsigned_tx.output[0].value;
    // 548 is the witstrip weight of an unvault tx (1 segwit input, 2 P2WSH txouts), 6 is the
    // feerate is sat/WU, and 30_000 is the CPFP output value.
    assert_eq!(
        unvault_tx.fees().as_sat(),
        (548 + deposit_txin_sat_cost as u64) * 6
    );

    // Create and sign the cancel transaction
    let rev_unvault_txin = unvault_tx.revault_unvault_txin(&der_unvault_descriptor);
    assert_eq!(rev_unvault_txin.txout().txout().value, unvault_value);
    let mut cancel_tx = CancelTransaction::new(
        rev_unvault_txin.clone(),
        &der_deposit_descriptor,
        Amount::from_sat(50),
    )?;
    roundtrip!(cancel_tx, CancelTransaction);
    assert_eq!(h_cancel.feerate_200(), &cancel_tx);
    assert_eq!(
        cancel_tx.deposit_txin(&der_deposit_descriptor).outpoint(),
        OutPoint {
            txid: cancel_tx.txid(),
            vout: 0
        }
    );
    // 376 is the witstrip weight of a cancel tx (1 segwit input, 1 P2WSH txout), 50 is the feerate is sat/WU
    assert_eq!(
        cancel_tx.fees().as_sat(),
        (376 + rev_unvault_txin.txout().max_sat_weight() as u64) * 50,
    );
    let cancel_tx_sighash = cancel_tx.sig_hash().expect("Input exists");
    roundtrip!(cancel_tx, CancelTransaction);
    satisfy_transaction_input(
        &secp,
        &mut cancel_tx,
        0,
        &cancel_tx_sighash,
        &stakeholders_priv,
        child_number,
    )?;
    roundtrip!(cancel_tx, CancelTransaction);
    cancel_tx.finalize(&secp).unwrap();
    roundtrip!(cancel_tx, CancelTransaction);

    let mut unemergency_tx =
        UnvaultEmergencyTransaction::new(rev_unvault_txin.clone(), emergency_address.clone())?;
    roundtrip!(unemergency_tx, UnvaultEmergencyTransaction);
    assert_eq!(h_unemer, unemergency_tx);
    assert_eq!(
        unemergency_tx.emergency_outpoint(),
        OutPoint {
            txid: unemergency_tx.txid(),
            vout: 0
        }
    );

    // 376 is the witstrip weight of an emer tx (1 segwit input, 1 P2WSH txout), 75 is the feerate is sat/WU
    assert_eq!(
        unemergency_tx.fees().as_sat(),
        (376 + rev_unvault_txin.txout().max_sat_weight() as u64) * 250,
    );
    let unemergency_tx_sighash = unemergency_tx.sig_hash().expect("Input exists");
    roundtrip!(unemergency_tx, UnvaultEmergencyTransaction);
    satisfy_transaction_input(
        &secp,
        &mut unemergency_tx,
        0,
        &unemergency_tx_sighash,
        &stakeholders_priv,
        child_number,
    )?;
    roundtrip!(unemergency_tx, UnvaultEmergencyTransaction);
    unemergency_tx.finalize(&secp)?;
    roundtrip!(unemergency_tx, UnvaultEmergencyTransaction);

    // Now we can sign the unvault
    let unvault_tx_sighash = unvault_tx.sig_hash().expect("Input exists");
    satisfy_transaction_input(
        &secp,
        &mut unvault_tx,
        0,
        &unvault_tx_sighash,
        &stakeholders_priv,
        child_number,
    )?;
    roundtrip!(unvault_tx, UnvaultTransaction);
    unvault_tx.finalize(&secp)?;
    roundtrip!(unvault_tx, UnvaultTransaction);

    // Create a CPFP transaction for the unvault
    // Some fake listunspent outputs
    let listunspent = vec![
        CpfpTxIn::new(
            OutPoint::from_str(
                "f21596dd9df36b86bcf65f0884f1f20675c1fc185bc78a37a9cddb4ae5e3dd9f:0",
            )
            .unwrap(),
            CpfpTxOut::new(Amount::from_sat(30_000), &der_cpfp_descriptor),
        ),
        CpfpTxIn::new(
            OutPoint::from_str(
                "f21596dd9df36b86bcf65f0884f1f20675c1fc185bc78a37a9cddb4ae5e3dd9f:1",
            )
            .unwrap(),
            CpfpTxOut::new(Amount::from_sat(30_000), &der_cpfp_descriptor),
        ),
    ];

    let cpfp_txin = unvault_tx.cpfp_txin(&cpfp_descriptor, &secp).unwrap();
    let cpfp_txins = vec![cpfp_txin.clone(), cpfp_txin];
    let tbc_weight = unvault_tx.max_weight() * 2;
    let tbc_fees = unvault_tx.fees() * 2;
    // Let's ask for a decent feerate
    let added_feerate = 6121;
    // We try to feebump two unvaults in 1 transaction
    let cpfp_tx = CpfpTransaction::from_txins(
        cpfp_txins.clone(),
        tbc_weight,
        tbc_fees,
        added_feerate,
        listunspent.clone(),
    )
    .unwrap();

    // The cpfp tx contains the input of the tx to be cpfped, right?
    assert!(cpfp_tx.tx().input.contains(&cpfp_txins[0].unsigned_txin()));

    assert_eq!(cpfp_tx.tx().output.len(), 1);
    {
        let o = &cpfp_tx.tx().output[0];
        // Either the change is 0 with an OP_RETURN,
        // or its value is bigger than CPFP_MIN_CHANGE, and we send
        // back to the cpfp_txin script_pubkey
        assert!(
            (o.value == 0 && o.script_pubkey.is_op_return())
                || (o.value >= CPFP_MIN_CHANGE
                    && o.script_pubkey == cpfp_txins[0].txout().txout().script_pubkey)
        );
    }

    // we sign the cpfp and then check the package feerate
    let cpfp_fees = cpfp_tx.fees();
    let inputs_len = cpfp_tx.psbt().inputs.len();
    let mut psbt = cpfp_tx.into_psbt();
    for i in 0..inputs_len {
        sign_psbt(&secp, &mut psbt, i, &mancpfp_priv, child_number);
    }
    finalize_psbt(&secp, &mut psbt);
    assert!(
        1000 * (cpfp_fees + unvault_tx.fees()).as_sat()
            / (psbt.global.unsigned_tx.get_weight() as u64 + unvault_tx.max_weight())
            >= unvault_tx.max_feerate() * 1000 + added_feerate
    );

    // Create and sign a spend transaction
    let spend_unvault_txin = unvault_tx.spend_unvault_txin(&der_unvault_descriptor);
    let unvault_value = spend_unvault_txin.txout().txout().value;
    let dummy_txo = TxOut::default();
    let cpfp_value = SpendTransaction::cpfp_txout(
        vec![spend_unvault_txin.clone()],
        vec![SpendTxOut::new(dummy_txo.clone())],
        None,
        &der_cpfp_descriptor,
        0,
    )
    .txout()
    .value;
    let change_value = unvault_value
        .checked_sub(cpfp_value)
        .expect("We would never create such a tx chain (dust)");
    // The overhead incurred to the value of the CPFP output by the change output
    // See https://github.com/revault/practical-revault/blob/master/transactions.md#spend_tx
    const P2WSH_TXO_WEIGHT: u64 = 43 * 4;
    let cpfp_change_overhead = 16 * P2WSH_TXO_WEIGHT;
    let fees = 10_000;
    let (spend_txo, change_txo) = if unvault_value
        > change_value + cpfp_value + cpfp_change_overhead + fees
        && change_value > DEPOSIT_MIN_SATS + fees + cpfp_change_overhead
    {
        (
            TxOut {
                value: unvault_value - cpfp_value - cpfp_change_overhead - change_value - fees,
                ..TxOut::default()
            },
            Some(DepositTxOut::new(
                Amount::from_sat(change_value - cpfp_value - fees),
                &der_deposit_descriptor,
            )),
        )
    } else {
        (
            TxOut {
                value: unvault_value - cpfp_value - fees,
                ..TxOut::default()
            },
            None,
        )
    };
    let mut spend_tx = SpendTransaction::new(
        vec![spend_unvault_txin.clone()],
        vec![SpendTxOut::new(spend_txo.clone())],
        change_txo,
        &der_cpfp_descriptor,
        0,
        true,
    )
    .expect("Amounts ok");
    roundtrip!(spend_tx, SpendTransaction);
    let spend_tx_sighash = spend_tx.signature_hash(0).expect("Input exists");
    satisfy_transaction_input(
        &secp,
        &mut spend_tx,
        0,
        &spend_tx_sighash,
        &managers_priv
            .iter()
            .chain(cosigners_priv.iter())
            .copied()
            .collect::<Vec<bip32::ExtendedPrivKey>>(),
        child_number,
    )?;
    roundtrip!(spend_tx, SpendTransaction);
    spend_tx.finalize(&secp)?;
    roundtrip!(spend_tx, SpendTransaction);

    // We can't create a dust output with the Spend
    let dust_txo = TxOut {
        value: 470,
        ..TxOut::default()
    };
    SpendTransaction::new(
        vec![spend_unvault_txin.clone()],
        vec![SpendTxOut::new(dust_txo.clone())],
        None,
        &der_cpfp_descriptor,
        0,
        true,
    )
    .expect_err("Creating a dust output");

    // We can't create a dust change output with the Spend
    SpendTransaction::new(
        vec![spend_unvault_txin],
        vec![],
        Some(DepositTxOut::new(
            Amount::from_sat(329),
            &der_deposit_descriptor,
        )),
        &der_cpfp_descriptor,
        0,
        true,
    )
    .expect_err("Creating a dust output");

    // The spend transaction can also batch multiple unvault txos
    if unvault_spends.len() == 0 {
        return Err(Error::TransactionCreation(
            TransactionCreationError::NegativeFees,
        ));
    }
    let spend_unvault_txins: Vec<UnvaultTxIn> = unvault_spends
        .into_iter()
        .map(|(outpoint, value)| {
            UnvaultTxIn::new(
                outpoint,
                UnvaultTxOut::new(Amount::from_sat(value), &der_unvault_descriptor),
                csv,
            )
        })
        .collect();
    let n_txins = spend_unvault_txins.len();
    let dummy_txo = TxOut::default();
    let cpfp_value = SpendTransaction::cpfp_txout(
        spend_unvault_txins.clone(),
        vec![SpendTxOut::new(dummy_txo.clone())],
        None,
        &der_cpfp_descriptor,
        0,
    )
    .txout()
    .value;
    let fees = 30_000;
    let mut in_value: u64 = 0;
    for txin in spend_unvault_txins.iter() {
        in_value = in_value
            .checked_add(txin.txout().txout().value)
            .ok_or(TransactionCreationError::InsaneAmounts)?;
    }
    let spend_txo = TxOut {
        value: in_value
            .checked_sub(cpfp_value)
            .ok_or(TransactionCreationError::InsaneAmounts)?
            .checked_sub(fees)
            .ok_or(TransactionCreationError::InsaneAmounts)?,
        ..TxOut::default()
    };
    let mut spend_tx = SpendTransaction::new(
        spend_unvault_txins,
        vec![SpendTxOut::new(spend_txo.clone())],
        None,
        &der_cpfp_descriptor,
        0,
        true,
    )?;
    roundtrip!(spend_tx, SpendTransaction);
    assert_eq!(spend_tx.fees().as_sat(), fees);
    let mut hash_cache = SigHashCache::new(spend_tx.tx());
    let sighashes: Vec<SigHash> = (0..n_txins)
        .into_iter()
        .map(|i| {
            spend_tx
                .signature_hash_cached(i, &mut hash_cache)
                .expect("Input exists")
        })
        .collect();
    for (i, spend_tx_sighash) in sighashes.into_iter().enumerate() {
        satisfy_transaction_input(
            &secp,
            &mut spend_tx,
            i,
            &spend_tx_sighash,
            &managers_priv
                .iter()
                .chain(cosigners_priv.iter())
                .copied()
                .collect::<Vec<bip32::ExtendedPrivKey>>(),
            child_number,
        )?
    }

    // Create a CPFP transaction for the (not yet finalized) Spend
    // Some fake listunspent outputs
    let listunspent = vec![
        CpfpTxIn::new(
            OutPoint::from_str(
                "f21596dd9df36b86bcf65f0884f1f20675c1fc185bc78a37a9cddb4ae5e3dd9f:0",
            )
            .unwrap(),
            CpfpTxOut::new(Amount::from_sat(58_000), &der_cpfp_descriptor),
        ),
        CpfpTxIn::new(
            OutPoint::from_str(
                "f21596dd9df36b86bcf65f0884f1f20675c1fc185bc78a37a9cddb4ae5e3dd9f:1",
            )
            .unwrap(),
            CpfpTxOut::new(Amount::from_sat(23_000), &der_cpfp_descriptor),
        ),
    ];

    let cpfp_txin = spend_tx.cpfp_txin(&cpfp_descriptor, &secp).unwrap();
    let cpfp_txins = vec![cpfp_txin.clone()];
    let tbc_weight = spend_tx.max_weight();
    let tbc_fees = spend_tx.fees();
    let added_feerate = 6121;
    let cpfp_tx = CpfpTransaction::from_txins(
        cpfp_txins,
        tbc_weight,
        tbc_fees,
        added_feerate,
        listunspent.clone(),
    )
    .unwrap();

    // The cpfp tx contains the input of the tx to be cpfped
    assert!(cpfp_tx.tx().input.contains(&cpfp_txin.unsigned_txin()));
    assert_eq!(cpfp_tx.tx().output.len(), 1);
    // Either the change is 0 with an OP_RETURN,
    // or its value is bigger than CPFP_MIN_CHANGE, and we send
    // back to the cpfp_txin script_pubkey
    {
        let o = &cpfp_tx.tx().output[0];
        assert!(
            (o.value == 0 && o.script_pubkey.is_op_return())
                || (o.value >= CPFP_MIN_CHANGE
                    && o.script_pubkey == cpfp_txin.txout().txout().script_pubkey)
        );
    }

    // we sign the cpfp and then check the package feerate
    let cpfp_fees = cpfp_tx.fees();
    let inputs_len = cpfp_tx.psbt().inputs.len();
    let mut psbt = cpfp_tx.into_psbt();
    for i in 0..inputs_len {
        sign_psbt(&secp, &mut psbt, i, &mancpfp_priv, child_number);
    }
    finalize_psbt(&secp, &mut psbt);
    assert!(
        1000 * (cpfp_fees + spend_tx.fees()).as_sat()
            / (psbt.global.unsigned_tx.get_weight() as u64 + spend_tx.max_weight())
            >= spend_tx.max_feerate() * 1000 + added_feerate
    );

    roundtrip!(spend_tx, SpendTransaction);
    spend_tx.finalize(&secp)?;
    roundtrip!(spend_tx, SpendTransaction);

    Ok(())
}

pub fn seed_rng(seed: u64) {
    fastrand::seed(seed);
}