Polkadot Backend API Creation
Polkadot is a next-generation blockchain protocol connecting multiple specialized blockchains into one unified network. Designed as part of a broad vision for a web that returns control to individuals over internet monopolies, Polkadot builds on the revolutionary promise of previous blockchain networks while offering several fundamental advantages.
First things first, if you have never touched Polkadot, I recommend reading their whitepaper here or checking out their medium. Let’s get you up and code if you have a base understanding of Polkadot.
About the project: In this article, I am creating an API that connects the Polkadot test net, generates accounts, Gets balance, and sends transactions on the Polkadot test net called Westend. In this project, we are using Polkadot.js. Polkadot-JS is a collection of tools that interfaces with the Polkadot blockchain in a granular way.
Summary: In this project, we will archive the down below function to create the polka dot backend.
- Blockchain Connection: We are using testnet Westend for this project-testnet Url: wss://westend-rpc.polkadot.io- Account creation (GET): This is a method that is used to generate an account. With this account, we can access information such as Mnemonic, Address, etc. http://ip_address:8081/generatePolkaAddress-Import account (POST): This method is used to import the account with a given Mnemonic parameter.- Transaction (POST): This method is used for transaction purposes. Parameters are the sender mnemonics, receiver address, and amount to be sent. After getting the current info from the network, the transaction object is created and signed with the sender's account. Then this transaction is broadcast to the network.- Balance Check (GET): This method gets the balance of the given address.
Tech Stack: Tech Stack was used in this project.
NodePolkador.jsExpress
Polkadot Accounts
Cryptography
- Hashing Algorithm: The hashing algorithm used in Polkadot is Blake2b.
- Keypairs and signing: Polkadot uses Schnorrkel/Ristretto x25519 (“sr25519”) as its key derivation and signing algorithm.
- Address Type: The address format used in Substrate-based chains is SS58. SS58 is a modification of Base-58-check from Bitcoin with some minor changes.
Address type prefix that identifies an address as belonging to a specific network.- Polkadot addresses always start with the number 1.- Kusama addresses always start with a capital letter, such as C D, F, G, H, J.- Generic Substrate addresses always start with the number 5- In this project we are creating Polka , Kusama and Generic account.
Prerequisites
NodeJS
To run this API, NodeJS should be installed in the running environment. Please follow these steps:
- Download the installer from NodeJS WebSite https://nodejs.org
- Run the installer.
- Follow the installer steps, agree on the license agreement, and click the following button.
Now, test NodeJS by printing its version using the following command in Command Prompt:
node -v // to check node version, use v16.14.0npm -v // to check npm version
Step 1: The project runs as follows step:
Clone Project- Clone the repository, open a terminal and paste below command.git clone https://github.com/Achinta123/polkadot_backendGo to the project folder, and run this command in the project folder in order to install npm packages.npm install
- Once the project is cloned and installed, this command should be run in the project location in the terminal.
npm run node
Step 2: Connect Postman
- Postman collection: https://github.com/Achinta123/Blockchian-Base-Soical-Platform
- Open the postman and send a post request to create our first accounts.
Step 3: Create two accounts and fund one account.
Adding balance to your account.Click on below linkhttps://app.element.io/#/room/#westend_faucet:matrix.orgCreate account and signInType and send "!drip your_account_address"1 WND will we added in your accountMy account details :Account1 DetailsAddress 1 : 5GWpWvJEDfpTNLokdVYh7S1W2MAA8pi1ummy76LecnH4qhM6Mnemonics : “exotic ski jazz network mobile unit subway slim rebel garbage milk fringe”Account 2 DetailsAddress : 5ERPedz5k3PyiQMgYNHn8NtGu2q3XKT7i7EuPNFKpgNYet8CMnemonics : “together behave vivid layer attend exile segment analyst ostrich give group little”
Step 4. Decrypting & Encrypting:
- Decrypting the addresses using sr25519.
- Encrypting the public key using sr25519.
Step 5: Balance Check :
Using postman ( http://localhost:8081/getBalance/Type account_address)
Step 6: Transfer Coin
Send a post request for the transaction. http://localhost:8081/getBalance/createPolkaTrx
Sidenote: most BN operations have a “n” version, like div and divn, mul and muln. Theis expects a number, while the standard a BN instance.
Now, the only line that might seem strange is.muln(110).divn(100) where we seem to multiply our final value by 110 and divide by 100, which is effectively multiplying it with 1.1. This was taken directly from the Polkadot.js source code, which is an excellent resource that we’ll be exploring for using the API.
- Check transaction details on Westend Explorer. Paste transaction id in it
Explorer: https://westend.subscan.io/
Github link: https://github.com/Achinta123/polkadot_backend.git
Contact: achintad171@gmail.com