aiken_design_patterns/apply_params
In some cases, validators need to be aware of instances of a parameterized script in order to have a more robust control over the flow of assets.
As a simple example, consider a minting script that needs to ensure the destination of its tokens can only be instances of a specific spending script, e.g. parameterized by users’ wallets.
Since each different wallet leads to a different script address, without verifying instances, instances can only be seen as arbitrary scripts from the minting script’s point of view.
This can be resolved by validating an instance is the result of applying specific parameters to a given parameterized script.
To allow this validation on-chain, some restrictions are needed:
- Parameters of the script must have constant lengths, which can be achieved by having them hashed
- Consequently, for each transaction, the resolved value of those parameters must be provided through the redeemer
- The dependent script must be provided with CBOR bytes of instances before and after the parameter(s)
- Wrapping of instances’ logics in an outer function so that there’ll be single occurances of each parameter
This pattern provides two sets of functions. One for applying parameter(s) in the dependent script (i.e. the minting script in the example above), and one for wrapping your parameterized scripts with.
After defining your parameterized scripts, you’ll need to generate
instances of them with dummy data in order to obtain the required prefix
and postfix
values for your target script to utilize.
Types
Datatype for parameterized scripts that don’t need a redeemer.
Constructors
-
Parameter { param: p }
Constructors
-
Parameter2 { param_0: p, param_1: q }
Constructors
-
Parameter3 { param_0: p, param_1: q, param_2: s }
Datatype for redeemer of your single parameterized scripts.
Constructors
-
ParameterizedRedeemer { param: p, redeemer: r }
Constructors
-
ParameterizedRedeemer2 { param_0: p, param_1: q, redeemer: r }
Constructors
-
ParameterizedRedeemer3 { param_0: p, param_1: q, param_2: s, redeemer: r }
Functions
Use this inside your dependent script for single parameter scripts. The
parameter must be serialised before getting passed here. It’ll be hashed
with sha2_256
before placement between prefix
and postfix
.
apply_param_2(
prefix: ByteArray,
param_0: ByteArray,
param_1: ByteArray,
postfix: ByteArray,
) -> ByteArray
Similar to apply_param
, but for scripts with 2 parameters.
apply_param_3(
prefix: ByteArray,
param_0: ByteArray,
param_1: ByteArray,
param_2: ByteArray,
postfix: ByteArray,
) -> ByteArray
Similar to apply_param
, but for scripts with 3 parameters.
spend_wrapper_validator(
hashed_parameter: Hash<Sha2_256, p>,
parameter_serialiser: fn(p) -> ByteArray,
validator_function: fn(p, Option<d>, redeemer, OutputReference, Transaction) ->
Bool,
datum: Option<d>,
outer_redeemer: ParameterizedRedeemer<p, redeemer>,
output_reference: OutputReference,
tx: Transaction,
) -> Bool
Helper function for parameterized spending scripts, which takes care of validating resolved parameter hashes, provides you with both the parameter, and your custom redeemer.
no_datum_wrapper_validator(
hashed_parameter: Hash<Sha2_256, p>,
parameter_serialiser: fn(p) -> ByteArray,
validator_function: fn(p, redeemer, endpoint_specific, Transaction) -> Bool,
outer_redeemer: ParameterizedRedeemer<p, redeemer>,
variable_arg: endpoint_specific,
tx: Transaction,
) -> Bool
Similar to spend_wrapper_validator
, but for
other endpoints that are not provided with a datum (mint, withdraw, etc.).
spend_wrapper_validator_2(
hashed_parameter_0: Hash<Sha2_256, p>,
hashed_parameter_1: Hash<Sha2_256, q>,
parameter_serialiser_0: fn(p) -> ByteArray,
parameter_serialiser_1: fn(q) -> ByteArray,
validator_function: fn(p, q, Option<d>, r, OutputReference, Transaction) ->
Bool,
datum: Option<d>,
outer_redeemer: ParameterizedRedeemer2<p, q, r>,
output_reference: OutputReference,
tx: Transaction,
) -> Bool
no_datum_wrapper_validator_2(
hashed_parameter_0: Hash<Sha2_256, p>,
hashed_parameter_1: Hash<Sha2_256, q>,
parameter_serialiser_0: fn(p) -> ByteArray,
parameter_serialiser_1: fn(q) -> ByteArray,
validator_function: fn(p, q, redeemer, endpoint_specific, Transaction) -> Bool,
outer_redeemer: ParameterizedRedeemer2<p, q, redeemer>,
variable_arg: endpoint_specific,
tx: Transaction,
) -> Bool
spend_wrapper_validator_3(
hashed_parameter_0: Hash<Sha2_256, p>,
hashed_parameter_1: Hash<Sha2_256, q>,
hashed_parameter_2: Hash<Sha2_256, s>,
parameter_serialiser_0: fn(p) -> ByteArray,
parameter_serialiser_1: fn(q) -> ByteArray,
parameter_serialiser_2: fn(s) -> ByteArray,
validator_function: fn(p, q, s, Option<d>, r, OutputReference, Transaction) ->
Bool,
datum: Option<d>,
outer_redeemer: ParameterizedRedeemer3<p, q, s, r>,
output_reference: OutputReference,
tx: Transaction,
) -> Bool
no_datum_wrapper_validator_3(
hashed_parameter_0: Hash<Sha2_256, p>,
hashed_parameter_1: Hash<Sha2_256, q>,
hashed_parameter_2: Hash<Sha2_256, s>,
parameter_serialiser_0: fn(p) -> ByteArray,
parameter_serialiser_1: fn(q) -> ByteArray,
parameter_serialiser_2: fn(s) -> ByteArray,
validator_function: fn(p, q, s, redeemer, endpoint_specific, Transaction) ->
Bool,
outer_redeemer: ParameterizedRedeemer3<p, q, s, redeemer>,
variable_arg: endpoint_specific,
tx: Transaction,
) -> Bool
spend_wrapper_validator_no_redeemer(
hashed_parameter: Hash<Sha2_256, p>,
parameter_serialiser: fn(p) -> ByteArray,
validator_function: fn(p, Option<d>, OutputReference, Transaction) -> Bool,
datum: Option<d>,
outer_redeemer: Parameter<p>,
output_reference: OutputReference,
tx: Transaction,
) -> Bool
Wrapper function for spending scripts with one parameter that don’t need a redeemer.
no_datum_wrapper_validator_no_redeemer(
hashed_parameter: Hash<Sha2_256, p>,
parameter_serialiser: fn(p) -> ByteArray,
validator_function: fn(p, endpoint_specific, Transaction) -> Bool,
outer_redeemer: Parameter<p>,
variable_arg: endpoint_specific,
tx: Transaction,
) -> Bool
Wrapper function for datumless scripts with one parameters that don’t need a redeemer.