Building a Cryptocurrency with Kotlin : Part 1

File:Kotlin-logo.svgKotlin has been becoming more and more mature with every release. We arguably have the most concise modern programming language that currently exists. With language idioms like data classes, extensions, kotlin objects, delegated properties, lambdas and of course, the latest version of Kotlin adds coroutine support. Lets talk about Node.js for a second. Node.js helped us to create many amazing websites and services, and provides numerous modules to make our lives as developers easier. But just because a tool is good enough does not mean we should stop innovation there. Kotlin is now Node.js' competitor because Kotlin's powerful language idioms enable us to create a powerful and fast framework for building asynchronous servers and clients, called Ktor.


Related imageLets go back in time for a moment. Back in 2008 a small C++ side project was started by Satoshi Nakamoto. This project would change the world for ever. 10 years later Bitcoin managed to inspire dozens of crypto currency projects, in languages such as Rust and even Java. With features ranging from proof-of-stake, smart contracts and atomic-swaps.

For this series of tutorials we will learn how cryptocurrencies are written by creating our own, by using Kotlin and nothing else. This currency will have most common features like a blockchain, peer-to-peer consensus, transaction validation, proof-of-work, wallets, smart contracts, and more. Don't worry its actually easier than you think. Also for simplicity we will skip some security features, so final result will not be fit for production use. The reason for these tutorial series is to learn about Kotlin and cryptocurrencies by writing one from scratch. So lets get started!

It All Starts With a Chain of Blocks

Blockchain is a very simple data structure, similar to a linked list. The main difference is that each block holds the hash of the previous block. Think of a hash as a unique signature of that block, given all the data it currently contains, and even if a single bit of that data is different the hash would change. Just like a linked list, each item in the block chain has data. The base minimum data that a block needs to hold are:
  • index: the number of the block in the chain.
  • previous hash: calculated hash of the previous block at the moment of creation of the current block.
  • data: this could be any data like transaction inputs and outputs, smart contract states and inputs, or any other data you wish to store. For this lesson we will keep a string value but in the next lessons we will store transaction data in this field.
  • hash: unlike other fields this is a function that is called on the block to calculate its hash.

There is a built in security feature in such a data structure. In order for a malicious actor to change a single block's data, the actor would have to modify the hash of that block and all the block following it in order to maintain the validity of the data structure.

We will create a class to help us manage our collection of Blocks. Because we only need a single instance of the BlockChain we will make it a Kotlin singleton. For this lesson we will keep the BlockChain object simple. It will have two main functions addNewBlock and isValid. Lets see how our singleton object would look like in Kotlin.

To add a new block to our list of blocks we simply create a new instance on the Block data class with the index of the current block, the hash of the previous block, the time stamp and the data string.

To validate the chain we will iterate through every Block until the genesis block and make sure the previousHash matches the actual hash of the block, and the first block matches the hardcoded genesis block, that way we know none of the individual block has been modified. In the next lesson we will see how to pick the longest chain in the network to meet consensus but for now lets see how to implements the validation function:

And to add some final touches to our node implementation lets create couple endpoints to call the functions on or BlockChain object. But first lets add the following Ktor dependencies to our gradle build file:
    compile "io.ktor:ktor-server-netty:$ktor_version"
    compile "io.ktor:ktor-html-builder:$ktor_version"

Next we will implement a simple Ktor Application module and create 3 endpoints, root that will display a list of all blocks, /add that will generate a new block on our chain, and finally a /isValid endpoint to check the validity of the current chain.


And as you can see the basic underlying structure of most blockchains is very simple. Hopefully this lesson helped you understand the basics of blockchain, the fundamental technology of most crypto currencies.


Comments

  1. I am all that much satisfied with the substance you have specified. I needed to thank you for this awesome article.  Coins Libra

    ReplyDelete
  2. You may find that cryptocurrency is for you or you may not but rather at any rate you'll have the option to talk with a level of sureness and learning that others won't have.
    TradingPlatform

    ReplyDelete
  3. Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks Payeer Invest

    ReplyDelete
  4. Thanks for sharing is so amazing and helpful to us.
    Buy Hydrocodone online

    ReplyDelete
  5. Looking for English to Spanish Translators? We provide professional Translation Services at highly competitive rates without compromising the quality.
    english to spanish translation services

    ReplyDelete
  6. Thanks a lot for sharing
    Having good health is what most people out there wants but can not achieve. some people takes buy ibogaine online AND buy weed online to get it.

    ReplyDelete
  7. Most of the Calgary roofers are going to be certified, dependable, and honest in their work. But not all roofing companies in Calgary offer the same quality of shingles or other roofing materials. And every Calgary roofing company has different guarantees and warranty offers.

    ReplyDelete
  8. medical care is what many people lack in the interior areas of the world. people face a lot of health issues everyday without solution. buy magic mushroom, Buy weed online, Buy heroin online

    ReplyDelete
  9. Mr Sanjay Baghela focus to reflects as the best leader of SEO in the world, and his dedication and expertise in SEO field make him enable as the owner of the best  BEST SEO Expert India. You can connect with him anytime and you can see his knowledge about SEO itself. Drop your request and dial his number now! best seo expert in india

    ReplyDelete
  10. With Sanjay Baghela, your business Website is in safe hands. He and his team are the best SEO Expert in India the leading digital marketing field.
    Best SEO Expert In India
    Best Seo Company in India

    ReplyDelete
  11. Thanks for sharing the post.. parents are worlds best person in each lives of individual..they need or must succeed to sustain needs of the family. Best bitcoin trading software

    ReplyDelete
  12. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. ethereum generator online 2020

    ReplyDelete
  13. Some truly wonderful work on behalf of the owner of this internet site , perfectly great articles . comprar bitcoin brasil

    ReplyDelete
  14. What could be one big problem with this system of transaction? No members can sell Bitcoin if they don't have one. It means you have to first acquire it by tendering something valuable you possess or through Bitcoin mining. A large chunk of these valuable things ultimately goes to a person who is the original seller of Bitcoin. Of course, some amount as profit will certainly go to other members who are not the original producer of Bitcoins. Some members will also lose their valuables. As demand for Bitcoin increases, the original seller can produce more Bitcoins as is being done by central banks. As the price of Bitcoin increases in their market, the original producers can slowly release their bitcoins into the system and make a huge profit..   Visit here today

    ReplyDelete
  15. I learn some new stuff from it too, thanks for sharing your information. url

    ReplyDelete
  16. I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! btc to skrill

    ReplyDelete
  17. It's very useful article with inforamtive and insightful content and i had good experience with this information. We, at the CRS info solutions ,help candidates in acquiring certificates, master interview questions, and prepare brilliant resumes.Go through some helpful and rich content Salesforce Admin syllabus from learn in real time team. This Salesforce Development syllabus is 100% practical and highly worth reading. Recently i have gone through Salesforce Development syllabus and Salesforce Admin syllabus which includes Salesforce training in USA so practically designed.

    ReplyDelete
  18. In the event that a fiat cash, similar to the USD or GBP, gets swelled, its value rises and its buying power drops.Getnode Test

    ReplyDelete
  19. This is my first time visit to your blog and I am very interested in the articles that you serve. Provide enough knowledge for me. Thank you for sharing useful and don't forget, keep sharing useful info: Bitmain antminer S19 Pro price

    ReplyDelete
  20. Amazing post with quality information! Many wonder, how to recover stolen or scammed Bitcoin? Hire a bitcoin recovery expert online

    ReplyDelete
  21. Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. recesión económica

    ReplyDelete
  22. I was surfing the Internet for information and came across your blog. I am impressed by the information you have on this blog. It shows how well you understand this subject. recesión

    ReplyDelete
  23. It was wondering if I could use this write-up on my other website, I will link it back to your website though.Great Thanks. 도메인 가격

    ReplyDelete
  24. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. 도메인 등록 절차

    ReplyDelete
  25. The post is written in very a good manner and it contains many useful information for me. 借錢管道

    ReplyDelete
  26. very interesting post.this is my first time visit here.i found so mmany interesting stuff in your blog especially its discussion..thanks for the post! 小額借錢

    ReplyDelete
  27. It's very useful blog post with informative and insightful content and i had good experience with this information.I have gone through CRS Info Solutions Home which really nice.Find this real time DevOps Training and great teaching. Join now on Selenium Training online course. Upskill career with Tableau training by crs info solutions. Latest trending course is Salesforce Lightning training with excellent jobs.

    ReplyDelete
  28. Bitcoin is an open-source item, available by any individual who is a client. BTC escrow

    ReplyDelete
  29. There will be an area with a box for you to paste a code when you want to send money to someone or to yourself on an exchange or different wallet. accept bitcoin

    ReplyDelete
  30. Amazing post with quality information! Many wonder, how to recover stolen or scammed Bitcoin? bandarqq terpercaya

    ReplyDelete
  31. Forex is my passion, that's why I sincerely love supporting all and sundry to conquer their demanding situations and come to be worthwhile in their very own buying and selling.How to trade forex and cryptocurrency

    ReplyDelete
  32. Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors. how to track and recover stolen bitcoin

    ReplyDelete
  33. A famous enterprise to acquire the most cutting-edge charge in Bitcoin valuation is XE.How to recover your stolen bitcoin

    ReplyDelete
  34. In fact, this evolution is an ongoing process and thus, Play games online and earn Bitcoin human life on earth is improving constantly day in and day out. One of the latest inclusions in this aspect is cryptocurrencies.

    ReplyDelete
  35. Bitcoin' clients are generally terrified by various news occasions including the announcements by government authorities and international occasions that 'Bitcoin' can be directed. bitcoin mixer

    ReplyDelete
  36. Your work is great and I value you and jumping for some more educational posts. Much obliged to you for sharing incredible data to us. Best ethereum cryptocurrency investment website

    ReplyDelete
  37. Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome! What is the best cryptocurrency to invest in 2020

    ReplyDelete
  38. Approaching connects to a site are known as backlinks. seonocurenopay.com

    ReplyDelete
  39. I love your substance, they are outstandingly in general very accommodating to us, and this substance justifies everyone's thought. Canadian bitcoin exchange

    ReplyDelete
  40. magnificentincense.com
    24K Monkey Classic Incense 10g
    AK-47 – X10 / PREMIUM
    Bizarro Incense
    Buy Black Mamba Incense Online
    Buy WTF Herbal Incense
    Cloud9 Mad Hatter Incense
    Crazy Monkey Incense
    k2 spray on paper
    k2 paper sheets
    Klimax Potpourri 15xxx Coconut(10g)
    Crazy Monkey Incense
    Cloud9 Mad Hatter Incense
    Buy Purple Diesel Incense Online
    Buy Pure Fire Herbal Incense Online
    Buy Kisha Cole Incense (11g) online
    Buy KUSH HERBAL INCENSE online
    Buy Mind Trip Incense Online
    Buy Platinum XXX Herbal Incense online
    buy Orange Platinum Caution 10G
    Buy OMG HERBAL POTPOURRI 10G online

    ReplyDelete
  41. I have now been engaged with the Web for more than 10 years and, explicitly, with the SEO industry for very nearly 4 years and I've seen the internal operations of major SEO organizations and worked with customers who had been singed by their past SEO crusades. Webdesign

    ReplyDelete
  42. Thanks for sharing What an interesting site you have i really take a good look buy cocaine online and you can also take a good look at our blog too for more information buy crack cocaine online we also recommend you to take a good look at this site too they have good content buy mdma crystals online more of the site still want you to take a closer look too as they have really good content buy crystal meth online thanks for using your time to check on the link .order cocaine online

    ReplyDelete
  43. Hi there to everyone, the contents present at this web page are actually amazing for people knowledge, well, you can also visit Dogecoin Price Prediction for more CryptoNewsBytes related information and knowledge. Keep up the good work.

    ReplyDelete
  44. Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. how to earn bitcoins on android

    ReplyDelete
  45. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. farmacia online bitcoin españa

    ReplyDelete
  46. https://k2incenseonlineheadshop.com/
    k2incenseonlineheadshop
    info@k2incenseonlineheadshop.com
    Buy liquid incense cheap
    Buy liquid incense cheap For Sale At The Best Incense Online Shop
    K2 Spice Spray | Liquid K2 | Liquid Spice | K2 Spray for sale
    https://k2herbalblendshop.com/

    ReplyDelete
  47. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. 토토사이트

    ReplyDelete
  48. https://bestmjstrainsonline.com/

    Buy Liquid Incense Cheap
    Buy K2 E-Liquid online
    Cheap Herbal incense

    Buy K2 Infused paper spray online, order K2 Spray online +1 (925) 526-5453

    https://bestmjstrainsonline.com/product/5-co2-cannabis-oil-cartridges/



    ReplyDelete
  49. Everyone needs something new now and again but then some need a fix of something very surprising. SEO Antwerpen

    ReplyDelete
  50. The Bitcoin information which you have provided is very good and essential for everyone. Keep sharing this kind of information. Thank you. How to buy a Bitcoin in Canada

    ReplyDelete
  51. howdy, your websites are really good. I appreciate your work. 토토사이트

    ReplyDelete
  52. This particular is usually apparently essential and moreover outstanding truth along with for sure fair-minded and moreover admittedly useful My business is looking to find in advance designed for this specific useful stuffs… How to invest in cryptocurrency

    ReplyDelete


  53. https://buydocumentation.com/

    Buy Driver’s License online, US driver license, drivers license renewal ca WhatsApp +1 (925) 526-5453


    Here at Buy Documentation, you can buy fake documents at a fraction of a cost. We keep our prices low to meet the needs of all our customers. We provide Best Quality Novelty Real and Fake IDs and Passport, Marriage Certificates and Drivers license online

    https://buydocumentation.com/canada-visa/

    https://buydocumentation.com/buy-real-drivers-license/

    https://buydocumentation.com/buy-real-id-cards/

    https://buydocumentation.com/social-security-number-ssn/

    ReplyDelete
  54. Extremely useful information which you have shared here. This is a great way to enhance knowledge for us, and also helpful for us. Thankful to you for sharing an article like this. Trade Bitcoin Online

    ReplyDelete
  55. You have worked nicely with your insights that makes our work easy.How to buy cryptocurrency in Canada The information you have provided is really factual and significant for us. Keep sharing these types of article, Thank you.

    ReplyDelete
  56. Loja de internet - comprar sapatos de saltos altos sensuais em portugal e brasil www.saltos-altos.com

    ReplyDelete
  57. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Asia Gaming

    ReplyDelete
  58. This article gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice article. เว็บยูฟ่าเบทดีอย่างไร

    ReplyDelete
  59. Apply for membership into the Joker Slot system, the best online slots website, deposit, withdraw, auto 10 seconds, Joker Gaming website, all slots sites, must be here, Joker lot, download app, play on mobile, apply now and receive promotions by Here jokerslot

    ReplyDelete
  60. This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post.! best Spanish Translation Services service provider.

    ReplyDelete
  61. อีกทั้งเรายังให้บริการ เกมสล็อต ยิงปลา แทงบอลออนไลน์ รองรับทุกการใช้งานในอุปกรณ์ต่าง ๆ HTML5 คอมพิวเตอร์ แท็บเล็ต สมาทโฟน คาสิโนออนไลน์ และมือถือทุกรุ่น เล่นได้ตลอด 24ชม. ไม่ต้อง Downloads เกมส์ให้ยุ่งยาก ด้วยระบบที่เสถียรที่สุดในประเทศไทย

    ReplyDelete
  62. You delivered such an impressive piece to read, giving every subject enlightenment for us to gain information. Thanks for sharing such information with us due to which my several concepts have been cleared. 슈어맨

    ReplyDelete
  63. If more people that write articles really concerned themselves with writing great content like you, more readers would be interested in their writings. Thank you for caring about your content. allbet

    ReplyDelete
  64. Wonderful article. Fascinating to read. I love to read such an excellent article. Thanks! It has made my task more and extra easy. Keep rocking. 토토커뮤니티

    ReplyDelete
  65. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. ทดลอง เล่น สล็อต ค่าย pp

    ReplyDelete
  66. You delivered such an impressive piece to read, giving every subject enlightenment for us to gain information. Thanks for sharing such information with us due to which my several concepts have been cleared. 마추자

    ReplyDelete
  67. Never thought about it, right? It's time to think because in this post we are going to provide you a four-step guide on creating your own cryptocurrency. https://binorcoin.com/

    ReplyDelete
  68. Impressive and powerful suggestion by the author of this blog are really helpful to me. Bittrex Review

    ReplyDelete
  69. I found your blog on Google and read a few of your other posts too, it is very informative and also influencing me to read more of yours. You have done an exceptional job by adding your hardwork and dedication to it. I am a big fan of art and craft. I just added you to my bookmarks and recommending your blog to my social profiles too. Just like the other budding bloggers i have started my blog, You can support me by visiting my site for more paper airplane related information and knowledge, Keep up the great work Look forward to reading more from you in the future. Thanks in anticipation.
    how to make a paper airplane that flies far and straight | classic dart paper airplane | how to make a good paper | nakamura lock paper airplane instructions | best paper airplane design for distance and accuracy | best paper airplane design for distance and speed | Boomerang paper airplane instructions | Zazoom internet | Eagle paper airplane | how to make a paper airplane that flies far and straight step by step | 4 aerodynamic forces | Paper airplane templates for distance | the eagle paper airplane | best paper plane in the world | How to make a successful paper airplane

    ReplyDelete

  70. herbal incense 2021,
    trusted herbal incense sites 2021,
    k2 liquid incense,
    order cheap herbal incense,
    cheap strong herbal incense,
    super strong herbal incense liquid,
    most potent herbal incense on the market,
    best herbal incense website 2021,
    k2 liquid spray on paper for sale,
    where to buy liquid herbal incense,
    buy herbal incense with debit card,
    buy herbal incense overnight shipping,
    strongest liquid incense,
    buy herbal incense online cash on delivery,
    liquid herbal incense 2020,
    k2 spray that get you high for sale,
    buy herbal net


    https://k2herbalspice.com/
    Call Us: (+1) 747-500-1520
    Email: sales@k2herbalspice.com



    herbal incense 2021,
    trusted herbal incense sites 2021,
    k2 liquid incense,
    order cheap herbal incense,
    cheap strong herbal incense,
    super strong herbal incense liquid,
    most potent herbal incense on the market,
    best herbal incense website 2021,
    k2 liquid spray on paper for sale,
    where to buy liquid herbal incense,
    buy herbal incense with debit card,
    buy herbal incense overnight shipping,
    strongest liquid incense,
    buy herbal incense online cash on delivery,
    liquid herbal incense 2020,
    k2 spray that get you high for sale,
    buy herbal net


    website: https://herbalincensespices.com/
    Call Us: +1 (312)-319-2341
    Email: sales@herbalincensespices.com


    We are located in USA AND UK. Buy High Quality Grade(%99.96) Mephedrone (2- methylamino 1-one) also known as 4-methylmethcathinone(4-MMC).We are one of the top suppliers in wholesale and retail of Benzodiazepine,synthetic cannabinoids, Blotters chemicals. Mephedrone and other plants research chemicals such as: Amphetamines Pentylone Mephedrone Flephedrone Bulytone (bk-MBDB) MDAI Analgesic Chemical CB1 and CB2 CP 47497 CP-55940 HU-210 HU-331 Ephedrine Hcl Powder JWH-018 / JWH-200,JWH-250 TFMPP 2C-E, 2C-I, 2C-P, 2C-C, 2C-T-2 DOC, DOI Bromo DragonFly TCB-2 Testosterone 5-Meo-DMT 4-Aco-DMT 4-Ho-MIPT 4-Meo-PCP Naphyrone HEROIN. Methylone (bk-MDMA) Ethylone , Oxycodone Flephedrone (4-FMC, 4-Fluoromethcathinone) Methedrone (BK-PMMA, Methoxyphedrine) 4-Fluoromethamphetamine (4-FMA) a-Pyrrolidinopropiophenone (a-PPP). MDPV (Methylenedioxypyrovalerone, MDPK) Testosterone JWH-073, 1-butyl-3-(1-naphthoyl)indole Hydrocodone Dimethocaine (Larocaine/DMC) Morphine JWH-018, 1-Pentyl-3-(1-naphthoyl)indole Herione 4-Fluoroamphetamine (4-FA, 4-FMP, or Flux) Ketamine Our quality is the best you can find around and we sell in small/large quantities with guaranteed discreet delivery in good time Shipping

    website: https://uslegitresearchchemicals.com/
    Call Us: +1 760-573-6020
    Email: info@uslegitresearchchemicals.com

    ReplyDelete
  71. Sometimes blogs were goes away from the topic what actually mentioned. But this is not like that. Thanks for sharing this.
    AWS Certification in Chennai
    DevOps Course in Chennai

    ReplyDelete
  72. You're amazing since the article you've written is both engaging and informative. Thank you for sharing that. Keep up the good work. Bitmain Antminer L7

    ReplyDelete
  73. I'm glad to see the great detail here!. Investasi bitcoin

    ReplyDelete
  74. It’s actually a great and helpful piece of info. I’m happy that you just shared this helpful info with us. Please keep us informed like this. Thanks for sharing! Best antminer l7 service provider.

    ReplyDelete
  75. I am very thankful to you that you have shared this information with us. I got some different kind of knowledge from your web page, and it is really helpful for everyone. Thanks for share it. Crypto Airdrops 2021

    ReplyDelete
  76. This is really a good source of information, I will often follow it to know more information and expand my knowledge, I think everyone should know it, thanks Best antminer l7 service provider.

    ReplyDelete

Post a Comment

Popular posts from this blog

Android Architecture Components : LiveData

Android Data Binding : Dynamic RecyclerView Adapter