bitcoinkernel 0.2.0

Safe Rust bindings to libbitcoinkernel
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
//! Script verification and validation.
//!
//! This module provides functionality for verifying that transaction inputs satisfy
//! the spending conditions defined by their corresponding output scripts.
//!
//! # Overview
//!
//! Script verification involves checking that a transaction input's
//! unlocking script (scriptSig) and witness data satisfy the conditions
//! specified in the output's locking script (scriptPubkey). The verification
//! process depends on the script type and the consensus rules active at the
//! time.
//!
//! # Verification Flags
//!
//! Consensus rules have evolved over time through soft forks. Verification flags
//! allow you to specify which consensus rules to enforce:
//!
//! | Flag | Description | BIP |
//! |------|-------------|-----|
//! | [`VERIFY_P2SH`] | Pay-to-Script-Hash validation | BIP 16 |
//! | [`VERIFY_DERSIG`] | Strict DER signature encoding | BIP 66 |
//! | [`VERIFY_NULLDUMMY`] | Dummy stack element must be empty | BIP 147 |
//! | [`VERIFY_CHECKLOCKTIMEVERIFY`] | CHECKLOCKTIMEVERIFY opcode | BIP 65 |
//! | [`VERIFY_CHECKSEQUENCEVERIFY`] | CHECKSEQUENCEVERIFY opcode | BIP 112 |
//! | [`VERIFY_WITNESS`] | Segregated Witness validation | BIP 141/143 |
//! | [`VERIFY_TAPROOT`] | Taproot validation | BIP 341/342 |
//!
//! # Common Flag Combinations
//!
//! - [`VERIFY_ALL_PRE_TAPROOT`]: All rules except Taproot (for pre-Taproot blocks)
//! - [`VERIFY_ALL`]: All consensus rules including Taproot
//!
//! # Examples
//!
//! ## Basic verification with all consensus rules
//!
//! ```no_run
//! # use bitcoinkernel::{prelude::*, PrecomputedTransactionData, Transaction, verify, VERIFY_ALL};
//! # let spending_tx_bytes = vec![];
//! # let prev_tx_bytes = vec![];
//! # let spending_tx = Transaction::new(&spending_tx_bytes).unwrap();
//! # let prev_tx = Transaction::new(&prev_tx_bytes).unwrap();
//! let prev_output = prev_tx.output(0).unwrap();
//! let tx_data = PrecomputedTransactionData::new(&spending_tx, &[prev_output]).unwrap();
//!
//! let result = verify(
//!     &prev_output.script_pubkey(),
//!     Some(prev_output.value()),
//!     &spending_tx,
//!     0,
//!     Some(VERIFY_ALL),
//!     &tx_data,
//! );
//!
//! match result {
//!     Ok(()) => println!("Script verification passed"),
//!     Err(e) => println!("Script verification failed: {}", e),
//! }
//! ```
//!
//! ## Verifying pre-Taproot transactions
//!
//! ```no_run
//! # use bitcoinkernel::{prelude::*, Transaction, PrecomputedTransactionData, verify, VERIFY_ALL_PRE_TAPROOT};
//! # let spending_tx_bytes = vec![];
//! # let prev_tx_bytes = vec![];
//! # let spending_tx = Transaction::new(&spending_tx_bytes).unwrap();
//! # let prev_tx = Transaction::new(&prev_tx_bytes).unwrap();
//! # let prev_output = prev_tx.output(0).unwrap();
//! let tx_data = PrecomputedTransactionData::new(&prev_tx, &[prev_output]).unwrap();
//! let result = verify(
//!     &prev_output.script_pubkey(),
//!     Some(prev_output.value()),
//!     &spending_tx,
//!     0,
//!     Some(VERIFY_ALL_PRE_TAPROOT),
//!     &tx_data,
//! );
//! ```
//!
//! ## Verifying with multiple spent outputs
//!
//! ```no_run
//! # use bitcoinkernel::{prelude::*, PrecomputedTransactionData, Transaction, verify, VERIFY_ALL};
//! # let spending_tx_bytes = vec![];
//! # let prev_tx1_bytes = vec![];
//! # let prev_tx2_bytes = vec![];
//! # let spending_tx = Transaction::new(&spending_tx_bytes).unwrap();
//! # let prev_tx1 = Transaction::new(&prev_tx1_bytes).unwrap();
//! # let prev_tx2 = Transaction::new(&prev_tx2_bytes).unwrap();
//! let spent_outputs = vec![
//!     prev_tx1.output(0).unwrap(),
//!     prev_tx2.output(1).unwrap(),
//! ];
//! let tx_data = PrecomputedTransactionData::new(&prev_tx1, &spent_outputs).unwrap();
//!
//! let result = verify(
//!     &spent_outputs[0].script_pubkey(),
//!     Some(spent_outputs[0].value()),
//!     &spending_tx,
//!     0,
//!     Some(VERIFY_ALL),
//!     &tx_data,
//! );
//! ```
//!
//! ## Handling verification errors
//!
//! ```no_run
//! # use bitcoinkernel::{prelude::*, PrecomputedTransactionData, Transaction, verify, VERIFY_ALL, KernelError, ScriptVerifyError};
//! # let spending_tx_bytes = vec![];
//! # let prev_tx_bytes = vec![];
//! # let spending_tx = Transaction::new(&spending_tx_bytes).unwrap();
//! # let prev_tx = Transaction::new(&prev_tx_bytes).unwrap();
//! # let prev_output = prev_tx.output(0).unwrap();
//! # let tx_data = PrecomputedTransactionData::new(&prev_tx, &[prev_output]).unwrap();
//! let result = verify(
//!     &prev_output.script_pubkey(),
//!     Some(prev_output.value()),
//!     &spending_tx,
//!     0,
//!     Some(VERIFY_ALL),
//!     &tx_data,
//! );
//!
//! match result {
//!     Ok(()) => {
//!         println!("Valid transaction");
//!     }
//!     Err(KernelError::ScriptVerify(ScriptVerifyError::SpentOutputsRequired)) => {
//!         println!("This script type requires spent outputs");
//!     }
//!     Err(KernelError::ScriptVerify(ScriptVerifyError::InvalidFlagsCombination)) => {
//!         println!("Invalid combination of verification flags");
//!     }
//!     Err(KernelError::ScriptVerify(ScriptVerifyError::Invalid)) => {
//!         println!("Script verification failed - invalid script");
//!     }
//!     Err(e) => {
//!         println!("Other error: {}", e);
//!     }
//! }
//! ```
//!
//! # Thread Safety
//!
//! The [`verify`] function is thread-safe and can be called concurrently from multiple
//! threads. All types used in verification are `Send + Sync`.

use std::{
    error::Error,
    fmt::{self, Display, Formatter},
};

use libbitcoinkernel_sys::{
    btck_PrecomputedTransactionData, btck_ScriptVerificationFlags, btck_ScriptVerifyStatus,
    btck_TransactionOutput, btck_precomputed_transaction_data_copy,
    btck_precomputed_transaction_data_create, btck_precomputed_transaction_data_destroy,
    btck_script_pubkey_verify,
};

use crate::{
    c_helpers,
    ffi::{
        sealed::AsPtr, BTCK_SCRIPT_VERIFICATION_FLAGS_ALL,
        BTCK_SCRIPT_VERIFICATION_FLAGS_CHECKLOCKTIMEVERIFY,
        BTCK_SCRIPT_VERIFICATION_FLAGS_CHECKSEQUENCEVERIFY, BTCK_SCRIPT_VERIFICATION_FLAGS_DERSIG,
        BTCK_SCRIPT_VERIFICATION_FLAGS_NONE, BTCK_SCRIPT_VERIFICATION_FLAGS_NULLDUMMY,
        BTCK_SCRIPT_VERIFICATION_FLAGS_P2SH, BTCK_SCRIPT_VERIFICATION_FLAGS_TAPROOT,
        BTCK_SCRIPT_VERIFICATION_FLAGS_WITNESS,
        BTCK_SCRIPT_VERIFY_STATUS_ERROR_INVALID_FLAGS_COMBINATION,
        BTCK_SCRIPT_VERIFY_STATUS_ERROR_SPENT_OUTPUTS_REQUIRED, BTCK_SCRIPT_VERIFY_STATUS_OK,
    },
    KernelError, ScriptPubkeyExt, TransactionExt, TxOutExt,
};

/// No verification flags.
pub const VERIFY_NONE: btck_ScriptVerificationFlags = BTCK_SCRIPT_VERIFICATION_FLAGS_NONE;

/// Validate Pay-to-Script-Hash (BIP 16).
pub const VERIFY_P2SH: btck_ScriptVerificationFlags = BTCK_SCRIPT_VERIFICATION_FLAGS_P2SH;

/// Require strict DER encoding for ECDSA signatures (BIP 66).
pub const VERIFY_DERSIG: btck_ScriptVerificationFlags = BTCK_SCRIPT_VERIFICATION_FLAGS_DERSIG;

/// Require the dummy element in OP_CHECKMULTISIG to be empty (BIP 147).
pub const VERIFY_NULLDUMMY: btck_ScriptVerificationFlags = BTCK_SCRIPT_VERIFICATION_FLAGS_NULLDUMMY;

/// Enable OP_CHECKLOCKTIMEVERIFY (BIP 65).
pub const VERIFY_CHECKLOCKTIMEVERIFY: btck_ScriptVerificationFlags =
    BTCK_SCRIPT_VERIFICATION_FLAGS_CHECKLOCKTIMEVERIFY;

/// Enable OP_CHECKSEQUENCEVERIFY (BIP 112).
pub const VERIFY_CHECKSEQUENCEVERIFY: btck_ScriptVerificationFlags =
    BTCK_SCRIPT_VERIFICATION_FLAGS_CHECKSEQUENCEVERIFY;

/// Validate Segregated Witness programs (BIP 141/143).
pub const VERIFY_WITNESS: btck_ScriptVerificationFlags = BTCK_SCRIPT_VERIFICATION_FLAGS_WITNESS;

/// Validate Taproot spends (BIP 341/342). Requires spent outputs.
pub const VERIFY_TAPROOT: btck_ScriptVerificationFlags = BTCK_SCRIPT_VERIFICATION_FLAGS_TAPROOT;

/// All consensus rules.
pub const VERIFY_ALL: btck_ScriptVerificationFlags = BTCK_SCRIPT_VERIFICATION_FLAGS_ALL;

/// All consensus rules except Taproot.
pub const VERIFY_ALL_PRE_TAPROOT: btck_ScriptVerificationFlags = VERIFY_P2SH
    | VERIFY_DERSIG
    | VERIFY_NULLDUMMY
    | VERIFY_CHECKLOCKTIMEVERIFY
    | VERIFY_CHECKSEQUENCEVERIFY
    | VERIFY_WITNESS;

/// Precomputed transaction data for verifying a transaction's scripts.
///
/// Precomputes the hashes required to verify a transaction and avoids quadratic
/// hashing costs when verifying multiple scripts from a transaction.
///
/// PrecomputedTransactionData is created from a transaction, and if doing
/// taproot verification, its previous outputs [`crate::TxOut`]. It is required
/// to perform script verification.
///
/// Previous outputs are only required if verifying a taproot transaction. An
/// empty slice may be passed in otherwise.
///
/// # Arguments
///
/// * `tx` - The transaction to precompute data for
/// * `spent_outputs` - Previous transaction outputs being spent (required for taproot)
///
/// # Returns
///
/// * `Ok(...)` - The PrecomputedTransactionData
/// * `Err(KernelError::MismatchedOutputsSize)` - Number of outputs does not match
/// the number of the transaction's inputs.
///
/// # Examples
///
/// Creating a PrecomputedTransactionData:
///
/// ```no_run
/// # use bitcoinkernel::{prelude::*, Transaction, TxOut, PrecomputedTransactionData};
/// # let raw_tx = vec![0u8; 100]; // placeholder
/// # let tx = Transaction::new(&raw_tx).unwrap();
/// # let tx_data = PrecomputedTransactionData::new(&tx, &Vec::<TxOut>::new());
/// ```
#[derive(Debug)]
pub struct PrecomputedTransactionData {
    inner: *mut btck_PrecomputedTransactionData,
}

impl PrecomputedTransactionData {
    pub fn new(
        tx: &impl TransactionExt,
        spent_outputs: &[impl TxOutExt],
    ) -> Result<PrecomputedTransactionData, KernelError> {
        let kernel_spent_outputs: Vec<*const btck_TransactionOutput> =
            spent_outputs.iter().map(|utxo| utxo.as_ptr()).collect();

        let kernel_spent_outputs_ptr = if kernel_spent_outputs.is_empty() {
            std::ptr::null_mut()
        } else {
            if spent_outputs.len() != tx.input_count() {
                return Err(KernelError::MismatchedOutputsSize);
            }
            kernel_spent_outputs.as_ptr() as *mut *const btck_TransactionOutput
        };

        let inner = unsafe {
            btck_precomputed_transaction_data_create(
                tx.as_ptr(),
                kernel_spent_outputs_ptr,
                spent_outputs.len(),
            )
        };
        if inner.is_null() {
            return Err(KernelError::Internal(
                "Failed to create PrecomputedTransactionData".to_string(),
            ));
        }
        Ok(PrecomputedTransactionData { inner })
    }
}

impl AsPtr<btck_PrecomputedTransactionData> for PrecomputedTransactionData {
    fn as_ptr(&self) -> *const btck_PrecomputedTransactionData {
        self.inner as *const _
    }
}

impl Clone for PrecomputedTransactionData {
    fn clone(&self) -> Self {
        PrecomputedTransactionData {
            inner: unsafe { btck_precomputed_transaction_data_copy(self.inner) },
        }
    }
}

impl Drop for PrecomputedTransactionData {
    fn drop(&mut self) {
        unsafe {
            btck_precomputed_transaction_data_destroy(self.inner);
        }
    }
}

unsafe impl Send for PrecomputedTransactionData {}
unsafe impl Sync for PrecomputedTransactionData {}

/// Verifies a transaction input against its corresponding output script.
///
/// This function checks that the transaction input at the specified index properly
/// satisfies the spending conditions defined by the output script. The verification
/// process depends on the script type and the consensus rules specified by the flags.
///
/// # Arguments
///
/// * `script_pubkey` - The output script (locking script) to verify against
/// * `amount` - The amount in satoshis of the output being spent. Required for SegWit
///   and Taproot scripts (when [`VERIFY_WITNESS`] or [`VERIFY_TAPROOT`] flags are set).
///   Optional for pre-SegWit scripts.
/// * `tx_to` - The transaction containing the input to verify (the spending transaction)
/// * `input_index` - The zero-based index of the input within `tx_to` to verify
/// * `flags` - Verification flags specifying which consensus rules to enforce. If `None`,
///   defaults to [`VERIFY_ALL`]. Combine multiple flags using bitwise OR (`|`).
/// * `precomputed_txdata` - The pre-computed hashes required to verify the script. For verifying taproot scripts,
///   this must contain all outputs spent by all inputs in the transaction.
///
/// # Returns
///
/// * `Ok(())` - Verification succeeded; the input properly spends the output
/// * `Err(KernelError::ScriptVerify(ScriptVerifyError::TxInputIndex))` - Input index out of bounds
/// * `Err(KernelError::ScriptVerify(ScriptVerifyError::SpentOutputsMismatch))` - The spent_outputs
///   length is non-zero but doesn't match the number of inputs
/// * `Err(KernelError::ScriptVerify(ScriptVerifyError::InvalidFlags))` - Invalid verification flags
/// * `Err(KernelError::ScriptVerify(ScriptVerifyError::InvalidFlagsCombination))` - Incompatible
///   combination of flags
/// * `Err(KernelError::ScriptVerify(ScriptVerifyError::SpentOutputsRequired))` - Spent outputs
///   are required for this script type but were not provided
/// * `Err(KernelError::ScriptVerify(ScriptVerifyError::Invalid))` - Script verification failed;
///   the input does not properly satisfy the output's spending conditions
///
/// # Examples
///
/// ## Verifying a P2PKH transaction
///
/// ```no_run
/// # use bitcoinkernel::{prelude::*, PrecomputedTransactionData, Transaction, TxOut, verify, VERIFY_ALL};
/// # let tx_bytes = vec![];
/// # let spending_tx = Transaction::new(&tx_bytes).unwrap();
/// # let prev_tx = Transaction::new(&tx_bytes).unwrap();
/// let prev_output = prev_tx.output(0).unwrap();
/// let tx_data = PrecomputedTransactionData::new(&spending_tx, &Vec::<TxOut>::new()).unwrap();
///
/// let result = verify(
///     &prev_output.script_pubkey(),
///     None,
///     &spending_tx,
///     0,
///     Some(VERIFY_ALL),
///     &tx_data,
/// );
/// ```
///
/// ## Using custom flags
///
/// ```no_run
/// # use bitcoinkernel::{prelude::*, PrecomputedTransactionData, Transaction, TxOut, verify, VERIFY_P2SH, VERIFY_DERSIG};
/// # let tx_bytes = vec![];
/// # let spending_tx = Transaction::new(&tx_bytes).unwrap();
/// # let prev_output = spending_tx.output(0).unwrap();
/// // Only verify P2SH and DERSIG rules
/// let custom_flags = VERIFY_P2SH | VERIFY_DERSIG;
/// let tx_data = PrecomputedTransactionData::new(&spending_tx, &Vec::<TxOut>::new()).unwrap();
///
/// let result = verify(
///     &prev_output.script_pubkey(),
///     None,
///     &spending_tx,
///     0,
///     Some(custom_flags),
///     &tx_data,
/// );
/// ```
///
/// # Panics
///
/// This function does not panic under normal circumstances. All error conditions
/// are returned as `Result::Err`.
pub fn verify(
    script_pubkey: &impl ScriptPubkeyExt,
    amount: Option<i64>,
    tx_to: &impl TransactionExt,
    input_index: usize,
    flags: Option<u32>,
    precomputed_txdata: &PrecomputedTransactionData,
) -> Result<(), KernelError> {
    let input_count = tx_to.input_count();

    if input_index >= input_count {
        return Err(KernelError::ScriptVerify(ScriptVerifyError::TxInputIndex));
    }

    let kernel_flags = if let Some(flag) = flags {
        if (flag & !VERIFY_ALL) != 0 {
            return Err(KernelError::ScriptVerify(ScriptVerifyError::InvalidFlags));
        }
        flag
    } else {
        VERIFY_ALL
    };

    let kernel_amount = amount.unwrap_or_default();
    let mut status = ScriptVerifyStatus::Ok.into();

    let ret = unsafe {
        btck_script_pubkey_verify(
            script_pubkey.as_ptr(),
            kernel_amount,
            tx_to.as_ptr(),
            precomputed_txdata.as_ptr(),
            input_index as u32,
            kernel_flags,
            &mut status,
        )
    };

    let script_status = ScriptVerifyStatus::try_from(status).map_err(|_| {
        KernelError::Internal(format!("Invalid script verify status: {:?}", status))
    })?;

    if !c_helpers::verification_passed(ret) {
        let err = match script_status {
            ScriptVerifyStatus::ErrorInvalidFlagsCombination => {
                ScriptVerifyError::InvalidFlagsCombination
            }
            ScriptVerifyStatus::ErrorSpentOutputsRequired => {
                ScriptVerifyError::SpentOutputsRequired
            }
            _ => ScriptVerifyError::Invalid,
        };
        Err(KernelError::ScriptVerify(err))
    } else {
        Ok(())
    }
}

/// Internal status codes from the C verification function.
///
/// These are used internally to distinguish between setup errors (invalid flags,
/// missing data) and actual script verification failures. Converted to
/// [`KernelError::ScriptVerify`] variants in the public API.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
enum ScriptVerifyStatus {
    /// Script verification completed successfully
    Ok = BTCK_SCRIPT_VERIFY_STATUS_OK,

    /// Invalid or inconsistent verification flags were provided.
    ///
    /// This occurs when the supplied `script_verify_flags` combination violates
    /// internal consistency rules. For example:
    ///
    /// - `SCRIPT_VERIFY_CLEANSTACK` is set without also enabling either
    ///   `SCRIPT_VERIFY_P2SH` or `SCRIPT_VERIFY_WITNESS`.
    /// - `SCRIPT_VERIFY_WITNESS` is set without also enabling `SCRIPT_VERIFY_P2SH`.
    ///
    /// These combinations are considered invalid and result in an immediate
    /// verification setup failure rather than a script execution failure.
    ErrorInvalidFlagsCombination = BTCK_SCRIPT_VERIFY_STATUS_ERROR_INVALID_FLAGS_COMBINATION,

    /// Spent outputs are required but were not provided.
    ///
    /// Taproot scripts require the complete set of outputs being spent to properly
    /// validate witness data. This occurs when the TAPROOT flag is set but no spent
    /// outputs were provided.
    ErrorSpentOutputsRequired = BTCK_SCRIPT_VERIFY_STATUS_ERROR_SPENT_OUTPUTS_REQUIRED,
}

impl From<ScriptVerifyStatus> for btck_ScriptVerifyStatus {
    fn from(status: ScriptVerifyStatus) -> Self {
        status as btck_ScriptVerifyStatus
    }
}

impl From<btck_ScriptVerifyStatus> for ScriptVerifyStatus {
    fn from(value: btck_ScriptVerifyStatus) -> Self {
        match value {
            BTCK_SCRIPT_VERIFY_STATUS_OK => ScriptVerifyStatus::Ok,
            BTCK_SCRIPT_VERIFY_STATUS_ERROR_INVALID_FLAGS_COMBINATION => {
                ScriptVerifyStatus::ErrorInvalidFlagsCombination
            }
            BTCK_SCRIPT_VERIFY_STATUS_ERROR_SPENT_OUTPUTS_REQUIRED => {
                ScriptVerifyStatus::ErrorSpentOutputsRequired
            }
            _ => panic!("Unknown script verify status: {}", value),
        }
    }
}

/// Errors that can occur during script verification.
///
/// These errors represent both configuration problems (incorrect parameters)
/// and actual verification failures (invalid scripts).
#[derive(Debug)]
pub enum ScriptVerifyError {
    /// The specified input index is out of bounds.
    ///
    /// The `input_index` parameter is greater than or equal to the number
    /// of inputs in the transaction.
    TxInputIndex,

    /// Invalid verification flags were provided.
    ///
    /// The flags parameter contains bits that don't correspond to any
    /// defined verification flag.
    InvalidFlags,

    /// Invalid or inconsistent verification flags were provided.
    ///
    /// This occurs when the supplied `script_verify_flags` combination violates
    /// internal consistency rules.
    InvalidFlagsCombination,

    /// Spent outputs are required but were not provided.
    SpentOutputsRequired,

    /// Script verification failed.
    Invalid,
}

impl Display for ScriptVerifyError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            ScriptVerifyError::TxInputIndex => write!(f, "Transaction input index out of bounds"),
            ScriptVerifyError::InvalidFlags => write!(f, "Invalid verification flags"),
            ScriptVerifyError::InvalidFlagsCombination => {
                write!(f, "Invalid combination of verification flags")
            }
            ScriptVerifyError::SpentOutputsRequired => {
                write!(f, "Spent outputs required for verification")
            }
            ScriptVerifyError::Invalid => write!(f, "Script verification failed"),
        }
    }
}

impl Error for ScriptVerifyError {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_verify_constants() {
        assert_eq!(VERIFY_NONE, BTCK_SCRIPT_VERIFICATION_FLAGS_NONE);
        assert_eq!(VERIFY_P2SH, BTCK_SCRIPT_VERIFICATION_FLAGS_P2SH);
        assert_eq!(VERIFY_DERSIG, BTCK_SCRIPT_VERIFICATION_FLAGS_DERSIG);
        assert_eq!(VERIFY_NULLDUMMY, BTCK_SCRIPT_VERIFICATION_FLAGS_NULLDUMMY);
        assert_eq!(
            VERIFY_CHECKLOCKTIMEVERIFY,
            BTCK_SCRIPT_VERIFICATION_FLAGS_CHECKLOCKTIMEVERIFY
        );
        assert_eq!(
            VERIFY_CHECKSEQUENCEVERIFY,
            BTCK_SCRIPT_VERIFICATION_FLAGS_CHECKSEQUENCEVERIFY
        );
        assert_eq!(VERIFY_WITNESS, BTCK_SCRIPT_VERIFICATION_FLAGS_WITNESS);
        assert_eq!(VERIFY_TAPROOT, BTCK_SCRIPT_VERIFICATION_FLAGS_TAPROOT);
        assert_eq!(VERIFY_ALL, BTCK_SCRIPT_VERIFICATION_FLAGS_ALL);
    }

    #[test]
    fn test_verify_all_pre_taproot() {
        let expected = VERIFY_P2SH
            | VERIFY_DERSIG
            | VERIFY_NULLDUMMY
            | VERIFY_CHECKLOCKTIMEVERIFY
            | VERIFY_CHECKSEQUENCEVERIFY
            | VERIFY_WITNESS;

        assert_eq!(VERIFY_ALL_PRE_TAPROOT, expected);

        assert_eq!(VERIFY_ALL_PRE_TAPROOT & VERIFY_TAPROOT, 0);
    }

    #[test]
    fn test_verification_flag_combinations() {
        let flags = VERIFY_P2SH | VERIFY_WITNESS;
        assert!(flags & VERIFY_P2SH != 0);
        assert!(flags & VERIFY_WITNESS != 0);
        assert!(flags & VERIFY_TAPROOT == 0);
    }

    #[test]
    #[allow(clippy::assertions_on_constants)]
    fn test_verify_all_includes_all_flags() {
        assert!((VERIFY_ALL & VERIFY_P2SH) != 0);
        assert!((VERIFY_ALL & VERIFY_DERSIG) != 0);
        assert!((VERIFY_ALL & VERIFY_NULLDUMMY) != 0);
        assert!((VERIFY_ALL & VERIFY_CHECKLOCKTIMEVERIFY) != 0);
        assert!((VERIFY_ALL & VERIFY_CHECKSEQUENCEVERIFY) != 0);
        assert!((VERIFY_ALL & VERIFY_WITNESS) != 0);
        assert!((VERIFY_ALL & VERIFY_TAPROOT) != 0);
    }

    #[test]
    fn test_script_verify_status_from_kernel() {
        let ok: ScriptVerifyStatus = BTCK_SCRIPT_VERIFY_STATUS_OK.into();
        assert_eq!(ok, ScriptVerifyStatus::Ok);

        let invalid_flags: ScriptVerifyStatus =
            BTCK_SCRIPT_VERIFY_STATUS_ERROR_INVALID_FLAGS_COMBINATION.into();
        assert_eq!(
            invalid_flags,
            ScriptVerifyStatus::ErrorInvalidFlagsCombination
        );

        let spent_required: ScriptVerifyStatus =
            BTCK_SCRIPT_VERIFY_STATUS_ERROR_SPENT_OUTPUTS_REQUIRED.into();
        assert_eq!(
            spent_required,
            ScriptVerifyStatus::ErrorSpentOutputsRequired
        );
    }

    #[test]
    fn test_script_verify_status_to_kernel() {
        let ok: btck_ScriptVerifyStatus = ScriptVerifyStatus::Ok.into();
        assert_eq!(ok, BTCK_SCRIPT_VERIFY_STATUS_OK);

        let invalid_flags: btck_ScriptVerifyStatus =
            ScriptVerifyStatus::ErrorInvalidFlagsCombination.into();
        assert_eq!(
            invalid_flags,
            BTCK_SCRIPT_VERIFY_STATUS_ERROR_INVALID_FLAGS_COMBINATION
        );

        let spent_required: btck_ScriptVerifyStatus =
            ScriptVerifyStatus::ErrorSpentOutputsRequired.into();
        assert_eq!(
            spent_required,
            BTCK_SCRIPT_VERIFY_STATUS_ERROR_SPENT_OUTPUTS_REQUIRED
        );
    }

    #[test]
    fn test_script_verify_status_round_trip() {
        let statuses = vec![
            ScriptVerifyStatus::Ok,
            ScriptVerifyStatus::ErrorInvalidFlagsCombination,
            ScriptVerifyStatus::ErrorSpentOutputsRequired,
        ];

        for status in statuses {
            let kernel: btck_ScriptVerifyStatus = status.into();
            let back: ScriptVerifyStatus = kernel.into();
            assert_eq!(status, back);
        }
    }

    #[test]
    #[should_panic(expected = "Unknown script verify status")]
    fn test_script_verify_status_invalid_value() {
        let _: ScriptVerifyStatus = 255.into();
    }

    #[test]
    fn test_script_verify_status_traits() {
        let status1 = ScriptVerifyStatus::Ok;
        let status2 = ScriptVerifyStatus::Ok;

        let cloned = status1.clone();
        assert_eq!(cloned, status2);

        let copied = status1;
        assert_eq!(copied, status2);

        assert_eq!(status1, status2);
        assert_ne!(status1, ScriptVerifyStatus::ErrorInvalidFlagsCombination);

        let debug_str = format!("{:?}", status1);
        assert!(debug_str.contains("Ok"));
    }

    #[test]
    fn test_script_verify_error_debug() {
        let errors = vec![
            ScriptVerifyError::TxInputIndex,
            ScriptVerifyError::InvalidFlags,
            ScriptVerifyError::InvalidFlagsCombination,
            ScriptVerifyError::SpentOutputsRequired,
            ScriptVerifyError::Invalid,
        ];

        for err in errors {
            let debug_str = format!("{:?}", err);
            assert!(!debug_str.is_empty());
        }
    }
}