Add a blueprint policy command

Computes the policy ID of a minting policy; added guards for blueprint address to check that it's not a minting policy; Wasn't 100% sure where the errors should live, so I'm happy to move them if there's objections
This commit is contained in:
Pi Lanningham
2023-07-01 14:14:09 -04:00
committed by Lucas
parent 42544af799
commit 4a8cb72708
4 changed files with 117 additions and 2 deletions

View File

@@ -49,6 +49,26 @@ pub enum Error {
))]
ParameterizedValidator { n: usize },
#[error(
"I couldn't compute the address of the given validator because it's actually a minting policy!",
)]
#[diagnostic(code("aiken::blueprint::address::minting_validator"))]
#[diagnostic(help(
"I can only compute addresses for spending validators. Did you mean to call {blueprint_policy_command} instead?",
blueprint_policy_command = "blueprint policy".if_supports_color(Stdout, |s| s.purple()),
))]
UnexpectedMintingValidator,
#[error(
"I couldn't compute the policyId of the given validator because it's actually a spending policy!",
)]
#[diagnostic(code("aiken::blueprint::address::spending_validator"))]
#[diagnostic(help(
"I can only compute policyIds for minting validators. Did you mean to call {blueprint_address_command} instead?",
blueprint_address_command = "blueprint address".if_supports_color(Stdout, |s| s.purple()),
))]
UnexpectedSpendingValidator,
#[error("I stumble upon something else than a constant when I expected one.")]
#[diagnostic(code("aiken:blueprint::apply::malformed::argument"))]
#[diagnostic(help(

View File

@@ -27,9 +27,10 @@ use indexmap::IndexMap;
use miette::NamedSource;
use options::{CodeGenMode, Options};
use package_name::PackageName;
use pallas::ledger::addresses::{
use pallas::ledger::{addresses::{
Address, Network, ShelleyAddress, ShelleyDelegationPart, StakePayload,
};
}, primitives::babbage::{self as cardano, PolicyId}};
use pallas_traverse::ComputeHash;
use script::{EvalHint, EvalInfo, Script};
use std::{
collections::HashMap,
@@ -369,6 +370,11 @@ where
let when_missing = |known_validators| Error::NoValidatorNotFound { known_validators };
blueprint.with_validator(title, when_too_many, when_missing, |validator| {
// Make sure we're not calculating the address for a minting validator
if validator.datum.is_none() {
return Err(blueprint::error::Error::UnexpectedMintingValidator.into());
}
let n = validator.parameters.len();
if n > 0 {
Err(blueprint::error::Error::ParameterizedValidator { n }.into())
@@ -380,6 +386,37 @@ where
})
}
pub fn policy(
&self,
title: Option<&String>
) -> Result<PolicyId, Error> {
// Read blueprint
let blueprint = File::open(self.blueprint_path())
.map_err(|_| blueprint::error::Error::InvalidOrMissingFile)?;
let blueprint: Blueprint = serde_json::from_reader(BufReader::new(blueprint))?;
// Error handlers for ambiguous / missing validators
let when_too_many =
|known_validators| Error::MoreThanOneValidatorFound { known_validators };
let when_missing = |known_validators| Error::NoValidatorNotFound { known_validators };
blueprint.with_validator(title, when_too_many, when_missing, |validator| {
// Make sure we're not calculating the policy for a spending validator
if validator.datum.is_some() {
return Err(blueprint::error::Error::UnexpectedSpendingValidator.into());
}
let n = validator.parameters.len();
if n > 0 {
Err(blueprint::error::Error::ParameterizedValidator { n }.into())
} else {
let cbor = validator.program.to_cbor().unwrap();
let validator_hash = cardano::PlutusV2Script(cbor.into()).compute_hash();
Ok(validator_hash)
}
})
}
pub fn apply_parameter(
&self,
title: Option<&String>,