Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/actions/simplicity/graph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::str::FromStr;

use simplicity::jet;

use crate::hal_simplicity::Program;

#[derive(Debug, thiserror::Error)]
pub enum SimplicityGraphError {
#[error("invalid program: {0}")]
ProgramParse(simplicity::ParseError),
}

#[derive(Debug, Clone, Copy)]
pub enum SharingLevel {
NoSharing,
MaxSharing,
}

impl FromStr for SharingLevel {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"none" => Ok(SharingLevel::NoSharing),
"max" => Ok(SharingLevel::MaxSharing),
other => Err(format!("unknown sharing level: {}", other)),
}
}
}

#[derive(Debug, Clone, Copy)]
pub enum GraphFormat {
Dot,
Mermaid,
}

impl FromStr for GraphFormat {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"graphviz" | "dot" => Ok(GraphFormat::Dot),
"mermaid" | "mermaidjs" => Ok(GraphFormat::Mermaid),
other => Err(format!("unknown graph format: {}", other)),
}
}
}

pub fn simplicity_graph(
program: &str,
witness: Option<&str>,
sharing_level: SharingLevel,
graph_format: GraphFormat,
) -> Result<String, SimplicityGraphError> {
let program = Program::<jet::Elements>::from_str(program, witness)
.map_err(SimplicityGraphError::ProgramParse)?;

let sharing_level = match sharing_level {
SharingLevel::MaxSharing => simplicity::node::SharingLevel::Max,
SharingLevel::NoSharing => simplicity::node::SharingLevel::None,
};
let graph_format = match graph_format {
GraphFormat::Dot => simplicity::node::GraphFormat::Dot,
GraphFormat::Mermaid => simplicity::node::GraphFormat::Mermaid,
};

let res = if let Some(node) = program.redeem_node() {
let graph = node.display_as_graph(graph_format, sharing_level);
graph.to_string()
} else {
let graph = program.commit_prog().display_as_graph(graph_format, sharing_level);
graph.to_string()
};
Ok(res)
}
2 changes: 2 additions & 0 deletions src/actions/simplicity/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod graph;
pub mod info;
pub mod pset;
pub mod sighash;

pub use graph::*;
pub use info::*;
pub use sighash::*;

Expand Down
50 changes: 50 additions & 0 deletions src/bin/hal-simplicity/cmd/simplicity/graph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use clap::value_t;
use hal_simplicity::actions::simplicity::{GraphFormat, SharingLevel};

use crate::cmd;

use super::Error;

pub fn cmd<'a>() -> clap::App<'a, 'a> {
cmd::subcommand("graph", "Parse a base64-encoded Simplicity program and display a graph").args(
&[
cmd::arg("program", "a Simplicity program in base64").takes_value(true).required(true),
cmd::arg("witness", "a hex encoding of all the witness data for the program")
.takes_value(true)
.required(false),
cmd::arg(
"sharing",
"the level of node sharing to use when displaying. Either none or max",
)
.short("s")
.long("sharing")
.takes_value(true)
.required(false)
.default_value("none")
.possible_values(&["none", "max"]),
cmd::arg("format", "the format for the graph, either graphviz (alias dot) or mermaid")
.long("format")
.takes_value(true)
.required(false)
.default_value("graphviz")
.possible_values(&["graphviz", "dot", "mermaid", "mermaidjs"]),
],
)
}

pub fn exec<'a>(matches: &clap::ArgMatches<'a>) {
let program = matches.value_of("program").expect("program is mandatory");
let witness = matches.value_of("witness");
let sharing = value_t!(matches, "sharing", SharingLevel).unwrap_or(SharingLevel::NoSharing);
let format = value_t!(matches, "format", GraphFormat).unwrap_or(GraphFormat::Dot);

match hal_simplicity::actions::simplicity::simplicity_graph(program, witness, sharing, format) {
Ok(graph) => println!("{}", graph),
Err(e) => cmd::print_output(
matches,
&Error {
error: format!("{}", e),
},
),
}
}
3 changes: 3 additions & 0 deletions src/bin/hal-simplicity/cmd/simplicity/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2025 Andrew Poelstra
// SPDX-License-Identifier: CC0-1.0

mod graph;
mod info;
mod pset;
mod sighash;
Expand All @@ -16,6 +17,7 @@ struct Error {

pub fn subcommand<'a>() -> clap::App<'a, 'a> {
cmd::subcommand_group("simplicity", "manipulate Simplicity programs")
.subcommand(self::graph::cmd())
.subcommand(self::info::cmd())
.subcommand(self::pset::cmd())
.subcommand(self::sighash::cmd())
Expand All @@ -24,6 +26,7 @@ pub fn subcommand<'a>() -> clap::App<'a, 'a> {
pub fn execute<'a>(matches: &clap::ArgMatches<'a>) {
match matches.subcommand() {
("info", Some(m)) => self::info::exec(m),
("graph", Some(m)) => self::graph::exec(m),
("pset", Some(m)) => self::pset::exec(m),
("sighash", Some(m)) => self::sighash::exec(m),
(_, _) => unreachable!("clap prints help"),
Expand Down
Loading