From 27c0f25606db7537106981cbb389693a931562c2 Mon Sep 17 00:00:00 2001 From: Pi Lanningham Date: Fri, 21 Jul 2023 22:49:53 -0400 Subject: [PATCH] Add a blueprint hash command Similar to blueprint address and blueprint policy, this just prints the hash of the validator; useful if you need the hash, and you don't want to pipe the address to a bech32 decoder and juggle the hex. --- crates/aiken/src/cmd/blueprint/hash.rs | 55 ++++++++++++++++++++++++ crates/aiken/src/cmd/blueprint/mod.rs | 3 ++ crates/aiken/src/cmd/blueprint/policy.rs | 2 +- 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 crates/aiken/src/cmd/blueprint/hash.rs diff --git a/crates/aiken/src/cmd/blueprint/hash.rs b/crates/aiken/src/cmd/blueprint/hash.rs new file mode 100644 index 00000000..0e7ca4f9 --- /dev/null +++ b/crates/aiken/src/cmd/blueprint/hash.rs @@ -0,0 +1,55 @@ +use crate::with_project; +use aiken_lang::ast::Tracing; +use std::path::PathBuf; + +/// Compute a validator's hash +#[derive(clap::Args)] +pub struct Args { + /// Path to project + directory: Option, + + /// Name of the validator's module within the project. Optional if there's only one validator + #[clap(short, long)] + module: Option, + + /// Name of the validator within the module. Optional if there's only one validator + #[clap(short, long)] + validator: Option, + + /// Force the project to be rebuilt, otherwise relies on existing artifacts (i.e. plutus.json) + #[clap(long)] + rebuild: bool, +} + +pub fn exec( + Args { + directory, + module, + validator, + rebuild, + }: Args, +) -> miette::Result<()> { + with_project(directory, |p| { + if rebuild { + p.build(false, Tracing::NoTraces)?; + } + + let title = module.as_ref().map(|m| { + format!( + "{m}{}", + validator + .as_ref() + .map(|v| format!(".{v}")) + .unwrap_or_default() + ) + }); + + let title = title.as_ref().or(validator.as_ref()); + + let address = p.address(title, None)?; + + println!("{}", address.payment().to_hex()); + + Ok(()) + }) +} diff --git a/crates/aiken/src/cmd/blueprint/mod.rs b/crates/aiken/src/cmd/blueprint/mod.rs index fe37ebf2..77b4add5 100644 --- a/crates/aiken/src/cmd/blueprint/mod.rs +++ b/crates/aiken/src/cmd/blueprint/mod.rs @@ -1,6 +1,7 @@ pub mod address; pub mod apply; pub mod convert; +pub mod hash; pub mod policy; use clap::Subcommand; @@ -10,6 +11,7 @@ use clap::Subcommand; pub enum Cmd { Address(address::Args), Policy(policy::Args), + Hash(hash::Args), Apply(apply::Args), Convert(convert::Args), } @@ -18,6 +20,7 @@ pub fn exec(cmd: Cmd) -> miette::Result<()> { match cmd { Cmd::Address(args) => address::exec(args), Cmd::Policy(args) => policy::exec(args), + Cmd::Hash(args) => hash::exec(args), Cmd::Apply(args) => apply::exec(args), Cmd::Convert(args) => convert::exec(args), } diff --git a/crates/aiken/src/cmd/blueprint/policy.rs b/crates/aiken/src/cmd/blueprint/policy.rs index 0c32f583..982c17d5 100644 --- a/crates/aiken/src/cmd/blueprint/policy.rs +++ b/crates/aiken/src/cmd/blueprint/policy.rs @@ -2,7 +2,7 @@ use crate::with_project; use aiken_lang::ast::Tracing; use std::path::PathBuf; -/// Compute a validator's address. +/// Compute a minting scripts Policy ID #[derive(clap::Args)] pub struct Args { /// Path to project