Serialize StateObject to byte array so that it can be wots-signed
(&self)
| 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 { |