Skip to content

Commit 004260f

Browse files
committed
feat(cli): resolve witness and argument files against the program
1 parent 1c0b0a6 commit 004260f

4 files changed

Lines changed: 48 additions & 28 deletions

File tree

examples/p2pk.args

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
{
2-
"ALICE_PUBLIC_KEY": {
3-
"value": "0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
4-
"type": "Pubkey"
5-
}
2+
"ALICE_PUBLIC_KEY": "0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
63
}

examples/p2pk.wit

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
{
2-
"ALICE_SIGNATURE": {
3-
"value": "0xf74b3ca574647f8595624b129324afa2f38b598a9c1c7cfc5f08a9c036ec5acd3c0fbb9ed3dae5ca23a0a65a34b5d6cccdd6ba248985d6041f7b21262b17af6f",
4-
"type": "Signature"
5-
}
2+
"ALICE_SIGNATURE": "0xf74b3ca574647f8595624b129324afa2f38b598a9c1c7cfc5f08a9c036ec5acd3c0fbb9ed3dae5ca23a0a65a34b5d6cccdd6ba248985d6041f7b21262b17af6f"
63
}

src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,11 @@ pub(crate) mod tests {
679679
arguments_file_path: P,
680680
) -> TestCase<CompiledProgram> {
681681
let arguments_text = std::fs::read_to_string(arguments_file_path).unwrap();
682-
let arguments = match serde_json::from_str::<Arguments>(&arguments_text) {
682+
let unresolved = match serde_json::from_str::<UnresolvedValues>(&arguments_text) {
683+
Ok(x) => x,
684+
Err(error) => panic!("{error}"),
685+
};
686+
let arguments = match unresolved.resolve(self.program.parameters()) {
683687
Ok(x) => x,
684688
Err(error) => panic!("{error}"),
685689
};
@@ -741,7 +745,11 @@ pub(crate) mod tests {
741745
witness_file_path: P,
742746
) -> TestCase<SatisfiedProgram> {
743747
let witness_text = std::fs::read_to_string(witness_file_path).unwrap();
744-
let witness_values = match serde_json::from_str::<WitnessValues>(&witness_text) {
748+
let unresolved = match serde_json::from_str::<UnresolvedValues>(&witness_text) {
749+
Ok(x) => x,
750+
Err(error) => panic!("{error}"),
751+
};
752+
let witness_values = match unresolved.resolve(self.program.witness_types()) {
745753
Ok(x) => x,
746754
Err(error) => panic!("{error}"),
747755
};

src/main.rs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use simplicityhl::ast::ElementsJetHinter;
66
use simplicityhl::version::SimcDirective;
77
use simplicityhl::{
88
resolution::DependencyMapBuilder, source::CanonPath, source::CanonSourceFile, AbiMeta,
9-
CompiledProgram,
9+
TemplateProgram,
1010
};
1111
use simplicityhl::{UnstableFeature, UnstableFeatures};
1212
use std::path::Path;
@@ -132,24 +132,25 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
132132
UnstableFeatures::new(features.copied())
133133
});
134134

135+
// Argument values are resolved against the program's parameter types,
136+
// so the file is parsed here and resolved once the template is built.
135137
#[cfg(feature = "serde")]
136-
let args_opt: simplicityhl::Arguments = match matches.get_one::<String>("args_file") {
137-
None => simplicityhl::Arguments::default(),
138-
Some(args_file) => {
139-
let args_path = Path::new(&args_file);
140-
let args_text = std::fs::read_to_string(args_path).map_err(|e| e.to_string())?;
141-
serde_json::from_str::<simplicityhl::Arguments>(&args_text)?
142-
}
143-
};
138+
let unresolved_args: Option<simplicityhl::UnresolvedValues> =
139+
match matches.get_one::<String>("args_file") {
140+
None => None,
141+
Some(args_file) => {
142+
let args_path = Path::new(&args_file);
143+
let args_text = std::fs::read_to_string(args_path).map_err(|e| e.to_string())?;
144+
Some(serde_json::from_str(&args_text)?)
145+
}
146+
};
144147
#[cfg(not(feature = "serde"))]
145-
let args_opt: simplicityhl::Arguments = if matches.contains_id("args_file") {
148+
if matches.contains_id("args_file") {
146149
return Err(
147150
"Program was compiled without the 'serde' feature and cannot process .args files."
148151
.into(),
149152
);
150-
} else {
151-
simplicityhl::Arguments::default()
152-
};
153+
}
153154

154155
let dep_args = matches
155156
.get_many::<String>("dependencies")
@@ -195,14 +196,30 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
195196
};
196197

197198
let source = CanonSourceFile::new(main_path.clone(), std::sync::Arc::from(main_text));
198-
let compiled = match CompiledProgram::new_with_dep(
199+
let template = match TemplateProgram::new_with_dep(
199200
source,
200201
&dependencies,
201202
&unstable_features,
202-
args_opt,
203-
include_debug_symbols,
204203
Box::new(ElementsJetHinter::new()),
205204
) {
205+
Ok(program) => program,
206+
Err(e) => {
207+
// `Display` is message-only; render for source snippets with
208+
// file and line pointers, as the single-file path does.
209+
eprintln!("{}", e.render_to_string());
210+
std::process::exit(1);
211+
}
212+
};
213+
214+
#[cfg(feature = "serde")]
215+
let args_opt: simplicityhl::Arguments = match unresolved_args {
216+
None => simplicityhl::Arguments::default(),
217+
Some(unresolved) => unresolved.resolve(template.parameters())?,
218+
};
219+
#[cfg(not(feature = "serde"))]
220+
let args_opt = simplicityhl::Arguments::default();
221+
222+
let compiled = match template.instantiate(args_opt, include_debug_symbols) {
206223
Ok(program) => program,
207224
Err(e) => {
208225
eprintln!("{}", e);
@@ -216,8 +233,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
216233
.map(|wit_file| -> Result<simplicityhl::WitnessValues, String> {
217234
let wit_path = Path::new(wit_file);
218235
let wit_text = std::fs::read_to_string(wit_path).map_err(|e| e.to_string())?;
219-
let witness = serde_json::from_str::<simplicityhl::WitnessValues>(&wit_text).unwrap();
220-
Ok(witness)
236+
let unresolved = serde_json::from_str::<simplicityhl::UnresolvedValues>(&wit_text)
237+
.map_err(|e| e.to_string())?;
238+
unresolved.resolve(compiled.witness_types())
221239
})
222240
.transpose()?;
223241
#[cfg(not(feature = "serde"))]

0 commit comments

Comments
 (0)