First, we’ll need to install the CryptoCoinJs library. We’ll use npm (Node Package Manager) which comes with node.js, to install the library.
Let’s check if we have node.js install on our system by typing the following in our terminal/cmd:
generating a bitcoin address in javascript
Now, let’s create a new project directory, and make it our working directory
generating a bitcoin address in javascript
mkdir Bitcoin
cd Bitcoin
mkdir Bitcoin
cd Bitcoin
To install the library, type the following in your terminal/cmd:
generating a bitcoin address in javascript
npm i coinkey
npm i coinkey
Note: You will need to have your python version match one of the compatible versions listed in the instructions above if you encounter the node-gyp issue.
Another common issue is a stale cache; clear your npm cache by simply typing the below into your terminal:
generating a bitcoin address in javascript
npm cache clean
npm cache clean
If everything goes right, CryptoCoinJSwill be installed on your system.
Now open a text editor of your choice and create a new javascript file address.js, and copy-paste the following in it:
generating a bitcoin address in javascript
var CoinKey = require('coinkey');
var wallet = new CoinKey.createRandom();
console.log("SAVE BUT DO NOT SHARE THIS:", wallet.privateKey.toString('hex'));
console.log("Address:", wallet.publicAddress);
var CoinKey = require('coinkey');
var wallet = new CoinKey.createRandom();
console.log("SAVE BUT DO NOT SHARE THIS:", wallet.privateKey.toString('hex'));
console.log("Address:", wallet.publicAddress);
var CoinKey = require('coinkey');
var wallet = new CoinKey.createRandom();
console.log("SAVE BUT DO NOT SHARE THIS:", wallet.privateKey.toString('hex'));
console.log("Address:", wallet.publicAddress);
Explanation of the code above
Line 1: Importing the CryptoCoinJS library.
Line 2: Creating a random address with keys using Coinkey.createRandom method and storing it in the wallet variable.
Line3: Printing the private key to the console along with a warning.
Line4: Printing the address to the console along with a string.
Save the script file and run it using the following command.
generating a bitcoin address in javascript
node address
node address
It will give an output that is similar to this.