Direct access to Script Context fields
When working with Cardano validators, you often need to access specific fields from the ScriptContext
rather than processing the entire structure. Scalus provides efficient ways to extract only the data you need.
Instead of converting the full ScriptContext
to Scott-encoded lambdas (which can be verbose and inefficient), you can use the fieldAsData
macro or the .field
extension method to directly access specific fields from a Data
object representing a complex structure like ScriptContext
.
import scalus.*, Compiler.*, builtin.{Data, Builtins, ByteString}, Builtins.*, ByteString.given
import scalus.ledger.api.v3.*
val sir = compile:
def validator(ctxData: Data) =
// this generates headList(...headList(sndPair(unConstrData(ctxData)))) code
// to extract the signatories field from the ScriptContext
val signatories = fieldAsData[ScriptContext](_.txInfo.signatories)(ctxData)
// or like this, which is equivalent
val signatories2 = ctxData.field[ScriptContext](_.txInfo.signatories)
val sigs = unListData(signatories)
// or like this, which is equivalent
val sigs2 = signatories2.toList
unBData(sigs.head) == hex"deadbeef"
// same as above
sigs2.head.toByteString == hex"deadbeef"
Last updated on