Skip to main content

blockle_core/
encode.rs

1//! Canonical binary encoding for consensus hashing and signing.
2//!
3//! Fixed-width integers are little-endian; variable-length byte strings are
4//! u32-length-prefixed. This encoding is what gets hashed — the JSON used for
5//! disk storage is non-consensus.
6
7pub fn put_u32(out: &mut Vec<u8>, v: u32) {
8    out.extend_from_slice(&v.to_le_bytes());
9}
10
11pub fn put_u64(out: &mut Vec<u8>, v: u64) {
12    out.extend_from_slice(&v.to_le_bytes());
13}
14
15pub fn put_i64(out: &mut Vec<u8>, v: i64) {
16    out.extend_from_slice(&v.to_le_bytes());
17}
18
19pub fn put_bytes(out: &mut Vec<u8>, v: &[u8]) {
20    put_u32(out, v.len() as u32);
21    out.extend_from_slice(v);
22}
23
24pub fn put_fixed(out: &mut Vec<u8>, v: &[u8]) {
25    out.extend_from_slice(v);
26}