Use of JavaScript for Blockchain Projects

Posted by Webbylab Technical Writer on 2018-03-22

The concept of the blockchain is most often mentioned along the topic of crypto-currency, although it can find a much wider application. Why? Let us try to discover it. In fact, a blockchain is a certain distributed database that interacts with a dynamic list of ordered records. The main idea is that existing records cannot be modified and new ones require a specific verification to be accepted. The most popular blockchain implementations were created for Bitcoin and Ethereum. Blockchain is closely related to e-money transfers and crypto-currency transactions, smart contracts, supply chain logistics, etc.

From the technical point of view, each block can be presented by five components (this is the base model, with the minimum necessary data set): index, mark, data, hash and hash of the previous block. That is why the implementation of the basic blockchain system is quite simple, and can be put into 200 lines. The key element of such a structure is security, which is ensured by:

  • well-thought organization of access rights;
  • encryption keys generated using the highest security standards;
  • the “right” programming language.

JavaScript for Blockchain Projects

Is JavaScript good for blockchain? Year by year, and from release to release, the universality of this programming language grows. Originally designed for the creation of front-ends, currently, along with PHP, JS is used to build backends and even mobile apps. But this is not all the spheres of its application - given the rapid pace of development, JS is increasingly being employed for the blockchain projects.

JavaScript Tools for Blockchain

As for our team, we use JavaScript to create projects that employ blockchain, because we consider it to be an incredibly advanced language, which, in addition, has a huge community support and provides easy interaction with blockchain security tools such as Mythril, a framework that helps checking the created decentralized apps for security vulnerabilities. Blockchain systems often attract the attention of malefactors, thus, ensuring the security of your app is a must. An interesting example of such testing is provided by the Mythril developer in this detailed manual. Below we present to your attention a number of software tools for creating a blockchain-related projects using JS.

Truffle

Using JavaScript for blockchain-based projects development, you will definitely need to select some kind of development environment. Among environments that support the creation of solutions for the smart contracts - Embark, Dapple, and Truffle, we chose the latter. Why? Firstly, it provides an extremely flexible approach to the creation of a blockchain system and, in addition, is easy to master (an interactive console is used for work). Secondly, due to the powerful built-in testing framework, Truffle supports the generation of automatic tests (which cannot be avoided when creating a code basis for smart contracts) and provides access to the NPM and EthPM package managers. And last but not least - it is impossible not to mention the performance of this framework. Regardless of what you do, Truffle will use intelligent optimization, speeding up the execution of worker threads.

Web3.js

Web3.js is the API for Ethereum, available with npm or bower, which implements the specification of the common JSON RPC. Being a JS library, Web3.js allows addressing the Ethereum API from a regular JavaScript code, thus simplifying the usage of JavaScript for implementing blockchain-based projects. In fact, with its help you can connect to the node, after which something alike the Geth console becomes available in a browser.

Metamask

Metamask is a specialized Chrome plugin, which is a bridge between blockchain nodes and the browser, thus providing opportunities to use Web3.js. Metamask works very simply - it attaches this library to each page directly. In turn Web3.js automatically connects to Metamask RPC servers. As a result, you get full access to the Ethereum functionality through the JS scripts embedded into the site’s pages.

Swarm and IPFS

Swarm and IPFS are two slightly different decentralized content storage and delivery technologies that use a peer-to-peer protocol developed for Ethereum - ERC20. In fact, both depositories use multiple computers via an internet connection to create a kind of public cloud storage outfitted with the benefits of blockchain (security, anonymity and trustability) and content delivery network (the content is shipped to the requester from a node that is closest to them). In the case of blockchain systems, they also allow providing the JSON interface to ABI, which is necessary for the implementation of smart contracts.

As for the key differences between Swarm and IPFS, they are as follows:

  • SWARM can be used as a cloud hosting;
  • SWARM was developed as part of the Ethereum ecosystem, which means that it provides the developer with optimal opportunities for deeper interaction with this blockchain than IPFS;
  • SWARM is a part of the Ethereum Foundation tech stack, while IPFS is developed and supported by the Protocol Labs company, which recently got a lot of attention after their crypto-currency’s (Filecoin) closed ICO broke an all-time record and collected USD 257 million;
  • IPFS scales better;
  • IPFS has a much larger number of users due to its time validity.

If you need a more detailed analysis of their similarities and differences, you can read more on this at Github.

CCXT

The ccxt library is a set of software tools that are designed to support the integration with cryptocurrency exchanges and can be implemented at any ecommerce site. It provides a unified API consisting of public and private parts. Public API makes it possible to discover the market data, design diagrams that display the exchange rate plots, and also review the trade history. In turn, the private API is used to trade the currencies, for which you will also need the user credibilities for particular markets. To date, this solution supports more than 102 cryptocurrency exchanges and is available for three languages: JS, PHP and Python. Ccxt is easily installed using npm.

Blockchain Development in JavaScript: Examples

Below we propose to consider a couple of examples of the JavaScript solutions for blockchain, which you can use as sources for inspiration.

Gekko is an open source trading bot with a graphical web interface that integrates easily with the 24 most popular cryptocurrency exchanges, supports the creation of custom flexible trading strategies, has an extensive plugin repository and versions for all the leading desktop OSes - Windows, Linux, and macOS.

KryptoWar is a simple but captivating wargame in which players build their armies and fight each other to earn experience and resources, and become the strongest warlord on the blockchain.

LightWallet is a lightweight implementation of cryptocurrency wallet that can work both locally and remotely. Moreover, thanks to the in-built private key storage, LightWallet also supports running Ethereum Dapps without hosting the local node.

Educational Projects: Learn the Principles Behind Blockchain

The projects described below are greatly documented and commented, providing the possibility to understand the blockchain mechanics better.

SavjeeCoin, being a simple and intuitive implementation of the blockchain, can serve a template or a learning example.

Blockchain demo is also a basic interpretation of the blockchain, which can be used for the purpose of study. In order to make a viable solution out of this blockchain system, you will need to take great care of security and additional functionality.

Naivechain is also an excellent option for getting the hands-on acquaintance with the blockchain, which employs the HTTP and Websockets interfaces to manage data connections between nodes.

Summary

As you can see, using such an advanced software tools and JavaScript that is familiar to any web developer, building blockchain-based projects and distributed applications (smart contracts) is not difficult. However,if you are new to software development, it is better not to take risks (after all, we are talking about the software that would potentially serve a notable money transfers) and entrust this procedure to real professionals. Pay special attention to ensuring the proper level of security and contract a team of experienced pentesters. After all, the openness of the created solution is not only an opportunity to provide your code for general evaluation, but also, in fact, a wonderful chance for network scammers to take advantage of probable vulnerabilities.

Use the form below to subscribe and receive the best insights and tutorials right to the messenger of your choice.


Comments: