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
use super::*;

#[derive(Clone)]
pub struct CoinbaseBuilder {
    address: Address,
    aux: BTreeMap<String, String>,
    enonce1: Extranonce,
    enonce2_size: usize,
    height: u64,
    pool_sig: Option<String>,
    timestamp: Option<u64>,
    value: Amount,
    witness_commitment: ScriptBuf,
}

impl CoinbaseBuilder {
    const MAX_COINBASE_SCRIPT_SIG_SIZE: usize = 100;

    pub fn new(
        address: Address,
        enonce1: Extranonce,
        enonce2_size: usize,
        height: u64,
        value: Amount,
        witness_commitment: ScriptBuf,
    ) -> Self {
        Self {
            address,
            aux: BTreeMap::new(),
            enonce1,
            enonce2_size,
            height,
            value,
            witness_commitment,
            timestamp: None,
            pool_sig: None,
        }
    }

    pub fn with_aux(mut self, aux: BTreeMap<String, String>) -> Self {
        self.aux = aux;
        self
    }

    pub fn with_timestamp(mut self, timestamp: u64) -> Self {
        self.timestamp = Some(timestamp);
        self
    }

    pub fn with_pool_sig(mut self, pool_sig: String) -> Self {
        self.pool_sig = Some(pool_sig);
        self
    }

    pub fn build(self) -> Result<(Transaction, String, String)> {
        let mut buf: Vec<u8> = Vec::with_capacity(Self::MAX_COINBASE_SCRIPT_SIG_SIZE);

        // BIP34 encode block height
        if self.height > 0 && self.height <= 16 {
            // Append dummy to increase scriptSig size to 2 (see bad-cb-length consensus rule)
            buf.push(0x50 + (self.height as u8));
            buf.push(0x00);
        } else {
            let mut minimally_encoded_serialized_cscript = [0u8; 8];
            let len = write_scriptint(
                &mut minimally_encoded_serialized_cscript,
                self.height.try_into().expect("height should always fit"),
            );
            // byte length should be fine for the next 150 years
            buf.push(len as u8);
            buf.extend_from_slice(&minimally_encoded_serialized_cscript[..len]);
        }

        for (_, value) in self.aux.into_iter() {
            buf.extend_from_slice(hex::decode(value)?.as_slice());
        }

        let script_prefix_size = buf.len();

        buf.extend_from_slice(self.enonce1.as_bytes());
        buf.extend_from_slice(vec![0u8; self.enonce2_size].as_slice());

        if let Some(sig) = self.pool_sig {
            buf.extend_from_slice(sig.as_bytes())
        }

        if let Some(ts) = self.timestamp {
            buf.extend_from_slice(&ts.to_le_bytes());
        }

        buf.extend_from_slice(&[0x7c, 0x70, 0x61, 0x72, 0x61, 0x7c]);

        let script_sig = ScriptBuf::from_bytes(buf);
        let script_sig_size = script_sig.len();

        ensure!(
            script_sig_size <= Self::MAX_COINBASE_SCRIPT_SIG_SIZE,
            "Script sig too large is {script_sig_size} bytes (max {})",
            Self::MAX_COINBASE_SCRIPT_SIG_SIZE
        );

        let coinbase = Transaction {
            version: bitcoin::transaction::Version::TWO,
            lock_time: LockTime::ZERO,
            input: vec![TxIn {
                previous_output: OutPoint::null(),
                script_sig,
                sequence: Sequence::MAX,
                witness: Witness::new(),
            }],
            output: vec![
                TxOut {
                    value: self.value,
                    script_pubkey: self.address.script_pubkey(),
                },
                TxOut {
                    value: Amount::ZERO,
                    script_pubkey: self.witness_commitment,
                },
            ],
        };

        let total_extranonce_size = self.enonce1.len() + self.enonce2_size;

        // offset = size of tx version
        //  + size of #inputs
        //  + size of coinbase outpoint
        //  + size of scriptSig length
        //  + size of everything before enonce1 + enonce2
        let offset = 4
            + VarInt(coinbase.input.len().try_into().unwrap()).size()
            + 36
            + VarInt(script_sig_size.try_into().unwrap()).size()
            + script_prefix_size;

        let bin = consensus::serialize(&coinbase);
        let coinb1 = hex::encode(&bin[..offset]);
        let coinb2 = hex::encode(&bin[offset + total_extranonce_size..]);

        Ok((coinbase, coinb1, coinb2))
    }
}

#[cfg(test)]
mod tests {
    use {
        super::*, bitcoin::address::NetworkUnchecked,
        pretty_assertions::assert_eq as pretty_assert_eq,
    };

    fn address() -> Address {
        "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"
            .parse::<Address<NetworkUnchecked>>()
            .unwrap()
            .assume_checked()
    }

    #[test]
    fn exceed_script_size_limit() {
        let result = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            8,
            0,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        )
        .with_pool_sig("aa".repeat(100))
        .build();

        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Script sig too large")
        );
    }

    #[test]
    fn split_reassembles_with_zero_enonce2() {
        let (tx, coinb1, coinb2) = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            8,
            500_000,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        )
        .with_pool_sig("|parasite|".into())
        .build()
        .unwrap();

        let enonce1 = hex::decode("abcd1234").unwrap();
        let enonce2 = vec![0u8; 8];

        let full = {
            let mut v = hex::decode(&coinb1).unwrap();
            v.extend_from_slice(&enonce1);
            v.extend_from_slice(&enonce2);
            v.extend_from_slice(&hex::decode(&coinb2).unwrap());
            v
        };

        pretty_assert_eq!(full, bitcoin::consensus::serialize(&tx));
    }

    #[test]
    fn split_allows_custom_enonce2() {
        let (tx, coinb1, coinb2) = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            8,
            0,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        )
        .build()
        .unwrap();

        let enonce1 = hex::decode("abcd1234").unwrap();
        let enonce2_custom = [0x11; 8];

        let joined = {
            let mut v = hex::decode(&coinb1).unwrap();
            v.extend_from_slice(&enonce1);
            v.extend_from_slice(&enonce2_custom);
            v.extend_from_slice(&hex::decode(&coinb2).unwrap());
            v
        };

        let original = bitcoin::consensus::serialize(&tx);
        assert_eq!(joined.len(), original.len(), "length must match");
        assert_ne!(
            joined, original,
            "bytes should differ when enonce2 != zeros"
        );
    }

    #[test]
    fn deterministic_with_same_inputs() {
        let base = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            8,
            0,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        );

        let (tx1, c1_1, c1_2) = base.clone().build().unwrap();
        let (tx2, c2_1, c2_2) = base.build().unwrap();

        assert_eq!(
            bitcoin::consensus::serialize(&tx1),
            bitcoin::consensus::serialize(&tx2)
        );
        assert_eq!(c1_1, c2_1);
        assert_eq!(c1_2, c2_2);
    }

    #[test]
    fn aux_invalid_hex_errors() {
        let mut aux = BTreeMap::new();
        aux.insert("bad".to_string(), "zz".to_string());

        let err = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            8,
            800_000,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        )
        .with_aux(aux)
        .build()
        .unwrap_err()
        .to_string();

        assert!(err.contains("Invalid character"));
    }

    #[test]
    fn coinb1_ends_before_enonce1() {
        let (_tx, coinb1, _coinb2) = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            8,
            1_000_000,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        )
        .build()
        .unwrap();

        assert!(
            !coinb1.contains("abcd1234"),
            "coinb1 must end before enonce1 bytes"
        );
    }

    #[test]
    fn enonce2_boundary_occurs_once() {
        let (tx, coinb1, coinb2) = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            8,
            900_000,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        )
        .build()
        .unwrap();

        let enonce1 = hex::decode("abcd1234").unwrap();
        let enonce2_zeros = vec![0u8; 8];

        let mut full = hex::decode(&coinb1).unwrap();
        full.extend_from_slice(&enonce1);
        full.extend_from_slice(&enonce2_zeros);
        full.extend_from_slice(&hex::decode(&coinb2).unwrap());

        let bin = bitcoin::consensus::serialize(&tx);
        pretty_assert_eq!(full, bin);

        let mut needle = enonce1.clone();
        needle.extend_from_slice(&enonce2_zeros);

        let count = bin
            .windows(needle.len())
            .filter(|w| *w == needle.as_slice())
            .count();

        assert_eq!(count, 1, "enonce1 || zeros should appear exactly once");
    }

    #[test]
    fn pool_sig_is_present_when_set() {
        let tag = "|parasite|".as_bytes();
        let (tx, _, _) = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            8,
            0,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        )
        .with_pool_sig("|parasite|".into())
        .build()
        .unwrap();

        let ss = tx.input[0].script_sig.as_bytes();
        assert!(
            ss.windows(tag.len()).any(|w| w == tag),
            "pool signature bytes must be present in scriptSig"
        );
    }

    #[test]
    fn coinb1_prefix_bytes_constant_when_only_tail_changes() {
        let base = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            8,
            0,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        );

        let (_tx_a, c1_a, _c2_a) = base.clone().build().unwrap();
        let (_tx_b, c1_b, _c2_b) = base.clone().with_timestamp(42).build().unwrap();
        let (_tx_c, c1_c, _c2_c) = base.with_pool_sig("hello".into()).build().unwrap();

        let mut tmp = [0u8; 8];
        let hlen = write_scriptint(&mut tmp, 0);
        let aux_len = 0;
        let script_prefix_len = 1 + hlen + aux_len;

        let b_a = hex::decode(&c1_a).unwrap();
        let b_b = hex::decode(&c1_b).unwrap();
        let b_c = hex::decode(&c1_c).unwrap();

        assert_eq!(
            &b_a[b_a.len() - script_prefix_len..],
            &b_b[b_b.len() - script_prefix_len..],
            "prefix bytes must be identical; only the VarInt(scriptSigLen) value before them may differ"
        );
        assert_eq!(
            &b_a[b_a.len() - script_prefix_len..],
            &b_c[b_c.len() - script_prefix_len..]
        );
    }

    #[test]
    fn coinb1_varint_len_stable_but_value_changes_with_tail() {
        let base = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            8,
            0,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        );

        let (tx_a, c1_a, _) = base.clone().build().unwrap();
        let (tx_b, c1_b, _) = base.clone().with_timestamp(42).build().unwrap();

        let ss_a = tx_a.input[0].script_sig.len() as u64;
        let ss_b = tx_b.input[0].script_sig.len() as u64;

        assert_eq!(VarInt(ss_a).size(), VarInt(ss_b).size());
        assert_ne!(c1_a, c1_b);
    }

    #[test]
    fn join_roundtrip_various_enonce2_sizes() {
        for x2 in [0, 1, 8, 16, 32] {
            let (tx, c1, c2) = CoinbaseBuilder::new(
                address(),
                "abcd1234".parse().unwrap(),
                x2,
                0,
                Amount::from_sat(50 * COIN_VALUE),
                ScriptBuf::new(),
            )
            .build()
            .unwrap();

            let x1 = hex::decode("abcd1234").unwrap();
            let x2_zeros = vec![0u8; x2];

            let mut full = hex::decode(&c1).unwrap();
            full.extend_from_slice(&x1);
            full.extend_from_slice(&x2_zeros);
            full.extend_from_slice(&hex::decode(&c2).unwrap());

            pretty_assert_eq!(full, bitcoin::consensus::serialize(&tx));
        }
    }

    #[test]
    fn aux_bytes_extend_prefix_and_shift_boundary() {
        let base = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            8,
            0,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        );

        let (_, c1_base, _) = base.clone().build().unwrap();

        let mut aux = BTreeMap::new();
        aux.insert("a".into(), "00112233".into());
        let (_, c1_aux, _) = base.with_aux(aux).build().unwrap();

        assert_eq!(c1_aux.len(), c1_base.len() + 2 * 4);
    }

    #[test]
    fn offset_matches_varint_formula() {
        let height = 600_000;

        let mut aux = BTreeMap::new();
        aux.insert("k".into(), "cafebabe".into());

        let (tx, c1, _c2) = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            8,
            height,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        )
        .with_aux(aux.clone())
        .with_pool_sig("hey".into())
        .with_timestamp(1)
        .build()
        .unwrap();

        let script_sig_len = tx.input[0].script_sig.len();

        let mut tmp = [0u8; 8];
        let hlen = write_scriptint(&mut tmp, height.try_into().unwrap());
        let aux_len: usize = aux.values().map(|h| hex::decode(h).unwrap().len()).sum();
        let script_prefix_len = 1 + hlen + aux_len;

        let expected_offset =
            4 + VarInt(1).size() + 36 + VarInt(script_sig_len as u64).size() + script_prefix_len;

        assert_eq!(
            c1.len() / 2,
            expected_offset,
            "coinb1 byte length must equal computed offset"
        );
    }

    #[test]
    fn pool_sig_resides_after_boundary() {
        let tag = "|parasite|";
        let (_tx, c1, c2) = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            8,
            0,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        )
        .with_pool_sig(tag.into())
        .build()
        .unwrap();

        let tag_hex = hex::encode(tag.as_bytes());
        assert!(!c1.contains(&tag_hex), "pool sig must not be in coinb1");
        assert!(c2.contains(&tag_hex), "pool sig must be in coinb2");
    }

    #[test]
    fn coinbase_script_sig_too_large_via_enonce2_errors() {
        let enonce2_size = CoinbaseBuilder::MAX_COINBASE_SCRIPT_SIG_SIZE;
        let err = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            enonce2_size,
            2222,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        )
        .build()
        .unwrap_err()
        .to_string();

        assert!(err.contains("Script sig too large"));
    }

    #[test]
    fn coinbase_script_sig_too_large_via_aux_bytes_errors() {
        let err = CoinbaseBuilder::new(
            address(),
            "abcd1234".parse().unwrap(),
            8,
            1111,
            Amount::from_sat(50 * COIN_VALUE),
            ScriptBuf::new(),
        )
        .with_aux(
            [(
                "pad".to_string(),
                "00".repeat(CoinbaseBuilder::MAX_COINBASE_SCRIPT_SIG_SIZE),
            )]
            .into_iter()
            .collect(),
        )
        .build()
        .unwrap_err()
        .to_string();

        assert!(err.contains("Script sig too large"));
    }

    #[test]
    fn bip34_height() {
        for h in [1, 16, 17, 500_000] {
            let (tx, _, _) = CoinbaseBuilder::new(
                address(),
                "abcd1234".parse().unwrap(),
                8,
                h,
                Amount::from_sat(50 * COIN_VALUE),
                ScriptBuf::new(),
            )
            .build()
            .unwrap();

            let script_sig = tx.input[0].script_sig.as_bytes();

            if h <= 16 {
                assert!(matches!(script_sig[0], 0x51..=0x60));
                assert_eq!(script_sig[1], 0x00);
            } else {
                let len = script_sig[0] as usize;
                assert!(
                    (1..=75).contains(&len),
                    "expect small push for realistic heights"
                );
                assert!(script_sig.len() > len, "enough bytes for height");
            }
        }
    }
}