fix: encoding updates

This commit is contained in:
Oleksandr Pravdyvyi 2025-09-02 10:17:00 +03:00
parent ca131b2c26
commit fbcc9cf3e2
No known key found for this signature in database
GPG Key ID: 9F8955C63C443871
4 changed files with 61 additions and 20 deletions

View File

@ -65,12 +65,12 @@ impl From<HashableBlockData> for Block {
impl From<Block> for HashableBlockData {
fn from(value: Block) -> Self {
Self {
block_id: value.block_id,
prev_block_id: value.prev_block_id,
prev_block_hash: value.prev_block_hash,
timestamp: value.timestamp,
signature: value.signature,
transactions: value.transactions,
block_id: value.header.block_id,
prev_block_id: value.header.prev_block_id,
prev_block_hash: value.header.prev_block_hash,
timestamp: value.header.timestamp,
signature: value.header.signature,
transactions: value.body.transactions,
}
}
}
@ -81,6 +81,8 @@ impl HashableBlockData {
bytes.extend_from_slice(&self.block_id.to_le_bytes());
bytes.extend_from_slice(&self.prev_block_id.to_le_bytes());
bytes.extend_from_slice(&self.prev_block_hash);
bytes.extend_from_slice(&self.timestamp.to_le_bytes());
bytes.extend_from_slice(&self.signature.to_bytes());
let num_transactions: u32 = self.transactions.len() as u32;
bytes.extend_from_slice(&num_transactions.to_le_bytes());
for tx in &self.transactions {
@ -117,14 +119,14 @@ impl HashableBlockData {
}
// TODO: Improve error handling. Remove unwraps.
fn u32_from_cursor(cursor: &mut Cursor<&[u8]>) -> u32 {
pub fn u32_from_cursor(cursor: &mut Cursor<&[u8]>) -> u32 {
let mut word_buf = [0u8; 4];
cursor.read_exact(&mut word_buf).unwrap();
u32::from_le_bytes(word_buf)
}
// TODO: Improve error handling. Remove unwraps.
fn u64_from_cursor(cursor: &mut Cursor<&[u8]>) -> u64 {
pub fn u64_from_cursor(cursor: &mut Cursor<&[u8]>) -> u64 {
let mut word_buf = [0u8; 8];
cursor.read_exact(&mut word_buf).unwrap();
u64::from_le_bytes(word_buf)

View File

@ -1,3 +1,5 @@
use std::io::Cursor;
use k256::ecdsa::{
signature::{Signer, Verifier},
Signature, SigningKey, VerifyingKey,
@ -240,10 +242,48 @@ impl Transaction {
}
}
impl Transaction {
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::new();
let body_bytes = self.body.to_bytes();
let signature_bytes = self.signature.to_bytes();
let public_key_bytes = self.public_key.to_sec1_bytes();
let body_bytes_len = body_bytes.len() as u32;
let signature_bytes_len = signature_bytes.len() as u32;
let public_key_bytes_len = public_key_bytes.len() as u32;
bytes.extend_from_slice(&body_bytes_len.to_le_bytes());
bytes.extend_from_slice(&signature_bytes_len.to_le_bytes());
bytes.extend_from_slice(&public_key_bytes_len.to_le_bytes());
bytes.extend_from_slice(&self.body.to_bytes());
bytes.extend_from_slice(&self.signature.to_bytes());
bytes.extend_from_slice(&self.public_key.to_sec1_bytes());
bytes
}
// TODO: Improve error handling. Remove unwraps.
pub fn from_bytes(data: &[u8]) -> Self {
let mut cursor = Cursor::new(data);
let body_bytes_len = u32_from_cursor(&mut cursor) as usize;
let signature_bytes_len = u32_from_cursor(&mut cursor) as usize;
let public_key_bytes_len = u32_from_cursor(&mut cursor) as usize;
let body_bytes = Vec::with_capacity(body_bytes_len);
let signature_bytes = Vec::with_capacity(signature_bytes_len);
let public_key_bytes = Vec::with_capacity(public_key_bytes_len);
}
}
/// A transaction with a valid signature over the hash of its body.
/// Can only be constructed from an `Transaction`
/// if the signature is valid
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AuthenticatedTransaction {
hash: TransactionHash,
transaction: Transaction,
@ -265,6 +305,15 @@ impl AuthenticatedTransaction {
}
}
impl AuthenticatedTransaction {
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::new();
bytes.extend_from_slice(&self.hash);
bytes.extend_from_slice(&self.transaction.to_bytes());
bytes
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -506,16 +506,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "chrono"
version = "0.4.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d"
dependencies = [
"android-tzdata",
"iana-time-zone",
"num-traits",
"serde",
"windows-link",
name = "chacha20"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"

View File

@ -16,7 +16,7 @@ const MESSAGE_ENCODING_PREFIX_LEN: usize = 22;
const MESSAGE_ENCODING_PREFIX: &[u8; MESSAGE_ENCODING_PREFIX_LEN] = b"\x01/NSSA/v0.1/TxMessage/";
impl EncryptedAccountData {
pub(crate) fn to_bytes(&self) -> Vec<u8> {
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes = self.ciphertext.to_bytes();
bytes.extend_from_slice(&self.epk.to_bytes());
bytes.push(self.view_tag);