DEV Community

CoinMonks
CoinMonks

Posted on • Originally published at Medium on

A Python Implementation of GoofyCoin

A simple and useless cryptocurrency

Photo by Liam Read on Unsplash

1 — Introduction

GoofyCoin is about the simplest (and thus useless) cryptocurrency we can imagine. It is described in Chapter 1 of the book “Bitcoin and Cryptocurrency Technologies”, written by Arvind Narayanan, Joseph Bonneau, Edward Felten, Andrew Miller, Steven Goldfeder, and published by Princeton University Press in 2016. As a blockchain beginner, I tried to implement it in Python for learning purposes. I’m sure that my code contains mistakes and maybe my understanding of GoofyCoin rules, as described in the book, is not 100% accurate. If anyone finds any of those problems, feel free to comment here.

A jupyter notebook containing this introduction, explanation of GoofyCoin (as described in the book), and my code is available in the following GitHub: https://github.com/thiagoguimaraesdf/goofyCoinPython

2 — GoofyCoin rules and protocol

The two main GoofyCoin rules are:

(i) Goofy, which is a unique entity, can create new coins whenever he wants and these newly created coins belong to him.

(ii) Hoever owns a GoofyCoin can transfer it on to someone else. Transferring a coin is not simply a matter of sending the coin data structure to the recipient — it’s done using cryptographic operations.
Enter fullscreen mode Exit fullscreen mode

The protocol for creating a new GoofyCoin is described below:

"To create a coin, Goofy generates a unique coin ID uniqueCoinID that he’s never generated before
and constructs the string “CreateCoin [uniqueCoinID]”. He then computes the digital signature of
this string with his secret signing key. The string, together with Goofy’s signature, is a coin. Anyone
can verify that the coin contains Goofy’s valid signature of a CreateCoin statement, and is therefore a
valid coin."
Enter fullscreen mode Exit fullscreen mode

The protocol for transferring an existing coin is described below:

"Let’s say Goofy wants to transfer a coin that he created to Alice. To do this he creates a new
statement that says “Pay this to Alice” where “this” is a hash pointer that references the coin in
question. And as we saw earlier, identities are really just public keys, so “Alice” refers to Alice’s public
key. Finally, Goofy signs the string representing the statement. Since Goofy is the one who originally
owned that coin, he has to sign any transaction that spends the coin. Once this data structure
representing Goofy’s transaction signed by him exists, Alice owns the coin. She can prove to anyone
that she owns the coin, because she can present the data structure with Goofy’s valid signature.
Furthermore, it points to a valid coin that was owned by Goofy. So the validity and ownership of coins
are self‐evident in the system.

Once Alice owns the coin, she can spend it in turn. To do this she creates a statement that says, “Pay
this coin to Bob’s public key” where “this” is a hash pointer to the coin that was owned by her. And of
course, Alice signs this statement. Anyone, when presented with this coin, can verify that Bob is the
owner. They would follow the chain of hash pointers back to the coin’s creation and verify that at
each step, the rightful owner signed a statement that says “pay this coin to [new owner]”."
Enter fullscreen mode Exit fullscreen mode

A more formal summary of GoofyCoin rules is described in the book:

● Goofy can create new coins by simply signing a statement that he’s making a new coin with a
unique coin ID.

● Whoever owns a coin can pass it on to someone else by signing a statement that saying, “Pass
on this coin to X” (where X is specified as a public key)

● Anyone can verify the validity of a coin by following the chain of hash pointers back to its
creation by Goofy, verifying all of the signatures along the way.
Enter fullscreen mode Exit fullscreen mode

3 — Implementation Code

3.1 — Library Import

The main libraries used here are ecdsa, for generating digital signatures, and Haslib, for employing Hash Function (SHA-3)

More info about ecdsa: https://pypi.org/project/ecdsa/

More info about hashlib: https://docs.python.org/3/library/hashlib.html

3.2 — Our random string generator

Defining a function to generate a random string with length n.

Note: There are probably more randomized algo’s for generating a random n length str. I’m employing here a very simple one.

3.3 — Creating Goofy Keys

We are going to create the real Goofy Signature and Public Keys outside our GoofyCoin class. I’m using here the ECDSA library (and algo) to generate and further verify Goofy private signature.

For more information about ECDSA algo: https://www.cs.miami.edu/home/burt/learning/Csc609.142/ecdsa-cert.pdf

3.4 — The GoofyCoin Class

The code for creating GoofyCoin class — which will enable us to (i) create a coin — only with Goofy Secret Signature, (ii) transfer a coin; and (iii) check if a coin is valid — is described and well commented below.

4 — Testing our Coin

I test here for the following cases:

Test 1: Verifying a brand new coin

Test 2: Trying to transfer an inexistent coin

Test 3: Trying to create a new coin without Goofy Secret Key

Test 4: Verifying a coin that was already transfered once

Test 5: Verifying a coin that was already transfered twice

Test 6: Verifying a fake coin transfered by Max

5 — Final remarks

Despite being a shitty coin, GoofyCoin guarantees a central authority (unfortunately Goofy in this case) the power to create coins and transfer them with a valid method of verification. Anyone receiving a coin can check if that coin is valid by following its “route” and checking if transferences were made by the right users.

The main problem with Goofy Coin is that anyone can double-spend its own coin. We don’t have a valid method to guarantee that a bad user won’t transfer his coin to two or more different persons. That’s why we call it Goofy Coin


Top comments (0)