fedimint_wallet_common/
keys.rs

1use std::io::{Error, Write};
2use std::str::FromStr;
3
4use bitcoin::secp256k1::{self, PublicKey, Secp256k1, Signing, Verification};
5use fedimint_core::encoding::{Decodable, Encodable};
6use miniscript::bitcoin::hashes::{hash160, ripemd160, sha256};
7use miniscript::{MiniscriptKey, ToPublicKey, hash256};
8use serde::{Deserialize, Serialize};
9
10use crate::tweakable::{Contract, Tweakable};
11
12#[derive(
13    Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize, Decodable,
14)]
15pub struct CompressedPublicKey {
16    pub key: PublicKey,
17}
18
19impl CompressedPublicKey {
20    pub fn new(key: PublicKey) -> Self {
21        CompressedPublicKey { key }
22    }
23}
24
25impl Encodable for CompressedPublicKey {
26    fn consensus_encode<W: Write>(&self, writer: &mut W) -> Result<(), Error> {
27        self.key.serialize().consensus_encode(writer)
28    }
29}
30
31impl MiniscriptKey for CompressedPublicKey {
32    fn is_uncompressed(&self) -> bool {
33        false
34    }
35
36    fn num_der_paths(&self) -> usize {
37        0
38    }
39
40    type Sha256 = miniscript::bitcoin::hashes::sha256::Hash;
41    type Hash256 = miniscript::hash256::Hash;
42    type Ripemd160 = miniscript::bitcoin::hashes::ripemd160::Hash;
43    type Hash160 = miniscript::bitcoin::hashes::hash160::Hash;
44}
45
46impl ToPublicKey for CompressedPublicKey {
47    fn to_public_key(&self) -> miniscript::bitcoin::PublicKey {
48        miniscript::bitcoin::PublicKey {
49            compressed: true,
50            inner: self.key,
51        }
52    }
53
54    fn to_sha256(hash: &<Self as MiniscriptKey>::Sha256) -> sha256::Hash {
55        *hash
56    }
57
58    fn to_hash256(hash: &<Self as MiniscriptKey>::Hash256) -> hash256::Hash {
59        *hash
60    }
61
62    fn to_ripemd160(hash: &<Self as MiniscriptKey>::Ripemd160) -> ripemd160::Hash {
63        *hash
64    }
65
66    fn to_hash160(hash: &<Self as MiniscriptKey>::Hash160) -> hash160::Hash {
67        *hash
68    }
69}
70
71impl std::fmt::Display for CompressedPublicKey {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        std::fmt::Display::fmt(&self.key, f)
74    }
75}
76
77impl FromStr for CompressedPublicKey {
78    type Err = secp256k1::Error;
79
80    fn from_str(s: &str) -> Result<Self, Self::Err> {
81        Ok(CompressedPublicKey {
82            key: PublicKey::from_str(s)?,
83        })
84    }
85}
86
87impl Tweakable for CompressedPublicKey {
88    fn tweak<Ctx: Verification + Signing, Ctr: Contract>(
89        &self,
90        tweak: &Ctr,
91        secp: &Secp256k1<Ctx>,
92    ) -> Self {
93        CompressedPublicKey {
94            key: self.key.tweak(tweak, secp),
95        }
96    }
97}
98
99impl From<CompressedPublicKey> for bitcoin::PublicKey {
100    fn from(key: CompressedPublicKey) -> Self {
101        bitcoin::PublicKey {
102            compressed: true,
103            inner: key.key,
104        }
105    }
106}