Skip to content

Coin Toss

Commit-reveal coin toss. toss verifies Alice and Bob's revealed nonces against self.aliceHash and self.bobHash. Bob signs when both nonces are equal or Alice reveals sentinel self.N; otherwise Alice signs.

txt
# A commit-reveal coin toss where both sides must reveal the bytes they committed to.
Contract CoinToss:

    def toss(aliceNonce: hex, bobNonce: hex, sig: hex):
        # Reject immediately if either reveal does not match its stored hash.
        EqualVerify(Hash256(aliceNonce.Clone()), self.aliceHash)
        EqualVerify(Hash256(bobNonce.Clone()), self.bobHash)

        # Equal reveals, or Alice's sentinel reveal, route the spend to Bob.
        if Or(Equal(aliceNonce.Clone(), bobNonce), Equal(aliceNonce, self.N)) == 1:
            CheckSigVerify(sig, self.bob)
        else:
            CheckSigVerify(sig, self.alice)