user4422: I would like to write a factory script that can initialize another parameterized script address by sending it an NFT. The factory validator gets the parameters of the target script as redeemer and has to check if the NFT is actually sent to correct script address.
Testing this with EmulatorTrace gives me an error that lets me doubt if calculating/validating a parameterized address is possible on-chain.
Writing it in Plutus looks like this:
{-# INLINABLE validateCreate #-}
validateCreate :: Factory
               -> [TargetParams] -- Datum: List of already created scripts
               -> TargetParams   -- Redeemer: parameters for new script to be created
               -> ScriptContext
               -> Bool
validateCreate factory existingScriptParams newScritpParam ctx =
    someOtherValidationLogic &&
    Constraints.checkTxConstraint ctx
      (MustPayToOtherScript (Scripts.validatorHash $ targetInst newScritpParam)
        calculateDatumLogic nftValue)
  where
    someOtherValidationLogic :: Bool
    calculateDatumLogic :: TargetDatum
    nftValue :: Value

With the targetInst function looking pretty standard:
{-# INLINABLE targetInst #-}
targetInst :: TargetParams -> Scripts.TypedValidator Target
targetInst p = Scripts.mkTypedValidator @Target
    ($$(PlutusTx.compile [|| mkTargetValidator ||])
    `PlutusTx.applyCode` PlutusTx.liftCode p)
    $$(PlutusTx.compile [|| wrap ||])
  where
    wrap = Scripts.wrapValidator @Integer @TargetAction

This loads in cabal repl without problems. But when executing the EmulatorTrace, it yields the following error:
*** Exception: Error: Unsupported feature: Type constructor: GHC.Prim.ByteArray#
Context: Compiling definition of: Target.targetInst
Context: Compiling definition of: Factory.validateCreate
Context: Compiling definition of: Factory.mkFactoryValidator
Context: Compiling expr at "ProjectName-0.1.0.0-inplace:Offchain:(84,9)-(84,52)"

I do not know what is going on in the template Haskell of targetInst. It looks to me like the calculation of a script address with the parameters given in the redeemer is not possible in the on-chain validator.
This raises a couple of questions for me:

Is it just a stupid mistake on my side?
Is there another way to calculate or validate script addresses with parameters given at runtime?
Would another architecture be better for what I am trying to accomplish? My goal is to separate different instances of my target script while ensuring that there is only one for each set of parameters.

I think this question is related to this one. But there were no helpful answers and I would like to give a more detailed description.
Thanks for your help.

user1130: You are asking a couple different questions here.

This loads in cabal repl without problems. But when executing the EmulatorTrace, it yields the following error:

*** Exception: Error: Unsupported feature: Type constructor: GHC.Prim.ByteArray#

Does your Target script include ByteArray? I believe this is saying it's not INLINABLE, and thus can't be compiled into Plutus. GHC wouldn't see any issues with it, that's why cabal repl doesn't complain.


Would another architecture be better for what I am trying to accomplish? My goal is to separate different instances of my target script while ensuring that there is only one for each set of parameters.

Generally the way these sort of access NFT validators would be written is the NFT isn't sent to some target address, but rather must be included in the txn (and returned to the validator in the same txn with a new datum). The target script can then just check that it's included in the txn.
You say you want "a factory script that can initialize another parameterized script". What is to stop you from sending any arbitrary token to "initialize" instead of your desired NFT. There are no constraints on Datum creation for a script (anyone can initialize a script) there are just constraints on who can spend the Datum UTxO with a Redeemer.
I'm not sure what behavior you want from your Target scripts, but as a rule of thumb I'd recommend:

Add constraints to the Factory script to ensure the NFT is returned to itself with a new Datum rather than sent to some param address
Use the params to ensure the rest of the txn is valid to spend some Datum at the Target address
Then have Target just check that the NFT is included in the txn--if the NFT is spent, then that means Targets requirements are met


4422: <p>I would like to write a factory script that can initialize another parameterized script address by sending it an NFT. The factory validator gets the parameters of the target script as redeemer and has to check if the NFT is actually sent to correct script address.
Testing this with EmulatorTrace gives me an error that lets me doubt if calculating/validating a parameterized address is possible on-chain.</p>
<p>Writing it in Plutus looks like this:</p>
<pre><code>{-# INLINABLE validateCreate #-}
validateCreate :: Factory
               -&gt; [TargetParams] -- Datum: List of already created scripts
               -&gt; TargetParams   -- Redeemer: parameters for new script to be created
               -&gt; ScriptContext
               -&gt; Bool
validateCreate factory existingScriptParams newScritpParam ctx =
    someOtherValidationLogic &amp;&amp;
    Constraints.checkTxConstraint ctx
      (MustPayToOtherScript (Scripts.validatorHash $ targetInst newScritpParam)
        calculateDatumLogic nftValue)
  where
    someOtherValidationLogic :: Bool
    calculateDatumLogic :: TargetDatum
    nftValue :: Value
</code></pre>
<p>With the targetInst function looking pretty standard:</p>
<pre><code>{-# INLINABLE targetInst #-}
targetInst :: TargetParams -&gt; Scripts.TypedValidator Target
targetInst p = Scripts.mkTypedValidator @Target
    ($$(PlutusTx.compile [|| mkTargetValidator ||])
    `PlutusTx.applyCode` PlutusTx.liftCode p)
    $$(PlutusTx.compile [|| wrap ||])
  where
    wrap = Scripts.wrapValidator @Integer @TargetAction
</code></pre>
<p>This loads in <code>cabal repl</code> without problems. But when executing the EmulatorTrace, it yields the following error:</p>
<pre><code>*** Exception: Error: Unsupported feature: Type constructor: GHC.Prim.ByteArray#
Context: Compiling definition of: Target.targetInst
Context: Compiling definition of: Factory.validateCreate
Context: Compiling definition of: Factory.mkFactoryValidator
Context: Compiling expr at &quot;ProjectName-0.1.0.0-inplace:Offchain:(84,9)-(84,52)&quot;
</code></pre>
<p>I do not know what is going on in the template Haskell of targetInst. It looks to me like the calculation of a script address with the parameters given in the redeemer is not possible in the on-chain validator.</p>
<p>This raises a couple of questions for me:</p>
<ul>
<li>Is it just a stupid mistake on my side?</li>
<li>Is there another way to calculate or validate script addresses with parameters given at runtime?</li>
<li>Would another architecture be better for what I am trying to accomplish? My goal is to separate different instances of my target script while ensuring that there is only one for each set of parameters.</li>
</ul>
<p>I think this question is related to <a href="https://cardano.stackexchange.com/questions/3825/getting-the-address-of-a-parameterized-contract-from-within-the-validator-of-ano">this one</a>. But there were no helpful answers and I would like to give a more detailed description.</p>
<p>Thanks for your help.</p>
 1130: <p>You are asking a couple different questions here.</p>
<blockquote>
<p>This loads in cabal repl without problems. But when executing the EmulatorTrace, it yields the following error:</p>
</blockquote>
<pre><code>*** Exception: Error: Unsupported feature: Type constructor: GHC.Prim.ByteArray#
</code></pre>
<p>Does your <code>Target</code> script include <code>ByteArray</code>? I believe this is saying it's not <code>INLINABLE</code>, and thus can't be compiled into Plutus. GHC wouldn't see any issues with it, that's why <code>cabal repl</code> doesn't complain.</p>
<hr />
<blockquote>
<p>Would another architecture be better for what I am trying to accomplish? My goal is to separate different instances of my target script while ensuring that there is only one for each set of parameters.</p>
</blockquote>
<p>Generally the way these sort of access NFT validators would be written is the NFT isn't sent to some target address, but rather must be included in the txn (and returned to the validator in the same txn with a new datum). The target script can then just check that it's included in the txn.</p>
<p>You say you want &quot;a factory script that can initialize another parameterized script&quot;. What is to stop you from sending any arbitrary token to &quot;initialize&quot; instead of your desired NFT. There are no constraints on Datum creation for a script (anyone can initialize a script) there are just constraints on who can spend the Datum UTxO with a Redeemer.</p>
<p>I'm not sure what behavior you want from your <code>Target</code> scripts, but as a rule of thumb I'd recommend:</p>
<ul>
<li>Add constraints to the Factory script to ensure the NFT is returned to itself with a new Datum rather than sent to some param address</li>
<li>Use the params to ensure the rest of the txn is valid to spend some Datum at the <code>Target</code> address</li>
<li>Then have <code>Target</code> just check that the NFT is included in the txn--if the NFT is spent, then that means <code>Target</code>s requirements are met</li>
</ul>
