bwk 0.0.8

Bitcoin wallet kit
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
use std::str::FromStr;
pub mod error;

use std::{
    collections::{BTreeMap, BTreeSet},
    sync::mpsc,
};

use error::Error;
use serde::{Deserialize, Serialize};
use {
    bip39,
    miniscript::{
        bitcoin::{
            self,
            bip32::{self, DerivationPath},
            ecdsa,
            psbt::Input,
            secp256k1::{self, All, Message},
            sighash, EcdsaSighashType, NetworkKind, Psbt,
        },
        descriptor::DescriptorMultiXKey,
        Descriptor, DescriptorPublicKey, ForEachKey,
    },
};

use crate::account::AddrAccount;
use crate::{derivator::Derivator, signing_manager};

#[derive(Debug)]
pub enum SignerNotif {
    Info(bip32::Fingerprint, serde_json::Value),
    Xpub(bip32::Fingerprint, OXpub),
    Descriptor(bip32::Fingerprint, DescriptorMultiXKey<bip32::Xpub>),
    DescriptorRegistered(bip32::Fingerprint, Descriptor<DescriptorPublicKey>, bool),
    Signed(bip32::Fingerprint, Psbt),
    Error(bip32::Fingerprint, Error),
    Manager(signing_manager::Error),
}

/// This trait implement features that are available when the signer is connected.
pub trait Signer {
    /// Initialyse the signer with a new channel, in return the signer
    /// must return a [`SignerNotif::Info`] notification to the newly
    /// registered channel.
    fn init(&mut self, channel: mpsc::Sender<SignerNotif>);
    /// Request general informations from the signer.
    /// The signer must return a [`SignerNotif::Info`] notification.
    fn info(&self);
    /// Request descriptor to generate an Xpub using the given derivation
    /// path. `display` must be set to true for non standard derivation path,
    /// allow some signer to generate non-standard Xpub with user approval.
    /// The signer must return a [`SignerNotif::Xpub`] notification.
    fn get_xpub(&self, deriv: DerivationPath, display: bool);
    /// Request signer if the given descriptor is registered.
    /// The signer must return a [`SignerNotif::DescriptorRegistered`] notification.
    fn is_descriptor_registered(&self, descriptor: Descriptor<DescriptorPublicKey>);
    /// Prompt user to register given descriptor.
    /// The signer must return a [`SignerNotif::DescriptorRegistered`] notification.
    fn register_descriptor(&mut self, descriptor: Descriptor<DescriptorPublicKey>);
    /// Request the signer to sign the given psbt. A descriptor must be loaded
    /// prior to call this function.
    /// The signer must return a [`SignerNotif::DescriptorLoaded`] notification.
    fn sign(&self, psbt: Psbt, descriptor: Descriptor<DescriptorPublicKey>);
    /// Request the signer to display the address for verification.
    /// No notification is expected in return.
    fn display_address(&self, _deriv: (AddrAccount, u32)) {}
}

macro_rules! send {
    ($s:ident, $notif:ident($val1:expr)) => {
        if let Some(sender) = &$s.sender {
            if let Err(e) = sender.send(SignerNotif::$notif($s.fingerprint(), $val1)) {
                log::error!("Signer fail to send notification: {e:?}");
            }
        }
    };
    ($s:ident, $notif:ident($val1:expr, $val2:expr)) => {
        if let Some(sender) = &$s.sender {
            if let Err(e) = sender.send(SignerNotif::$notif($s.fingerprint(), $val1, $val2)) {
                log::error!("Signer fail to send notification: {e:?}");
            }
        }
    };
}

impl Signer for HotSigner {
    fn init(&mut self, channel: mpsc::Sender<SignerNotif>) {
        self.sender = Some(channel);
        self.info();
    }

    fn info(&self) {
        send!(self, Info(serde_json::Value::Null));
    }

    fn get_xpub(&self, deriv: DerivationPath, _display: bool) {
        let xpub = self.xpub(&deriv);
        if let Some(sender) = &self.sender {
            let _ = sender.send(SignerNotif::Xpub(self.fingerprint(), xpub));
        }
    }

    fn is_descriptor_registered(&self, descriptor: Descriptor<DescriptorPublicKey>) {
        let registered = self.descriptors.contains(&descriptor);
        if let Some(sender) = &self.sender {
            let _ = sender.send(SignerNotif::DescriptorRegistered(
                self.fingerprint(),
                descriptor,
                registered,
            ));
        }
    }

    fn register_descriptor(&mut self, descriptor: Descriptor<DescriptorPublicKey>) {
        let wrong_network = descriptor.for_any_key(|k| match k {
            DescriptorPublicKey::Single(_) => true,
            DescriptorPublicKey::XPub(key) => match (self.network, key.xkey.network) {
                (bitcoin::Network::Bitcoin, NetworkKind::Main) => false,
                (bitcoin::Network::Bitcoin, NetworkKind::Test) => true,
                (_, NetworkKind::Main) => true,
                _ => false,
            },
            DescriptorPublicKey::MultiXPub(key) => match (self.network, key.xkey.network) {
                (bitcoin::Network::Bitcoin, NetworkKind::Main) => false,
                (bitcoin::Network::Bitcoin, NetworkKind::Test) => true,
                (_, NetworkKind::Main) => true,
                _ => false,
            },
        });
        if !wrong_network {
            self.descriptors.insert(descriptor.clone());
        }
        if let Some(sender) = &self.sender {
            let response = if wrong_network {
                SignerNotif::Error(self.fingerprint(), Error::DescriptorNetwork)
            } else {
                SignerNotif::DescriptorRegistered(self.fingerprint(), descriptor, true)
            };
            let _ = sender.send(response);
        }
    }

    fn sign(&self, mut psbt: Psbt, descriptor: Descriptor<DescriptorPublicKey>) {
        let response = if self.descriptors.contains(&descriptor) {
            if let Err(e) = self.inner_sign(&mut psbt, &descriptor) {
                SignerNotif::Error(self.fingerprint(), e)
            } else {
                SignerNotif::Signed(self.fingerprint(), psbt)
            }
        } else {
            SignerNotif::Error(self.fingerprint(), Error::UnregisteredDescriptor)
        };
        if let Some(sender) = &self.sender {
            let _ = sender.send(response);
        }
    }
}

/// A struct that represents a hot signer for Bitcoin transactions.
///
/// This struct is responsible for managing the private keys and generating
/// addresses for receiving and change. It can create signatures for transactions
/// using the provided private keys.
#[derive(Debug, Clone)]
pub struct HotSigner {
    master_xpriv: bip32::Xpriv,
    fingerprint: bip32::Fingerprint,
    secp: secp256k1::Secp256k1<All>,
    mnemonic: Option<bip39::Mnemonic>,
    descriptors: BTreeSet<Descriptor<DescriptorPublicKey>>,
    network: bitcoin::Network,
    sender: Option<mpsc::Sender<SignerNotif>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonSigner {
    mnemonic: bip39::Mnemonic,
    descriptors: BTreeSet<String>,
    network: bitcoin::Network,
}

/// A struct that represents an extended private key.
///
/// This struct contains the origin fingerprint and derivation path
/// associated with the extended private key, as well as the key itself.
///
/// # Fields
/// * `origin` - A tuple containing the fingerprint and derivation path.
/// * `xkey` - The extended private key.
pub struct OXpriv {
    pub origin: (bip32::Fingerprint, bip32::DerivationPath),
    pub xkey: bip32::Xpriv,
}

/// A struct that represents an extended public key.
///
/// This struct contains the origin fingerprint and derivation path
/// associated with the extended public key, as well as the key itself.
///
/// # Fields
/// * `origin` - A tuple containing the fingerprint and derivation path.
/// * `xkey` - The extended public key.
#[derive(Debug)]
pub struct OXpub {
    pub origin: (bip32::Fingerprint, bip32::DerivationPath),
    pub xkey: bip32::Xpub,
}

impl HotSigner {
    pub fn to_json(&self) -> Option<JsonSigner> {
        let descriptors = self.descriptors.iter().map(|d| d.to_string()).collect();
        self.mnemonic.as_ref().map(|mnemonic| JsonSigner {
            mnemonic: mnemonic.clone(),
            descriptors,
            network: self.network,
        })
    }
    pub fn from_json(json: JsonSigner) -> Self {
        let mut signer = HotSigner::new_from_mnemonics(json.network, &json.mnemonic.to_string())
            .expect("valid signer");
        #[allow(clippy::mutable_key_type)]
        let descriptors = json
            .descriptors
            .into_iter()
            .filter_map(|d| Descriptor::from_str(&d).ok())
            .collect();
        signer.descriptors = descriptors;
        signer
    }
    /// Create a new [`HotSigner`] instance from the provided Xpriv key.
    ///
    /// # Arguments
    /// * `network` - The Bitcoin network (e.g., Bitcoin, Testnet, Signet, Regtest).
    /// * `xpriv` - The extended private key that the signer will use.
    ///
    /// # Returns
    /// A new instance of [`HotSigner`].
    pub fn new_from_xpriv(network: bitcoin::Network, xpriv: bip32::Xpriv) -> Self {
        let secp = secp256k1::Secp256k1::new();
        let fingerprint = xpriv.fingerprint(&secp);
        let master_xpriv = xpriv;

        HotSigner {
            master_xpriv,
            fingerprint,
            secp,
            mnemonic: None,
            descriptors: BTreeSet::new(),
            network,
            sender: None,
        }
    }

    /// Create a new [`HotSigner`] instance from a mnemonic phrase.
    ///
    /// The mnemonic is stored in the [`HotSigner::mnemonic`] field.
    ///
    /// # Arguments
    /// * `network` - The Bitcoin network (e.g., Bitcoin, Testnet, Signet, Regtest).
    /// * `mnemonic` - A string representing the mnemonic phrase used to generate the keys.
    ///
    /// # Returns
    /// A result containing a new instance of [`HotSigner`] or an error if the mnemonic is invalid.
    pub fn new_from_mnemonics(network: bitcoin::Network, mnemonic: &str) -> Result<Self, Error> {
        let mnemonic = bip39::Mnemonic::from_str(mnemonic)?;
        let seed = mnemonic.to_seed("");
        let key = bip32::Xpriv::new_master(network, &seed).map_err(|_| Error::XPrivFromSeed)?;
        let mut signer = Self::new_from_xpriv(network, key);
        signer.mnemonic = Some(mnemonic);
        Ok(signer)
    }

    /// Generate a new signer and it's private key.
    /// The mnemonic is stored in [`HotSigner::mnemonic`] field.
    ///
    /// Note: generating a private key by this way is not safe enough
    ///   to use on mainnet, so we decide to forbid usage of this method on mainnet.
    ///   This method will panic if `network` have [`Network::Bitcoin`] value.
    pub fn new(network: bitcoin::Network) -> Result<Self, Error> {
        // Should not be used on mainnet
        assert_ne!(network, bitcoin::Network::Bitcoin);
        let mnemonic = bip39::Mnemonic::generate(12).expect("12 words must not fail");
        let mut signer = Self::new_from_mnemonics(network, &mnemonic.to_string())?;
        signer.mnemonic = Some(mnemonic);
        Ok(signer)
    }

    /// Registers a descriptor for the signer.
    ///
    /// This function adds the given descriptor to the signer's internal set of
    /// descriptors if it is not already registered.
    ///
    /// # Arguments
    /// * `descriptor` - The descriptor to be registered.
    pub fn inner_register_descriptor(&mut self, descriptor: Descriptor<DescriptorPublicKey>) {
        if !self.descriptors.contains(&descriptor) {
            self.descriptors.insert(descriptor);
        }
    }

    /// Retrieves the extended private key at the specified derivation path.
    ///
    /// # Arguments
    /// * `path` - The derivation path for which to retrieve the extended private key.
    ///
    /// # Returns
    /// An instance of `OXpriv` containing the origin fingerprint and the derived
    /// extended private key.
    pub fn xpriv(&self, path: &DerivationPath) -> OXpriv {
        let xkey = self
            .master_xpriv
            .derive_priv(&self.secp, path)
            .expect("cannot fail");

        OXpriv {
            origin: (self.fingerprint, path.clone()),
            xkey,
        }
    }

    /// Retrieves the extended public key at the specified derivation path.
    ///
    /// # Arguments
    /// * `path` - The derivation path for which to retrieve the extended public key.
    ///
    /// # Returns
    /// An instance of `OXpub` containing the origin fingerprint and the derived
    /// extended public key.
    pub fn xpub(&self, path: &DerivationPath) -> OXpub {
        let xpriv = self.xpriv(path);
        let xkey = bip32::Xpub::from_priv(&self.secp, &xpriv.xkey);

        OXpub {
            origin: xpriv.origin,
            xkey,
        }
    }

    /// Retrieves the private key at the specified derivation path from the master_xpriv.
    ///
    /// # Arguments
    /// * `path` - The derivation path for which to retrieve the private key.
    ///
    /// # Returns
    /// The private key as a [`secp256k1::SecretKey`].
    fn private_key_at(&self, path: &DerivationPath) -> secp256k1::SecretKey {
        self.master_xpriv
            .derive_priv(self.secp(), path)
            .expect("deriveable")
            .private_key
    }

    /// Retrieves the public key at the specified derivation path from the master_xpriv.
    ///
    /// # Arguments
    /// * `path` - The derivation path for which to retrieve the public key.
    ///
    /// # Returns
    /// The public key as a [`secp256k1::PublicKey`].
    pub fn public_key_at(&self, path: &DerivationPath) -> secp256k1::PublicKey {
        self.private_key_at(path).public_key(self.secp())
    }

    /// Sign the provided PSBT
    ///
    /// This function processes each input of the PSBT, checks for the necessary
    /// witness UTXO, and generates the required signatures using the private keys
    /// derived from the signer's extended private key. It only supports the
    /// SIGHASH_ALL signature type.
    ///
    /// # Arguments
    /// * `psbt` - A mutable reference to the PSBT that will be signed.
    ///
    /// # Returns
    /// A result indicating success or failure. Returns an error if:
    /// * Any input's witness UTXO is missing.
    /// * The expected script public key does not match.
    /// * The signature generation fails.
    pub fn inner_sign(
        &self,
        psbt: &mut Psbt,
        descriptor: &Descriptor<DescriptorPublicKey>,
    ) -> Result<(), Error> {
        let mut cache = sighash::SighashCache::new(psbt.unsigned_tx.clone());
        let derivator = Derivator::new(descriptor.clone(), self.network).unwrap();

        let mut inputs_to_sign = BTreeMap::new();
        for (index, input) in psbt.inputs.iter().enumerate() {
            let mut derivation_paths = vec![];
            input.bip32_derivation.iter().for_each(|(_, (fg, deriv))| {
                if *fg == self.fingerprint() {
                    derivation_paths.push(deriv.clone());
                }
            });
            if !derivation_paths.is_empty() {
                if input.witness_utxo.is_none() {
                    Err(Error::MissingWitnessUtxo)?
                }
                // FIXME: process sighash w/o psbt helper?
                let (hash, sighash_type) = psbt.sighash_ecdsa(0, &mut cache).map_err(|e| {
                    log::error!("Fail to generate sig hash: {e}");
                    Error::SighashFail
                })?;
                // NOTE: we only allow SigHash ALL for now
                if sighash_type != EcdsaSighashType::All {
                    return Err(Error::SighashFail);
                }
                inputs_to_sign.insert(index, (hash, psbt.inputs[index].clone(), derivation_paths));
            }
        }
        for (index, (hash, mut input, deriv)) in inputs_to_sign {
            self.sign_input(hash, &mut input, deriv, &derivator)?;
            psbt.inputs[index] = input;
        }
        Ok(())
    }

    /// Sign the input for a transaction.
    ///
    /// # Arguments
    /// * `hash` - The hash of the transaction to be signed.
    /// * `input` - A mutable reference to the input that will be signed.
    /// * `deriv` - A vector of derivation paths used to derive the signing keys.
    ///
    /// # Returns
    /// A result indicating success or failure. Returns an error if:
    /// * The input's witness UTXO is missing.
    /// * The expected script public key does not match.
    /// * The signature is invalid.
    ///
    /// This function iterates over the provided derivation paths, derives the signing key,
    /// and signs the transaction input with the corresponding private key.
    /// Note: only SIGHASH_ALL is supported for now
    pub fn sign_input(
        &self,
        hash: Message,
        input: &mut Input,
        deriv: Vec<DerivationPath>,
        derivator: &Derivator,
    ) -> Result<(), Error> {
        for d in &deriv {
            let signing_key = self.private_key_at(d);
            let pubkey = self.public_key_at(d);

            if !input.bip32_derivation.contains_key(&pubkey) {
                // NOTE: this can happen in case of fingerprint collision
                continue;
            }

            if let Some(wit) = &input.witness_utxo {
                let ap = account_path(d)?;
                let expected_spk = match ap.0 {
                    AddrAccount::Receive => derivator.receive_at(ap.1),
                    AddrAccount::Change => derivator.change_at(ap.1),
                }
                .script_pubkey();
                if wit.script_pubkey != expected_spk {
                    Err(Error::SpkNotMatch)
                } else {
                    Ok(())
                }
            } else {
                Err(Error::MissingWitnessUtxo)
            }?;

            let signature = self.secp.sign_ecdsa_low_r(&hash, &signing_key);

            self.secp()
                .verify_ecdsa(&hash, &signature, &pubkey)
                .map_err(|_| Error::InvalidSignature)?;

            let signature = ecdsa::Signature {
                signature,
                // NOTE: we only allow SigHash ALL for now
                sighash_type: EcdsaSighashType::All,
            };
            input.partial_sigs.insert(pubkey.into(), signature);
        }

        Ok(())
    }

    /// Returns the [`Fingerprint`] of this [`HotSigner`].
    pub fn fingerprint(&self) -> bip32::Fingerprint {
        self.fingerprint
    }

    /// Return the secp context of this signer
    fn secp(&self) -> &secp256k1::Secp256k1<All> {
        &self.secp
    }

    /// Returns a copy of the mnemonic if not None
    #[allow(unused)]
    fn mnemonic(&self) -> Option<bip39::Mnemonic> {
        self.mnemonic.clone()
    }
}

/// Converts a tuple containing an account type and an index into a derivation path.
///
/// # Arguments
/// * `path` - A tuple where the first element is an [`AddrAccount`] representing the account type,
///   and the second element is a `u32` representing the index.
///
/// # Returns
/// A result containing the derived [`DerivationPath`] or an error if the conversion fails.
pub fn deriv_path(path: &(AddrAccount, u32)) -> Result<DerivationPath, Error> {
    let account_u32: u32 = path.0.into();
    DerivationPath::from_str(&format!("m/{}/{}", account_u32, path.1))
        .map_err(|_| Error::DerivationPath)
}

/// Converts a derivation path into a tuple containing an account type and an index.
///
/// # Arguments
/// * `path` - A reference to a [`DerivationPath`] that contains the account type and index.
///
/// # Returns
/// A result containing a tuple of the account type as [`AddrAccount`] and the index as `u32`.
/// Returns an error if the derivation path does not have the expected length.
pub fn account_path(path: &DerivationPath) -> Result<(AddrAccount, u32), Error> {
    if path.len() != 2 {
        Err(Error::DerivationPath)?
    }
    let path = path.to_u32_vec();
    Ok((path[0].into(), path[1]))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        descriptor::wpkh,
        test_utils::{random_output, setup_logger, txid},
    };
    use bitcoin::Network;
    use miniscript::bitcoin::{absolute::Height, Amount, ScriptBuf, TxIn, Witness};
    use std::sync::mpsc;

    #[test]
    fn test_create_hot_signer_from_xpriv() {
        let network = Network::Testnet;
        let xpriv =
            bip32::Xpriv::new_master(network, &bip39::Mnemonic::generate(12).unwrap().to_seed(""))
                .unwrap();
        let signer = HotSigner::new_from_xpriv(network, xpriv);
        assert_eq!(signer.network, network);
    }

    #[test]
    fn test_create_hot_signer_from_mnemonic() {
        let network = Network::Testnet;
        let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
        let signer = HotSigner::new_from_mnemonics(network, mnemonic).unwrap();
        assert_eq!(signer.network, network);
    }

    #[test]
    fn test_sign_transaction() {
        setup_logger();
        let network = Network::Testnet;
        let xpriv =
            bip32::Xpriv::new_master(network, &bip39::Mnemonic::generate(12).unwrap().to_seed(""))
                .unwrap();
        let signer = HotSigner::new_from_xpriv(network, xpriv);
        let xpub = signer.xpub(&DerivationPath::from_str("m/84'/0'/0'/1'").unwrap());
        let descriptor = wpkh(xpub);

        let txin = TxIn {
            previous_output: bitcoin::OutPoint {
                txid: txid(0),
                vout: 1,
            },
            script_sig: ScriptBuf::new(),
            sequence: bitcoin::Sequence::ZERO,
            witness: Witness::new(),
        };

        let txout = random_output();

        let tx = bitcoin::Transaction {
            version: bitcoin::transaction::Version(2),
            lock_time: bitcoin::absolute::LockTime::Blocks(Height::ZERO),
            input: vec![txin],
            output: vec![txout],
        };

        let mut psbt = Psbt::from_unsigned_tx(tx).unwrap();

        let deriv = &(AddrAccount::Receive, 0);
        let deriv_p = deriv_path(deriv).unwrap();
        let pubkey = signer.public_key_at(&deriv_p);

        // there is no signature
        assert!(psbt.inputs[0].partial_sigs.is_empty());

        // try to sign the tx
        signer.inner_sign(&mut psbt, &descriptor).unwrap();

        // there is no signature as bip32_derivation is missing
        assert!(psbt.inputs[0].partial_sigs.is_empty());

        // add a wrong derivation path
        let w_deriv = &(AddrAccount::Change, 0);
        let w_deriv_path = deriv_path(w_deriv).unwrap();
        psbt.inputs
            .get_mut(0)
            .unwrap()
            .bip32_derivation
            .insert(pubkey, (signer.fingerprint(), w_deriv_path));

        // try to sign the tx
        let res = signer.inner_sign(&mut psbt, &descriptor);

        // witness_utxo is missing
        assert_eq!(res, Err(Error::MissingWitnessUtxo));

        // there is no signature
        assert!(psbt.inputs[0].partial_sigs.is_empty());

        let derivator = Derivator::new(descriptor.clone(), bitcoin::Network::Regtest).unwrap();

        // add spent TxOut
        psbt.inputs.get_mut(0).unwrap().witness_utxo = Some(bitcoin::TxOut {
            value: Amount::from_sat(100_000),
            script_pubkey: derivator.receive_spk_at(deriv.1),
        });

        // try to sign the tx
        signer.inner_sign(&mut psbt, &descriptor).unwrap();

        // there is no signature as bip32_derivation is wrong and the public key
        // do not match only the fingerprint
        assert!(psbt.inputs[0].partial_sigs.is_empty());

        // cleanup deriv path map
        psbt.inputs[0].bip32_derivation.clear();

        // add the bip32 deriv
        psbt.inputs
            .get_mut(0)
            .unwrap()
            .bip32_derivation
            .insert(pubkey, (signer.fingerprint(), deriv_p));

        // sign the tx
        signer.inner_sign(&mut psbt, &descriptor).unwrap();

        // signature was added
        assert!(!psbt.inputs[0].partial_sigs.is_empty());
    }

    // Notification Signer tests

    struct MockSender {
        receiver: mpsc::Receiver<SignerNotif>,
    }

    impl MockSender {
        fn new() -> (mpsc::Sender<SignerNotif>, Self) {
            let (sender, receiver) = mpsc::channel();
            (sender, MockSender { receiver })
        }
    }

    #[test]
    fn test_signer_init() {
        let (sender, mock) = MockSender::new();
        let mut signer = HotSigner::new_from_xpriv(
            Network::Regtest,
            bip32::Xpriv::new_master(
                Network::Regtest,
                &bip39::Mnemonic::generate(12).unwrap().to_seed(""),
            )
            .unwrap(),
        );
        signer.init(sender);

        let notif = mock.receiver.recv().unwrap();
        match notif {
            SignerNotif::Info(fg, _) => {
                assert_eq!(signer.fingerprint(), fg);
            }
            _ => panic!("Expected Info notification"),
        }
    }

    #[test]
    fn test_signer_info() {
        let (sender, mock) = MockSender::new();
        let mut signer = HotSigner::new_from_xpriv(
            Network::Regtest,
            bip32::Xpriv::new_master(
                Network::Regtest,
                &bip39::Mnemonic::generate(12).unwrap().to_seed(""),
            )
            .unwrap(),
        );
        signer.init(sender);
        signer.info();

        let notif = mock.receiver.recv().unwrap();
        match notif {
            SignerNotif::Info(fg, _) => {
                assert_eq!(signer.fingerprint(), fg);
            }
            _ => panic!("Expected Info notification"),
        }
    }

    #[test]
    fn test_signer_get_xpub() {
        let (sender, mock) = MockSender::new();
        let mut signer = HotSigner::new_from_xpriv(
            Network::Regtest,
            bip32::Xpriv::new_master(
                Network::Regtest,
                &bip39::Mnemonic::generate(12).unwrap().to_seed(""),
            )
            .unwrap(),
        );
        signer.init(sender);
        let derivation_path = DerivationPath::from_str("m/84'/0'/0'/0").unwrap();
        signer.get_xpub(derivation_path, false);

        // first notif in info
        let _ = mock.receiver.recv().unwrap();

        // second is expected to be xpub
        let notif = mock.receiver.recv().unwrap();
        match notif {
            SignerNotif::Xpub(fg, _) => {
                assert_eq!(signer.fingerprint(), fg);
            }
            _ => panic!("Expected Xpub notification"),
        }
    }

    #[test]
    fn test_signer_is_descriptor_registered() {
        let (sender, mock) = MockSender::new();
        let mut signer = HotSigner::new_from_xpriv(
            Network::Regtest,
            bip32::Xpriv::new_master(
                Network::Regtest,
                &bip39::Mnemonic::generate(12).unwrap().to_seed(""),
            )
            .unwrap(),
        );
        signer.init(sender);
        // info notif
        let _ = mock.receiver.recv();
        let descriptor = wpkh(signer.xpub(&DerivationPath::from_str("m/84'/0'/0'/0").unwrap()));

        signer.is_descriptor_registered(descriptor.clone());
        let notif = mock.receiver.recv().unwrap();
        match notif {
            SignerNotif::DescriptorRegistered(fg, desc, false) => {
                assert_eq!(signer.fingerprint(), fg);
                assert_eq!(desc, descriptor);
            }
            _ => panic!("Expected DescriptorRegistered notification"),
        }

        signer.register_descriptor(descriptor.clone());
        let notif = mock.receiver.recv().unwrap();
        match notif {
            SignerNotif::DescriptorRegistered(fg, desc, true) => {
                assert_eq!(signer.fingerprint(), fg);
                assert_eq!(desc, descriptor);
            }
            _ => panic!("Expected DescriptorRegistered notification"),
        }

        signer.is_descriptor_registered(descriptor.clone());
        let notif = mock.receiver.recv().unwrap();
        match notif {
            SignerNotif::DescriptorRegistered(fg, desc, true) => {
                assert_eq!(signer.fingerprint(), fg);
                assert_eq!(desc, descriptor);
            }
            _ => panic!("Expected DescriptorRegistered notification"),
        }
    }

    #[test]
    fn test_signer_sign() {
        let (sender, mock) = MockSender::new();
        let mut signer = HotSigner::new_from_xpriv(
            Network::Regtest,
            bip32::Xpriv::new_master(
                Network::Regtest,
                &bip39::Mnemonic::generate(12).unwrap().to_seed(""),
            )
            .unwrap(),
        );
        let derivation_path = DerivationPath::from_str("m/84'/0'/0'/0").unwrap();
        let descriptor = wpkh(signer.xpub(&derivation_path));

        signer.init(sender);
        let notif = mock.receiver.recv().unwrap();
        match notif {
            SignerNotif::Info(fg, _) => {
                assert_eq!(signer.fingerprint(), fg);
            }
            _ => panic!("Expected DescriptorRegistered notification"),
        }

        signer.register_descriptor(descriptor.clone());
        let notif = mock.receiver.recv().unwrap();
        match notif {
            SignerNotif::DescriptorRegistered(fg, desc, true) => {
                assert_eq!(signer.fingerprint(), fg);
                assert_eq!(desc, descriptor);
            }
            _ => panic!("Expected DescriptorRegistered notification"),
        }

        let txin = TxIn {
            previous_output: bitcoin::OutPoint {
                txid: txid(0),
                vout: 1,
            },
            script_sig: ScriptBuf::new(),
            sequence: bitcoin::Sequence::ZERO,
            witness: Witness::new(),
        };

        let txout = random_output();

        let tx = bitcoin::Transaction {
            version: bitcoin::transaction::Version(2),
            lock_time: bitcoin::absolute::LockTime::Blocks(Height::ZERO),
            input: vec![txin],
            output: vec![txout],
        };

        let mut psbt = Psbt::from_unsigned_tx(tx).unwrap();

        let deriv = &(AddrAccount::Receive, 0);
        let deriv_p = deriv_path(deriv).unwrap();
        let pubkey = signer.public_key_at(&deriv_p);

        let derivator = Derivator::new(descriptor.clone(), bitcoin::Network::Regtest).unwrap();

        // add spent TxOut
        psbt.inputs.get_mut(0).unwrap().witness_utxo = Some(bitcoin::TxOut {
            value: Amount::from_sat(100_000),
            script_pubkey: derivator.receive_spk_at(deriv.1),
        });

        // add the bip32 deriv
        psbt.inputs
            .get_mut(0)
            .unwrap()
            .bip32_derivation
            .insert(pubkey, (signer.fingerprint(), deriv_p));

        signer.sign(psbt, descriptor);
        let notif = mock.receiver.recv().unwrap();
        match notif {
            SignerNotif::Signed(fg, psbt) => {
                assert_eq!(signer.fingerprint(), fg);
                assert!(!psbt.inputs[0].partial_sigs.is_empty());
            }
            _ => panic!("Expected DescriptorRegistered notification"),
        }
    }
}