Package Exports
- coinstring
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (coinstring) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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 --saveUsage
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
Bufferof 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
stringthat 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
stringthat 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; //Bitcoin private key
console.log(coinstring(version, privateKeyHexBuf)); // => 5Hx15HFGyep2CfPxsJKe2fXJsCVn5DEiyoeGGF6JZjGbTRnqfiDConvert hash160 (aka pubkeyhash) to Bitcoin Address
var conv = require('binstring');
var coinstring = require('coinstring');
var hash160 = "3c176e659bea0f29a3e9bf7880c112b1b31b4dc8"; //hash representing uncompressed
var hash160Buf = conv(hash160, {in: 'hex', out: 'buffer'});
var version = 0x00; //Bitcoin public address
console.log(coinstring(version, hash160Buf)); // => 16UjcYNBG9GTK4uq2f7yYEbuifqCzoLMGSConvert Private Key to Compressed Bitcoin Wallet Import Format
var conv = require('binstring');
var coinstring = require('coinstring');
var privateKeyHex = "1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd";
//for compressed, append "01"
privateKeyHex += '01';
var privateKeyHexBuf = conv(privateKeyHex, {in: 'hex', out: 'buffer'});
var version = 0x080; //Bitcoin private key
console.log(coinstring(version, privateKeyHexBuf)); // => KwomKti1X3tYJUUMb1TGSM2mrZk1wb1aHisUNHCQXTZq5auC2qc3Convert hash160 (aka pubkeyhash) to Dogecoin Address
var conv = require('binstring');
var coinstring = require('coinstring');
var hash160 = "3c176e659bea0f29a3e9bf7880c112b1b31b4dc8"; //hash representing uncompressed
var hash160Buf = conv(hash160, {in: 'hex', out: 'buffer'});
var version = 0x1E; //Dogecoin public address
console.log(coinstring(version, hash160Buf)); // => DAcq9oJpZZAjr56RmF7Y5zmWboZWQ4HAsWFunctional 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; //Bitcoin private key
var toBtcWif = coinstring(version)
//later in your program
console.log(toBtcWif(privateKeyHexBuf)); // => 5Hx15HFGyep2CfPxsJKe2fXJsCVn5DEiyoeGGF6JZjGbTRnqfiDFunction to Parse Bitcoin Wallet Import Format
var conv = require('binstring');
var coinstring = require('coinstring');
var wif = "5Hx15HFGyep2CfPxsJKe2fXJsCVn5DEiyoeGGF6JZjGbTRnqfiD";
var version = 0x080; //Bitcoin private key
var fromBtcWif = coinstring.decode(version)
//later in your program
console.log(fromBtcWif(wif).toString('hex')); // => 51184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fdFunction to Validate Bitcoin Testnet Addresses
var conv = require('binstring');
var coinstring = require('coinstring');
var hash160 = "3c176e659bea0f29a3e9bf7880c112b1b31b4dc8"; //hash representing uncompressed
var hash160Buf = conv(hash160, {in: 'hex', out: 'buffer'});
var version = 0x6F; //Bitcoin Testnet Address
var testnetAddressValidator = coinstring.validate(version);
console.log(testnetAddressValidator("mkzgubTA5Ahi6BPSkE6MN9pEafRutznkMe")) // => trueList 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/coinstringInstall Browserify
npm install -g browserifyNav to repo:
cd coinstringInstall dependencies:
npm installRun browserify:
browserify --standalone coinstring < lib/coinstring.js > lib/coinstring.bundle.jsYou can now drop coinstring.bundle.js in a <script> tag.
References
License
(MIT License)