Building a Cryptocurrency From Scratch

Eight years ago, my dad told me I had to play Minecraft.

He told me I needed to dig and mine for emeralds because it would be good for my future. As a 9 year old kid, I was very confused. Not only was I a Minecraft addict who spend every waking day of my life collecting my pile of diamonds, but what he was telling me to do went against everything he had been preaching to me for the first part of my life.

So why the sudden change in mindset? What had made my dad do a full one-eighty with his values? Well, it turns out that he had been pushing me to play on a specific Minecraft server because on this specific server, the owner would trade every emerald mined and sold for one tenth of a Bitcoin.

Now, being a rebellious kid combined with my contrarian personality, I ignored him and used my hours to collect emeralds on my own private world. Had I spent even one day on that server and collected only 5 emeralds, I would have earned myself almost $10,000 worth of Bitcoin today plus, there’s no telling how much I would have made had I spent my usual “Minecraft time” on the server until it shut down the next year.

Cryptocurrency is a digital asset used as a medium of exchange. It could represent a certain amount of United States dollars, a contract between parties or, in this case, an emerald on a Minecraft server. Through complex cryptographic systems and a dedicated blockchain, cryptocurrencies can serve as the perfect “money” and are certain to be very prominent in the future of economics.

Following the remorse of not “mining” Bitcoin, I have been digging deep into the “in-s” and “out-s” of cryptocurrencies' and found myself getting into how they are actually developed. My thought process was:

“Okay. I know a ton about the fundaments of cryptocurrency and many concepts on the ‘how’s, but I don’t actually know how it’s made.”

So I decided to create my own cryptocurrency from scratch in python. I called it NuggetDigital. (At the time I was around 9 years old, my dad would call me ‘Nugget’)

I created NuggetDigital (or NGD for time’s sake) on Atom using python and tested my code on Command Prompt. My first step was to set up my blockchain.

Blockchain

The first and most important part of the blockchain was to import the sha-256 library which is essentially the set of cryptographic hash functions that will “protect” our cryptocurrency. To do this in python, you simply type:

from hashlib import sha256

Next, I set up all of my important variables/classes such as defining what a block in my blockchain actually meant, which was a table including data (what does it represent), the number block in the blockchain, the has of the previous block (in order to confirm the block), and the nonce which I explain in my Introduction to Cryptocurrency article.

Now that I had my blocks defined, it was time to set up the blockchain itself. I defined a chain and what it means to add and remove (if faulty) a block:

def __init__(self):
self.chain = []

def add(self, block):
self.chain.append(block)

def remove(self, block):
self.chain.remove(block)

I defined mining in order to cultivate more blocks in the future. I also created an “isValid” variable in order to check each block to see if it is legitimate.

def mine(self, block):

def isValid(self):

And finally set my difficulty. Now I had a decision to make as I had yet to set up a difficulty adjuster, so mine would be fixed. I decided to set it to 100 as I am planning to mine some NGD in the future yet do not want it to be too easy for someone to mine it all say on a laptop or something low power like that.

Finally, I plugged in some filler information to test my blockchain and moved onto the “data” part of the blocks.

NuggetDigital

Setting up my database was a lot harder than I had expected. Before this project, I had never been introduced to MySQL, however I used it as my database. MySQL has a very confusing download process and it was especially challenging integrating it with Command Prompt as there were limited tutorials outside of Mac. I eventually setup my local database and began to use it.

Once on MySQL, I set up my data base within Command Prompt and set it up as so to resemble the table I had made within my blockchain by including the nonce of the block, the data included in the block, the previous block’s hash, and the current block’s hash:

create table blockchain(number varchar(), hash varchar(), previous varchar(), data varchar(), nonce varchar());

I then imported this table from MySQL into my code and began to create the application/website around my cryptocurrency. I defined users and protected their accounts using the algorithms from sha-256 as well as set up passwords to their accounts. This way, I will not have to create dedicated wallets to each of the users. Each account comes equipped with a randomly generated public and private key so that all transactions involving NGD can be within the website.

if isnewuser(username):
password = sha256_crypt.encrypt(form.password.data)
users.insert(name,email,username,password)
log_in_user(username)
return redirect(url_for(‘dashboard’))

Next and most importantly, I setup transactions (sending, receiving and requesting NGD). NGD’s transactions work just like any other cryptocurrencies as they contain two users both with their own public and private keys along with a set balance that no party may exceed. No NGD can be double spent and no one public user can take another user’s NGD without verification.

def transaction():
form = SendMoneyForm(request.form)
balance = get_balance(session.get(‘username’))

if request.method == ‘POST’:
try:
send_money(session.get(‘username’), form.username.data, form.amount.data)
flash(“Money Sent!”, “success”)
except Exception as e:
flash(str(e), ‘danger’)

return redirect(url_for(‘transaction’))

return render_template(‘transaction.html’, balance=balance, form=form, page=’transaction’)

I also set up a centralized “bank” which holds all of the non existent (yet) NGD before it is mined. Although I say centralized, I do not mean it in the traditional sense as this “bank” holds no power nor existing value, it simply holds the unmined blocks and validates them when they inevitably are mined by a user and recorded in the forms.

In NGD, all transactions are public within a tab on the website in order to enforce safe and valid transactions. The transaction history shows the hash of the transaction as well as the two parties included and the size of the transaction.

In Conclusion

There is a lot more that I had to do in order to fully develop my website and get NGD fully operational such as using a website template and defining important variables in a separate document which were then imported to my main script however, I have covered the basics and general gist of coding a cryptocurrency through Python.

The website I created is not public however as I am using a local MySQL database to run it so if any other users would want to be able to join in on the ledger, they would have to download the code and install my MySQL database.

If you would like to contact me to see the full code or even just to reach out, you can contact on the platforms below!

Twitter

LinkedIn

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store