<p>I finally found the answer myself. It is kind of obvious but easy to forget :-)</p>
<p>When using the command <code>cardano-cli transaction build</code>, the script gets executed although the transaction has not been signed yet. So the Plutus function <code>txSignedBy</code> is doomed to fail.</p>
<p>The trick simply consists in specifying that you will sign it later on with the required key, thanks to the option <code>--required-signer-hash</code>.</p>
<p>As usual, the problem was where I didn't expected to be...</p>

CC BY-SA 4.0
2022-02-23T22:28:23.360Z
2022-02-23T22:28:23.360Z
Jean-Sébastien Gonsette
<p>I'm trying to use Plutus function <code>txSignedBy</code> with a transaction involving the usage of <code>cardano-cli</code></p>
<p>I first sent some UtxO to my script address, with a datum whose structure is like this:</p>
<pre><code>data TradeDetail = TradeDetail
    { tradeOwner        :: !PubKeyHash
    } deriving (Generic, ToJSON, FromJSON)

data TradeDatum = TradeDatum TradeDetail
    deriving (Generic, ToJSON, FromJSON)
</code></pre>
<p>To build the datum,</p>
<ol>
<li><p>I first generated the public key hash from my verificaiton key like this:</p>
<p><code>cardano-cli address key-hash --payment-verification-key-file mykey.vkey</code>
and got <code>59b07e6d2500c82f6325ac18d7624cfbdb83a92e061c31068e6b03f9</code></p>
</li>
<li><p>Then I created the Datum JSON file encapsulating the public key hash</p>
</li>
</ol>
<pre><code>{
   &quot;fields&quot;:[
      {
         &quot;fields&quot;:[
            {
               &quot;bytes&quot;:&quot;59b07e6d2500c82f6325ac18d7624cfbdb83a92e061c31068e6b03f9&quot;
            }
         ],
         &quot;constructor&quot;:0
      }
   ],
   &quot;constructor&quot;:0
}
</code></pre>
<p>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:</p>
<pre><code>{-# INLINABLE getback #-}
getback :: ContractInfo -&gt; TradeDetail -&gt; ScriptContext -&gt; Bool
getback ci details context =

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

        message :: BuiltinString
        message = &quot;Tx has to be signed by the item owner &quot; `appendString` decodeUtf8 (getPubKeyHash (tradeOwner details))
</code></pre>
<p>When using <code>cardano-cli</code> to get back the UtxO, signing with <code>mykey.skey</code>,  I get the error:</p>
<pre><code>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)
]
</code></pre>
<p>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.</p>
<p>But the question is, why function <code>txSignedBy</code> returns false ?</p>

<plutus><plutus-pioneer-program><cardano-cli>
Plutus function TxSIgnedBy - transaction build Error: The Plutus script evaluation failed
# Plutus function TxSIgnedBy - transaction build Error: The Plutus script evaluation failed

(Asked by: username_0 on 2022-02-23)

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,

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

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 ?

**Tags:** plutus, plutus-pioneer-program, cardano-cli

---

## 1 answers

### Answer by username_0 on 2022-02-23. Score: 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...


---

