-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathencode.rs
More file actions
526 lines (474 loc) · 16.1 KB
/
encode.rs
File metadata and controls
526 lines (474 loc) · 16.1 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Rust Elements Library
// Written in 2018 by
// Andrew Poelstra <apoelstra@blockstream.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//
//! Consensus-encodable types
//!
use std::io::Cursor;
use std::{any, error, fmt, io, mem};
use bitcoin::ScriptBuf;
use hex_conservative::{DecodeFixedLengthBytesError, DecodeVariableLengthBytesError};
use secp256k1_zkp::{self, RangeProof, SurjectionProof, Tweak};
use crate::hashes::{sha256, Hash};
use crate::pset;
pub use bitcoin::{self, consensus::encode::MAX_VEC_SIZE};
use crate::taproot::TapLeafHash;
/// Encoding error
#[derive(Debug)]
pub enum Error {
/// And I/O error
Io(io::Error),
/// A Bitcoin encoding error.
Bitcoin(bitcoin::consensus::encode::Error),
/// Tried to allocate an oversized vector
OversizedVectorAllocation {
/// The capacity requested
requested: usize,
/// The maximum capacity
max: usize,
},
/// Parsing error
ParseFailed(&'static str),
/// We unexpectedly hit the end of the buffer
UnexpectedEOF,
/// Invalid prefix for the confidential type.
InvalidConfidentialPrefix(u8),
/// Parsing within libsecp256k1 failed
Secp256k1(secp256k1_zkp::UpstreamError),
/// Parsing within libsecp256k1-zkp failed
Secp256k1zkp(secp256k1_zkp::Error),
/// Pset related Errors
PsetError(pset::Error),
/// Hex fixed parsing errors
HexFixedError(DecodeFixedLengthBytesError),
/// Hex variable parsing errors
HexVariableError(DecodeVariableLengthBytesError),
/// Got a time-based locktime when expecting a height-based one, or vice-versa
BadLockTime(crate::LockTime),
/// `VarInt` was encoded in a non-minimal way.
NonMinimalVarInt,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Io(ref e) => write!(f, "I/O error: {}", e),
Error::Bitcoin(ref e) => write!(f, "a Bitcoin type encoding error: {}", e),
Error::OversizedVectorAllocation {
requested: ref r,
max: ref m,
} => write!(
f,
"oversized vector allocation: requested {}, maximum {}",
r, m
),
Error::ParseFailed(e) => write!(f, "parse failed: {}", e),
Error::UnexpectedEOF => write!(f, "unexpected EOF"),
Error::InvalidConfidentialPrefix(p) => {
write!(f, "invalid confidential prefix: 0x{:02x}", p)
}
Error::Secp256k1(ref e) => write!(f, "{}", e),
Error::Secp256k1zkp(ref e) => write!(f, "{}", e),
Error::PsetError(ref e) => write!(f, "Pset Error: {}", e),
Error::HexFixedError(ref e) => write!(f, "Hex fixed error: {}", e),
Error::HexVariableError(ref e) => write!(f, "Hex variable error: {}", e),
Error::BadLockTime(ref lt) => write!(f, "Invalid locktime {}", lt),
Error::NonMinimalVarInt => write!(f, "non-minimal varint"),
}
}
}
impl error::Error for Error {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::Secp256k1zkp(ref e) => Some(e),
_ => None,
}
}
}
#[doc(hidden)]
impl From<bitcoin::consensus::encode::Error> for Error {
fn from(e: bitcoin::consensus::encode::Error) -> Error {
Error::Bitcoin(e)
}
}
#[doc(hidden)]
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Error::Io(error)
}
}
#[doc(hidden)]
impl From<pset::Error> for Error {
fn from(e: pset::Error) -> Error {
Error::PsetError(e)
}
}
#[doc(hidden)]
impl From<secp256k1_zkp::UpstreamError> for Error {
fn from(e: secp256k1_zkp::UpstreamError) -> Self {
Error::Secp256k1(e)
}
}
#[doc(hidden)]
impl From<secp256k1_zkp::Error> for Error {
fn from(e: secp256k1_zkp::Error) -> Self {
Error::Secp256k1zkp(e)
}
}
#[doc(hidden)]
impl From<DecodeFixedLengthBytesError> for Error {
fn from(e: DecodeFixedLengthBytesError) -> Self {
Error::HexFixedError(e)
}
}
#[doc(hidden)]
impl From<DecodeVariableLengthBytesError> for Error {
fn from(e: DecodeVariableLengthBytesError) -> Self {
Error::HexVariableError(e)
}
}
/// Data which can be encoded in a consensus-consistent way
pub trait Encodable {
/// Encode an object with a well-defined format, should only ever error if
/// the underlying `Write` errors. Returns the number of bytes written on
/// success
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, Error>;
}
/// Data which can be encoded in a consensus-consistent way
pub trait Decodable: Sized {
/// Decode an object with a well-defined format
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error>;
}
/// Encode an object into a vector
pub fn serialize<T: Encodable + ?Sized>(data: &T) -> Vec<u8> {
let mut encoder = Cursor::new(vec![]);
data.consensus_encode(&mut encoder).unwrap();
encoder.into_inner()
}
/// Encode an object into a hex-encoded string
pub fn serialize_hex<T: Encodable + ?Sized>(data: &T) -> String {
crate::hex::ToHex::to_hex(&serialize(data)[..])
}
/// Deserialize an object from a vector, will error if said deserialization
/// doesn't consume the entire vector.
pub fn deserialize<T: Decodable>(data: &[u8]) -> Result<T, Error> {
let (rv, consumed) = deserialize_partial(data)?;
// Fail if data are not consumed entirely.
if consumed == data.len() {
Ok(rv)
} else {
Err(Error::ParseFailed(
"data not consumed entirely when explicitly deserializing",
))
}
}
/// Deserialize an object from a vector, but will not report an error if said deserialization
/// doesn't consume the entire vector.
pub fn deserialize_partial<T: Decodable>(data: &[u8]) -> Result<(T, usize), Error> {
let mut decoder = Cursor::new(data);
let rv = Decodable::consensus_decode(&mut decoder)?;
let consumed = decoder.position() as usize;
Ok((rv, consumed))
}
impl Encodable for sha256::Midstate {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, Error> {
self.to_byte_array().consensus_encode(e)
}
}
impl Decodable for sha256::Midstate {
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error> {
Ok(Self::from_byte_array(<[u8; 32]>::consensus_decode(d)?))
}
}
pub(crate) fn consensus_encode_with_size<S: crate::WriteExt>(
data: &[u8],
mut s: S,
) -> Result<usize, Error> {
let vi_len = VarInt(data.len() as u64).consensus_encode(&mut s)?;
s.emit_slice(data)?;
Ok(vi_len + data.len())
}
// Specific locktime types (which appear in PSET/PSBT2 but not in rust-bitcoin PSBT)
impl Encodable for crate::locktime::Height {
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, Error> {
crate::LockTime::from(*self).consensus_encode(s)
}
}
impl Decodable for crate::locktime::Height {
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error> {
match crate::LockTime::consensus_decode(d)? {
crate::LockTime::Blocks(h) => Ok(h),
x @ crate::LockTime::Seconds(_) => Err(Error::BadLockTime(x)),
}
}
}
impl Encodable for crate::locktime::Time {
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, Error> {
crate::LockTime::from(*self).consensus_encode(s)
}
}
impl Decodable for crate::locktime::Time {
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error> {
match crate::LockTime::consensus_decode(d)? {
crate::LockTime::Seconds(t) => Ok(t),
x @ crate::LockTime::Blocks(_) => Err(Error::BadLockTime(x)),
}
}
}
/// A variable sized integer.
pub struct VarInt(pub u64);
impl Encodable for VarInt {
fn consensus_encode<W: crate::WriteExt>(&self, mut e: W) -> Result<usize, Error> {
Ok(e.emit_varint(self.0)?)
}
}
impl Decodable for VarInt {
fn consensus_decode<D: crate::ReadExt>(mut d: D) -> Result<Self, Error> {
Ok(VarInt(d.read_varint()?))
}
}
impl VarInt {
/// returns the byte size used if this var int is serialized
pub fn size(&self) -> usize {
match self.0 {
0..=0xFC => 1,
0xFD..=0xFFFF => 3,
0x10000..=0xFFFF_FFFF => 5,
_ => 9,
}
}
}
// Primitive types
macro_rules! impl_int {
($ty:ident, $meth_dec:ident, $meth_enc:ident) => {
impl Encodable for $ty {
fn consensus_encode<W: crate::WriteExt>(&self, mut w: W) -> Result<usize, Error> {
w.$meth_enc(*self)?;
Ok(mem::size_of::<$ty>())
}
}
impl Decodable for $ty {
fn consensus_decode<R: crate::ReadExt>(mut r: R) -> Result<Self, Error> {
crate::ReadExt::$meth_dec(&mut r)
}
}
};
}
impl_int!(u8, read_u8, emit_u8);
impl_int!(u16, read_u16, emit_u16);
impl_int!(u32, read_u32, emit_u32);
impl_int!(u64, read_u64, emit_u64);
impl Encodable for bitcoin::ScriptBuf {
fn consensus_encode<W: io::Write>(&self, w: W) -> Result<usize, Error> {
consensus_encode_with_size(self.as_script().as_bytes(), w)
}
}
impl Decodable for bitcoin::ScriptBuf {
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error> {
let bytes = Vec::<u8>::consensus_decode(d)?;
Ok(ScriptBuf::from_bytes(bytes))
}
}
impl Encodable for bitcoin::hashes::sha256d::Hash {
fn consensus_encode<W: io::Write>(&self, mut w: W) -> Result<usize, Error> {
self.as_byte_array().consensus_encode(&mut w)
}
}
impl Decodable for bitcoin::hashes::sha256d::Hash {
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error> {
Ok(Self::from_byte_array(
<<Self as Hash>::Bytes>::consensus_decode(d)?,
))
}
}
// Vectors
impl<T: Encodable + any::Any> Encodable for [T] {
#[inline]
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, Error> {
if any::TypeId::of::<T>() == any::TypeId::of::<u8>() {
// SAFETY: checked that T is exactly u8, so &self, of type, &[T], is exactly &[u8]
let u8_slice = unsafe {
std::slice::from_raw_parts(self.as_ptr().cast::<u8>(), self.len())
};
consensus_encode_with_size(u8_slice, s)
} else {
let mut len = 0;
len += VarInt(self.len() as u64).consensus_encode(&mut s)?;
for c in self {
len += c.consensus_encode(&mut s)?;
}
Ok(len)
}
}
}
impl<T: Encodable + any::Any> Encodable for Vec<T> {
#[inline]
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, Error> {
self[..].consensus_encode(s)
}
}
impl<T: Encodable + any::Any> Encodable for Box<[T]> {
#[inline]
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, Error> {
self[..].consensus_encode(s)
}
}
impl<T: Decodable + any::Any> Decodable for Vec<T> {
#[inline]
fn consensus_decode<D: crate::ReadExt>(mut d: D) -> Result<Self, Error> {
if any::TypeId::of::<T>() == any::TypeId::of::<u8>() {
let s = VarInt::consensus_decode(&mut d)?.0 as usize;
if s > MAX_VEC_SIZE {
return Err(self::Error::OversizedVectorAllocation {
requested: s,
max: MAX_VEC_SIZE,
});
}
let mut v = vec![0; s];
d.read_slice(&mut v)?;
// SAFETY: checked that T is exactly u8, so v, of type, Vec<u8>, is exactly Vec<T>
unsafe {
Ok(std::mem::transmute::<Vec<u8>, Vec<T>>(v))
}
} else {
let len = VarInt::consensus_decode(&mut d)?.0;
let byte_size = (len as usize)
.checked_mul(mem::size_of::<T>())
.ok_or(self::Error::ParseFailed("Invalid length"))?;
if byte_size > MAX_VEC_SIZE {
return Err(self::Error::OversizedVectorAllocation {
requested: byte_size,
max: MAX_VEC_SIZE,
});
}
let mut ret = Vec::with_capacity(len as usize);
for _ in 0..len {
ret.push(Decodable::consensus_decode(&mut d)?);
}
Ok(ret)
}
}
}
impl<T: Decodable + any::Any> Decodable for Box<[T]> {
#[inline]
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error> {
let v = Vec::<T>::consensus_decode(d)?;
Ok(v.into())
}
}
macro_rules! impl_array {
( $size:literal ) => {
impl Encodable for [u8; $size] {
#[inline]
fn consensus_encode<W: crate::WriteExt>(
&self,
mut w: W,
) -> core::result::Result<usize, Error> {
w.emit_slice(&self[..])?;
Ok($size)
}
}
impl Decodable for [u8; $size] {
#[inline]
fn consensus_decode<R: crate::ReadExt>(mut r: R) -> core::result::Result<Self, Error> {
let mut ret = [0; $size];
r.read_slice(&mut ret)?;
Ok(ret)
}
}
};
}
impl_array!(4);
impl_array!(32);
impl_array!(33);
macro_rules! impl_box_option {
($type: ty) => {
impl Encodable for Option<Box<$type>> {
#[inline]
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, Error> {
match self {
None => Vec::<u8>::new().consensus_encode(e),
Some(v) => v.serialize().consensus_encode(e),
}
}
}
impl Decodable for Option<Box<$type>> {
#[inline]
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
let v: Vec<u8> = Decodable::consensus_decode(&mut d)?;
if v.is_empty() {
Ok(None)
} else {
Ok(Some(Box::new(<$type>::from_slice(&v)?)))
}
}
}
};
}
// special implementations for elements only fields
impl Encodable for Tweak {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, Error> {
self.as_ref().consensus_encode(e)
}
}
impl Decodable for Tweak {
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error> {
Ok(Tweak::from_inner(<[u8; 32]>::consensus_decode(d)?)?)
}
}
impl Encodable for RangeProof {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, Error> {
self.serialize().consensus_encode(e)
}
}
impl Decodable for RangeProof {
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error> {
Ok(RangeProof::from_slice(&<Vec<u8>>::consensus_decode(d)?)?)
}
}
impl Encodable for SurjectionProof {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, Error> {
self.serialize().consensus_encode(e)
}
}
impl Decodable for SurjectionProof {
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error> {
Ok(SurjectionProof::from_slice(&<Vec<u8>>::consensus_decode(
d,
)?)?)
}
}
impl Encodable for sha256::Hash {
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, Error> {
self.to_byte_array().consensus_encode(s)
}
}
impl Decodable for sha256::Hash {
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error> {
Ok(Self::from_byte_array(
<<Self as Hash>::Bytes>::consensus_decode(d)?,
))
}
}
impl Encodable for TapLeafHash {
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, Error> {
self.to_byte_array().consensus_encode(s)
}
}
impl Decodable for TapLeafHash {
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error> {
Ok(Self::from_byte_array(
<<Self as Hash>::Bytes>::consensus_decode(d)?,
))
}
}
impl_box_option!(RangeProof);
impl_box_option!(SurjectionProof);