MCPcopy Create free account
hub / github.com/BitVM/BitVM / serialize_to_byte_array

Method serialize_to_byte_array

bitvm/src/chunk/elements.rs:134–158  ·  view source on GitHub ↗

Serialize StateObject to byte array so that it can be wots-signed

(&self)

Source from the content-addressed store, hash-verified

132
133 // Serialize StateObject to byte array so that it can be wots-signed
134 pub(crate) fn serialize_to_byte_array(&self) -> Vec<u8> {
135 fn nib_to_byte_array(digits: &[u8]) -> Vec<u8> {
136 let mut msg_bytes = Vec::with_capacity(digits.len() / 2);
137
138 for nibble_pair in digits.chunks(2) {
139 let byte = (nibble_pair[1] << 4) | (nibble_pair[0] & 0b00001111);
140 msg_bytes.push(byte);
141 }
142
143 msg_bytes
144 }
145 match self {
146 CompressedStateObject::Hash(h) => {
147 let bal: [u8; 32] = nib_to_byte_array(h).try_into().unwrap();
148 let bal: [u8; BLAKE3_HASH_LENGTH] =
149 bal[32 - BLAKE3_HASH_LENGTH..32].try_into().unwrap();
150 bal.to_vec()
151 }
152 CompressedStateObject::U256(n) => {
153 let n = extern_bigint_to_nibbles(*n);
154 let bal: [u8; 32] = nib_to_byte_array(&n).try_into().unwrap();
155 bal.to_vec()
156 }
157 }
158 }
159
160 // Deserialize wots-signed byte array to object
161 pub(crate) fn deserialize_from_byte_array(byte_array: Vec<u8>) -> Self {

Calls 2

nib_to_byte_arrayFunction · 0.85
extern_bigint_to_nibblesFunction · 0.85