For a contract, I want to use Constraints.mustPayToPubKey for an arbitrary pubKeyHash (pkh), such as a lucky winner in a lottery.

Question: Does the validator need to check that the transaction sees that pkh (using txSignedBy or similar)? If so, why?

1 Answer 1


Does the validator need to check that the transaction sees that pkh?

Yes. There is more than one way to organize the logic though.

Whichever way you structure the lottery, I will assume that the winnings are owned by the script. Any transaction that spends UTxOs owned by the script need to meet the qualifications of the validator.

As you say, one way to construct the smart contract would be to make sure you mustPayToPubkey of the winner. The validator would then need to guarantee that the output of the "pay out winnings" transaction includes the winnings and they are going to the winner's pkh. This wouldn't use txSignedBy, but rather do some combination of checks to guarantee the winnings are going to the correct pkh.

The way I would suggest modelling it is by saying only the winner can claim the winnings. In that case you could use txSignedBy.

This approach is similar to how the vesting example works in the PPP. It doesn't use txSignedBy, but rather just checks that the beneficiary is among the signers, essentially doing the same thing.

You don't even need to specify mustPayToPubKey at that point because it will default to the transaction builder's wallet. See the vesting example's grab constraints.

  • 1
    Scripts take of space on the chain and cost gas to execute. Simple is better. Not to mention safer. And, as I mentioned. Only the winner is motivated to submit a tx to claim the funds, there is no need to provide multiple ways to access the money. Both approaches are allowed because there are infinite different smart contracts people might want to write. It's easy to imagine a script that would require you to send funds to some wallet. Maybe a tip to the contract writer or something :P Aug 12, 2021 at 18:40
  • 1
    I imagine that the funds are on the script, and no-one owns them. That's the point of a decentralized smart contract after all. If there is some central owner of the funds, there would be no need for a smart contract on-chain, you could just have an agreement off-chain. Aug 12, 2021 at 18:54
  • 1
    Yes. Since the script is passive, the winner wouldn't get paid unless they proactively submit a "claim" transaction of some sort. This of course could be automated off-chain, either to notify the winner or possibly have the wallet submit the "claim" transaction automatically. Aug 12, 2021 at 19:14
  • 1
    FYI the SimpleEscrow plutus use-case uses mustPayToPubKey
    – Kwaggy
    Aug 12, 2021 at 21:07
  • 1
    That's a perfect example of a transaction that needs to send outputs to different parties. Notice in the validator github.com/input-output-hk/plutus/blob/… it only checks that the payee was paid. The line you linked isn't technically necessary, it's just explicitly saying you want the rest of the money. You could send it anywhere and the validator would still pass, as long as the payee gets their due. You would probably never do that, but it's a good example of how validators stay simple. Aug 12, 2021 at 21:22

Your Answer

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

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