Skip to content

Commit

Permalink
feat: Add TransactionButton
Browse files Browse the repository at this point in the history
  • Loading branch information
uncomputable committed Oct 20, 2024
1 parent 4d4cfb9 commit b0c7edb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/components/program_window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod program_tab;
mod run_button;
mod share_button;
mod tools_dropdown;
mod transaction_button;

use leptos::{component, view, IntoView};

Expand All @@ -12,6 +13,7 @@ use self::examples_dropdown::ExamplesDropdown;
use self::program_tab::ProgramTab;
use self::run_button::RunButton;
use self::share_button::ShareButton;
use self::transaction_button::TransactionButton;
use crate::components::toolbar::Toolbar;

pub use self::program_tab::Program;
Expand All @@ -23,6 +25,7 @@ pub fn ProgramWindow() -> impl IntoView {
<RunButton />
<ExamplesDropdown />
<AddressButton />
<TransactionButton />
<ShareButton />
<div class="beta-tag">beta</div>
</Toolbar>
Expand Down
30 changes: 30 additions & 0 deletions src/components/program_window/transaction_button.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use elements::pset::serialize::Serialize;
use hex_conservative::DisplayHex;
use leptos::{component, use_context, view, IntoView, SignalWith};
use simfony::elements;

use crate::components::copy_to_clipboard::CopyToClipboard;
use crate::components::program_window::Program;
use crate::components::run_window::TxEnv;

#[component]
pub fn TransactionButton() -> impl IntoView {
let program = use_context::<Program>().expect("program should exist in context");
let tx_env = use_context::<TxEnv>().expect("transaction environment should exist in context");

let transaction = move || {
tx_env.params.with(|params| match program.satisfied() {
Ok(satisfied) => params
.transaction(&satisfied)
.serialize()
.to_lower_hex_string(),
Err(..) => "Invalid program".to_string(),
})
};
view! {
<CopyToClipboard content=transaction class="button" tooltip_below=true>
<i class="fa-solid fa-right-left"></i>
" Transaction"
</CopyToClipboard>
}
}
21 changes: 20 additions & 1 deletion src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use elements::confidential;
use elements::hashes::Hash;
use simfony::{elements, simplicity};
use simfony::{elements, simplicity, SatisfiedProgram};
use simplicity::jet::elements::{ElementsEnv, ElementsUtxo};

use crate::util;
Expand Down Expand Up @@ -85,4 +85,23 @@ impl TxParams {
util::liquid_testnet_genesis(),
)
}

pub fn transaction(&self, satisfied: &SatisfiedProgram) -> elements::Transaction {
let mut tx = self.unsatisfied_transaction();
let (simplicity_program_bytes, simplicity_witness_bytes) =
satisfied.redeem().encode_to_vec();
let (script_pubkey, control_block) = util::script_control_block(satisfied.redeem().cmr());
tx.input[0].witness = elements::TxInWitness {
amount_rangeproof: None,
inflation_keys_rangeproof: None,
script_witness: vec![
simplicity_witness_bytes,
simplicity_program_bytes,
script_pubkey.into_bytes(),
control_block.serialize(),
],
pegin_witness: vec![],
};
tx
}
}

0 comments on commit b0c7edb

Please sign in to comment.