added apply_params_to_script function

This commit is contained in:
alessandrokonrad 2022-10-22 23:31:26 +02:00 committed by Lucas
parent e67aa7b943
commit c6de827721
3 changed files with 31 additions and 2 deletions

View File

@ -4,7 +4,8 @@
### Changed
- **aiken**: Fixed overflow issue by changing `i64` to `i128` in `BigInt::Int` instances
- **uplc**: Fixed overflow issue by changing `i64` to `i128` in `BigInt::Int` instances
- **uplc**: Added `apply_params_to_script` function (applies params to script and serializes the new script).
## [v0.0.20] - 2022-10-17

View File

@ -9,7 +9,11 @@ pub use eval::get_script_and_datum_lookup_table;
pub use phase_one::eval_phase_one;
use script_context::{ResolvedInput, SlotConfig};
use crate::machine::cost_model::ExBudget;
use crate::{
ast::{DeBruijn, Program},
machine::cost_model::ExBudget,
PlutusData,
};
pub mod error;
mod eval;
@ -130,3 +134,25 @@ pub fn eval_phase_two_raw(
_ => todo!("Wrong era. Please use babbage"),
}
}
pub fn apply_params_to_script(
params_bytes: &Vec<u8>, // PlutusData array
plutus_script_bytes: &Vec<u8>,
) -> Result<Vec<u8>, Error> {
let params = match PlutusData::decode_fragment(params_bytes).unwrap() {
PlutusData::Array(res) => res,
_ => unreachable!(),
};
let mut buffer = Vec::new();
let mut program = Program::<DeBruijn>::from_cbor(plutus_script_bytes, &mut buffer)?;
for param in params {
program = program.apply_data(param);
}
match program.to_cbor() {
Ok(res) => Ok(res),
Err(_) => Err(Error::ApplyParamsError),
}
}

View File

@ -55,4 +55,6 @@ pub enum Error {
index: u32,
err: Box<Error>,
},
#[error("Failed to apply parameters to Plutus script.")]
ApplyParamsError,
}