Impl ssz Enc/Decode on Address. Decode on Vec<T>

This commit is contained in:
Paul Hauner 2018-12-28 19:39:44 +11:00
parent d9c472ec37
commit e6977b5f4b
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
2 changed files with 27 additions and 2 deletions

View File

@ -1,5 +1,5 @@
use super::decode::decode_ssz_list;
use super::ethereum_types::H256;
use super::ethereum_types::{Address, H256};
use super::{Decodable, DecodeError};
macro_rules! impl_decodable_for_uint {
@ -49,6 +49,16 @@ impl Decodable for H256 {
}
}
impl Decodable for Address {
fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError> {
if bytes.len() < 20 || bytes.len() - 20 < index {
Err(DecodeError::TooShort)
} else {
Ok((Address::from(&bytes[index..(index + 20)]), index + 20))
}
}
}
impl<T> Decodable for Vec<T>
where
T: Decodable,

View File

@ -1,7 +1,7 @@
extern crate bytes;
use self::bytes::{BufMut, BytesMut};
use super::ethereum_types::H256;
use super::ethereum_types::{Address, H256};
use super::{Encodable, SszStream};
/*
@ -52,6 +52,21 @@ impl Encodable for H256 {
}
}
impl Encodable for Address {
fn ssz_append(&self, s: &mut SszStream) {
s.append_encoded_raw(&self.to_vec());
}
}
impl<T> Encodable for Vec<T>
where
T: Encodable,
{
fn ssz_append(&self, s: &mut SszStream) {
s.append_vec(&self);
}
}
#[cfg(test)]
mod tests {
use super::*;