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
use std::convert::TryFrom;
use std::fmt;
use std::str::FromStr;
use crate::bitcoin::{Bitcoin, BitcoinTaproot, Btc, Strategy};
use crate::consensus::{self, CanonicalBytes};
use crate::crypto::{DeriveKeys, SharedKeyId};
use bitcoin::secp256k1::{schnorr::Signature, KeyPair, XOnlyPublicKey};
#[derive(Clone, Debug, Copy, Eq, PartialEq)]
pub struct Taproot;
impl Strategy for Taproot {}
impl fmt::Display for Bitcoin<Taproot> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Bitcoin<Taproot>")
}
}
impl FromStr for Bitcoin<Taproot> {
type Err = consensus::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Taproot" | "taproot" => Ok(Self::new()),
_ => Err(consensus::Error::UnknownType),
}
}
}
impl From<BitcoinTaproot> for Btc {
fn from(v: BitcoinTaproot) -> Self {
Self::Taproot(v)
}
}
impl TryFrom<Btc> for Bitcoin<Taproot> {
type Error = consensus::Error;
fn try_from(v: Btc) -> Result<Self, consensus::Error> {
match v {
Btc::Taproot(v) => Ok(v),
_ => Err(consensus::Error::TypeMismatch),
}
}
}
impl DeriveKeys for Bitcoin<Taproot> {
type PublicKey = XOnlyPublicKey;
type PrivateKey = KeyPair;
fn extra_public_keys() -> Vec<u16> {
vec![]
}
fn extra_shared_private_keys() -> Vec<SharedKeyId> {
vec![]
}
}
impl CanonicalBytes for XOnlyPublicKey {
fn as_canonical_bytes(&self) -> Vec<u8> {
self.serialize().as_ref().into()
}
fn from_canonical_bytes(bytes: &[u8]) -> Result<Self, consensus::Error>
where
Self: Sized,
{
XOnlyPublicKey::from_slice(bytes).map_err(consensus::Error::new)
}
}
impl CanonicalBytes for Signature {
fn as_canonical_bytes(&self) -> Vec<u8> {
(*self.as_ref()).into()
}
fn from_canonical_bytes(bytes: &[u8]) -> Result<Self, consensus::Error>
where
Self: Sized,
{
Signature::from_slice(bytes).map_err(consensus::Error::new)
}
}