Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

coinkey

Package Overview
Dependencies
Maintainers
4
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

coinkey - npm Package Compare versions

Comparing version 0.1.0 to 1.0.0

.min-wd

17

CHANGELOG.md

@@ -0,1 +1,18 @@

1.0.0 / 2014-06-07
-------------------------
* added `mochify` as a dev dep
* removed semicolons per http://cryptocoinjs.com/about/contributing/#semicolons
* removed `terst` and moved to `assert`
* upgraded `"eckey": "~0.4.0"` to `"eckey": "^0.6.0"`
* deleted fields `pubKeyHash` and alias `publicHash` because they're now present in inherited `ECKey`
* upgraded `"coinstring": "~0.2.0"` to `"coinstring": "^1.0.1"`
* upgraded `"secure-random": "~0.2.0"` to `"secure-random": "^1.0.0"`
* removed `CoinKey.compressByDefault`, default is now `true`
* removed ability for `CoinKey` constructor to generate random key when `privateKey` aren't passed
* removed `binstring` dev dep
* added Travis CI
* added Coveralls
* added new class method `createRandom()`
* changed constructor signature from `CoinKey([privateKey], [compresssed], [versions])` to `CoinKey(privateKey, [versions])`
0.1.0 / 2014-03-10

@@ -2,0 +19,0 @@ ------------------

111

lib/coinkey.js

@@ -1,49 +0,22 @@

var util = require('util');
var util = require('util')
var assert = require('assert')
var ECKey = require('eckey');
var coinstring = require('coinstring');
var secureRandom = require('secure-random');
var ECKey = require('eckey')
var cs = require('coinstring')
var secureRandom = require('secure-random')
var hashing = coinstring.hashing; //non-standard
var DEFAULT_VERSIONS = {public: 0x0, private: 0x80}
module.exports = CoinKey
function CoinKey (privateKey, versions) {
if (!(this instanceof CoinKey)) return new CoinKey(privateKey, versions)
function CoinKey (bytes, compressed, versions) {
if (!(this instanceof CoinKey)) return new CoinKey(bytes, compressed, versions);
if (!Array.isArray(privateKey) && !(privateKey instanceof Uint8Array) && !Buffer.isBuffer(privateKey)) //must be Arrayish
throw new Error('Must pass a private key')
this._versions = versions || JSON.parse(JSON.stringify(DEFAULT_VERSIONS))
var DEF_VERSIONS = {public: 0x0, private: 0x80};
this._versions = versions || DEF_VERSIONS;
if (arguments.length === 3) {
ECKey.call(this, bytes, compressed);
} else if (arguments.length < 3 && arguments.length > 0) {
var args = [].slice.call(arguments);
var newBytes = null, newCompressed = CoinKey.compressByDefault, newVersions = DEF_VERSIONS;
for (var i = 0; i < args.length; ++i) {
if (args[i]['length'] != null) //Array or Buffer
newBytes = args[i];
else if (typeof args[i] == 'object' && typeof args[i]['length'] == 'undefined')
newVersions = args[i];
else if (typeof args[i] == 'boolean')
newCompressed = args[i];
}
if (newBytes == null)
newBytes = secureRandom(32);
ECKey.call(this, newBytes, newCompressed);
this._versions = newVersions || DEF_VERSIONS;
} else { // no args
bytes = secureRandom(32);
compressed = CoinKey.compressByDefault || false;
ECKey.call(this, bytes, compressed);
}
ECKey.call(this, privateKey, true) // true => default compressed
}
util.inherits(CoinKey, ECKey);
util.inherits(CoinKey, ECKey)
//Static Props
CoinKey.compressByDefault = false;
//Instance props

@@ -54,6 +27,6 @@

get: function() {
return this._versions;
return this._versions
},
set: function(versions) {
this._versions = versions;
this._versions = versions
}

@@ -64,3 +37,3 @@ })

get: function() {
return coinstring(this.versions.private, this.privateExportKey);
return cs.encode(this.privateExportKey, this.versions.private)
}

@@ -71,45 +44,33 @@ })

get: function() {
return coinstring(this.versions.public, this.pubKeyHash);
return cs.encode(this.pubKeyHash, this.versions.public)
}
})
Object.defineProperty(CoinKey.prototype, 'pubKeyHash', {
get: function() {
if (!this._pubKeyHash)
this._pubKeyHash = hashing.sha256ripe160(this.publicKey, {in: 'buffer', out: 'buffer'}); //sha256ripe160 should default on buffer, fix
return this._pubKeyHash;
}
})
CoinKey.prototype.toString = function() {
return this.privateWif + ': ' + this.publicAddress
}
Object.defineProperty(CoinKey.prototype, 'publicHash', {
get: function() {
return this.pubKeyHash;
}
})
//Class methods
CoinKey.fromWif = function(wif, versions) { //this may make more sense to put in the constructor
var res = coinstring.decode(wif);
var bytes = res.bytes
var compressed = (res.bytes.length === 33);
if (compressed) bytes = bytes.slice(0, 32); //slice off compression byte
var res = cs.decode(wif)
var privateKey = res.payload
var compressed = (res.payload.length === 33)
if (compressed) privateKey = privateKey.slice(0, 32) //slice off compression byte
var v = versions || {};
v.private = v.private || res.version;
v.public = v.public || v.private - 0x80;
var v = versions || {}
v.private = v.private || res.version.readUInt8(0)
v.public = v.public || v.private - 0x80
var ck = new CoinKey(bytes, compressed, v);
return ck;
var ck = new CoinKey(privateKey, v)
ck.compressed = compressed
return ck
}
CoinKey.createRandom = function(versions) {
var privateKey = secureRandom.randomBuffer(32)
return new CoinKey(privateKey, versions)
}
module.exports = CoinKey
{
"name": "coinkey",
"version": "0.1.0",
"version": "1.0.0",
"description": "JavaScript component for private keys, public keys, and addresess for crypto currencies such as Bitcoin, Litecoin, and Dogecoin",

@@ -14,5 +14,7 @@ "keywords": [

"devDependencies": {
"mocha": "1.*",
"terst": "~0.1.0",
"binstring": "~0.2.0"
"mocha": "~1.17.1",
"mochify": "~0.5.0",
"istanbul": "^0.2.10",
"coveralls": "^2.10.0",
"mocha-lcov-reporter": "0.0.1"
},

@@ -25,6 +27,12 @@ "repository": {

"dependencies": {
"coinstring": "~0.2.0",
"eckey": "~0.4.0",
"secure-random": "~0.2.0"
"coinstring": "^1.0.1",
"secure-random": "^1.0.0",
"eckey": "^0.6.0"
},
"scripts": {
"test": "mocha --ui bdd",
"unit": "./node_modules/.bin/mocha",
"coverage": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --reporter list test/*.js",
"coveralls": "npm run-script coverage && node ./node_modules/.bin/coveralls < coverage/lcov.info"
}
}
coinkey
=======
JavaScript component for private keys, public keys, and addresess for crypto currencies such as Bitcoin, Litecoin, and Dogecoin.
[![build status](https://secure.travis-ci.org/cryptocoinjs/coinkey.png)](http://travis-ci.org/cryptocoinjs/coinkey)
[![Coverage Status](https://img.shields.io/coveralls/cryptocoinjs/coinkey.svg)](https://coveralls.io/r/cryptocoinjs/coinkey)
[![Version](http://img.shields.io/npm/v/coinkey.svg)](https://www.npmjs.org/package/coinkey)
Why?
----
JavaScript component for private keys, public keys, and addresess for crypto currencies such as Bitcoin, Litecoin, and Dogecoin. Works
in both Node.js and the browser.
This module provides a convenient way to compute all of the relevant crypto currency details of private keys, public keys, and addresses. It inherits from [ECKey][eckey] and adds the utility of [coinstring][coinstring].
Official documenation:
Installation
------------
npm install --save coinkey
Usage
-----
### Common Use Cases
### Generate a Bunch of Bitcoin Keys/Addresses
```js
var CoinKey = require('coinkey');
var bitcoinAddresses = [];
for (var i = 0; i < 10; ++i) {
bitcoinAddresses.push(new CoinKey()); //Bitcoin supported by default
}
```
#### Generate a Bunch of Namecoin Keys/Addresses
```js
var CoinKey = require('coinkey');
var ci = require('coininfo');
var namecoins = [];
for (var i = 0; i < 10; ++i) {
namecoins.push(new CoinKey(ci('NMC').versions));
}
```
#### Parse a Wallet Import Key and Determine Crypto Currency
```js
var CoinKey = require('coinkey');
var ci = require('coininfo');
var ck = CoinKey.fromWif('QVD3x1RPiWPvyxbTsfxVwaYLyeBZrQvjhZ2aZJUsbuRgsEAGpNQ2');
console.log(ck.privateKey.toString('hex')) // => c4bbcb1fbec99d65bf59d85c8cb62ee2db963f0fe106f483d9afa73bd4e39a8a
console.log(ck.publicAddress) // => DGG6AicS4Qg8Y3UFtcuwJqbuRZ3Q7WtYXv
console.log(ck.compressed) // => true
console.log(ck.versions.public === ci('DOGE').versions.public) // => true
```
#### Change to Testnet Later
```js
var CoinKey = require('coinkey');
var ci = require('coininfo');
var ck = new CoinKey(new Buffer('1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd', 'hex'));
console.log(ck.publicAddress); // => 16UjcYNBG9GTK4uq2f7yYEbuifqCzoLMGS
//change to Testnet
ck.versions = ci('TEST');
console.log(ck.publicAddress); // => mkzgubTA5Ahi6BPSkE6MN9pEafRutznkMe
```
### API
#### CoinKey([bytes], [compressed], [versions])
Constructor function.
- **bytes**: The private key bytes. Must be 32 bytes in length. Should be an `Array`, `Uint8Array`, or a `Buffer`. If not passed, one will be randomlyl generated.
- **compressed**: Specify whether the key should be compressed or not.
- **versions**: An object that specifies the public and private key versions for addresses and wifs. Defaults to Bitcoin `mainnet`.
```js
var CoinKey = require('coinkey');
var secureRandom = require('secure-random');
var bytes = secureRandom(32); //https://github.com/jprichardson/secure-random
var key1 = new ECKey(bytes);
var key2 = CoinKey(bytes); //<--- can also use without "new"
var compressedKey = new CoinKey(bytes, true);
```
### Properties
#### compressed
Inherited from [ECKey][eckey]. [eckey.compressed](https://github.com/cryptocoinjs/eckey#compressed)
#### privateKey
Inherited from [ECKey][eckey]. [eckey.privateKey](https://github.com/cryptocoinjs/eckey#privatekey)
#### privateExportKey
Inherited from [ECKey][eckey]. [eckey.privateExportKey](https://github.com/cryptocoinjs/eckey#privateexportkey)
#### privateWif
Get the private WIF (Wallet Import Format).
```js
var CoinKey = require('coinkey');
var conv = require('binstring');
var privateKeyHex = "1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd";
//Bitcoin WIF
var key = new CoinKey(conv(privateKeyHex, {in: 'hex', out: 'buffer'}), false);
console.log(key.privateWif) // => 5Hx15HFGyep2CfPxsJKe2fXJsCVn5DEiyoeGGF6JZjGbTRnqfiD
//Litecoin WIF
var key = new CoinKey(conv(privateKeyHex, {in: 'hex', out: 'buffer'}), false, {private: 0xB0, public: 0x30});
console.log(key.privateWif) // => 6uFjYQnot5Gtg3HpP87bp4JUpg4FH1gkkV3RyS7LHBbD9Hpt1na
```
#### publicKey
Inherited from [ECKey][eckey]. [eckey.publicKey](https://github.com/cryptocoinjs/eckey#publickey)
#### publicAddress
Get the public address.
```js
var CoinKey = require('coinkey');
var conv = require('binstring');
var privateKeyHex = "1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd";
//Bitcoin Address
var key = new CoinKey(conv(privateKeyHex, {in: 'hex', out: 'buffer'}), false);
console.log(key.publicAddress) // => 16UjcYNBG9GTK4uq2f7yYEbuifqCzoLMGS
//Litecoin Address
var key = new CoinKey(conv(privateKeyHex, {in: 'hex', out: 'buffer'}), false, {private: 0xB0, public: 0x30});
console.log(key.publicAddress) // => 16UjcYNBG9GTK4uq2f7yYEbuifqCzoLMGS
```
#### publicHash
Alias: `pubKeyHash`
Get the public hash i.e. the ripemd160(sha256(publicKey))
```js
var CoinKey = require('coinkey');
var conv = require('binstring');
var privateKeyHex = "1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd";
var key = new CoinKey(conv(privateKeyHex, {in: 'hex', out: 'buffer'}), false);
console.log(key.publicHash.toString('hex')) // => 3c176e659bea0f29a3e9bf7880c112b1b31b4dc8
console.log(key.publKeyHash.toString('hex')) // => 3c176e659bea0f29a3e9bf7880c112b1b31b4dc8
var keyCompressed = CoinKey(conv(privateKeyHex, {in: 'hex', out: 'buffer'}), true);
console.log(key.publicHash.toString('hex')) // => a1c2f92a9dacbd2991c3897724a93f338e44bdc1
console.log(key.publKeyHash.toString('hex')) // => a1c2f92a9dacbd2991c3897724a93f338e44bdc1
```
#### publicPoint
Inherited from [ECKey][eckey]. [eckey.publicPoint](https://github.com/cryptocoinjs/eckey#publicpoint)
#### toString()
Returns the string representation of the private key.
### Methods
#### fromWif(wif, [versions])
Class method to create a `CoinKey` from a wif.
```js
var ck = CoinKey.fromWif('KwomKti1X3tYJUUMb1TGSM2mrZk1wb1aHisUNHCQXTZq5auC2qc3');
console.log(ck.compressed); // => true
console.log(ck.privateKey.toString('hex')) // => 1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd
console.log(ck.publicAddress); // => 1FkKMsKNJqWSDvTvETqcCeHcUQQ64kSC6s
```
Browser Support
---------------
Clone the repo:
git clone https://github.com/cryptocoinjs/coinkey
Install Browserify
npm install -g browserify
Nav to repo:
cd coinkey
Install dependencies:
npm install
Run browserify:
browserify --standalone coinkey < lib/coinkey.js > lib/coinkey.bundle.js
You can now drop `coinkey.bundle.js` in a `<script>` tag.
References
----------
- http://procbits.com/2013/08/27/generating-a-bitcoin-address-with-javascript
- https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/src/eckey.js
- https://github.com/vbuterin/bitcoinjs-lib/blob/master/src/eckey.js
[eckey]: https://github.com/cryptocoinjs/eckey
[coinstring]: https://github.com/cryptocoinjs/coinstring
http://cryptocoinjs.com/modules/currency/coinkey/
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc