fedimint_recoverytool/
key.rs

1use std::cmp::Ordering;
2use std::fmt::{Display, Formatter};
3use std::hash::Hasher;
4
5use fedimint_wallet_server::common::keys::CompressedPublicKey;
6use miniscript::MiniscriptKey;
7
8/// `MiniscriptKey` that is either a WIF-encoded private key or a compressed,
9/// hex-encoded public key
10#[derive(Debug, Clone, Copy, Eq)]
11pub enum Key {
12    Public(CompressedPublicKey),
13    Private(bitcoin::key::PrivateKey),
14}
15
16impl PartialOrd for Key {
17    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
18        Some(
19            self.to_compressed_public_key()
20                .cmp(&other.to_compressed_public_key()),
21        )
22    }
23}
24
25impl Ord for Key {
26    fn cmp(&self, other: &Self) -> Ordering {
27        self.to_compressed_public_key()
28            .cmp(&other.to_compressed_public_key())
29    }
30}
31
32impl PartialEq for Key {
33    fn eq(&self, other: &Self) -> bool {
34        self.to_compressed_public_key()
35            .eq(&other.to_compressed_public_key())
36    }
37}
38
39impl std::hash::Hash for Key {
40    fn hash<H: Hasher>(&self, state: &mut H) {
41        self.to_compressed_public_key().hash(state);
42    }
43}
44
45impl Key {
46    fn to_compressed_public_key(self) -> CompressedPublicKey {
47        match self {
48            Key::Public(pk) => pk,
49            Key::Private(sk) => CompressedPublicKey::new(
50                bitcoin::secp256k1::PublicKey::from_secret_key_global(&sk.inner),
51            ),
52        }
53    }
54}
55
56impl Display for Key {
57    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
58        match self {
59            Key::Public(pk) => Display::fmt(pk, f),
60            Key::Private(sk) => Display::fmt(sk, f),
61        }
62    }
63}
64
65impl MiniscriptKey for Key {
66    fn is_uncompressed(&self) -> bool {
67        false
68    }
69
70    fn num_der_paths(&self) -> usize {
71        0
72    }
73
74    type Sha256 = bitcoin::hashes::sha256::Hash;
75    type Hash256 = miniscript::hash256::Hash;
76    type Ripemd160 = bitcoin::hashes::ripemd160::Hash;
77    type Hash160 = bitcoin::hashes::hash160::Hash;
78}