bsv-rs 0.3.4

BSV blockchain SDK for Rust - primitives, script, transactions, and more
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
//! PushDrop Script Template
//!
//! A data envelope template that embeds arbitrary data in a Bitcoin transaction
//! output, protected by a signature. Used by token protocols like BSV-20.
//!
//! # Script Structure
//!
//! ## Lock-Before Pattern (default)
//! ```text
//! <pubkey> OP_CHECKSIG <field1> <field2> ... OP_2DROP ... OP_DROP
//! ```
//!
//! ## Lock-After Pattern
//! ```text
//! <field1> <field2> ... OP_2DROP ... OP_DROP <pubkey> OP_CHECKSIG
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use bsv_rs::script::templates::PushDrop;
//! use bsv_rs::primitives::ec::PrivateKey;
//!
//! let privkey = PrivateKey::random();
//! let pubkey = privkey.public_key();
//! let fields = vec![b"hello".to_vec(), b"world".to_vec()];
//!
//! let pushdrop = PushDrop::new(pubkey, fields);
//! let script = pushdrop.lock();
//! ```

use crate::error::Error;
use crate::primitives::bsv::TransactionSignature;
use crate::primitives::ec::{PrivateKey, PublicKey};
use crate::script::op::*;
use crate::script::template::{
    compute_sighash_scope, ScriptTemplateUnlock, SignOutputs, SigningContext,
};
use crate::script::{LockingScript, Script, ScriptChunk, UnlockingScript};
use crate::Result;

/// Lock position for PushDrop template.
///
/// Determines whether the public key and OP_CHECKSIG come before or after
/// the data fields in the locking script.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LockPosition {
    /// Pubkey and OP_CHECKSIG come before data fields (default).
    /// Script: `<pubkey> OP_CHECKSIG <fields...> OP_2DROP... OP_DROP`
    #[default]
    Before,
    /// Pubkey and OP_CHECKSIG come after data fields.
    /// Script: `<fields...> OP_2DROP... OP_DROP <pubkey> OP_CHECKSIG`
    After,
}

/// PushDrop locking script template.
///
/// This template creates scripts that embed arbitrary data fields alongside
/// a P2PK (Pay-to-Public-Key) lock. The data fields are pushed onto the stack
/// and then dropped, leaving only the signature verification.
///
/// # Example
///
/// ```rust,ignore
/// use bsv_rs::script::templates::PushDrop;
/// use bsv_rs::primitives::ec::PrivateKey;
///
/// let privkey = PrivateKey::random();
/// let pubkey = privkey.public_key();
///
/// // Create with embedded token data
/// let fields = vec![
///     b"BSV20".to_vec(),
///     b"transfer".to_vec(),
///     b"1000".to_vec(),
/// ];
///
/// let pushdrop = PushDrop::new(pubkey, fields);
/// let locking_script = pushdrop.lock();
/// ```
#[derive(Debug, Clone)]
pub struct PushDrop {
    /// The public key that can unlock this output.
    pub locking_public_key: PublicKey,
    /// Embedded data fields.
    pub fields: Vec<Vec<u8>>,
    /// Lock position (before or after data).
    pub lock_position: LockPosition,
}

impl PushDrop {
    /// Creates a new PushDrop template with lock-before pattern.
    ///
    /// # Arguments
    ///
    /// * `locking_public_key` - Public key that can spend this output
    /// * `fields` - Data fields to embed
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use bsv_rs::script::templates::PushDrop;
    /// use bsv_rs::primitives::ec::PrivateKey;
    ///
    /// let privkey = PrivateKey::random();
    /// let pubkey = privkey.public_key();
    /// let fields = vec![b"hello".to_vec(), b"world".to_vec()];
    ///
    /// let pushdrop = PushDrop::new(pubkey, fields);
    /// let script = pushdrop.lock();
    /// ```
    pub fn new(locking_public_key: PublicKey, fields: Vec<Vec<u8>>) -> Self {
        Self {
            locking_public_key,
            fields,
            lock_position: LockPosition::Before,
        }
    }

    /// Sets the lock position and returns self for chaining.
    ///
    /// # Arguments
    ///
    /// * `position` - The lock position (Before or After)
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use bsv_rs::script::templates::{PushDrop, LockPosition};
    /// use bsv_rs::primitives::ec::PrivateKey;
    ///
    /// let privkey = PrivateKey::random();
    /// let pubkey = privkey.public_key();
    ///
    /// let pushdrop = PushDrop::new(pubkey, vec![b"data".to_vec()])
    ///     .with_position(LockPosition::After);
    /// ```
    pub fn with_position(mut self, position: LockPosition) -> Self {
        self.lock_position = position;
        self
    }

    /// Creates the locking script.
    ///
    /// # Returns
    ///
    /// The locking script containing the embedded data and P2PK lock.
    pub fn lock(&self) -> LockingScript {
        let mut chunks = Vec::new();

        match self.lock_position {
            LockPosition::Before => {
                // <pubkey> OP_CHECKSIG <fields...> OP_2DROP... OP_DROP
                chunks.push(ScriptChunk::new_push(
                    self.locking_public_key.to_compressed().to_vec(),
                ));
                chunks.push(ScriptChunk::new_opcode(OP_CHECKSIG));

                // Add fields with minimal encoding
                for field in &self.fields {
                    chunks.push(Self::create_minimally_encoded_chunk(field));
                }

                // Add DROP operations
                Self::add_drop_operations(&mut chunks, self.fields.len());
            }
            LockPosition::After => {
                // <fields...> OP_2DROP... OP_DROP <pubkey> OP_CHECKSIG
                for field in &self.fields {
                    chunks.push(Self::create_minimally_encoded_chunk(field));
                }

                Self::add_drop_operations(&mut chunks, self.fields.len());

                chunks.push(ScriptChunk::new_push(
                    self.locking_public_key.to_compressed().to_vec(),
                ));
                chunks.push(ScriptChunk::new_opcode(OP_CHECKSIG));
            }
        }

        LockingScript::from_chunks(chunks)
    }

    /// Creates a minimally encoded script chunk for data.
    ///
    /// Bitcoin Script requires minimal encoding for data pushes:
    /// - Empty data or single zero byte -> OP_0
    /// - Single byte 1-16 -> OP_1 through OP_16
    /// - Single byte 0x81 (-1) -> OP_1NEGATE
    /// - Otherwise -> standard data push
    fn create_minimally_encoded_chunk(data: &[u8]) -> ScriptChunk {
        // Empty or single zero byte -> OP_0
        if data.is_empty() || (data.len() == 1 && data[0] == 0) {
            return ScriptChunk::new_opcode(OP_0);
        }

        // Single byte special cases
        if data.len() == 1 {
            let byte = data[0];
            // Values 1-16 -> OP_1 through OP_16
            if (1..=16).contains(&byte) {
                // OP_1 = 0x51, so OP_N = 0x50 + N
                return ScriptChunk::new_opcode(0x50 + byte);
            }
            // 0x81 (-1 in Bitcoin script number encoding) -> OP_1NEGATE
            if byte == 0x81 {
                return ScriptChunk::new_opcode(OP_1NEGATE);
            }
        }

        // Otherwise, standard data push
        ScriptChunk::new_push(data.to_vec())
    }

    /// Adds appropriate DROP operations for the field count.
    ///
    /// Uses OP_2DROP for pairs of fields and OP_DROP for a remaining single field.
    fn add_drop_operations(chunks: &mut Vec<ScriptChunk>, count: usize) {
        let mut remaining = count;

        // Use OP_2DROP for pairs
        while remaining >= 2 {
            chunks.push(ScriptChunk::new_opcode(OP_2DROP));
            remaining -= 2;
        }

        // Use OP_DROP for remaining single item
        if remaining == 1 {
            chunks.push(ScriptChunk::new_opcode(OP_DROP));
        }
    }

    /// Decodes a PushDrop locking script.
    ///
    /// Extracts the public key and embedded fields from a locking script.
    ///
    /// # Arguments
    ///
    /// * `script` - The locking script to decode
    ///
    /// # Returns
    ///
    /// The decoded PushDrop template, or an error if the script format is invalid.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use bsv_rs::script::templates::PushDrop;
    /// use bsv_rs::script::LockingScript;
    ///
    /// let script = LockingScript::from_hex("...")?;
    /// let pushdrop = PushDrop::decode(&script)?;
    /// println!("Fields: {:?}", pushdrop.fields);
    /// ```
    pub fn decode(script: &LockingScript) -> Result<Self> {
        let chunks = script.chunks();

        if chunks.len() < 2 {
            return Err(Error::ScriptParseError(
                "Script too short for PushDrop".into(),
            ));
        }

        // Determine lock position by checking first chunk
        // If first chunk is a 33 or 65 byte data push, it's lock-before
        let first_is_pubkey = chunks[0]
            .data
            .as_ref()
            .map(|d| d.len() == 33 || d.len() == 65)
            .unwrap_or(false);

        if first_is_pubkey {
            Self::decode_lock_before(&chunks)
        } else {
            Self::decode_lock_after(&chunks)
        }
    }

    /// Decodes a lock-before pattern script.
    /// Pattern: <pubkey> OP_CHECKSIG <fields...> OP_2DROP... OP_DROP
    fn decode_lock_before(chunks: &[ScriptChunk]) -> Result<Self> {
        // First chunk must be pubkey data
        let pubkey_data = chunks[0]
            .data
            .as_ref()
            .ok_or_else(|| Error::ScriptParseError("Expected public key".into()))?;
        let locking_public_key = PublicKey::from_bytes(pubkey_data)?;

        // Second chunk must be OP_CHECKSIG
        if chunks[1].op != OP_CHECKSIG {
            return Err(Error::ScriptParseError(
                "Expected OP_CHECKSIG after pubkey".into(),
            ));
        }

        // Extract fields (between OP_CHECKSIG and first DROP)
        let mut fields = Vec::new();
        for chunk in chunks.iter().skip(2) {
            // Stop when we hit a DROP operation
            if chunk.op == OP_DROP || chunk.op == OP_2DROP {
                break;
            }
            fields.push(Self::chunk_to_bytes(chunk));
        }

        Ok(Self {
            locking_public_key,
            fields,
            lock_position: LockPosition::Before,
        })
    }

    /// Decodes a lock-after pattern script.
    /// Pattern: <fields...> OP_2DROP... OP_DROP <pubkey> OP_CHECKSIG
    fn decode_lock_after(chunks: &[ScriptChunk]) -> Result<Self> {
        if chunks.len() < 2 {
            return Err(Error::ScriptParseError("Script too short".into()));
        }

        let last_idx = chunks.len() - 1;

        // Last chunk must be OP_CHECKSIG
        if chunks[last_idx].op != OP_CHECKSIG {
            return Err(Error::ScriptParseError(
                "Expected OP_CHECKSIG at end".into(),
            ));
        }

        // Second to last must be pubkey
        let pubkey_data = chunks[last_idx - 1].data.as_ref().ok_or_else(|| {
            Error::ScriptParseError("Expected public key before OP_CHECKSIG".into())
        })?;
        let locking_public_key = PublicKey::from_bytes(pubkey_data)?;

        // Extract fields (before the DROP operations)
        let mut fields = Vec::new();
        for chunk in chunks.iter().take(last_idx - 1) {
            // Stop when we hit a DROP operation
            if chunk.op == OP_DROP || chunk.op == OP_2DROP {
                break;
            }
            fields.push(Self::chunk_to_bytes(chunk));
        }

        Ok(Self {
            locking_public_key,
            fields,
            lock_position: LockPosition::After,
        })
    }

    /// Converts a script chunk to bytes, handling minimal encoding.
    fn chunk_to_bytes(chunk: &ScriptChunk) -> Vec<u8> {
        // If chunk has data, return it
        if let Some(ref data) = chunk.data {
            return data.clone();
        }

        // Handle opcodes that represent data
        let op = chunk.op;

        // OP_0 -> [0]
        if op == OP_0 {
            return vec![0];
        }

        // OP_1 through OP_16 -> [1] through [16]
        if (OP_1..=OP_16).contains(&op) {
            return vec![op - 0x50];
        }

        // OP_1NEGATE -> [0x81]
        if op == OP_1NEGATE {
            return vec![0x81];
        }

        // Other opcodes have no data representation
        Vec::new()
    }

    /// Creates an unlock template for spending a PushDrop output.
    ///
    /// PushDrop uses a P2PK (Pay-to-Public-Key) lock, so the unlocking script
    /// is just a signature. Unlike P2PKH, the public key is already in the
    /// locking script, so it doesn't need to be repeated in the unlock.
    ///
    /// # Arguments
    ///
    /// * `private_key` - The private key for signing (must match the public key in the lock)
    /// * `sign_outputs` - Which outputs to sign
    /// * `anyone_can_pay` - Whether to allow other inputs to be added
    ///
    /// # Returns
    ///
    /// A [`ScriptTemplateUnlock`] that can sign transaction inputs.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use bsv_rs::script::templates::PushDrop;
    /// use bsv_rs::script::template::{SignOutputs, SigningContext};
    /// use bsv_rs::primitives::ec::PrivateKey;
    ///
    /// let private_key = PrivateKey::random();
    /// let public_key = private_key.public_key();
    /// let fields = vec![b"token_data".to_vec()];
    ///
    /// // Create locking script
    /// let pushdrop = PushDrop::new(public_key, fields);
    /// let locking_script = pushdrop.lock();
    ///
    /// // Create unlock template
    /// let unlock = PushDrop::unlock(&private_key, SignOutputs::All, false);
    ///
    /// // Estimate length for fee calculation (73 bytes)
    /// let estimated_size = unlock.estimate_length();
    ///
    /// // Sign with a transaction context
    /// let context = SigningContext::new(&raw_tx, input_index, satoshis, locking_script.as_script());
    /// let unlocking_script = unlock.sign(&context)?;
    /// ```
    pub fn unlock(
        private_key: &PrivateKey,
        sign_outputs: SignOutputs,
        anyone_can_pay: bool,
    ) -> ScriptTemplateUnlock {
        let key = private_key.clone();
        let scope = compute_sighash_scope(sign_outputs, anyone_can_pay);

        ScriptTemplateUnlock::new(
            move |context: &SigningContext| {
                // Compute the sighash
                let sighash = context.compute_sighash(scope)?;

                // Sign the sighash
                let signature = key.sign(&sighash)?;
                let tx_sig = TransactionSignature::new(signature, scope);

                // Build the unlocking script (signature only, no pubkey for P2PK)
                let sig_bytes = tx_sig.to_checksig_format();

                let mut script = Script::new();
                script.write_bin(&sig_bytes);

                Ok(UnlockingScript::from_script(script))
            },
            || {
                // Estimate length: 1 (push opcode) + 72 (max DER signature + sighash byte)
                // = 73 bytes (matches TypeScript SDK)
                73
            },
        )
    }

    /// Creates an unlocking script with a precomputed sighash.
    ///
    /// This is useful when you already have the sighash computed and don't
    /// need to parse the transaction.
    ///
    /// # Arguments
    ///
    /// * `private_key` - The private key for signing
    /// * `sighash` - The precomputed sighash to sign
    /// * `sign_outputs` - Which outputs to sign (for the scope byte)
    /// * `anyone_can_pay` - Whether to allow other inputs to be added
    ///
    /// # Returns
    ///
    /// The unlocking script, or an error if signing fails.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use bsv_rs::script::templates::PushDrop;
    /// use bsv_rs::script::template::SignOutputs;
    /// use bsv_rs::primitives::ec::PrivateKey;
    ///
    /// let private_key = PrivateKey::random();
    /// let sighash: [u8; 32] = compute_sighash_externally();
    ///
    /// let unlocking = PushDrop::sign_with_sighash(
    ///     &private_key,
    ///     &sighash,
    ///     SignOutputs::All,
    ///     false,
    /// )?;
    /// ```
    pub fn sign_with_sighash(
        private_key: &PrivateKey,
        sighash: &[u8; 32],
        sign_outputs: SignOutputs,
        anyone_can_pay: bool,
    ) -> Result<UnlockingScript> {
        let scope = compute_sighash_scope(sign_outputs, anyone_can_pay);

        // Sign the sighash
        let signature = private_key.sign(sighash)?;
        let tx_sig = TransactionSignature::new(signature, scope);

        // Build the unlocking script (signature only for P2PK)
        let sig_bytes = tx_sig.to_checksig_format();

        let mut script = Script::new();
        script.write_bin(&sig_bytes);

        Ok(UnlockingScript::from_script(script))
    }

    /// Estimates the unlocking script length.
    ///
    /// For a PushDrop output (P2PK lock), the unlocking script is just a signature.
    /// Unlike P2PKH, the public key is already in the locking script.
    ///
    /// # Returns
    ///
    /// The estimated length in bytes (73 bytes for signature only).
    /// This matches the TypeScript SDK's estimate.
    pub fn estimate_unlocking_length(&self) -> usize {
        // For P2PK style: 1 (push opcode) + ~72 (DER signature + sighash byte)
        // = 73 bytes max
        // This matches the TypeScript SDK's estimateLength() return value
        73
    }
}

impl PartialEq for PushDrop {
    fn eq(&self, other: &Self) -> bool {
        self.locking_public_key.to_compressed() == other.locking_public_key.to_compressed()
            && self.fields == other.fields
            && self.lock_position == other.lock_position
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::primitives::ec::PrivateKey;

    #[test]
    fn test_pushdrop_lock_before() {
        let privkey = PrivateKey::random();
        let pubkey = privkey.public_key();

        let fields = vec![b"field1".to_vec(), b"field2".to_vec()];
        let pushdrop = PushDrop::new(pubkey.clone(), fields.clone());
        let script = pushdrop.lock();

        // Decode and verify
        let decoded = PushDrop::decode(&script).unwrap();
        assert_eq!(
            decoded.locking_public_key.to_compressed(),
            pubkey.to_compressed()
        );
        assert_eq!(decoded.fields, fields);
        assert_eq!(decoded.lock_position, LockPosition::Before);
    }

    #[test]
    fn test_pushdrop_lock_after() {
        let privkey = PrivateKey::random();
        let pubkey = privkey.public_key();

        let fields = vec![b"data".to_vec()];
        let pushdrop =
            PushDrop::new(pubkey.clone(), fields.clone()).with_position(LockPosition::After);
        let script = pushdrop.lock();

        let decoded = PushDrop::decode(&script).unwrap();
        assert_eq!(decoded.lock_position, LockPosition::After);
        assert_eq!(
            decoded.locking_public_key.to_compressed(),
            pubkey.to_compressed()
        );
    }

    #[test]
    fn test_pushdrop_minimal_encoding() {
        let privkey = PrivateKey::random();
        let pubkey = privkey.public_key();

        // Test minimal encoding for small values
        let fields = vec![
            vec![0],    // Should become OP_0
            vec![1],    // Should become OP_1
            vec![16],   // Should become OP_16
            vec![0x81], // Should become OP_1NEGATE
            vec![17],   // Regular push (not a special opcode)
        ];

        let pushdrop = PushDrop::new(pubkey, fields.clone());
        let script = pushdrop.lock();

        let decoded = PushDrop::decode(&script).unwrap();
        assert_eq!(decoded.fields.len(), 5);

        // Check the decoded values match the original fields
        assert_eq!(decoded.fields[0], vec![0]);
        assert_eq!(decoded.fields[1], vec![1]);
        assert_eq!(decoded.fields[2], vec![16]);
        assert_eq!(decoded.fields[3], vec![0x81]);
        assert_eq!(decoded.fields[4], vec![17]);
    }

    #[test]
    fn test_pushdrop_empty_fields() {
        let privkey = PrivateKey::random();
        let pubkey = privkey.public_key();

        let pushdrop = PushDrop::new(pubkey.clone(), vec![]);
        let script = pushdrop.lock();

        // Should be simple <pubkey> OP_CHECKSIG (like P2PK)
        let chunks = script.chunks();
        assert_eq!(chunks.len(), 2);

        let decoded = PushDrop::decode(&script).unwrap();
        assert!(decoded.fields.is_empty());
    }

    #[test]
    fn test_pushdrop_large_field() {
        let privkey = PrivateKey::random();
        let pubkey = privkey.public_key();

        // 10KB field
        let large_field = vec![0xABu8; 10_000];
        let pushdrop = PushDrop::new(pubkey, vec![large_field.clone()]);
        let script = pushdrop.lock();

        let decoded = PushDrop::decode(&script).unwrap();
        assert_eq!(decoded.fields[0], large_field);
    }

    #[test]
    fn test_pushdrop_drop_count() {
        let privkey = PrivateKey::random();
        let pubkey = privkey.public_key();

        // 5 fields should produce 2x OP_2DROP + 1x OP_DROP
        let fields: Vec<Vec<u8>> = (0..5).map(|i| vec![i as u8 + 17]).collect(); // Use values > 16 to avoid OP_N
        let pushdrop = PushDrop::new(pubkey, fields);
        let script = pushdrop.lock();

        let chunks = script.chunks();
        let drop_count = chunks.iter().filter(|c| c.op == OP_DROP).count();
        let drop2_count = chunks.iter().filter(|c| c.op == OP_2DROP).count();

        assert_eq!(drop2_count, 2);
        assert_eq!(drop_count, 1);
    }

    #[test]
    fn test_pushdrop_estimate_unlocking_length() {
        let privkey = PrivateKey::random();
        let pubkey = privkey.public_key();

        let pushdrop = PushDrop::new(pubkey, vec![b"test".to_vec()]);
        // P2PK unlocking is just <signature>, so 73 bytes (1 push + 72 max DER + sighash)
        assert_eq!(pushdrop.estimate_unlocking_length(), 73);
    }

    #[test]
    fn test_pushdrop_unlock_estimate_length() {
        use crate::script::template::SignOutputs;

        let privkey = PrivateKey::random();
        let unlock = PushDrop::unlock(&privkey, SignOutputs::All, false);

        // Should match the instance method
        assert_eq!(unlock.estimate_length(), 73);
    }

    #[test]
    fn test_pushdrop_sign_with_sighash() {
        use crate::script::template::SignOutputs;

        let private_key = PrivateKey::from_hex(
            "0000000000000000000000000000000000000000000000000000000000000001",
        )
        .unwrap();

        // Create a simple test case with a mock sighash
        let sighash = [1u8; 32];
        let unlocking =
            PushDrop::sign_with_sighash(&private_key, &sighash, SignOutputs::All, false).unwrap();

        // The unlocking script should have 1 chunk: signature only (P2PK style)
        let chunks = unlocking.chunks();
        assert_eq!(chunks.len(), 1);

        // First (and only) chunk should be the signature (push data)
        assert!(chunks[0].data.is_some());
        let sig_data = chunks[0].data.as_ref().unwrap();
        // DER signature + 1 byte sighash
        assert!(sig_data.len() >= 70 && sig_data.len() <= 73);

        // Last byte should be the sighash type
        assert_eq!(
            sig_data.last().unwrap(),
            &0x41_u8 // SIGHASH_ALL | SIGHASH_FORKID
        );
    }

    #[test]
    fn test_pushdrop_sign_outputs_variants() {
        use crate::script::template::SignOutputs;

        let private_key = PrivateKey::random();
        let sighash = [1u8; 32];

        // Test ALL
        let unlocking =
            PushDrop::sign_with_sighash(&private_key, &sighash, SignOutputs::All, false).unwrap();
        let chunks = unlocking.chunks();
        let sig_data = chunks[0].data.as_ref().unwrap();
        assert_eq!(sig_data.last().unwrap(), &0x41u8); // ALL | FORKID

        // Test NONE
        let unlocking =
            PushDrop::sign_with_sighash(&private_key, &sighash, SignOutputs::None, false).unwrap();
        let chunks = unlocking.chunks();
        let sig_data = chunks[0].data.as_ref().unwrap();
        assert_eq!(sig_data.last().unwrap(), &0x42u8); // NONE | FORKID

        // Test SINGLE
        let unlocking =
            PushDrop::sign_with_sighash(&private_key, &sighash, SignOutputs::Single, false)
                .unwrap();
        let chunks = unlocking.chunks();
        let sig_data = chunks[0].data.as_ref().unwrap();
        assert_eq!(sig_data.last().unwrap(), &0x43u8); // SINGLE | FORKID

        // Test ALL | ANYONECANPAY
        let unlocking =
            PushDrop::sign_with_sighash(&private_key, &sighash, SignOutputs::All, true).unwrap();
        let chunks = unlocking.chunks();
        let sig_data = chunks[0].data.as_ref().unwrap();
        assert_eq!(sig_data.last().unwrap(), &0xC1u8); // ALL | FORKID | ANYONECANPAY
    }

    #[test]
    fn test_pushdrop_empty_data_becomes_op_0() {
        let privkey = PrivateKey::random();
        let pubkey = privkey.public_key();

        // Empty vec should become OP_0
        let fields = vec![vec![]];
        let pushdrop = PushDrop::new(pubkey, fields);
        let script = pushdrop.lock();

        // Third chunk (after pubkey and OP_CHECKSIG) should be OP_0
        let chunks = script.chunks();
        assert_eq!(chunks[2].op, OP_0);
        assert!(chunks[2].data.is_none());

        // Decoded should give us [0] back (our convention for OP_0)
        let decoded = PushDrop::decode(&script).unwrap();
        assert_eq!(decoded.fields[0], vec![0]);
    }

    #[test]
    fn test_pushdrop_roundtrip_all_special_values() {
        let privkey = PrivateKey::random();
        let pubkey = privkey.public_key();

        // Test all special encoding cases
        let fields = vec![
            vec![],     // OP_0
            vec![0],    // OP_0
            vec![1],    // OP_1
            vec![2],    // OP_2
            vec![15],   // OP_15
            vec![16],   // OP_16
            vec![0x81], // OP_1NEGATE
            vec![17],   // Regular push
            vec![255],  // Regular push
        ];

        let pushdrop = PushDrop::new(pubkey, fields);
        let script = pushdrop.lock();

        let decoded = PushDrop::decode(&script).unwrap();

        // Empty and [0] both decode to [0] (OP_0)
        assert_eq!(decoded.fields[0], vec![0]);
        assert_eq!(decoded.fields[1], vec![0]);
        assert_eq!(decoded.fields[2], vec![1]);
        assert_eq!(decoded.fields[3], vec![2]);
        assert_eq!(decoded.fields[4], vec![15]);
        assert_eq!(decoded.fields[5], vec![16]);
        assert_eq!(decoded.fields[6], vec![0x81]);
        assert_eq!(decoded.fields[7], vec![17]);
        assert_eq!(decoded.fields[8], vec![255]);
    }

    #[test]
    fn test_pushdrop_lock_after_multiple_fields() {
        let privkey = PrivateKey::random();
        let pubkey = privkey.public_key();

        let fields = vec![b"token".to_vec(), b"transfer".to_vec(), b"100".to_vec()];
        let pushdrop =
            PushDrop::new(pubkey.clone(), fields.clone()).with_position(LockPosition::After);
        let script = pushdrop.lock();

        let decoded = PushDrop::decode(&script).unwrap();
        assert_eq!(decoded.fields, fields);
        assert_eq!(decoded.lock_position, LockPosition::After);
        assert_eq!(
            decoded.locking_public_key.to_compressed(),
            pubkey.to_compressed()
        );
    }
}