-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
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,32 @@ | ||
// wasm.rs | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
pub struct SmartContract { | ||
pub storage: Vec<u8>, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl SmartContract { | ||
#[wasm_bindgen(constructor)] | ||
pub fn new() -> Self { | ||
Self { storage: Vec::new() } | ||
} | ||
|
||
#[wasm_bindgen(js_name = "store")] | ||
pub fn store(&mut self, key: &str, value: &str) { | ||
self.storage.push(format!("{}={}", key, value).as_bytes().to_vec()); | ||
} | ||
|
||
#[wasm_bindgen(js_name = "retrieve")] | ||
pub fn retrieve(&self, key: &str) -> Option<String> { | ||
for entry in &self.storage { | ||
let entry_str = std::str::from_utf8(entry).unwrap(); | ||
let parts: Vec<&str> = entry_str.split("=").collect(); | ||
if parts[0] == key { | ||
return Some(parts[1].to_string()); | ||
} | ||
} | ||
None | ||
} | ||
} |