Advertisement
theultraman20

main.rs

Jan 3rd, 2025
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.55 KB | None | 0 0
  1. use coin::block::*;
  2. use coin::user::*;
  3. use hex;
  4. use std::{iter::Once, sync::Mutex};
  5. use wasm_bindgen::prelude::*;
  6. use web_sys::window;
  7.  
  8. #[wasm_bindgen]
  9. extern "C" {
  10.     pub fn alert(s: &str);
  11. }
  12.  
  13. #[wasm_bindgen]
  14. pub fn greet(name: &str) {
  15.     alert(&format!("Hello, {}!", name));
  16. }
  17.  
  18. static STATE: Mutex<Option<State>> = Mutex::new(None);
  19. static USER: Mutex<Option<State>> = Mutex::new(None);
  20.  
  21. fn main() {
  22.     console_error_panic_hook::set_once();
  23.  
  24.     let mut state = State::with_genesis_block();
  25.     let mut first = &mut state.blocks[0];
  26.  
  27.     //alert("Hello world!");
  28.     let document = window()
  29.         .and_then(|win| win.document())
  30.         .expect("Could not access the document");
  31.     let body = document.body().expect("Could not access document.body");
  32.  
  33.     //for _ in 0..10 { };
  34.     //text_node.set_data(&format!("First block nonce: {}", hex::encode(first.nonce.to_be_bytes())));
  35.  
  36.     *STATE.lock().unwrap() = Some(state);
  37. }
  38.  
  39. #[wasm_bindgen]
  40. pub fn get_nonce() -> u64 {
  41.     let mut state = STATE.lock().unwrap();
  42.     let state = state.as_mut().unwrap();
  43.  
  44.     let nonce = state.blocks[0].mine(); // Mine the nonce
  45.     state.blocks[0].nonce = nonce; // Update the nonce in the state
  46.  
  47.     nonce // Return the mined nonce
  48. }
  49.  
  50. #[wasm_bindgen]
  51. pub fn get_balance(test: &str) -> u64 {
  52.     alert(test);
  53.     let window = window().unwrap();
  54.     let document = window.document().unwrap();
  55.  
  56.     let input = document
  57.         .get_element_by_id("privkey")
  58.         .expect("should find input element");
  59.  
  60.     let input_element = input.dyn_into::<web_sys::HtmlInputElement>().unwrap();
  61.  
  62.     let mut user = USER.lock().unwrap();
  63.     let user = user.as_mut().unwrap();
  64.  
  65.     let priv_key = input_element.value();
  66.     let pub_key = User::from_priv(priv_key.as_str()).verifying.into();
  67.     let mut state = STATE.lock().unwrap();
  68.     let state = state.as_mut().unwrap();
  69.  
  70.     state.get_balance(pub_key)
  71. }
  72.  
  73. #[wasm_bindgen]
  74. pub fn spend(amt: u64) {
  75.     let mut state = STATE.lock().unwrap();
  76.     let state = state.as_mut().unwrap();
  77.  
  78.     let window = window().unwrap();
  79.     let document = window.document().unwrap();
  80.     let input = document
  81.         .get_element_by_id("privkey")
  82.         .expect("should find input element");
  83.  
  84.     let input_element = input.dyn_into::<web_sys::HtmlInputElement>().unwrap();
  85.  
  86.     let priv_key = input_element.value();
  87.     let signing = User::from_priv(priv_key.as_str()).signing;
  88.  
  89.     let rand_user = User::random();
  90.     state.blocks[0].transact(&mut state.utxo_set, &signing, &rand_user.verifying, amt);
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement