Skip to content

Commit 8ff8a99

Browse files
committed
feat: Generate default program with first key
The secret keys were randomized in the previous commit. This commit adapts the default program to be P2PK for the first of these keys. The signature data is currently not being generated and it will not match the program. The program will fail to run, unless the user updates the signatures. To ensure that there is a first key, this commit makes the key count NonZeroU32.
1 parent 0a42ab7 commit 8ff8a99

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

src/components/app.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ pub struct ActiveRunTab(pub RwSignal<&'static str>);
1313
pub fn App() -> impl IntoView {
1414
let url_params = use_query_map().get_untracked();
1515

16-
let program = Program::default();
16+
let signing_keys = SigningKeys::from_map(&url_params).unwrap_or_default();
17+
provide_context(signing_keys);
18+
let program = Program::new(signing_keys.first_public_key());
1719
provide_context(program);
1820
let tx_params = TxParams::from_map(&url_params).unwrap_or_default();
1921
let tx_env = TxEnv::new(program, tx_params);
2022
provide_context(tx_env);
21-
provide_context(SigningKeys::from_map(&url_params).unwrap_or_default());
2223
provide_context(SignedData::new(tx_env.lazy_env));
2324
provide_context(HashedData::from_map(&url_params).unwrap_or_default());
2425
provide_context(Runtime::new(program, tx_env.lazy_env));

src/components/program_window/program_tab.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
use crate::components::copy_to_clipboard::CopyToClipboard;
2-
use crate::function::Runner;
1+
use std::sync::Arc;
2+
3+
use hex_conservative::DisplayHex;
34
use itertools::Itertools;
45
use leptos::{
56
component, create_node_ref, create_rw_signal, ev, event_target_value, html, spawn_local,
67
use_context, view, with, IntoView, NodeRef, RwSignal, Signal, SignalGetUntracked, SignalSet,
78
SignalUpdate, SignalWith,
89
};
10+
use simfony::elements::secp256k1_zkp as secp256k1;
911
use simfony::parse::ParseFromStr;
1012
use simfony::simplicity::jet::elements::ElementsEnv;
1113
use simfony::{elements, simplicity};
1214
use simfony::{CompiledProgram, SatisfiedProgram, WitnessValues};
13-
use std::sync::Arc;
15+
16+
use crate::components::copy_to_clipboard::CopyToClipboard;
17+
use crate::function::Runner;
1418

1519
#[derive(Copy, Clone, Debug)]
1620
pub struct Program {
@@ -21,9 +25,22 @@ pub struct Program {
2125
}
2226

2327
impl Program {
24-
pub fn new(program_text: String) -> Self {
28+
pub fn new(key: secp256k1::XOnlyPublicKey) -> Self {
29+
let text = format!(
30+
r#"mod witness {{
31+
const SIG: Signature = 0x1d7d93f350e2db564f90da49fb00ee47294bb6d8f061929818b26065a3e50fdd87e0e8ab45eecd04df0b92b427e6d49a5c96810c23706566e9093c992e075dc5; // TODO: update this
32+
}}
33+
34+
fn main() {{
35+
let pk: Pubkey = 0x{};
36+
let msg: u256 = jet::sig_all_hash();
37+
jet::bip_0340_verify((pk, msg), witness::SIG)
38+
}}"#,
39+
key.serialize().as_hex()
40+
);
41+
2542
Self {
26-
text: create_rw_signal(program_text),
43+
text: create_rw_signal(text),
2744
cached_text: create_rw_signal("".to_string()),
2845
lazy_cmr: create_rw_signal(Err("".to_string())),
2946
lazy_satisfied: create_rw_signal(Err("".to_string())),
@@ -60,15 +77,6 @@ impl Program {
6077
}
6178
}
6279

63-
impl Default for Program {
64-
fn default() -> Self {
65-
let text = crate::examples::get("✍️️ P2PK")
66-
.expect("P2PK example should exist")
67-
.program_text();
68-
Self::new(text.to_string())
69-
}
70-
}
71-
7280
#[derive(Copy, Clone)]
7381
pub struct Runtime {
7482
program: Program,

src/components/run_window/key_store_tab.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::num::NonZeroU32;
12
use std::sync::Arc;
23

34
use elements::hashes::{sha256, Hash};
@@ -16,23 +17,23 @@ use crate::components::copy_to_clipboard::CopyToClipboard;
1617
#[derive(Copy, Clone, Debug)]
1718
pub struct SigningKeys {
1819
pub key_offset: RwSignal<u32>,
19-
pub key_count: RwSignal<u32>,
20-
secret_keys: Memo<Vec<secp256k1_zkp::Keypair>>,
21-
public_keys: Memo<Vec<secp256k1_zkp::XOnlyPublicKey>>,
20+
pub key_count: RwSignal<NonZeroU32>,
21+
pub secret_keys: Memo<Vec<secp256k1_zkp::Keypair>>,
22+
pub public_keys: Memo<Vec<secp256k1_zkp::XOnlyPublicKey>>,
2223
}
2324

2425
impl Default for SigningKeys {
2526
fn default() -> Self {
26-
Self::new(rand::random(), 1)
27+
Self::new(rand::random(), NonZeroU32::MIN)
2728
}
2829
}
2930

3031
impl SigningKeys {
31-
pub fn new(key_offset: u32, key_count: u32) -> Self {
32+
pub fn new(key_offset: u32, key_count: NonZeroU32) -> Self {
3233
let key_offset = create_rw_signal(key_offset);
3334
let key_count = create_rw_signal(key_count);
3435
let secret_keys = create_memo(move |_| {
35-
(0..key_count.get())
36+
(0..key_count.get().get())
3637
.map(|index| new_key(key_offset.get(), index))
3738
.collect::<Vec<secp256k1_zkp::Keypair>>()
3839
});
@@ -52,14 +53,18 @@ impl SigningKeys {
5253
}
5354
}
5455

56+
pub fn first_public_key(&self) -> secp256k1_zkp::XOnlyPublicKey {
57+
self.public_keys.get_untracked()[0]
58+
}
59+
5560
pub fn push_key(&self) {
56-
self.key_count.update(|n| *n += 1);
61+
self.key_count.update(|n| *n = n.saturating_add(1));
5762
}
5863

5964
pub fn pop_key(&self) {
60-
let n = self.key_count.get();
61-
if 1 < n {
62-
self.key_count.set(n - 1);
65+
let n = self.key_count.get().get();
66+
if let Some(n_minus_one) = NonZeroU32::new(n - 1) {
67+
self.key_count.set(n_minus_one);
6368
}
6469
}
6570

src/components/state.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::num::NonZeroU32;
2+
13
use leptos::{use_context, SignalGetUntracked};
24
use leptos_router::ParamsMap;
35

@@ -19,7 +21,7 @@ pub trait ToParams {
1921
impl FromParams for SigningKeys {
2022
fn from_map(map: &ParamsMap) -> Option<Self> {
2123
let key_offset = map.get("seed").and_then(|s| s.parse::<u32>().ok())?;
22-
let key_count = map.get("keys").and_then(|s| s.parse::<u32>().ok())?;
24+
let key_count = map.get("keys").and_then(|s| s.parse::<NonZeroU32>().ok())?;
2325
Some(Self::new(key_offset, key_count))
2426
}
2527
}

0 commit comments

Comments
 (0)