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. 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
  5. 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
  6. 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
  7. Some truly wonderful work on behalf of the owner of this internet site , perfectly great articles . comprar bitcoin brasil

    ReplyDelete
  8. 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
  9. I learn some new stuff from it too, thanks for sharing your information. url

    ReplyDelete
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. The post is written in very a good manner and it contains many useful information for me. 借錢管道

    ReplyDelete
  18. 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
  19. Bitcoin is an open-source item, available by any individual who is a client. BTC escrow

    ReplyDelete
  20. 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
  21. Amazing post with quality information! Many wonder, how to recover stolen or scammed Bitcoin? bandarqq terpercaya

    ReplyDelete
  22. 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
  23. A famous enterprise to acquire the most cutting-edge charge in Bitcoin valuation is XE.How to recover your stolen bitcoin

    ReplyDelete
  24. 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
  25. 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
  26. 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
  27. 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
  28. Approaching connects to a site are known as backlinks. seonocurenopay.com

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

    ReplyDelete
  30. 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
  31. 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
  32. 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
  33. 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
  34. Everyone needs something new now and again but then some need a fix of something very surprising. SEO Antwerpen

    ReplyDelete
  35. 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
  36. howdy, your websites are really good. I appreciate your work. 토토사이트

    ReplyDelete
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. Impressive and powerful suggestion by the author of this blog are really helpful to me. Bittrex Review

    ReplyDelete
  48. 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
  49. I'm glad to see the great detail here!. Investasi bitcoin

    ReplyDelete
  50. 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
  51. 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
  52. 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
  53. Great article by the great author, it is very massive and informative but still preaches the way to sounds like that it has some beautiful thoughts described so I really appreciate this article. Best forex scam recovery service provider.

    ReplyDelete
  54. Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you Best scammed by online trading service provider.

    ReplyDelete
  55. Thank you so much for sharing this blog with us. It provides a collection of useful information. You obviously put a lot of effort into it! Best top crypto trading platforms service provider.

    ReplyDelete
  56. You've written a fantastic article about  linzhi. This article provided me with some useful knowledge. Thank you for providing this information.

    ReplyDelete
  57. If you don"t mind proceed with this extraordinary work and I anticipate a greater amount of your magnificent blog entries crypto trading bot

    ReplyDelete
  58. Antminer Z15
    https://cryptominingdevices.com/product/bitmain-antminer-z15-zcash-420-ksol-s/

    ReplyDelete
  59. I appreciate your work because i enjoyed reading alot. keep up the good work. Will look forward for more such informative blogs
    Hire Ethereum Developers

    ReplyDelete
  60. 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 new cryptocurrency exchange service provider.

    ReplyDelete
  61. Excellent information, This information will always help everyone for gaining knowledge. So please always share your valuable information. I am very thankful to you for providing good information.  Linzhi

    ReplyDelete
  62. After flirting with the $1,000 value just after the New Year, Bitcoin has been steadily trading at around $950 on the Mt. Gox exchange over the last fortnight and is being nicely supported by the 50 day moving average indicating Bitcoin is still decidedly bullish. Crypto ATM

    ReplyDelete
  63. The information you've provided is useful about buy btc in canada ,because it provides a wealth of knowledge that will be highly beneficial to me. Thank you for sharing that. Keep up the good work.

    ReplyDelete
  64. 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. Crypto liquidity provider

    ReplyDelete
  65. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me. Cryptocurrency investment

    ReplyDelete
  66. After reading the post about iPad pro keyboard case, I got some descriptive information which is very helpful for me. Thanks for posting it. Keep it up. Nokia Cases Covers & Accessories.Global Cryptocurrency Exchange Wallet Online

    ReplyDelete
  67. 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 crypto investment firms service provider.

    ReplyDelete
  68. I'm very impressed with your innovative ideas that are really amazing. You have good knowledge on this topic canadian bitcoin. Keep sharing your amazing article with us. Thanks for sharing your article.

    ReplyDelete
  69. 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 lost money recovery services service provider.

    ReplyDelete
  70. This comment has been removed by the author.

    ReplyDelete
  71. It's very nice of you to share your knowledge through posts. I love to read stories about your experiences. They're very useful and interesting. I am excited to read the next posts. I'm so grateful for all that you've done. Keep plugging. Many viewers like me fancy your writing. Thank you for sharing precious information with us. Best best cryptocurrency trading platform service provider

    ReplyDelete
  72. You've supplied us with an interesting article. This is a fantastic resource for expanding your understanding of the subject Bitcoin Recovery. Thank you very much.

    ReplyDelete
  73. 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 fund recovery services service provider

    ReplyDelete
  74. Great job, this is good information which is shared by you. This information is very meaningful and factual for us to increase our knowledge about it. Always keep sharing this type of information. Thanks. Read more info about rebate forex

    ReplyDelete
  75. This article provided me with a lot of useful information about ic markets copy trading. The material you presented during this post provided me with some excellent information. Continue to post.

    ReplyDelete
  76. 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! goldshell kd2

    ReplyDelete
  77. This blog is completely informative and productive in nature for Blockchain Crypto Mine Machine. All the things mentioned are true.

    ReplyDelete
  78. I am grateful to this blog site providing special as well as useful understanding concerning this subject. crypto mining machines for sale

    ReplyDelete
  79. Hello, this weekend is good for me, since this time i am reading this enormous informative article here at my home. antminer l7

    ReplyDelete
  80. Hello, this weekend is good for me, since this time i am reading this enormous informative article here at my home. avalonminer

    ReplyDelete
  81. DDoS extortion is certainly not a new trick by the hacker community, but there have been several new developments to it. Notable among them is the use of Bitcoin as a method of payment. DD4BC (DDoS for Bitcoin) is a hacker (or hacker group) who has been found to extort victims with DdoS attacks, demanding payment via Bitcoin. DD4BC seems to focus on the gaming and payment processing industries that use Bitcoin. bitcoin profit

    ReplyDelete
  82. buy twitch followers When it comes to pre-ordering, nowadays, you have no choice. In most cases if you don't pre-order your favorite video game, you will not have it on it's release date. Don't go through the same thing that I went through, going from game store to game store trying to find my favorite game because I didn't pre-order!

    ReplyDelete
  83. very nice blog thanks for sharing a valuable information . you make more blogs thanks again .
    Android development companies in chennai
    Mobile app development company chennai

    ReplyDelete
  84. https://hostinglelo.in There are a number of different types of games consoles available to buy online, with some outstanding features and playing capabilities. From the latest models with high definition graphics to take-anywhere handheld models, there is a console for you! Here are some useful hints and tips for things to consider when choosing a games console.

    ReplyDelete
  85. Great article, very useful and explanation. Your post is extremely incredible.That's really awesome blog because i found there lot of valuable Information. crypto events 2022

    ReplyDelete
  86. Your blog is really good. I’m really impressed with your post. Keep sharing your excellent work. Crypto Investment Firms in Dubai

    ReplyDelete
  87. After three years cryptographic forms of money actually stay beneficial, and the market is setting down deep roots. Wazirx referral code

    ReplyDelete
  88. Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks ido crypto

    ReplyDelete
  89. Hey what a brilliant post I have come a cross and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. play to earn crypto metaverse games

    ReplyDelete
  90. I admire this article for the well-researched content and excellent wording. INTRADAY Trading Strategies. 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.

    ReplyDelete
  91. I always check this type of advisory post and I found your article which is related to my interest. This is a great way to increase knowledge for us. Thanks for sharing an article like this.Crypto Exchange

    ReplyDelete
  92. Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share. NFT in Lebanon

    ReplyDelete
  93. This is very informative and interesting for me. Thank you for such a wonderful post about Upcoming Cryptocurrency Events In 2022 and for sharing. God bless you.

    ReplyDelete
  94. Thank you for sharing such a nice post about Germany Crypto Leads Cyprus . I love it so much. Check

    ReplyDelete
  95. Great articles and great layout. Your blog post deserves all of the positive feedback it’s been getting.
    Hire .Net Developers

    ReplyDelete
  96. At that time composing this web site, seventeen zillion bitcoin have been mined which is thought which complete twenty one zillion bitcoin might be mined. Blockchain

    ReplyDelete
  97. This is very interesting, but it is necessary to click on this link: Blockchain instagram

    ReplyDelete
  98. I like this post. I was searching about Forex CPA Leads Cyprus over search engines and found your post and it really helps thank you very much.

    ReplyDelete
  99. You composed this post cautiously which is beneficial for us. I got some different kinds of information from your article and I will suggest reading this article who need this info. Latest nft crypto news USA Thanks for sharing it.

    ReplyDelete
  100. Gains of 1,000% or more are not simply imaginable, they have been normal spot with a large number of these crypto monetary forms. NFT Calendar

    ReplyDelete
  101. You have achieved unbelievable work by circulating this article here. It is useful and beneficial information for us. Keep on overhauling our knowledge by share these sorts of articles.Invest Money To Get Good Returns

    ReplyDelete
  102. This comment has been removed by the author.

    ReplyDelete
  103. Thanks for sharing this informative blog! It will be helpful for those who are looking for this type of data. This means a lot!!
    Keep sharing..

    Bitcoin Mining Machine

    ReplyDelete
  104. Thanks for sharing this blog it was very helpful for my projects Custom ERP Software

    ReplyDelete
  105. Thank you so much for explaining so nicely. I read the above article and I got some knowledge from your article. It's actually great and useful data for us. Thanks for sharing it. Information is pretty good and impressed me a lot.
    Thank you for providing such valuable information about Cryptocurrency Promotion Agency.

    ReplyDelete
  106. You have done such a nice job by publishing such a good post about Cryptocurrency. I got some descriptive information from this post. Thanks for posting such a great article in this post. stock charting tool.

    ReplyDelete

  107. Shreyan IT, helps companies/employers get matched with talented candidates who meet their requirements. We provide staffing solutions for local, national, and global recruitment needs. Our goal is to assist job seekers in finding new positions while also assisting employers in finding the best applicant for their available positions.

    ReplyDelete
  108. Cryptocurrency is an intriguing topic, and your blog post covers it remarkably well! From its history to its impact on the financial world, you've provided valuable insights. Thanks for sharing this informative content! Cryptocurrency Trading Course for Beginners

    ReplyDelete

Post a Comment

Popular posts from this blog

Android Architecture Components : LiveData

Clean Code: Meaningful Names