C 391: Glow (20 pts extra)

What You Need

Purpose

To learn the basics of Glow, a new language for Distributed Applications.

Using Cloud Servers

You can't copy and paste into the Console in Proxmox. To enable copy and paste, do this:

On your Debian machine, execute the ip a command to see your IP address.

On your Windows machine, install PuTTY from

https://www.putty.org/

SSH into your Debian system from Windows using PuTTY.

Now you can copy and paste from a browser in Windows to get to Debian.

Installing Glow

Execute these commands, one at a time:
sudo apt update
sudo apt install curl
curl -L https://glow-lang.org/install/glow-install | sh
Close your Terminal window and open a new one. Execute this command to see the glow help page:
glow help

Running a Local Ethereum Testnet

Execute these commands to see the options available, and start a local Ethereum test net:
run-ethereum-test-net help
run-ethereum-test-net
The testnet starts, as shown below.

Creating Two Identities

Execute these commands:
glow generate-identity --nickname Alice --evm-network pet
glow generate-identity --nickname Bob --evm-network pet
Two identities are created, as shown below.

Getting Tokens from the Faucet

Execute these commands to see the two identities and fund them with tokens from the faucet:
glow list-identities 
glow faucet --to Alice --evm-network pet
glow faucet --to Bob --evm-network pet
as shown below.

Performing a Transfer

Execute this command to transfer 1 PET from Alice to Bob:
glow transfer --evm-network pet --database Alice -f Alice -t Bob -v 1
After the transfer, one account has 4 PET and the other has 6 PET, as shown below.

Signing a Document

Alice: Creating a Document and an Agreement

echo "This is a test. Bob sells, Alice buys." > document.txt
glow digest document.txt
glow start-interaction --database Alice --evm-network pet
You see a long hexadecimal "digest". Copy it into the clipboard--you'll need it below.

You also see a "Choose application" prompt, as shown below.

Enter these values:

You see two commands for Bob to execute, as shown below.

Leave this Terminal window open.

Bob: Agreeing to the Agreement

Open a new Terminal window.

Copy and paste in the first commands from the other window, starting with "glow start-interaction --agreement".

Add this to the end of the command:

 --evm-network pet
It asks for your identity.

Enter these values:

C 391.1 Signature (10 pts)

Both windows show a "interaction finished" message, as shown below.

The flag is covered by a green rectangle in the image below.

Glow Code for "Buy Signature"

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    }

Line-By-Line Explanation

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.

Notes

The lines of code with @Seller annotation are private.

The language itself takes care of prerequisites; If the buyer never deposits, then the seller never will be able to sign.

Failure Cases

Buyer Never Pays
Process stops at line 4, so seller never creates signature. The interaction times out and is cancelled. No funds are exchanged.
Buyer Pays, but Seller Never Signs
At line 4, the payment is deposited (into escrow).
The process stops at line 5.
The interaction times out and the funds are returned to the Buyer.
Seller Sends Invalid Signature
Process stops at line 7 because the signature does not validate.
The interaction times out and the funds are returned to the Buyer.

Creating a Modified Signature Dapp

Execute these commands to make the directory Dapps belong in, and create a Dapp named "sig2.glow":
mkdir ~/.local/share/glow/dapps
nano ~/.local/share/glow/dapps/sig2.glow
Paste in this code, as shown below:
#lang glow
@interaction([Buyer, Seller])
let payForSignature = (digest : Digest, price : Nat) => {
  deposit! Buyer -> price;
  @verifiably!(Seller) let signature = sign(digest);
  verify! signature;
  withdraw! Seller <- price;
};
Save the file with Ctrl+X, Y, Enter.

Running sig2.glow

Execute this command to see the available Dapps:
glow list-applications
"sig2" appears on the list, as shown below.

C 391.2 Error Message (5 pts)

Execute these commands to prepare another document to sign:
echo "Test #2. Bob sells, Alice buys." > document2.txt
glow digest document2.txt
glow start-interaction --database Alice --evm-network pet
Select Dapp 4: sig2.

An error message appears.

The flag is covered by a green rectangle in the image below.

C 391.3 Missing Line (5 pts)

Fix the "sig2" Dapp. You need to add one line. That line is the flag.

References

Cardano Testnets: Programming Languages
Glow Whitepaper
glow-mooc

Posted 9-29-2021
Extra failure case removed 10-22-21
Cloud server note added 11-8-21