coinstring
coinstring is a JavaScript component that is fully compatible with Node.js and the browser. It's used to generate relevant addresses and wallet import formats used by various crypto currencies.
Note: It's an experimental functional replacement for https://github.com/cryptocoinjs/btc-address
Installation
npm install coinstring --save
Usage
There are three functions in this module.
API
coinstring(version, bytes)
Used to convert either a hash160 or private key into an address or wallet import format string respectively.
- version: Is an integer representing the version. See below for more information.
- bytes: A
Buffer
of bytes, either the hash160 or private key.
decode(version, str)
It is the inverse of the coinstring()
function i.e. it converts the address or wallet import format into a Buffer
of bytes. It
throws if the address or wallet import format is not valid. Not valid means that the version doesn't match, or the checksum is
incorrect.
- version: Is an integer representing the version. See below for more information.
- str: A
string
that is either the wallet import format or public address.
validate(version, str)
Validates whether the address string or wallet import format string is valid. Returns a true
or false
.
- version: Is an integer representing the version. See below for more information.
- str: A
string
that is either the wallet import format or public address.
Common Examples
Convert Private Key to Bitcoin Wallet Import Format
var conv = require('binstring');
var coinstring = require('coinstring');
var privateKeyHex = "1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd";
var privateKeyHexBuf = conv(privateKeyHex, {in: 'hex', out: 'buffer'});
var version = 0x080;
console.log(coinstring(version, privateKeyHexBuf));
Convert hash160 (aka pubkeyhash) to Bitcoin Address
var conv = require('binstring');
var coinstring = require('coinstring');
var hash160 = "3c176e659bea0f29a3e9bf7880c112b1b31b4dc8";
var hash160Buf = conv(hash160, {in: 'hex', out: 'buffer'});
var version = 0x00;
console.log(coinstring(version, hash160Buf));
Convert Private Key to Compressed Bitcoin Wallet Import Format
var conv = require('binstring');
var coinstring = require('coinstring');
var privateKeyHex = "1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd";
privateKeyHex += '01';
var privateKeyHexBuf = conv(privateKeyHex, {in: 'hex', out: 'buffer'});
var version = 0x080;
console.log(coinstring(version, privateKeyHexBuf));
Convert hash160 (aka pubkeyhash) to Dogecoin Address
var conv = require('binstring');
var coinstring = require('coinstring');
var hash160 = "3c176e659bea0f29a3e9bf7880c112b1b31b4dc8";
var hash160Buf = conv(hash160, {in: 'hex', out: 'buffer'});
var version = 0x1E;
console.log(coinstring(version, hash160Buf));
Functional Goodness
coinstring
also has some functional goodies. All functions can be partially applied.
Function to Generate Bitcoin Wallet Import Format
var conv = require('binstring');
var coinstring = require('coinstring');
var privateKeyHex = "1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd";
var privateKeyHexBuf = conv(privateKeyHex, {in: 'hex', out: 'buffer'});
var version = 0x080;
var toBtcWif = coinstring(version)
console.log(toBtcWif(privateKeyHexBuf));
Function to Parse Bitcoin Wallet Import Format
var conv = require('binstring');
var coinstring = require('coinstring');
var wif = "5Hx15HFGyep2CfPxsJKe2fXJsCVn5DEiyoeGGF6JZjGbTRnqfiD";
var version = 0x080;
var fromBtcWif = coinstring.decode(version)
console.log(fromBtcWif(wif).toString('hex'));
Function to Validate Bitcoin Testnet Addresses
var conv = require('binstring');
var coinstring = require('coinstring');
var hash160 = "3c176e659bea0f29a3e9bf7880c112b1b31b4dc8";
var hash160Buf = conv(hash160, {in: 'hex', out: 'buffer'});
var version = 0x6F;
var testnetAddressValidator = coinstring.validate(version);
console.log(testnetAddressValidator("mkzgubTA5Ahi6BPSkE6MN9pEafRutznkMe"))
List of Common Crypto Currency Versions
The following is a table of common crypto currency versions. It may seem a bit user unfriendly to have to input the number instead of something like "BTC"; we agree. Another module will be created to address this. In the meantime, use the table below.
Crypto Coin | Public Address | Private Wallet Import Format |
---|
Bitcoin | 0x00 | 0x80 |
Bitcoin Script Hash | 0x05 | N/A |
Bitcoin Testnet | 0x6E | 0xEF |
Bitcoin Testnet Script Hash | 0xC4 | N/A |
Dogecoin | 0x1E | 0x9E |
Litecoin | 0x30 | 0xB0 |
Namecoin | 0x34 | 0xB4 |
Use in the Browser
Clone the repo:
git clone https://github.com/cryptocoinjs/coinstring
Install Browserify
npm install -g browserify
Nav to repo:
cd coinstring
Install dependencies:
npm install
Run browserify:
browserify --standalone coinstring < lib/coinstring.js > lib/coinstring.bundle.js
You can now drop coinstring.bundle.js
in a <script>
tag.
References
License
(MIT License)