diff --git a/objects/src/transaction/script.rs b/objects/src/transaction/script.rs index 8a4992105..5fc8465e9 100644 --- a/objects/src/transaction/script.rs +++ b/objects/src/transaction/script.rs @@ -1,13 +1,13 @@ -use super::{Felt, Vec, Word}; +use super::{Digest, Felt, Word}; use crate::{ advice::{AdviceInputsBuilder, ToAdviceInputs}, - assembly::ProgramAst, + assembly::{Assembler, AssemblyContext, CodeBlock, ProgramAst}, errors::TransactionScriptError, - utils::collections::BTreeMap, - Digest, + utils::collections::{BTreeMap, Vec}, }; -use assembly::{Assembler, AssemblyContext}; -use vm_core::code_blocks::CodeBlock; + +// TRANSACTION SCRIPT +// ================================================================================================ /// A struct that represents a transaction script. /// @@ -18,7 +18,7 @@ use vm_core::code_blocks::CodeBlock; /// - [code](TransactionScript::code): the transaction script source code. /// - [hash](TransactionScript::hash): the hash of the compiled transaction script. /// - [inputs](TransactionScript::inputs): a map of key, value inputs that are loaded into the -/// advice map such that the transaction script can access them. +/// advice map such that the transaction script can access them. #[derive(Clone, Debug)] pub struct TransactionScript { code: ProgramAst, @@ -27,19 +27,19 @@ pub struct TransactionScript { } impl TransactionScript { - // CONSTRUCTOR + // CONSTRUCTORS // -------------------------------------------------------------------------------------------- /// Returns a new instance of a [TransactionScript] with the provided script and inputs and the /// compiled script code block. - pub fn new( + /// + /// # Errors + /// Returns an error if script compilation fails. + pub fn new)>>( code: ProgramAst, inputs: T, assembler: &mut Assembler, - ) -> Result<(Self, CodeBlock), TransactionScriptError> - where - T: IntoIterator)>, - { + ) -> Result<(Self, CodeBlock), TransactionScriptError> { let code_block = assembler .compile_in_context(&code, &mut AssemblyContext::for_program(Some(&code))) .map_err(TransactionScriptError::ScriptCompilationError)?; @@ -53,6 +53,22 @@ impl TransactionScript { )) } + /// Returns a new instance of a [TransactionScript] instantiated from the provided components. + /// + /// Note: this constructor does not verify that a compiled code in fact results in the provided + /// hash. + pub fn from_parts)>>( + code: ProgramAst, + hash: Digest, + inputs: T, + ) -> Result { + Ok(Self { + code, + hash, + inputs: inputs.into_iter().map(|(k, v)| (k.into(), v)).collect(), + }) + } + // PUBLIC ACCESSORS // --------------------------------------------------------------------------------------------