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(self.cmp(other))
19    }
20}
21
22impl Ord for Key {
23    fn cmp(&self, other: &Self) -> Ordering {
24        self.to_compressed_public_key()
25            .cmp(&other.to_compressed_public_key())
26    }
27}
28
29impl PartialEq for Key {
30    fn eq(&self, other: &Self) -> bool {
31        self.to_compressed_public_key()
32            .eq(&other.to_compressed_public_key())
33    }
34}
35
36impl std::hash::Hash for Key {
37    fn hash<H: Hasher>(&self, state: &mut H) {
38        self.to_compressed_public_key().hash(state);
39    }
40}
41
42impl Key {
43    fn to_compressed_public_key(self) -> CompressedPublicKey {
44        match self {
45            Key::Public(pk) => pk,
46            Key::Private(sk) => CompressedPublicKey::new(
47                bitcoin::secp256k1::PublicKey::from_secret_key_global(&sk.inner),
48            ),
49        }
50    }
51}
52
53impl Display for Key {
54    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
55        match self {
56            Key::Public(pk) => Display::fmt(pk, f),
57            Key::Private(sk) => Display::fmt(sk, f),
58        }
59    }
60}
61
62impl MiniscriptKey for Key {
63    fn is_uncompressed(&self) -> bool {
64        false
65    }
66
67    fn num_der_paths(&self) -> usize {
68        0
69    }
70
71    type Sha256 = bitcoin::hashes::sha256::Hash;
72    type Hash256 = miniscript::hash256::Hash;
73    type Ripemd160 = bitcoin::hashes::ripemd160::Hash;
74    type Hash160 = bitcoin::hashes::hash160::Hash;
75}