What is private and public key and how they are related to blockchain addresses
When we would like to have a bank account, we have to find any bank office, talk with the sales executive, give him our ID and some more documents, next wait some time until bank employee will create a contract for us, and a bank will create an account and generate bank account number.
After this whole procedure, we can use our bank account and making finance actions like bank transfers.
However, when we want to do bank transfer we have to wait a long time until receiver will get money(with abroad transfers sometimes even a few days).
We can solve it with blockchain transactions, which are ultra fast (very often funds are on a different account in a few seconds).
If we want to create a blockchain account, we can find a wallet which will handle selected blockchain, connect with it, and create an account. As a result, we will get back, private key, public key, and account address but what next, what are these keys, what is blockchain account address and why we need them?
Now I will try to example you what they are, why we need all 3 of them, and what is the procedure of creating them in the more detailed way.
What is private key and how to create it in Javascript
A private key is a secret number(in blockchain most of them are 256bit long) which you can use to let you send money from your account/manage your funds, generate public keys and addresses.
What is important you never should share private key with nobody, with private key you can create your public key and address.
To make it simpler, we will use Ethereum blockchain as an example.
Ethereum to generate private keys is basing on EC(elliptic curve) ecp256k1 and what is interesting every number from 1 to ²²⁵⁶-1 could be a correct private key.
Let’s check some same code how we can create the simple private key with node.js:
var crypto = require(‘crypto’);
var privateKey = crypto.randomBytes(32);
console.log(privateKey.toString(‘hex’));
//369931d88354c0437a753adb1a373b95d0c44b2874dde134937725a7e819d7c9
To do this, we used the node.js crypto library and generated random 32 bytes-length key.
Next when we would like to see it in a human readable way we need to use method toString, and specify ‚hex’ type.
That’s it. We have own first blockchain-type privateKey!
What is public key and how to create it from private key
Now we know how what is a private key, how it works and what is the most important we shouldn’t share a private key to anybody, never, never!
However, what when we will want to share somebody key which will confirm our authenticity?
We need to create a public key.
A public key is enormous integer number represented by digits and letters, we can create a public key from the private key, but we cannot revert this operation, it means you cannot generate a private key from the public key.
To generate public key we need to use advanced math like elliptic curves, below is guide how to step by step create a public key from the private key in javascript:
1. We need to create new elliptic with the secp256k1 standard.
2. Next, we will create a generator point
3. Define a private key as a big number
5. Define public point by creating generator point multiplication
6. Create x and y coords of the public point
7. Just create a buffer of x and y concatenation.
8. That’s it! We have our public key.
So here is the code:
var crypto = require(‘crypto’);
var ellipticLib = require(‘elliptic’).ec;
var bigNumber = require(‘bn.js’);
// Generate Private Key
var privateKey = crypto.randomBytes(32);
console.log(‘privateKey: ‘, privateKey.toString(‘hex’));
// privateKey: 67f7f1ff9431e7c7ddaff18b3db344493de4d12c445bcae3fe2fe7091b477913
// Generate Public Key
var elliptic = new ellipticLib(‘secp256k1’);
// Here we define generator point
var generatorPoint = elliptic.g;
// Next we will change private key to big number
var bigNumberPrivateKey = new bigNumber(privateKey);
// Here we have to create generator point multiplication for define public point
var publicPoint = generatorPoint.mul(bigNumberPrivateKey);
// Next we will create x coords of public point
var x = publicPoint.getX().toBuffer();
// Next we will create y coords of public point
var y = publicPoint.getY().toBuffer();
// And we will create public key by creating buff of concat from x and y coords of the public point
var publicKey = Buffer.concat([x,y]);
console.log(‘publicKey: ‘, publicKey.toString(‘hex’))
// publicKey: bb98c1d8e489ffdf4d8752e6fb5357954471c52f76d7b35384556214605fb7a82305c64d26e8cb43fc6c64ff80a4af644033cb0c8f9c77e4749fbd7963e2b3a6
What is blockchain address and how to create it from public key
When we created a public key, we are kind of secure, because it would be very difficult(almost impossible) and very long operation to find our private key from a public key.
To add more security we will create our blockchain address by adding sha256 typed hash, so now we could be sure nobody will revert all of these operations to get our private key.
Creating blockchain address from the public key is simple, we need to hash our public key with the sha256 standard.
Here is the code:
var crypto = require(‘crypto’);
var ellipticLib = require(‘elliptic’).ec;
var bigNumber = require(‘bn.js’);
// Generate Private Key
var privateKey = crypto.randomBytes(32);
console.log(‘privateKey: ‘, privateKey.toString(‘hex’));
// privateKey: 3f1ac42c37c9726625bfa850902c49054a57f5c1aafc577736d2711048079af3
// Generate Public Key
var elliptic = new ellipticLib(‘secp256k1’);
// Here we define generator point
var generatorPoint = elliptic.g;
// Next we will change private key to big number
var bigNumberPrivateKey = new bigNumber(privateKey);
// Here we have to create generator point multiplication for define public point
var publicPoint = generatorPoint.mul(bigNumberPrivateKey);
// Next we will create x coords of public point
var x = publicPoint.getX().toBuffer();
// Next we will create y coords of public point
var y = publicPoint.getY().toBuffer();
// And we will create public key by creating buff of concat from x and y coords of the public point
var publicKey = Buffer.concat([x,y]);
console.log(‘publicKey: ‘, publicKey.toString(‘hex’));
// publicKey: d9c5797cd271799027a68e9bae2c92a54855940cc292ccaa1b24b264e27461b5e7bdff45918f328f993aa7a9094a59a60aaa0fa9c1bf422ee27a9af3669b82a7
// Generate address from the public key
var address = ‘0x’ + crypto.createHmac(‘sha256’, publicKey).digest(‘hex’);
console.log(‘address:’, address);
// address: 0x2b8dda3a53cae74974ade7600387b31dd3ba825d9b5207d65afb8db0b34b2230
Summary
Now you know what is private key and how important is to keep him safe.
Also, you learned what public key and address are, why we need them and how to create these keys from scratch.
If you have more questions about blockchain, keep reading my and duomly stories.
Thanks for reading,
Radoslaw Fabisiak,
Core developer in Duomly