fix localstorage error
This commit is contained in:
24
src/misc.rs
24
src/misc.rs
@@ -1,3 +1,4 @@
|
||||
use base64::{engine::general_purpose, Engine as _};
|
||||
use egui_plot::{Line, PlotPoint, PlotPoints, Points};
|
||||
use emath::Pos2;
|
||||
use getrandom::getrandom;
|
||||
@@ -165,24 +166,27 @@ pub type HashBytes = [u8; HASH_LENGTH];
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn hashed_storage_create(hashbytes: HashBytes, data: &[u8]) -> String {
|
||||
unsafe { std::mem::transmute::<Vec<u8>, String>([hashbytes.to_vec(), data.to_vec()].concat()) }
|
||||
let combined_data = [hashbytes.to_vec(), data.to_vec()].concat();
|
||||
general_purpose::STANDARD.encode(combined_data)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn hashed_storage_read(data: &str) -> Option<(HashBytes, &[u8])> {
|
||||
pub fn hashed_storage_read(data: &str) -> Option<(HashBytes, Vec<u8>)> {
|
||||
// Decode base64 data
|
||||
let decoded_bytes = general_purpose::STANDARD.decode(data).ok()?;
|
||||
|
||||
// Make sure data is long enough to decode
|
||||
if HASH_LENGTH >= data.len() {
|
||||
if HASH_LENGTH > decoded_bytes.len() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Transmute data into slice
|
||||
let decoded_1: &[u8] = unsafe { std::mem::transmute::<&str, &[u8]>(data) };
|
||||
// Split hash and data
|
||||
let (hash_bytes, data_bytes) = decoded_bytes.split_at(HASH_LENGTH);
|
||||
|
||||
// Return hash and decoded data
|
||||
Some((
|
||||
unsafe { *(decoded_1[..HASH_LENGTH].as_ptr() as *const HashBytes) },
|
||||
&decoded_1[HASH_LENGTH..],
|
||||
))
|
||||
// Convert hash bytes to HashBytes
|
||||
let hash: HashBytes = hash_bytes.try_into().ok()?;
|
||||
|
||||
Some((hash, data_bytes.to_vec()))
|
||||
}
|
||||
|
||||
/// Creates and returns random u64
|
||||
|
||||
Reference in New Issue
Block a user