-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 2024-10-25-adding-ethereum-to-subgraph-networks
- Loading branch information
Showing
5 changed files
with
208 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use super::*; | ||
use sha2::{Digest, Sha256}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] | ||
struct SerializedGuiState { | ||
config_hash: String, | ||
field_values: BTreeMap<String, String>, | ||
deposits: Vec<TokenDeposit>, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl DotrainOrderGui { | ||
fn compute_config_hash(&self) -> String { | ||
let config = self.get_gui_config(); | ||
let bytes = bincode::serialize(&config).expect("Failed to serialize config"); | ||
let hash = Sha256::digest(&bytes); | ||
format!("{:x}", hash) | ||
} | ||
|
||
#[wasm_bindgen(js_name = "serializeState")] | ||
pub fn serialize(&self) -> Result<String, GuiError> { | ||
let config_hash = self.compute_config_hash(); | ||
|
||
let state = SerializedGuiState { | ||
config_hash, | ||
field_values: self.field_values.clone(), | ||
deposits: self.deposits.clone(), | ||
}; | ||
let bytes = bincode::serialize(&state)?; | ||
|
||
let mut encoder = GzEncoder::new(Vec::new(), Compression::default()); | ||
encoder.write_all(&bytes)?; | ||
let compressed = encoder.finish()?; | ||
|
||
Ok(URL_SAFE.encode(compressed)) | ||
} | ||
|
||
#[wasm_bindgen(js_name = "deserializeState")] | ||
pub fn deserialize_state(&mut self, serialized: String) -> Result<(), GuiError> { | ||
let compressed = URL_SAFE.decode(serialized)?; | ||
|
||
let mut decoder = GzDecoder::new(&compressed[..]); | ||
let mut bytes = Vec::new(); | ||
decoder.read_to_end(&mut bytes)?; | ||
|
||
let state: SerializedGuiState = bincode::deserialize(&bytes)?; | ||
self.field_values = state.field_values; | ||
self.deposits = state.deposits; | ||
|
||
if state.config_hash != self.compute_config_hash() { | ||
return Err(GuiError::DeserializedConfigMismatch); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[wasm_bindgen(js_name = "clearState")] | ||
pub fn clear_state(&mut self) { | ||
self.field_values.clear(); | ||
self.deposits.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters