Programming Languages

Buy Signature

Let's imagine you want to have your testament notarized. What would you do?

You first write your testament You pay a notary to stamp it You store a copy for future reference

If you think about it, there are many interactions in the world that follow that same process:

  • When your diploma is certified by your University.
  • When you renew your driver's license, there is no test, just a renewal stamp.

We could generalize the process as:

A buyer (you in this case) Pays a seller Takes something, or better yet, a representation of the item (a digest) to a seller. The seller can now sign the digest. All parties can see that the digest was signed.

This is the interaction that the buy_sig.glow contract represents.

Visualizing the Buyer and Seller interactions


Glow code

1 #lang glow
2 @interaction([Buyer, Seller])
3 let payForSignature = (digest : Digest, price : Nat) => {
4 deposit! Buyer -> price;
5 @verifiably!(Seller) let signature = sign(digest);
6 publish! Seller -> signature;
7 verify! signature;
8 withdraw! Seller <- price;
9 }
  • 2 Buyer and seller have agreed to the terms of this sale. They both know what the signature is about, and they want to conduct this sale.
  • 3 The digest of the message to sign is a parameter of the interaction, as is the convened price.
  • 4 The buyer deposits the money according to the price.
  • 5 The seller signs, but it is private only to the seller.
  • 6 The signature is made public for everyone to see.
  • 7 The signature is verified by everyone in a way that the contract enforces.
  • 8 Finally, the money is transferred to the seller.

There are several things to note:

  • 1 The code looks a lot like the sequence diagram we created before.
  • 2 The lines of code with @Seller annotation are private.
  • 3 The language itself takes care of prerequisites; If the buyer never deposits, then the seller never will be able to sign.

Lessons learned

  • Identify the participants of a contract with @interaction
  • When an instruction is annotated with the participant's name, that value is private for the participant. Like @verifiably!(Seller)
  • There are clear instructions for deposit! and withdraw!

© IOHK 2015 - 2022

Cardano is an open-source project.

Cardano is a software platform ONLY and does not conduct any independent diligence on, or substantive review of, any blockchain asset, digital currency, cryptocurrency or associated funds. You are fully and solely responsible for evaluating your investments, for determining whether you will exchange blockchain assets based on your own judgement, and for all your decisions as to whether to exchange blockchain assets with Cardano. In many cases, blockchain assets you exchange on the basis of your research may not increase in value, and may decrease in value. Similarly, blockchain assets you exchange on the basis of your research may fall or rise in value after your exchange.

Past performance is not indicative of future results. Any investment in blockchain assets involves the risk of loss of part or all of your investment. The value of the blockchain assets you exchange is subject to market and other investment risks