hal_simplicity/actions/simplicity/
info.rs

1use crate::hal_simplicity::{elements_address, Program};
2use crate::simplicity::hex::parse::FromHex as _;
3use crate::simplicity::{jet, Amr, Cmr, Ihr};
4use serde::Serialize;
5
6#[derive(Debug, thiserror::Error)]
7pub enum SimplicityInfoError {
8	#[error("invalid program: {0}")]
9	ProgramParse(simplicity::ParseError),
10
11	#[error("invalid state: {0}")]
12	StateParse(elements::hashes::hex::HexToArrayError),
13}
14
15#[derive(Serialize)]
16pub struct RedeemInfo {
17	pub redeem_base64: String,
18	pub witness_hex: String,
19	pub amr: Amr,
20	pub ihr: Ihr,
21}
22
23#[derive(Serialize)]
24pub struct ProgramInfo {
25	pub jets: &'static str,
26	pub commit_base64: String,
27	pub commit_decode: String,
28	pub type_arrow: String,
29	pub cmr: Cmr,
30	pub liquid_address_unconf: String,
31	pub liquid_testnet_address_unconf: String,
32	pub is_redeem: bool,
33	#[serde(flatten)]
34	#[serde(skip_serializing_if = "Option::is_none")]
35	pub redeem_info: Option<RedeemInfo>,
36}
37
38/// Parse and analyze a Simplicity program.
39pub fn simplicity_info(
40	program: &str,
41	witness: Option<&str>,
42	state: Option<&str>,
43) -> Result<ProgramInfo, SimplicityInfoError> {
44	// In the future we should attempt to parse as a Bitcoin program if parsing as
45	// Elements fails. May be tricky/annoying in Rust since Program<Elements> is a
46	// different type from Program<Bitcoin>.
47	let program = Program::<jet::Elements>::from_str(program, witness)
48		.map_err(SimplicityInfoError::ProgramParse)?;
49
50	let redeem_info = program.redeem_node().map(|node| {
51		let disp = node.display();
52		let redeem_base64 = disp.program().to_string();
53		let witness_hex = disp.witness().to_string();
54		RedeemInfo {
55			redeem_base64,
56			witness_hex,
57			amr: node.amr(),
58			ihr: node.ihr(),
59		}
60	});
61
62	let state =
63		state.map(<[u8; 32]>::from_hex).transpose().map_err(SimplicityInfoError::StateParse)?;
64
65	Ok(ProgramInfo {
66		jets: "core",
67		commit_base64: program.commit_prog().to_string(),
68		// FIXME this is, in general, exponential in size. Need to limit it somehow; probably need upstream support
69		commit_decode: program.commit_prog().display_expr().to_string(),
70		type_arrow: program.commit_prog().arrow().to_string(),
71		cmr: program.cmr(),
72		liquid_address_unconf: elements_address(
73			program.cmr(),
74			state,
75			&elements::AddressParams::LIQUID,
76		)
77		.to_string(),
78		liquid_testnet_address_unconf: elements_address(
79			program.cmr(),
80			state,
81			&elements::AddressParams::LIQUID_TESTNET,
82		)
83		.to_string(),
84		is_redeem: redeem_info.is_some(),
85		redeem_info,
86	})
87}