eckey
JavaScript component for Elliptical curve cryptography for crypto currencies such as Bitcoin, Litecoin, Dogecoin, etc.
Why?
This module provides a convenient way to compute relevant crypto currency operations that adhere to elliptical curve cryptography. To
really understand how private keys, public keys, addresses, and how elliptical curve cryptography works with JavaScript, read this: http://procbits.com/2013/08/27/generating-a-bitcoin-address-with-javascript
Installation
npm install --save eckey
Usage
API
ECKey([bytes], [compressed])
Constructor function.
- bytes: The private key bytes. Must be 32 bytes in length. Should be an
Array
, Uint8Array
, or a Buffer
. - compressed: Specify whether the key should be compressed or not.
var ECKey = require('eckey');
var secureRandom = require('secure-random');
var bytes = secureRandom(32);
var key1 = new ECKey(bytes);
var key2 = ECKey(bytes);
var compressedKey = new ECKey(bytes, true);
Note: Previous versions of this module would generate a random array of bytes for you if you didn't pass as input any to the constructor. This behavior has been removed to remove complexity and to ensure that the random generation is done securely. In the past, it wasn't.
compressed
Get/set whether the point on the curve is compressed. Affects the output of the WIF (wallet import format) and the address.
privateKey
Get/set the private key. When setting, the type can be either an Array
, Buffer
, or Uint8Array
. When getting, the type is always Buffer
. Setting would be useful if you don't pass a private key to the constructor.
var ECKey = require('eckey');
var conv = require('binstring');
var privateKeyHex = "1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd";
var key = new ECKey(conv(privateKeyHex, {in: 'hex', out: 'buffer'}), false);
console.log(key.privatekey.toString('hex'));
var keyCompressed = ECKey(conv(privateKeyHex, {in: 'hex', out: 'buffer'}), true);
console.log(key.privatekey.toString('hex'));
privateExportKey
Get the private key along with a byte for compression if compressed
is true. i.e.
if compressed
privateExportKey = privateKey + 0x01
else
privateExportKey = privateKey
This is useful inconjunction with the package coinstring to generate
Wallet Import Format keys.
var ECKey = require('eckey');
var conv = require('binstring');
var privateKeyHex = "1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd";
var key = new ECKey(conv(privateKeyHex, {in: 'hex', out: 'buffer'}), false);
console.log(key.privateExportKey.toString('hex'));
var keyCompressed = new ECKey(conv(privateKeyHex, {in: 'hex', out: 'buffer'}), true);
console.log(key.privateExportKey.toString('hex'));
publicKey
Get the public key. The type is Buffer
.
var ECKey = require('eckey');
var conv = require('binstring');
var privateKeyHex = "1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd";
var key = new ECKey(conv(privateKeyHex, {in: 'hex', out: 'buffer'}), false);
console.log(key.publickey.toString('hex'));
var keyCompressed = ECKey(conv(privateKeyHex, {in: 'hex', out: 'buffer'}), true);
console.log(key.publickey.toString('hex'));
publicPoint
Get the Public Key Point on the Ellipitical Curve.
toString()
Returns the string representation of the private key.
var ECKey = require('eckey');
var conv = require('binstring');
var privateKeyHex = "1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd";
var key = new ECKey(conv(privateKeyHex, {in: 'hex', out: 'buffer'}), true);
console.log(key.toString())
References