coinkey
JavaScript component for private keys, public keys, and addresses for crypto currencies such as Bitcoin, Litecoin, and Dogecoin. Works
in both Node.js and the browser.
Package Info
Installation
npm i --save coinkey
Usage
Common Use Cases
Generate a Bunch of Bitcoin Keys/Addresses
var CoinKey = require('coinkey')
var bitcoinKeys = []
for (var i = 0; i < 10; ++i) {
bitcoinKeys.push(CoinKey.createRandom())
}
Generate a Bunch of Namecoin Keys/Addresses
var CoinKey = require('coinkey')
var ci = require('coininfo')
var namecoins = []
for (var i = 0; i < 10; ++i) {
namecoins.push(CoinKey.createRandom(ci('NMC')))
}
Parse a Wallet Import Key and Determine Crypto Currency
var CoinKey = require('coinkey')
var ci = require('coininfo')
var ck = CoinKey.fromWif('QVD3x1RPiWPvyxbTsfxVwaYLyeBZrQvjhZ2aZJUsbuRgsEAGpNQ2')
console.log(ck.privateKey.toString('hex'))
console.log(ck.publicAddress)
console.log(ck.compressed)
console.log(ck.versions.public === ci('DOGE').versions.public)
Change to Testnet Later
var CoinKey = require('coinkey')
var ci = require('coininfo')
var ck = new CoinKey(new Buffer('1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd', 'hex'))
console.log(ck.publicAddress)
ck.versions = ci('BTC-TEST')
console.log(ck.publicAddress)
API
CoinKey(privateKey, [versions])
Constructor function.
- privateKey: The private key bytes. Must be 32 bytes in length. Should be an
Array
, Uint8Array
, or a Buffer
. - versions: An object that specifies the public and private key versions for addresses and wifs. Defaults to Bitcoin
mainnet
.
Keys are default set to compressed
is true
.
var CoinKey = require('coinkey')
var secureRandom = require('secure-random')
var bytes = secureRandom.randomBuffer(32)
var key1 = new CoinKey(bytes)
console.log(key1.compressed)
Properties
compressed
Inherited from ECKey. eckey.compressed
privateKey
Inherited from ECKey. eckey.privateKey
privateExportKey
Inherited from ECKey. eckey.privateExportKey
privateWif
Get the private WIF (Wallet Import Format).
var CoinKey = require('coinkey')
var privateKeyHex = "1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd"
var key = new CoinKey(new Buffer(privateKeyHex, 'hex'))
key.compressed = false
console.log(key.privateWif)
var key = new CoinKey(new Buffer(privateKeyHex, 'hex'), {private: 0xB0, public: 0x30})
key.compressed = false
console.log(key.privateWif)
publicKey
Inherited from ECKey. eckey.publicKey
publicAddress
Get the public address.
var CoinKey = require('coinkey')
var privateKeyHex = "1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd"
var key = new CoinKey(new Buffer(privatKeyHex, 'hex'))
console.log(key.publicAddress)
var key = new CoinKey(new Buffer(privateKeyHex, 'hex'), {private: 0xB0, public: 0x30})
console.log(key.publicAddress)
publicHash
Alias: pubKeyHash
Inherited from ECKey. eckey.publicHash
publicPoint
Inherited from ECKey. eckey.publicPoint
toString()
Returns the string representation.
var CoinKey = require('coinkey')
var privateKeyHex = "1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd"
var key = new CoinKey(new Buffer(privateKeyHex, 'hex'), {private: 0xB0, public: 0x30})
console.log(key.toString())
Methods
fromWif(wif, [versions])
Class method to create a CoinKey
from a wif.
var ck = CoinKey.fromWif('KwomKti1X3tYJUUMb1TGSM2mrZk1wb1aHisUNHCQXTZq5auC2qc3');
console.log(ck.compressed)
console.log(ck.privateKey.toString('hex'))
console.log(ck.publicAddress)
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.
Hack on CoinKey