forked from BlockstreamResearch/SimplicityHL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
246 lines (222 loc) · 8.48 KB
/
Copy pathmain.rs
File metadata and controls
246 lines (222 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use base64::display::Base64Display;
use base64::engine::general_purpose::STANDARD;
use clap::{Arg, ArgAction, Command};
use simplicityhl::{
resolution::DependencyMapBuilder, source::CanonPath, source::CanonSourceFile, AbiMeta,
CompiledProgram,
};
use std::path::Path;
use std::{env, fmt};
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
/// The compilation output.
struct Output {
/// Simplicity program result, base64 encoded.
program: String,
/// Simplicity witness result, base64 encoded, if the .wit file was provided.
witness: Option<String>,
/// Simplicity program ABI metadata to the program which the user provides.
abi_meta: Option<AbiMeta>,
/// Commitment Merkle Root (CMR) of the program, hex encoded.
cmr: String,
}
impl fmt::Display for Output {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "Program:\n{}", self.program)?;
writeln!(f, "CMR:\n{}", self.cmr)?;
if let Some(witness) = &self.witness {
writeln!(f, "Witness:\n{}", witness)?;
}
if let Some(witness) = &self.abi_meta {
writeln!(f, "ABI meta:\n{:?}", witness)?;
}
Ok(())
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let command = {
Command::new(env!("CARGO_BIN_NAME"))
.about(
"\
Compile the given SimplicityHL program and print the resulting Simplicity base64 string.\n\
If a SimplicityHL witness is provided, then use it to satisfy the program (requires \
feature 'serde' to be enabled).\
",
)
.arg(
Arg::new("prog_file")
.required(true)
.value_name("PROGRAM_FILE")
.action(ArgAction::Set)
.help("SimplicityHL program file to build"),
)
.arg(
Arg::new("dependencies")
.long("dep")
.value_name("[CONTEXT:]ALIAS=PATH")
.action(ArgAction::Append)
.help("Link a dependency, optionally scoped to a specific module (e.g., --dep ./libs/merkle:math=./libs/math)"),
)
.arg(
Arg::new("wit_file")
.long("wit")
.short('w')
.value_name("WITNESS_FILE")
.action(ArgAction::Set)
.help("File containing the witness data"),
)
.arg(
Arg::new("args_file")
.long("args")
.short('a')
.value_name("ARGUMENTS_FILE")
.action(ArgAction::Set)
.help("File containing the arguments data"),
)
.arg(
Arg::new("debug")
.long("debug")
.action(ArgAction::SetTrue)
.help("Include debug symbols in the output"),
)
.arg(
Arg::new("json")
.long("json")
.action(ArgAction::SetTrue)
.help("Output in JSON"),
)
.arg(
Arg::new("abi")
.long("abi")
.action(ArgAction::SetTrue)
.help("Additional ABI .simf contract types"),
)
};
let matches = command.get_matches();
let prog_file = matches.get_one::<String>("prog_file").unwrap();
let main_path = CanonPath::canonicalize(Path::new(prog_file))?;
let main_text = std::fs::read_to_string(main_path.as_path()).map_err(|e| e.to_string())?;
let include_debug_symbols = matches.get_flag("debug");
let output_json = matches.get_flag("json");
let abi_param = matches.get_flag("abi");
#[cfg(feature = "serde")]
let args_opt: simplicityhl::Arguments = match matches.get_one::<String>("args_file") {
None => simplicityhl::Arguments::default(),
Some(args_file) => {
let args_path = Path::new(&args_file);
let args_text = std::fs::read_to_string(args_path).map_err(|e| e.to_string())?;
serde_json::from_str::<simplicityhl::Arguments>(&args_text)?
}
};
#[cfg(not(feature = "serde"))]
let args_opt: simplicityhl::Arguments = if matches.contains_id("args_file") {
return Err(
"Program was compiled without the 'serde' feature and cannot process .args files."
.into(),
);
} else {
simplicityhl::Arguments::default()
};
let dep_args = matches
.get_many::<String>("dependencies")
.unwrap_or_default();
let canon_root = main_path
.as_path()
.parent()
.and_then(|p| CanonPath::canonicalize(p).ok())
.ok_or("Failed to determine project root directory from entry file")?;
let mut builder = DependencyMapBuilder::new(canon_root.clone());
for arg in dep_args {
let (left_side, path_str) = arg.split_once('=').unwrap_or_else(|| {
eprintln!(
"Error: Library argument must be in format [CONTEXT:]ALIAS=PATH, got '{arg}'"
);
std::process::exit(1);
});
// Use the right most `:` because on Windows, there might be a drive letter, e.g. C:\
let (context_path, alias) = if let Some((ctx_str, alias_str)) = left_side.rsplit_once(':') {
// Specific context provided (e.g., merkle:base_math=...)
// We convert it to PathBuf so it shares the same type as main_path
let canon_path = CanonPath::canonicalize(Path::new(ctx_str))?;
(canon_path, alias_str)
} else {
// No context provided (e.g., math=...). Bind it to the workspace root!
(canon_root.clone(), left_side)
};
let target_path = CanonPath::canonicalize(Path::new(path_str))?;
builder = builder.add_dependency(context_path, alias.to_string(), target_path);
}
let dependencies = match builder.build() {
Ok(map) => map,
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
};
let source = CanonSourceFile::new(main_path.clone(), std::sync::Arc::from(main_text));
let compiled =
match CompiledProgram::new_with_dep(source, &dependencies, args_opt, include_debug_symbols)
{
Ok(program) => program,
Err(e) => {
eprintln!("{}", e);
std::process::exit(1);
}
};
#[cfg(feature = "serde")]
let witness_opt = matches
.get_one::<String>("wit_file")
.map(|wit_file| -> Result<simplicityhl::WitnessValues, String> {
let wit_path = Path::new(wit_file);
let wit_text = std::fs::read_to_string(wit_path).map_err(|e| e.to_string())?;
let witness = serde_json::from_str::<simplicityhl::WitnessValues>(&wit_text).unwrap();
Ok(witness)
})
.transpose()?;
#[cfg(not(feature = "serde"))]
let witness_opt = if matches.contains_id("wit_file") {
return Err(
"Program was compiled without the 'serde' feature and cannot process .wit files."
.into(),
);
} else {
None
};
let (program_bytes, witness_bytes) = match witness_opt {
Some(witness) => {
let satisfied = compiled.satisfy(witness)?;
let (program_bytes, witness_bytes) = satisfied.redeem().to_vec_with_witness();
(program_bytes, Some(witness_bytes))
}
None => {
let program_bytes = compiled.commit().to_vec_without_witness();
(program_bytes, None)
}
};
let abi_opt = if abi_param {
Some(compiled.generate_abi_meta()?)
} else {
None
};
let cmr_hex = compiled.commit().cmr().to_string();
let output = Output {
program: Base64Display::new(&program_bytes, &STANDARD).to_string(),
witness: witness_bytes.map(|bytes| Base64Display::new(&bytes, &STANDARD).to_string()),
abi_meta: abi_opt,
cmr: cmr_hex,
};
if output_json {
#[cfg(not(feature = "serde"))]
{
return Err(
"Program was compiled without the 'serde' feature and cannot output JSON.".into(),
);
}
#[cfg(feature = "serde")]
{
println!("{}", serde_json::to_string(&output)?);
return Ok(());
}
}
println!("{}", output);
Ok(())
}