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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#![allow(rustdoc::invalid_html_tags)]
pub mod protobuf {
include!(concat!(env!("OUT_DIR"), "/ipns.rs"));
}
use crate::cbor;
use crate::signer::{Signables, Signed};
use anyhow::{anyhow, Error, Result};
use cbor::Data;
use libp2p_identity::{ed25519, PeerId, PublicKey};
use multihash::Multihash;
use prost::Message; pub use protobuf::ipns_entry::ValidityType;
pub use protobuf::IpnsEntry;
use std::io::Cursor; impl IpnsEntry {
pub fn new(data: Data, signed: Signed) -> Self {
Self {
data: Some(data.to_bytes()),
value: Some(data.value),
validity: Some(data.validity),
validity_type: Some(ValidityType::Eol.into()),
signature_v1: Some(signed.v1),
signature_v2: Some(signed.v2),
sequence: Some(data.sequence),
ttl: Some(data.ttl),
pub_key: None,
}
}
pub fn to_bytes(&self) -> Vec<u8> {
let mut buf = Vec::new();
buf.reserve(self.encoded_len());
self.encode(&mut buf).unwrap();
assert!(buf.len() <= 10 * 1024);
buf
}
pub fn from_bytes(buf: &[u8]) -> Result<Self, prost::DecodeError> {
assert!(buf.len() <= 10 * 1024);
Self::decode(&mut Cursor::new(buf))
}
pub fn get_public_key(&self, peer_id: &PeerId) -> Result<PublicKey, anyhow::Error> {
if self.signature_v2.is_none() || self.data.is_none() {
return Err(anyhow!(
"Missing both IpnsEntry.signatureV2 and IpnsEntry.data"
));
}
match self.pub_key.as_ref() {
Some(pk) => Ok(PublicKey::try_decode_protobuf(pk)?),
None => match Multihash::from_bytes(&peer_id.to_bytes()) {
Ok(mh) => {
let d = mh.digest();
let ed_key =
ed25519::PublicKey::try_from_bytes(&d[4..]).expect("a Valid ed25519 key");
let pub_key: PublicKey = PublicKey::from(ed_key);
Ok(pub_key)
}
Err(_) => return Err(anyhow!("Invalid PeerId")),
},
}
}
pub fn decode_data(&self) -> Result<Data, Error> {
if self.data.is_none() {
return Err(anyhow!("Missing IpnsEntry.data"));
}
let data: &[u8] = self.data.as_ref().expect("Valid data");
match cbor::Data::from_bytes(data) {
Ok(data) => Ok(data),
Err(_) => return Err(anyhow!("Invalid DAG-CBOR")),
}
}
pub fn is_valid_for(&self, peer_id: &PeerId) -> Result<bool, Error> {
let data = self.decode_data()?;
if self.value.as_ref().expect("Valid value") != &data.value {
return Err(anyhow!(
"IpnsEntry.value does not match IpnsEntry.data[value]"
));
}
if self.validity.as_ref().expect("Valid validity") != &data.validity {
return Err(anyhow!(
"IpnsEntry.validity does not match IpnsEntry.data[validity]"
));
}
if self.validity_type.as_ref().expect("Valid validity_type") != &data.validity_type {
return Err(anyhow!(
"IpnsEntry.validityType does not match IpnsEntry.data[validityType]"
));
}
if self.sequence.as_ref().expect("Valid sequence") != &data.sequence {
return Err(anyhow!(
"IpnsEntry.sequence does not match IpnsEntry.data[sequence]"
));
}
if self.ttl.as_ref().expect("Valid TTL") != &data.ttl {
return Err(anyhow!("IpnsEntry.ttl does not match IpnsEntry.data[ttl]"));
}
let pub_key = self.get_public_key(peer_id)?;
let signature_v2 = self.signature_v2.as_ref().expect("Valid signature_v2");
let v2_signable = generate_v2_signable(self.data.as_ref().expect("Valid data"));
let valid_sig = pub_key.verify(&v2_signable, signature_v2);
Ok(valid_sig)
}
pub fn signables(&self) -> Result<Signables> {
Ok(Signables {
v1: generate_v1_signable(
self.value.as_ref().expect("Valid value"),
self.validity.as_ref().expect("Valid validity"),
),
v2: generate_v2_signable(self.data.as_ref().expect("Valid data")),
})
}
}
fn generate_v1_signable(value: &[u8], validity: &[u8]) -> Vec<u8> {
vec![value, validity, &[ValidityType::Eol as u8]].concat()
}
fn generate_v2_signable(data: &[u8]) -> Vec<u8> {
vec!["ipns-signature:".as_bytes(), data].concat()
}
pub fn deserialize(buf: &[u8]) -> Result<IpnsEntry, prost::DecodeError> {
IpnsEntry::decode(&mut Cursor::new(buf))
}
#[cfg(test)]
mod tests {
use super::*;
use libp2p_identity::Keypair;
#[test]
fn test_ipns_entry() {
let entry = IpnsEntry::default();
let bytes = entry.to_bytes();
let entry2 = IpnsEntry::from_bytes(&bytes).unwrap();
assert_eq!(entry, entry2);
}
#[test]
fn test_is_valid_for() {
let keypair = Keypair::generate_ed25519()
.try_into_ed25519()
.expect("Valid keypair");
let mut entry = IpnsEntry {
value: Some(b"value".to_vec()),
validity: Some(b"0".to_vec()),
validity_type: Some(ValidityType::Eol as i32),
sequence: Some(0),
ttl: Some(0),
..Default::default()
};
let data = Data {
value: entry.value.clone().unwrap(),
validity: entry.validity.clone().unwrap(),
validity_type: entry.validity_type.unwrap(),
sequence: entry.sequence.unwrap(),
ttl: entry.ttl.unwrap(),
};
entry.data = Some(data.to_bytes());
let signables = entry.signables().expect("valid signables");
println!("signables.v2: {:?}", signables.v2);
let signature_v2 = keypair.sign(&signables.v2);
let signature_v1 = keypair.sign(&signables.v1);
entry.signature_v2 = Some(signature_v2);
entry.signature_v1 = Some(signature_v1);
let peer_id = PeerId::from_public_key(&PublicKey::from(keypair.public()));
assert!(entry.is_valid_for(&peer_id).unwrap());
}
}