Learn to Code via Tutorials on Repl.it!

← Back to all posts
How I made a cryptocurrency (Eggcoin), and how you can! part 1?
generationXcode (513)

What have I made?

What is a cryptocurrency?

  • A kind commentor made me aware of this -

    cryptocurrency is basically a type of currency that doesn't actually exist, like Bitcoin or Dogecoin. They exist, but digitally, from which the name _crypto_currency comes from. The word crypto means digital. Digital currency. That's basically what cryptocurrency is.
    thanks @noway15

  • If we go more in detail on this, we can say it is a currency like any other and all the transactions are stored in a type of ledger called a blockchain. (ledger is a record of transactions or accounts)
  • in the transactions that are stored in the ledger, there are the coins that are being transferred. According to the bitcoin whitepaper ( https://bitcoin.org/bitcoin.pdf ), "We define an electronic coin as a chain of digital signatures.". This statement is easy to understand, but if you don't, read it again. A digital signature is similar to a real signature, it shows that the coin belongs to a person and is used to verify that ownership. You can imagine a coin as being a record of transactions itself. It is passed on from one person to the next and the record of this passing on is stored in the ledger. Each coin is chained to the next by signatures and encryption keys.

    dont be afraid if you havent understood all aspects of a cryptocurrency yet, wait and read, this is a large topic and it takes time to explain.

What is a blockchain? (detail)

peer to peer network

  • here we go into more detail onto what exactly a blockchain is.
  • a blockchain is a decentralized peer to peer record of some stuff.
  • (explanation of the above statement) You imagine a server as a central computer that all users send requests to and get responses from - you send your coordinates to a video game server and you get a response of everyone else's coordinates OR you send a request for a webpage and the server sends the webpage to you - , this is what a regular network is like. Now imagine there is no central server and all the users are connected to one another directly (no need for one large computer) - a popular example of this I think is skype, you send your image frames and audio data to everyone else in the call and they send their image and audio data to you -, this is how the peers (or nodes or users or computers) in a blockchain communicate.

    In peer-to-peer (P2P) networking, a group of computers are linked together with equal permissions and responsibilities for processing data. Unlike traditional client-server networking, no devices in a P2P network are designated solely to serve or to receive data. (got from webroot.com)

  • now you know what a peer to peer network is. So how does blockchain sofware use this? every computer keeps a copy of the ENTIRE blockchain (there are ways of not storing the entire blockchain and going into "pruning mode", but I might explain that later) and when a new computer joins the network, it requests for this copy of the blockchain from EVERY SINGLE PEER and it processes this and figures out which to use (the most popular and longest chain, I'll say why later). Each peer also gives the entire list of other peers to a newly joined peer as well. When a transaction is made from one person to the next (for say, to buy a bunch of eggs), the transaction is broadcasted to every peer on the network. Everyone sees the transaction. each peer then checks whether the transaction is valid or not and quitely adds it or doesnt add it to the current transactions list. When a block is mined, it is broadcasted to every single peer on the network as well, and this takes slightly long since every single peer has to check if the block is valid or not. If the block is invalid and not acccepted by majority of peers, The peers ignore it and keep mining.

everything else about blockchains

  • blockchains have blocks. the blocks have transactions.
  • now you know the whole peer to peer aspect of blockchain, but how does it work AND WHAT IS MINING? good question.
  • The reason why we all use blockchains is because they are very difficult, almost impossible to tamper with. With a large enough peer to peer network, they are virtually impossible to tamper with. The reason they are so difficult to tamper with (another word for this is "immutable": cannot be changed), is because they are each connected to each other with the previous block's hash and a nonce. I'll get to what a nonce is later, first I'll explain what hashing is:
  • a hash is a way of storing data without revealing it to others. It is a one way encryption system, it cannot be decrypted. imagine your teacher gives you a math problem to solve, but once you have solved it , the teacher wants you to check whether your solution is correct or not. Your teacher hashes the solution of the problem and gives you the hash of the solution. Once you have solved the problem, you yourself can hash your solution and see whether your hash and your teacher's hash match. If they do, you have solved the problem correctly.(of course everyone has a copy of the blockchain so hashes arent needed for secrecy here, they are needed for converting something large into something smaller, for referring to that block somewhere else in the blockchain : as the hash of the previous block in the new block) If you have any issues with understanding what hashing is still, search it up or ask about it in the comments section.

  • Now to what a nonce is. A nonce is a number (eg. 42). This number is concatenated with the hash of the previous block (added to, like "a"+"b" = "ab"). The resulting string is hashed once more. The blockchain then checks whether the resulting hash has a certain number of 0s at the beginning - eggcoin currently checks whether the hash has 5 0s at the beginning (eg.00000dacb5edd02d3a1205c97136bcea77465bd9666cc48d397647300759b4c7). Now you have to keep trying to find the correct nonce by adding 1 to the nonce and hashing it and checking whether it results in a hash beginning in a certain number of 0s. To do this you set up a loop and checking. This takes anywhere between 2-10 minutes to find. Once your computer has found the nonce, the blockchain awards you with a number of coins for doing this and keeping it secure. You keep it secure by mining since if someone else wanted to tamper with it, they wouldnt be able to since you have already found the proof of work and tampering changes the block's hash, which chages the nonce of the block in front and this is repeated over and over until they reach the current block being mined. It is simply not feasible to be dishonest here. The algorithm i have described to you is called hashcash and I would like to talk more about it here, but I won't. This is a good time to tell you that in an actual crypto currency like bitcoin, the difficulty of the mining process changes with time, since computers get faster and each block must take 10 minutes to mine (for some odd reason)so the difficulty is linked to the running average time taken to mine( or solve) a block.

  • now you know what mining is, but how are transactions validated?

transaction checking

  • to check a transaction and avoiding people from spending an already spent coin, I use a specific method. I use a json file to store all unspent coins. I loop over all transactions in the current block to find all the new blocks mined and unspent coins. If a coin is spent, I remove it from the unspent coins database and add the recieving end of the coin. To explain this better - each transaction has a number of inputs and outputs. The inputs contain all the coins that some people want to send to someone else and the outputs contain all the coins being given to the receiver. AHH HOW TO EXPLAIN THIS??? OK I'LL GIVE YOU AN EXAMPLE :

{"inputs": [{"owner_public_key": "egg", "amount": 1, "hash": "9bdb2af6799204a299c603994b8e400e4b1fd625efdb74066cc869fee42c9df3", "signature": "egg"}, {"owner_public_key": "egg", "amount": 1, "hash": "f6e0a1e2ac41945a9aa7ff8a8aaa0cebc12a3bcc981a929ad5cf810a090e11ae", "signature": "egg"}], "outputs": [{"owner_public_key": [97869116433446942323146560744474639613338861537970253325070066035899092552676025941743682798490625667613437043881341795995901359651547240478168111455670099640192604489299494248455527271650466560190346987463365455008801466974394486848293315785122522253307714470607743349364252398523627078541822621308510626499, 65537], "amount": 1, "hash": "688f85a6b1a371752332e13c4a05cbc525e06523bdde31f31fb31df3c773179b", "signature": "egg"}, {"owner_public_key": [97869116433446942323146560744474639613338861537970253325070066035899092552676025941743682798490625667613437043881341795995901359651547240478168111455670099640192604489299494248455527271650466560190346987463365455008801466974394486848293315785122522253307714470607743349364252398523627078541822621308510626499, 65537], "amount": 1, "hash": "5578213b070935a797de14abe3a6fdc283ac10ca8b9712ed7f1b63a4d8d5df24", "signature": "egg"}]}]}

  • obviously egg is not a correct signature but it is how i award coins to miners
  • all the coins in the input are removed from unspent coins and all coins from outputs are added to the unspent coins db
  • i check whether the inputs are actually in the unspent coins before doing the above. If they arent, the transaction is invalid.
  • now you know how to validate transactions, what else is left? wait let me check. yeah there is a lot left. not really.

Digital signatures

  • now i say what digital signatures are.
  • these rely on assymetric encryption
  • here i will explain assymetric encryption in the context of blockchain
  • assymetric encryption is where each peer has 2 keys - a public key and a private key. the public key is shared with everyone and the private key is kept with the peer secretly. The public key is used to identify the peer all accross the network. when a person makes a transaction, the reciever's public key is required to send the coin to him/her/them. Another use for the public key is to decrypt the signature. When a transaction is made, the previous owner of the coin signs on it, the previous owner uses his/her private key to encrypt the hash of the coin. The signature can then be decrypted with the previous owner's public key, and the hash checked. This is used to verify the ownership of the coin and the transfer of the coin to the new owner. If you have any questions about this, dont be afraid to google or ask me in the comments section. will be adding visuals some time later.

The end.

  • use my code as reference and please point out any issues and contibute to solving them at https://github.com/generationzcode/socoolblockchain
  • try not to copy it all, build it yourself, it will make it more clear to you
  • CHECK THIS OUT https://bitcoin.org/bitcoin.pdf
  • please post cool blockchain pics here, i'll probs include them in the explanation
  • I have probably missed a lot of points, if I have, please inform me about the missed points, I'll include them.
  • this is part 1 of 2 probably, if i feel like doing the second. I'll do part 2 where I explain all the code
  • that is it. have a great day.
    epic epic epic epic
  • got images from the bitcoin whitepaper to explain. thank you bitcoin whitepaper
sciolytic (4)

The root "crypto-" means hidden, not digital. In this case, it is in reference to the use of cryptography in maintaining trust in cryptocurrency.

NoelB33 (351)

Pretty cool, I want to try and make myself a PoS crypto, but not on repl.it.

programmeruser (604)

How to you find peers without a central server?

generationXcode (513)

@programmeruser that was actually a problem at the start, if there is only 1 server that doesnt know any other peers, how does it find them? To counter this, I made it impossible to delete one server (mine) from the peer list. So i guess you can say that there is one central server to find peers. After the first few peers were set up, I would frequently stop and start the main server (mine) and the network would still keep running. This is because the other peers would let the new peer know who they all are. Tldr -> there is a certain address that is never offline (or atleast promises not to be) and if you want a list of peers, that server is the first to request the list from. I dont know if this is all not very unclear. Also people usually fork my main server so they start off with that list of peers whom they can ask for all the other peers from. You cannot find peers without having a start point.

sojs (341)

Very nice in depth (without going to deep) tutorial

generationXcode (513)

@sojs yeah, what i mean to do really.

maxina (59)

Thanks! I am very interested, will read this in depth later and try to make my own!

Pabloes1949 (1)

Do you really earn money from it? Cause if so I might quit my job.As I do think cryptocurrency is "Money of the future" and if makes money then I might get into cryptocurrency again. Since I went for btc and lost some money which is why I stopped.

generationXcode (513)

@Pabloes1949 u cant earn money from this. Big oof. if u make a crypto maybe ppl cnpay u, but from eggcoin? no

korrykatti (3)

can i make my own crypto with this

generationXcode (513)

@i10 o lol forgot about that

alucca (1)

Is that blockchain? I really admire people with great ideas like you

Monk1 (9)

Awesome! I want to try do something like this

StringentDev (223)

Hello, Im just wandering if you want to help create Mint? IDK what else to put here aaaaaaaaaaaaaaaaaahhh heeeeelllllppp.

ooooh ik, i already created the encryption modules sooo yeah. Also. im creating a dashboard that will have a database on what each peer is in the network, but you must register it to your account by getting it's unique nodeID.

generationXcode (513)

@StringentDev lovely, but im really busy right now with studies, so i cant really help

StringentDev (223)

@generationXcode aah i see. Also. I have generated a SHA256 & RSA-8096 key using openssl