JavaScript bitcoin cash library

0 78
Avatar for ryosuke
4 years ago

Cryptocurrency is the trend nowadays, everybody is trying to collect them by investing or using faucets or other means, and this latter led web developers to integrate their own wallets in their websites(one of them is read.cash) but how they did that?

There are so many open source libraries created in JavaScript that helps developers to achieve this latter. Today I am going to present you the bitcoin cash JS library that's used for this kind of development so let's proceed

1-What's BitcoinCash.js:

BitcoinCash.js is the first JavaScript library specifically made for Bitcoin Cash. It supports all major Bitcoin Cash use cases right out of the box, keeping up-to-date with the latest network upgrades. This library can be used in the back-end (Node.js) and the front-end (web browsers). It is easy to use and well tested and support advanced features such as multi-sig and message verification.

2- how to install:

All you have to do is install Node.JS and include npm in the PATH then enter the commend below in your cmd

npm install --save bitcoincashjs

3-Include the library in your HTML file:

As we all know all JavaScript code is included in HTML file with the script tag

<script src="path-to-library/bitcoincashjs.0.1.7.min.js"></script>

4- Generate a random Bitcoin Cash address:

All you have to do in this case is to use the PrivateKey class to create an object and generate a privatekey then convert it to an address using the toAddress() function

const bch = require('bitcoincashjs');

const privateKey = new bch.PrivateKey();

const address = privateKey.toAddress();

5-Create a transaction:

To commit transactions you have to create an instance from the Transaction class as the code below shows:

const bch = require('bitcoincashjs');

const privateKey = new bch.PrivateKey('L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy');

const utxo = {

'txId' : '115e8f72f39fad874cfab0deed11a80f24f967a84079fb56ddf53ea02e308986',

'outputIndex' : 0,

'address' : '17XBj6iFEsf8kzDMGQk5ghZipxX49VXuaV',

'script' : '76a91447862fe165e6121af80d5dde1ecb478ed170565b88ac',

'satoshis' : 50000

};

const transaction = new bch.Transaction()

.from(utxo)

.to('1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK', 15000)

.sign(privateKey);

0
$ 0.00
Avatar for ryosuke
4 years ago

Comments