Skip to content

Commit

Permalink
feat: Add RunWindow component
Browse files Browse the repository at this point in the history
  • Loading branch information
uncomputable committed Sep 24, 2024
1 parent f3a67eb commit e56f525
Show file tree
Hide file tree
Showing 6 changed files with 661 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/components/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;

use crate::components::run_window::{HashedData, RunWindow, SignedData, SigningKeys};
use leptos::{component, create_rw_signal, provide_context, view, IntoView, RwSignal};
use simfony::str::WitnessName;

Expand All @@ -19,8 +20,15 @@ pub fn App() -> impl IntoView {
provide_context(WitnessWrapper(witness_values));
let tx_env = TxEnv::new(0, 0);
provide_context(tx_env);
let signing_keys = SigningKeys::new(1);
provide_context(signing_keys);
let signed_data = SignedData::new(tx_env.environment());
provide_context(signed_data);
let hashed_data = HashedData::new(1);
provide_context(hashed_data);

view! {
<ProgramWindow />
<RunWindow />
}
}
1 change: 1 addition & 0 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod apply_changes;
mod copy_to_clipboard;
mod error;
mod program_window;
mod run_window;
mod table_form;
mod tabs;

Expand Down
155 changes: 155 additions & 0 deletions src/components/run_window/hash_store_tab.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
use hashes::{sha256, Hash};
use hex_conservative::DisplayHex;
use leptos::{
component, create_rw_signal, use_context, view, with, For, IntoView, RwSignal, Signal,
SignalGet, SignalSet, SignalUpdate, View,
};
use simfony::elements::hashes;

use crate::components::copy_to_clipboard::CopyToClipboard;

#[derive(Clone, Copy, Debug)]
pub struct HashedData {
hash_count: RwSignal<u32>,
preimages: Signal<Vec<[u8; 32]>>,
}

impl HashedData {
pub fn new(hash_count: u32) -> Self {
let hash_count = create_rw_signal(hash_count);
let preimages = Signal::derive(move || -> Vec<[u8; 32]> {
(0..hash_count.get()).map(new_preimage).collect()
});
Self {
hash_count,
preimages,
}
}

pub fn push_hash(&self) {
self.hash_count.update(|n| *n += 1);
}

pub fn pop_hash(&self) {
let n = self.hash_count.get();
if 1 < n {
self.hash_count.set(n - 1);
}
}

pub fn hashes(self) -> Signal<Vec<sha256::Hash>> {
let preimages = self.preimages;
Signal::derive(move || {
with!(|preimages| {
preimages
.iter()
.map(|preimage| sha256::Hash::hash(preimage))
.collect()
})
})
}
}

fn new_preimage(index: u32) -> [u8; 32] {
let mut preimage = [0; 32];
preimage[28..].copy_from_slice(&index.to_be_bytes());
preimage
}

#[component]
pub fn HashStoreTab() -> impl IntoView {
view! {
<div>
<CopyHashesToClipboard />
<CopyPreimagesToClipboard />
</div>
}
}

#[component]
fn CopyHashesToClipboard() -> impl IntoView {
let hashed_data = use_context::<HashedData>().expect("hashed data should exist in context");
let copy_single_hash = move |(index, hash): (usize, sha256::Hash)| -> View {
let label = format!("Hash {}", index);
let hash_hex = format!("0x{}", hash.to_byte_array().as_hex());

view! {
<CopyToClipboard label=label content=hash_hex />
}
};

view! {
<div>
<h3 class="program-title">
Hashes
</h3>
<div class="button-row">
<For
each=move || hashed_data.hashes().get().into_iter().enumerate()
key=|(_index, hash)| *hash
children=copy_single_hash
/>
<button
class="push-button"
type="button"
on:click=move |_| hashed_data.push_hash()
>
More
<i class="fas fa-plus"></i>
</button>
<button
class="pop-button"
type="button"
on:click=move |_| hashed_data.pop_hash()
>
Less
<i class="fas fa-minus"></i>
</button>
</div>
</div>
}
}

#[component]
fn CopyPreimagesToClipboard() -> impl IntoView {
let hashed_data = use_context::<HashedData>().expect("hashed data should exist in context");
let copy_single_preimage = move |(index, preimage): (usize, [u8; 32])| -> View {
let label = format!("Pre {}", index);
let preimage_hex = format!("0x{}", preimage.as_hex());

view! {
<CopyToClipboard label=label content=preimage_hex />
}
};

view! {
<div>
<h3 class="program-title">
Preimages
</h3>
<div class="button-row">
<For
each=move || hashed_data.preimages.get().into_iter().enumerate()
key=|(_index, preimage)| *preimage
children=copy_single_preimage
/>
<button
class="push-button"
type="button"
on:click=move |_| hashed_data.push_hash()
>
More
<i class="far fa-plus"></i>
</button>
<button
class="pop-button"
type="button"
on:click=move |_| hashed_data.pop_hash()
>
Less
<i class="far fa-minus"></i>
</button>
</div>
</div>
}
}
Loading

0 comments on commit e56f525

Please sign in to comment.