<p>the <code>ValidatorHash</code> represents the hash of the serialized <code>Script</code> of your smart contract.</p>
<p>you can think at the <code>ValidatorHash</code> as being the equivalent of <code>PubKeyHash</code> used for people's wallets, but for smart contracts.</p>
<p>it is <strong>not</strong> needed to deploy the smart contract since the serialized <code>Script</code> is needed for that and not its hash.</p>
<p>it is instead useful to use to check if a smart contract is signing a transaction.</p>
<p>just like in the minting policy that allows only one wallet to mint some tokens by checking the <code>PubKeyHash</code> to be correct. The same logic could be applied in order to only allow a smart contract to mint, but in this case you'll need the <code>ValidatorHash</code> since a smart contract can't have <code>PubKey</code>s.</p>
<h3>N.B.</h3>
<p>your code compiles even if <code>valHash</code> is defined as</p>
<pre><code>valHash = undefined
</code></pre>
<p>thanks to the laziness of haskell</p>
<p>since you never need to use the <code>valHash</code> in this specific example, then you never evaluate it, then there is no reason not to compile</p>

CC BY-SA 4.0
2022-06-21T21:34:40.067Z
2022-06-21T21:34:40.067Z
Michele Nuzzi
<p>Lars explains this in the week 2 video 2 lecture from 30:48 to 31:40 that &quot;this is just boilerplate.&quot;  <a href="https://www.youtube.com/watch?v=xgnmMl-eIIM&amp;list=PLNEK_Ejlx3x0mhPmOjPSHZPtTFpfJo3Nd&amp;index=2" rel="nofollow noreferrer">https://www.youtube.com/watch?v=xgnmMl-eIIM&amp;list=PLNEK_Ejlx3x0mhPmOjPSHZPtTFpfJo3Nd&amp;index=2</a>  Basically, he's having you take a standard toolbox to a job that in this case needs only a screwdriver. You're bringing along the hammer and wrench and pliers because that is the standard package of how you would normally do things.</p>

CC BY-SA 4.0
2022-01-23T00:27:28.473Z
Answer could be improved by following [answer] and [How to reference material written by others](https://cardano.stackexchange.com/help/referencing) guidelines.
gRebel
CC BY-SA 4.0
2022-01-23T00:29:49.860Z
Answer is difficult to understand, especially if English is not your native language.  It should be just the facts.
gRebel
CC BY-SA 4.0
2022-01-23T00:30:20.337Z
As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer).
Community
CC BY-SA 4.0
2022-01-22T20:04:04.537Z
2022-01-22T20:04:04.537Z
XiTouch
<p><a href="https://youtu.be/xgnmMl-eIIM?t=1743" rel="nofollow noreferrer">The <code>valHash</code> expression was added when Lars was writing Gift.hs.</a> He added it and then did some inspections on it and <code>scrAddress</code> in the REPL. This was done for demonstration purposes as those two expressions are related to each other and they are important to understand when writing smart contracts.</p>
<p>After inspecting it in the REPL, he could have deleted <code>valHash</code> as it doesn't serve any purpose in any of lecture 2's scripts.</p>
<p>The reason you see it in Homework1.hs and Homework2.hs is because all of the scripts during week 2 are based off of the original Gift.hs.</p>

CC BY-SA 4.0
2022-01-21T21:44:49.453Z
2022-01-21T21:44:49.453Z
Travis
<p><strong>I think</strong> it's required only when deploying the smart contract on-chain and the redeemer provides the validator.</p>

CC BY-SA 4.0
2022-01-21T21:19:47.963Z
Sounds plausible. I'm hoping the question will get picked up for next week's Q/A. I'll mark it correct if confirmed.
Daniel Bidulock
CC BY-SA 4.0
2022-01-21T21:16:03.210Z
2022-01-21T21:16:03.210Z
wakathi
<p><code>valHash</code> seems to be used by a function on the off-chain code (<code>mustPayToOtherScript</code> inside the <code>give</code> function; I am looking at the <code>Burn.hs</code> file). Therefore, I believe it is working here as the script address itself (at the end, the script address is composed of the script hash if I am not mistaken). So, it uses <code>valHash</code> to pass the address of the script to the off-chain code so it knows with which script you are trying to create a transaction.</p>

CC BY-SA 4.0
2022-01-22T10:58:10.930Z
As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer).
Community
CC BY-SA 4.0
2022-01-22T10:56:35.403Z
2022-01-22T10:56:35.403Z
FTM
<p>It is not needed here because the Datum is (). If you recall, the Datum is passed the hash of the script. In this example however, the valHash is significant since the example datum is simply ().</p>

CC BY-SA 4.0
2022-06-21T21:22:38.107Z
the validator hash is not related to the Datum; the Datum should represent the state of the smart contract and is updated with each transaction using the off-chain code; the validator hash is something fixed, in particoular is the hash of the serialized script
Michele Nuzzi
CC BY-SA 4.0
2022-01-21T21:41:26.693Z
2022-01-21T21:41:26.693Z
d_fajardo
<p>The homework for Week 2 involves implementing validators for two Plutus contracts. Details are <a href="https://www.youtube.com/watch?v=_r-EpXzQGKo" rel="nofollow noreferrer">here</a>.</p>
<p><strong>Spoiler alert!</strong> This probably gives away the answer to Week 2's first homework problem:</p>
<pre><code>-- ...

module Week02.Homework1 where

-- ...

-- This should validate if and only if the two Booleans in the redeemer are equal!
mkValidator :: () -&gt; (Bool, Bool) -&gt; ScriptContext -&gt; Bool
mkValidator _ r _ = traceIfFalse &quot;Invalid redeemer&quot; $ fst r == snd r

data Typed
instance Scripts.ValidatorTypes Typed where
    type instance DatumType Typed = ()
    type instance RedeemerType Typed = (Bool, Bool)

typedValidator :: Scripts.TypedValidator Typed
typedValidator = Scripts.mkTypedValidator @Typed
    $$(PlutusTx.compile [|| mkValidator ||])
    $$(PlutusTx.compile [|| wrap ||])
  where
    wrap = Scripts.wrapValidator @() @(Bool, Bool)

validator :: Validator
validator = Scripts.validatorScript typedValidator

--
-- HERE!
--
-- This appears superfluous
--
valHash :: Ledger.ValidatorHash
valHash = undefined -- FIX ME!
--valHash = Scripts.validatorHash typedValidator

scrAddress :: Ledger.Address
scrAddress = scriptAddress validator

-- ...

mkSchemaDefinitions ''GiftSchema

mkKnownCurrencies []
</code></pre>
<p>As marked <em>superfluous</em> above...</p>
<p>My Plutus Playground simulations execute correctly whether this line is defined or not:</p>
<pre><code>valHash = undefined -- FIX ME!
</code></pre>
<p>This is also true of the same function definition in the second homework assignment for Week 2.</p>

CC BY-SA 4.0
2022-04-28T05:29:25.117Z
Should one of the answers be marked as accepted?
gRebel
<plutus-pioneer-program><plutus-playground>
What is the purpose of the `valHash :: Ledger.ValidatorHash` function in the Plutus Pioneer Program's Week 2 homework?
# What is the purpose of the `valHash :: Ledger.ValidatorHash` function in the Plutus Pioneer Program's Week 2 homework?

(Asked by: username_0 on 2022-01-21)

The homework for Week 2 involves implementing validators for two Plutus contracts. Details are here.
Spoiler alert! This probably gives away the answer to Week 2's first homework problem:
-- ...

module Week02.Homework1 where

-- ...

-- This should validate if and only if the two Booleans in the redeemer are equal!
mkValidator :: () -> (Bool, Bool) -> ScriptContext -> Bool
mkValidator _ r _ = traceIfFalse "Invalid redeemer" $ fst r == snd r

data Typed
instance Scripts.ValidatorTypes Typed where
    type instance DatumType Typed = ()
    type instance RedeemerType Typed = (Bool, Bool)

typedValidator :: Scripts.TypedValidator Typed
typedValidator = Scripts.mkTypedValidator @Typed
    $$(PlutusTx.compile [|| mkValidator ||])
    $$(PlutusTx.compile [|| wrap ||])
  where
    wrap = Scripts.wrapValidator @() @(Bool, Bool)

validator :: Validator
validator = Scripts.validatorScript typedValidator

--
-- HERE!
--
-- This appears superfluous
--
valHash :: Ledger.ValidatorHash
valHash = undefined -- FIX ME!
--valHash = Scripts.validatorHash typedValidator

scrAddress :: Ledger.Address
scrAddress = scriptAddress validator

-- ...

mkSchemaDefinitions ''GiftSchema

mkKnownCurrencies []

As marked superfluous above...
My Plutus Playground simulations execute correctly whether this line is defined or not:
valHash = undefined -- FIX ME!

This is also true of the same function definition in the second homework assignment for Week 2.

**Tags:** plutus-pioneer-program, plutus-playground
> **Comments:**
> **username_1**: Should one of the answers be marked as accepted?  

---

## 6 answers

### Answer by username_2 on 2022-06-21. Score: 2

the ValidatorHash represents the hash of the serialized Script of your smart contract.
you can think at the ValidatorHash as being the equivalent of PubKeyHash used for people's wallets, but for smart contracts.
it is not needed to deploy the smart contract since the serialized Script is needed for that and not its hash.
it is instead useful to use to check if a smart contract is signing a transaction.
just like in the minting policy that allows only one wallet to mint some tokens by checking the PubKeyHash to be correct. The same logic could be applied in order to only allow a smart contract to mint, but in this case you'll need the ValidatorHash since a smart contract can't have PubKeys.
N.B.
your code compiles even if valHash is defined as
valHash = undefined

thanks to the laziness of haskell
since you never need to use the valHash in this specific example, then you never evaluate it, then there is no reason not to compile


---

### Answer by username_3 on 2022-01-22. Score: 0

Lars explains this in the week 2 video 2 lecture from 30:48 to 31:40 that "this is just boilerplate." <https://www.youtube.com/watch?v=xgnmMl-eIIM&list=PLNEK_Ejlx3x0mhPmOjPSHZPtTFpfJo3Nd&index=2> Basically, he's having you take a standard toolbox to a job that in this case needs only a screwdriver. You're bringing along the hammer and wrench and pliers because that is the standard package of how you would normally do things.

> **Comments:**
> **username_1**: Answer could be improved by following [answer] and [How to reference material written by others](https://cardano.stackexchange.com/help/referencing) guidelines.  
> **username_1**: Answer is difficult to understand, especially if English is not your native language.  It should be just the facts.  
> **username_4**: As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer).  

---

### Answer by username_5 on 2022-01-21. Score: 0

[The `valHash` expression was added when Lars was writing Gift.hs.](https://youtu.be/xgnmMl-eIIM?t=1743) He added it and then did some inspections on it and `scrAddress` in the REPL. This was done for demonstration purposes as those two expressions are related to each other and they are important to understand when writing smart contracts.

After inspecting it in the REPL, he could have deleted `valHash` as it doesn't serve any purpose in any of lecture 2's scripts.

The reason you see it in Homework1.hs and Homework2.hs is because all of the scripts during week 2 are based off of the original Gift.hs.


---

### Answer by username_6 on 2022-01-21. Score: 0

**I think** it's required only when deploying the smart contract on-chain and the redeemer provides the validator.

> **Comments:**
> **username_0**: Sounds plausible. I'm hoping the question will get picked up for next week's Q/A. I'll mark it correct if confirmed.  

---

### Answer by username_7 on 2022-01-22. Score: 0

`valHash` seems to be used by a function on the off-chain code (`mustPayToOtherScript` inside the `give` function; I am looking at the `Burn.hs` file). Therefore, I believe it is working here as the script address itself (at the end, the script address is composed of the script hash if I am not mistaken). So, it uses `valHash` to pass the address of the script to the off-chain code so it knows with which script you are trying to create a transaction.

> **Comments:**
> **username_4**: As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer).  

---

### Answer by username_8 on 2022-01-21. Score: -1

It is not needed here because the Datum is (). If you recall, the Datum is passed the hash of the script. In this example however, the valHash is significant since the example datum is simply ().

> **Comments:**
> **username_2**: the validator hash is not related to the Datum; the Datum should represent the state of the smart contract and is updated with each transaction using the off-chain code; the validator hash is something fixed, in particoular is the hash of the serialized script  

---

