0

I'm trying to use Plutus function txSignedBy with a transaction involving the usage of cardano-cli

I first sent some UtxO to my script address, with a datum whose structure is like this:

data TradeDetail = TradeDetail
    { tradeOwner        :: !PubKeyHash
    } deriving (Generic, ToJSON, FromJSON)

data TradeDatum = TradeDatum TradeDetail
    deriving (Generic, ToJSON, FromJSON)

To build the datum,

  1. I first generated the public key hash from my verificaiton key like this:

    cardano-cli address key-hash --payment-verification-key-file mykey.vkey and got 59b07e6d2500c82f6325ac18d7624cfbdb83a92e061c31068e6b03f9

  2. Then I created the Datum JSON file encapsulating the public key hash

{
   "fields":[
      {
         "fields":[
            {
               "bytes":"59b07e6d2500c82f6325ac18d7624cfbdb83a92e061c31068e6b03f9"
            }
         ],
         "constructor":0
      }
   ],
   "constructor":0
}

My problem happens when I try to retrieve the UtxO from the script, providing the datum. The validator sub-function where the error happens, in the ON-chain code, is like this:

{-# INLINABLE getback #-}
getback :: ContractInfo -> TradeDetail -> ScriptContext -> Bool
getback ci details context =

    traceIfFalse message $ 
        (scriptContextTxInfo context) `txSignedBy` (tradeOwner details)
    
    where

        message :: BuiltinString
        message = "Tx has to be signed by the item owner " `appendString` decodeUtf8 (getPubKeyHash (tradeOwner details))

When using cardano-cli to get back the UtxO, signing with mykey.skey, I get the error:

Command failed: transaction build  Error: The following scripts have execution failures:
the script for transaction input 0 (in the order of the TxIds) failed with:
The Plutus script evaluation failed: An error has occurred:  User error:
The provided Plutus code called 'error'.
Caused by: [
  (builtin decodeUtf8)
  (con bytestring #59b07e6d2500c82f6325ac18d7624cfbdb83a92e061c31068e6b03f9)
]

I don't know why my error message is not well formatted, but at least the public key hash is correctly displayed. This shows that, at the minimum, the datum is unpacked correctly and the public key hash is extracted.

But the question is, why function txSignedBy returns false ?

1 Answer 1

1

I finally found the answer myself. It is kind of obvious but easy to forget :-)

When using the command cardano-cli transaction build, the script gets executed although the transaction has not been signed yet. So the Plutus function txSignedBy is doomed to fail.

The trick simply consists in specifying that you will sign it later on with the required key, thanks to the option --required-signer-hash.

As usual, the problem was where I didn't expected to be...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.