zinc-core 0.4.0

Core Rust library for Zinc Bitcoin + Ordinals wallet
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
#[cfg(test)]
mod tests {
    use crate::builder::{AddressScheme, Seed64, SignOptions, WalletBuilder};
    use base64::Engine;
    use bdk_wallet::bitcoin::hashes::Hash;
    use bdk_wallet::bitcoin::{Amount, Network, ScriptBuf, Transaction, TxOut, Txid};
    use bdk_wallet::chain::ConfirmationBlockTime;
    use bdk_wallet::KeychainKind;
    use bitcoin::psbt::Psbt;
    use std::str::FromStr;

    // Helper to create a dummy transaction for UTXO creation (copied from balance.rs)
    fn create_dummy_tx(output_value: u64, script_pubkey: ScriptBuf, uid: u8) -> Transaction {
        let mut hash_bytes = [0u8; 32];
        hash_bytes[31] = uid;
        let dummy_txid = Txid::from_byte_array(hash_bytes);

        let dummy_input = bdk_wallet::bitcoin::TxIn {
            previous_output: bdk_wallet::bitcoin::OutPoint::new(dummy_txid, 0),
            script_sig: bdk_wallet::bitcoin::ScriptBuf::new(),
            sequence: bdk_wallet::bitcoin::Sequence::ENABLE_RBF_NO_LOCKTIME,
            witness: bdk_wallet::bitcoin::Witness::default(),
        };

        Transaction {
            version: bdk_wallet::bitcoin::transaction::Version::TWO,
            lock_time: bdk_wallet::bitcoin::absolute::LockTime::ZERO,
            input: vec![dummy_input],
            output: vec![TxOut {
                value: Amount::from_sat(output_value),
                script_pubkey,
            }],
        }
    }

    #[test]
    fn test_sign_specific_inputs() {
        // 1. Setup Wallet
        let seed = [0u8; 64];
        let mut builder = WalletBuilder::from_seed(Network::Regtest, Seed64::from_array(seed))
            .with_scheme(AddressScheme::Unified)
            .build()
            .unwrap();

        let addr = builder
            .vault_wallet
            .reveal_next_address(KeychainKind::External)
            .address;
        let script = addr.script_pubkey();

        // 2. Insert 3 UTXOs
        let mut graph = bdk_wallet::chain::TxGraph::default();
        let dummy_block_hash = bdk_wallet::bitcoin::BlockHash::all_zeros();

        let tx1 = create_dummy_tx(10_000, script.clone(), 1);
        let tx2 = create_dummy_tx(20_000, script.clone(), 2);
        let tx3 = create_dummy_tx(30_000, script.clone(), 3);

        for tx in [&tx1, &tx2, &tx3] {
            let _ = graph.insert_tx(tx.clone());
            let _ = graph.insert_anchor(
                tx.compute_txid(),
                ConfirmationBlockTime {
                    block_id: bdk_wallet::chain::BlockId {
                        height: 100,
                        hash: dummy_block_hash,
                    },
                    confirmation_time: 1000,
                },
            );
        }

        let mut last_active = std::collections::BTreeMap::new();
        last_active.insert(KeychainKind::External, 5);
        let update = bdk_wallet::Update {
            tx_update: graph.into(),
            chain: Default::default(),
            last_active_indices: last_active,
        };
        builder.vault_wallet.apply_update(update).unwrap();

        // 3. Create a PSBT that spends all 3 inputs
        // We'll manually construct a PSBT since create_psbt relies on BDK coin selection which might not pick all 3.
        // Or simpler: create a manual transaction with 3 inputs referencing our UTXOs, then convert to PSBT.

        let inputs = vec![
            bdk_wallet::bitcoin::TxIn {
                previous_output: bdk_wallet::bitcoin::OutPoint::new(tx1.compute_txid(), 0),
                script_sig: ScriptBuf::new(),
                sequence: bdk_wallet::bitcoin::Sequence::ENABLE_RBF_NO_LOCKTIME,
                witness: bdk_wallet::bitcoin::Witness::default(),
            },
            bdk_wallet::bitcoin::TxIn {
                previous_output: bdk_wallet::bitcoin::OutPoint::new(tx2.compute_txid(), 0),
                script_sig: ScriptBuf::new(),
                sequence: bdk_wallet::bitcoin::Sequence::ENABLE_RBF_NO_LOCKTIME,
                witness: bdk_wallet::bitcoin::Witness::default(),
            },
            bdk_wallet::bitcoin::TxIn {
                previous_output: bdk_wallet::bitcoin::OutPoint::new(tx3.compute_txid(), 0),
                script_sig: ScriptBuf::new(),
                sequence: bdk_wallet::bitcoin::Sequence::ENABLE_RBF_NO_LOCKTIME,
                witness: bdk_wallet::bitcoin::Witness::default(),
            },
        ];

        let output = TxOut {
            value: Amount::from_sat(59_000), // Fee of 1000
            script_pubkey: script.clone(),
        };

        let unsigned_tx = Transaction {
            version: bdk_wallet::bitcoin::transaction::Version::TWO,
            lock_time: bdk_wallet::bitcoin::absolute::LockTime::ZERO,
            input: inputs,
            output: vec![output],
        };

        // Create PSBT from unsigned tx
        let mut psbt = Psbt::from_unsigned_tx(unsigned_tx).unwrap();

        // Add witness_utxo to PSBT inputs (required for signing Segwit/Taproot)
        psbt.inputs[0].witness_utxo = Some(tx1.output[0].clone());
        psbt.inputs[1].witness_utxo = Some(tx2.output[0].clone());
        psbt.inputs[2].witness_utxo = Some(tx3.output[0].clone());

        let psbt_base64 = base64::engine::general_purpose::STANDARD.encode(psbt.serialize());

        // 4. Test: Sign ONLY Input 1 (Index 1)
        let options = SignOptions {
            sign_inputs: Some(vec![1]),
            sighash: None,
            finalize: false,
        };

        let signed_base64 = builder
            .sign_psbt(&psbt_base64, Some(options))
            .expect("Signing failed");

        let signed_bytes = base64::engine::general_purpose::STANDARD
            .decode(signed_base64)
            .unwrap();
        let signed_psbt = Psbt::deserialize(&signed_bytes).unwrap();

        // Assertions
        // Input 0: Should NOT be final script witness (implementation detail of BDK default signing)
        // Actually BDK putting signature usually results in partial_sigs or final_script_witness.
        // Since we are using taproot (Unified mode default), we expect tap_key_sig or similar.

        // Check if Input 1 is signed
        let has_sig_1 = signed_psbt.inputs[1].final_script_witness.is_some()
            || signed_psbt.inputs[1].tap_key_sig.is_some();
        assert!(has_sig_1, "Input 1 should be signed");

        // Check if Input 0 is NOT signed
        let has_sig_0 = signed_psbt.inputs[0].final_script_witness.is_some()
            || signed_psbt.inputs[0].tap_key_sig.is_some();
        assert!(!has_sig_0, "Input 0 should NOT be signed");

        // Check if Input 2 is NOT signed
        let has_sig_2 = signed_psbt.inputs[2].final_script_witness.is_some()
            || signed_psbt.inputs[2].tap_key_sig.is_some();
        assert!(!has_sig_2, "Input 2 should NOT be signed");
    }

    #[test]
    fn test_sign_psbt_leaves_partial_sigs() {
        // 1. Setup Wallet
        let seed = [0u8; 64];
        let mut builder = WalletBuilder::from_seed(Network::Regtest, Seed64::from_array(seed))
            .with_scheme(AddressScheme::Unified) // Taproot
            .build()
            .unwrap();

        let addr = builder
            .vault_wallet
            .reveal_next_address(KeychainKind::External)
            .address;
        let script = addr.script_pubkey();

        // 2. Insert 1 UTXO
        let mut graph = bdk_wallet::chain::TxGraph::default();
        let dummy_block_hash = bdk_wallet::bitcoin::BlockHash::all_zeros();
        let tx1 = create_dummy_tx(10_000, script.clone(), 1);

        let _ = graph.insert_tx(tx1.clone());
        let _ = graph.insert_anchor(
            tx1.compute_txid(),
            ConfirmationBlockTime {
                block_id: bdk_wallet::chain::BlockId {
                    height: 100,
                    hash: dummy_block_hash,
                },
                confirmation_time: 1000,
            },
        );

        let mut last_active = std::collections::BTreeMap::new();
        last_active.insert(KeychainKind::External, 5);
        let update = bdk_wallet::Update {
            tx_update: graph.into(),
            chain: Default::default(),
            last_active_indices: last_active,
        };
        builder.vault_wallet.apply_update(update).unwrap();

        // 3. Create PSBT
        let inputs = vec![bdk_wallet::bitcoin::TxIn {
            previous_output: bdk_wallet::bitcoin::OutPoint::new(tx1.compute_txid(), 0),
            script_sig: ScriptBuf::new(),
            sequence: bdk_wallet::bitcoin::Sequence::ENABLE_RBF_NO_LOCKTIME,
            witness: bdk_wallet::bitcoin::Witness::default(),
        }];

        let output = TxOut {
            value: Amount::from_sat(9_000),
            script_pubkey: script.clone(),
        };

        let unsigned_tx = Transaction {
            version: bdk_wallet::bitcoin::transaction::Version::TWO,
            lock_time: bdk_wallet::bitcoin::absolute::LockTime::ZERO,
            input: inputs,
            output: vec![output],
        };

        let mut psbt = Psbt::from_unsigned_tx(unsigned_tx).unwrap();
        psbt.inputs[0].witness_utxo = Some(tx1.output[0].clone()); // Needed for signing

        let psbt_base64 = base64::engine::general_purpose::STANDARD.encode(psbt.serialize());

        // 4. Sign
        let signed_base64 = builder
            .sign_psbt(&psbt_base64, None)
            .expect("Signing failed");
        let signed_bytes = base64::engine::general_purpose::STANDARD
            .decode(signed_base64)
            .unwrap();
        let signed_psbt = Psbt::deserialize(&signed_bytes).unwrap();

        // 5. Assert: MUST be Partially Signed (Xverse style)
        // Should NOT have final witness
        assert!(
            signed_psbt.inputs[0].final_script_witness.is_none(),
            "Should NOT have final_script_witness (must be partial)"
        );

        // Should HAVE tap_key_sig
        assert!(
            signed_psbt.inputs[0].tap_key_sig.is_some(),
            "Should HAVE tap_key_sig"
        );
    }

    #[test]
    fn test_rejects_disallowed_sighash_types() {
        let seed = [42u8; 64];
        let mut builder = WalletBuilder::from_seed(Network::Regtest, Seed64::from_array(seed))
            .with_scheme(AddressScheme::Unified)
            .build()
            .unwrap();

        let addr = builder
            .vault_wallet
            .reveal_next_address(KeychainKind::External)
            .address;
        let script = addr.script_pubkey();

        let mut graph = bdk_wallet::chain::TxGraph::default();
        let dummy_block_hash = bdk_wallet::bitcoin::BlockHash::all_zeros();
        let tx = create_dummy_tx(10_000, script.clone(), 17);
        let _ = graph.insert_tx(tx.clone());
        let _ = graph.insert_anchor(
            tx.compute_txid(),
            ConfirmationBlockTime {
                block_id: bdk_wallet::chain::BlockId {
                    height: 100,
                    hash: dummy_block_hash,
                },
                confirmation_time: 1000,
            },
        );

        let mut last_active = std::collections::BTreeMap::new();
        last_active.insert(KeychainKind::External, 5);
        let update = bdk_wallet::Update {
            tx_update: graph.into(),
            chain: Default::default(),
            last_active_indices: last_active,
        };
        builder.vault_wallet.apply_update(update).unwrap();

        let unsigned_tx = Transaction {
            version: bdk_wallet::bitcoin::transaction::Version::TWO,
            lock_time: bdk_wallet::bitcoin::absolute::LockTime::ZERO,
            input: vec![bdk_wallet::bitcoin::TxIn {
                previous_output: bdk_wallet::bitcoin::OutPoint::new(tx.compute_txid(), 0),
                script_sig: ScriptBuf::new(),
                sequence: bdk_wallet::bitcoin::Sequence::ENABLE_RBF_NO_LOCKTIME,
                witness: bdk_wallet::bitcoin::Witness::default(),
            }],
            output: vec![TxOut {
                value: Amount::from_sat(9_000),
                script_pubkey: script.clone(),
            }],
        };
        let mut psbt = Psbt::from_unsigned_tx(unsigned_tx).unwrap();
        psbt.inputs[0].witness_utxo = Some(tx.output[0].clone());
        let psbt_base64 = base64::engine::general_purpose::STANDARD.encode(psbt.serialize());

        for disallowed_sighash in [2u8, 3u8, 129u8] {
            let err = builder
                .sign_psbt(
                    &psbt_base64,
                    Some(SignOptions {
                        sign_inputs: Some(vec![0]),
                        sighash: Some(disallowed_sighash),
                        finalize: false,
                    }),
                )
                .expect_err("disallowed sighash should be rejected");

            assert!(
                err.contains("Sighash type is not allowed"),
                "unexpected error for sighash {disallowed_sighash}: {err}"
            );
        }
    }

    #[test]
    fn test_rejects_out_of_range_sign_inputs() {
        let seed = [11u8; 64];
        let mut builder = WalletBuilder::from_seed(Network::Regtest, Seed64::from_array(seed))
            .with_scheme(AddressScheme::Unified)
            .build()
            .unwrap();

        let addr = builder
            .vault_wallet
            .reveal_next_address(KeychainKind::External)
            .address;
        let script = addr.script_pubkey();

        let mut graph = bdk_wallet::chain::TxGraph::default();
        let dummy_block_hash = bdk_wallet::bitcoin::BlockHash::all_zeros();
        let tx = create_dummy_tx(12_000, script.clone(), 19);
        let _ = graph.insert_tx(tx.clone());
        let _ = graph.insert_anchor(
            tx.compute_txid(),
            ConfirmationBlockTime {
                block_id: bdk_wallet::chain::BlockId {
                    height: 100,
                    hash: dummy_block_hash,
                },
                confirmation_time: 1000,
            },
        );

        let mut last_active = std::collections::BTreeMap::new();
        last_active.insert(KeychainKind::External, 5);
        let update = bdk_wallet::Update {
            tx_update: graph.into(),
            chain: Default::default(),
            last_active_indices: last_active,
        };
        builder.vault_wallet.apply_update(update).unwrap();

        let unsigned_tx = Transaction {
            version: bdk_wallet::bitcoin::transaction::Version::TWO,
            lock_time: bdk_wallet::bitcoin::absolute::LockTime::ZERO,
            input: vec![bdk_wallet::bitcoin::TxIn {
                previous_output: bdk_wallet::bitcoin::OutPoint::new(tx.compute_txid(), 0),
                script_sig: ScriptBuf::new(),
                sequence: bdk_wallet::bitcoin::Sequence::ENABLE_RBF_NO_LOCKTIME,
                witness: bdk_wallet::bitcoin::Witness::default(),
            }],
            output: vec![TxOut {
                value: Amount::from_sat(11_000),
                script_pubkey: script.clone(),
            }],
        };
        let mut psbt = Psbt::from_unsigned_tx(unsigned_tx).unwrap();
        psbt.inputs[0].witness_utxo = Some(tx.output[0].clone());
        let psbt_base64 = base64::engine::general_purpose::STANDARD.encode(psbt.serialize());

        let err = builder
            .sign_psbt(
                &psbt_base64,
                Some(SignOptions {
                    sign_inputs: Some(vec![1]),
                    sighash: None,
                    finalize: false,
                }),
            )
            .expect_err("out-of-range sign input should be rejected");

        assert!(
            err.contains("out of bounds"),
            "unexpected error message: {err}"
        );
    }

    #[test]
    fn test_rejects_requested_foreign_input_that_wallet_cannot_sign() {
        let seed = [88u8; 64];
        let mut builder = WalletBuilder::from_seed(Network::Regtest, Seed64::from_array(seed))
            .with_scheme(AddressScheme::Unified)
            .build()
            .unwrap();

        let my_addr = builder
            .vault_wallet
            .reveal_next_address(KeychainKind::External)
            .address;
        let my_script = my_addr.script_pubkey();
        let mut foreign_builder =
            WalletBuilder::from_seed(Network::Regtest, Seed64::from_array([7u8; 64]))
                .with_scheme(AddressScheme::Unified)
                .build()
                .unwrap();
        let foreign_script = foreign_builder
            .vault_wallet
            .reveal_next_address(KeychainKind::External)
            .address
            .script_pubkey();

        let mut graph = bdk_wallet::chain::TxGraph::default();
        let dummy_block_hash = bdk_wallet::bitcoin::BlockHash::all_zeros();
        let my_tx = create_dummy_tx(20_000, my_script.clone(), 23);
        let _ = graph.insert_tx(my_tx.clone());
        let _ = graph.insert_anchor(
            my_tx.compute_txid(),
            ConfirmationBlockTime {
                block_id: bdk_wallet::chain::BlockId {
                    height: 100,
                    hash: dummy_block_hash,
                },
                confirmation_time: 1000,
            },
        );

        let mut last_active = std::collections::BTreeMap::new();
        last_active.insert(KeychainKind::External, 5);
        let update = bdk_wallet::Update {
            tx_update: graph.into(),
            chain: Default::default(),
            last_active_indices: last_active,
        };
        builder.vault_wallet.apply_update(update).unwrap();

        let foreign_prev_tx = create_dummy_tx(15_000, foreign_script.clone(), 24);

        let unsigned_tx = Transaction {
            version: bdk_wallet::bitcoin::transaction::Version::TWO,
            lock_time: bdk_wallet::bitcoin::absolute::LockTime::ZERO,
            input: vec![
                bdk_wallet::bitcoin::TxIn {
                    previous_output: bdk_wallet::bitcoin::OutPoint::new(
                        foreign_prev_tx.compute_txid(),
                        0,
                    ),
                    script_sig: ScriptBuf::new(),
                    sequence: bdk_wallet::bitcoin::Sequence::ENABLE_RBF_NO_LOCKTIME,
                    witness: bdk_wallet::bitcoin::Witness::default(),
                },
                bdk_wallet::bitcoin::TxIn {
                    previous_output: bdk_wallet::bitcoin::OutPoint::new(my_tx.compute_txid(), 0),
                    script_sig: ScriptBuf::new(),
                    sequence: bdk_wallet::bitcoin::Sequence::ENABLE_RBF_NO_LOCKTIME,
                    witness: bdk_wallet::bitcoin::Witness::default(),
                },
            ],
            output: vec![TxOut {
                value: Amount::from_sat(34_000),
                script_pubkey: my_script,
            }],
        };
        let mut psbt = Psbt::from_unsigned_tx(unsigned_tx).unwrap();
        psbt.inputs[0].witness_utxo = Some(foreign_prev_tx.output[0].clone());
        psbt.inputs[1].witness_utxo = Some(my_tx.output[0].clone());
        let psbt_base64 = base64::engine::general_purpose::STANDARD.encode(psbt.serialize());

        let err = builder
            .sign_psbt(
                &psbt_base64,
                Some(SignOptions {
                    sign_inputs: Some(vec![0]),
                    sighash: None,
                    finalize: false,
                }),
            )
            .expect_err("foreign requested input should be rejected");

        assert!(
            err.contains("was not signed by this wallet"),
            "unexpected error message: {err}"
        );
    }

    #[test]
    fn test_rejects_requested_input_missing_utxo_metadata() {
        let seed = [90u8; 64];
        let mut builder = WalletBuilder::from_seed(Network::Regtest, Seed64::from_array(seed))
            .with_scheme(AddressScheme::Unified)
            .build()
            .unwrap();

        let my_addr = builder
            .vault_wallet
            .reveal_next_address(KeychainKind::External)
            .address;
        let my_script = my_addr.script_pubkey();
        let unknown_txid =
            Txid::from_str("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
                .unwrap();

        let unsigned_tx = Transaction {
            version: bdk_wallet::bitcoin::transaction::Version::TWO,
            lock_time: bdk_wallet::bitcoin::absolute::LockTime::ZERO,
            input: vec![bdk_wallet::bitcoin::TxIn {
                previous_output: bdk_wallet::bitcoin::OutPoint::new(unknown_txid, 0),
                script_sig: ScriptBuf::new(),
                sequence: bdk_wallet::bitcoin::Sequence::ENABLE_RBF_NO_LOCKTIME,
                witness: bdk_wallet::bitcoin::Witness::default(),
            }],
            output: vec![TxOut {
                value: Amount::from_sat(1_000),
                script_pubkey: my_script,
            }],
        };
        let psbt = Psbt::from_unsigned_tx(unsigned_tx).unwrap();
        let psbt_base64 = base64::engine::general_purpose::STANDARD.encode(psbt.serialize());

        let err = builder
            .sign_psbt(
                &psbt_base64,
                Some(SignOptions {
                    sign_inputs: Some(vec![0]),
                    sighash: None,
                    finalize: false,
                }),
            )
            .expect_err("requested input without UTXO metadata should be rejected");

        assert!(
            err.contains("Requested input #0 is missing UTXO metadata"),
            "unexpected error message: {err}"
        );
    }
}