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
use bitcoin::hashes::{sha256, Hmac};
use bitcoin::secp256k1;
use client_side_validation::commit_verify::EmbedCommitVerify;
use super::{
Container, Error, Proof, PubkeyCommitment, PubkeyContainer,
ScriptEncodeData,
};
#[derive(Clone, PartialEq, Eq, Hash, Debug, Display)]
#[display(Debug)]
pub struct TaprootContainer {
pub script_root: sha256::Hash,
pub intermediate_key: secp256k1::PublicKey,
pub tag: sha256::Hash,
pub tweaking_factor: Option<Hmac<sha256::Hash>>,
}
impl Container for TaprootContainer {
type Supplement = sha256::Hash;
type Host = Option<()>;
fn reconstruct(
proof: &Proof,
supplement: &Self::Supplement,
_: &Self::Host,
) -> Result<Self, Error> {
if let ScriptEncodeData::Taproot(ref tapscript_root) = proof.source {
Ok(Self {
script_root: tapscript_root.clone(),
intermediate_key: proof.pubkey,
tag: supplement.clone(),
tweaking_factor: None,
})
} else {
Err(Error::InvalidProofStructure)
}
}
fn deconstruct(self) -> (Proof, Self::Supplement) {
(
Proof {
pubkey: self.intermediate_key,
source: ScriptEncodeData::Taproot(self.script_root),
},
self.tag,
)
}
fn to_proof(&self) -> Proof {
Proof {
pubkey: self.intermediate_key.clone(),
source: ScriptEncodeData::Taproot(self.script_root.clone()),
}
}
fn into_proof(self) -> Proof {
Proof {
pubkey: self.intermediate_key,
source: ScriptEncodeData::Taproot(self.script_root),
}
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug, Display)]
#[display(Debug)]
pub struct TaprootCommitment {
pub script_root: sha256::Hash,
pub intermediate_key_commitment: PubkeyCommitment,
}
impl<MSG> EmbedCommitVerify<MSG> for TaprootCommitment
where
MSG: AsRef<[u8]>,
{
type Container = TaprootContainer;
type Error = Error;
fn embed_commit(
container: &mut Self::Container,
msg: &MSG,
) -> Result<Self, Self::Error> {
let mut pubkey_container = PubkeyContainer {
pubkey: container.intermediate_key.clone(),
tag: container.tag.clone(),
tweaking_factor: None,
};
let cmt = PubkeyCommitment::embed_commit(&mut pubkey_container, msg)?;
container.tweaking_factor = pubkey_container.tweaking_factor;
Ok(Self {
script_root: container.script_root,
intermediate_key_commitment: cmt,
})
}
}