init
This commit is contained in:
+2
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
.nyc_output
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
sudo: false
|
||||
language: node_js
|
||||
node_js:
|
||||
- "4"
|
||||
- "5"
|
||||
- "7"
|
||||
matrix:
|
||||
include:
|
||||
- node_js: "7"
|
||||
env: TEST_SUITE=standard
|
||||
- node_js: "7"
|
||||
env: TEST_SUITE=coverage
|
||||
env:
|
||||
- TEST_SUITE=unit
|
||||
script: npm run-script $TEST_SUITE
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Daniel Cousens
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
# WIF
|
||||
|
||||
[](http://travis-ci.org/bitcoinjs/wif)
|
||||
[](https://www.npmjs.org/package/wif)
|
||||
|
||||
[](https://github.com/feross/standard)
|
||||
|
||||
Bitcoin Wallet Import Format encoding/decoding module.
|
||||
|
||||
|
||||
## Example
|
||||
|
||||
``` javascript
|
||||
var wif = require('wif')
|
||||
|
||||
var privateKey = new Buffer('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
|
||||
|
||||
var key = wif.encode(128, privateKey, true)
|
||||
// => KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn
|
||||
|
||||
var obj = wif.decode(key)
|
||||
// => {
|
||||
// version: 128,
|
||||
// privateKey: <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01>,
|
||||
// compressed: true
|
||||
//}
|
||||
|
||||
wif.encode(obj)
|
||||
// => KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn
|
||||
```
|
||||
|
||||
## LICENSE [MIT](LICENSE)
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
var bs58check = require('bs58check')
|
||||
|
||||
function decodeRaw (buffer, version) {
|
||||
// check version only if defined
|
||||
if (version !== undefined && buffer[0] !== version) throw new Error('Invalid network version')
|
||||
|
||||
// uncompressed
|
||||
if (buffer.length === 33) {
|
||||
return {
|
||||
version: buffer[0],
|
||||
privateKey: buffer.slice(1, 33),
|
||||
compressed: false
|
||||
}
|
||||
}
|
||||
|
||||
// invalid length
|
||||
if (buffer.length !== 34) throw new Error('Invalid WIF length')
|
||||
|
||||
// invalid compression flag
|
||||
if (buffer[33] !== 0x01) throw new Error('Invalid compression flag')
|
||||
|
||||
return {
|
||||
version: buffer[0],
|
||||
privateKey: buffer.slice(1, 33),
|
||||
compressed: true
|
||||
}
|
||||
}
|
||||
|
||||
function encodeRaw (version, privateKey, compressed) {
|
||||
var result = new Buffer(compressed ? 34 : 33)
|
||||
|
||||
result.writeUInt8(version, 0)
|
||||
privateKey.copy(result, 1)
|
||||
|
||||
if (compressed) {
|
||||
result[33] = 0x01
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
function decode (string, version) {
|
||||
return decodeRaw(bs58check.decode(string), version)
|
||||
}
|
||||
|
||||
function encode (version, privateKey, compressed) {
|
||||
if (typeof version === 'number') return bs58check.encode(encodeRaw(version, privateKey, compressed))
|
||||
|
||||
return bs58check.encode(
|
||||
encodeRaw(
|
||||
version.version,
|
||||
version.privateKey,
|
||||
version.compressed
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
decode: decode,
|
||||
decodeRaw: decodeRaw,
|
||||
encode: encode,
|
||||
encodeRaw: encodeRaw
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "wif",
|
||||
"version": "2.0.6",
|
||||
"description": "Bitcoin Wallet Import Format (WIF) decoding/encoding module",
|
||||
"author": "Daniel Cousens",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/bitcoinjs/wif.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/bitcoinjs/wif/issues"
|
||||
},
|
||||
"homepage": "https://github.com/bitcoinjs/wif",
|
||||
"keywords": [
|
||||
"bitcoin",
|
||||
"base58",
|
||||
"base58check",
|
||||
"decode",
|
||||
"decoding",
|
||||
"encoding",
|
||||
"encode",
|
||||
"key",
|
||||
"private",
|
||||
"wif"
|
||||
],
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"coverage-report": "nyc report --reporter=lcov",
|
||||
"coverage": "nyc --check-coverage --branches 90 --functions 90 npm run unit",
|
||||
"standard": "standard",
|
||||
"test": "npm run standard && npm run coverage",
|
||||
"unit": "tape test/*.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"bs58check": "<3.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nyc": "^6.4.0",
|
||||
"standard": "*",
|
||||
"tape": "^4.6.2"
|
||||
}
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
{
|
||||
"valid": [
|
||||
{
|
||||
"WIF": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
|
||||
"compressed": true,
|
||||
"privateKeyHex": "0000000000000000000000000000000000000000000000000000000000000001",
|
||||
"version": 128
|
||||
},
|
||||
{
|
||||
"WIF": "5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf",
|
||||
"compressed": false,
|
||||
"privateKeyHex": "0000000000000000000000000000000000000000000000000000000000000001",
|
||||
"version": 128
|
||||
},
|
||||
{
|
||||
"WIF": "KxhEDBQyyEFymvfJD96q8stMbJMbZUb6D1PmXqBWZDU2WvbvVs9o",
|
||||
"compressed": true,
|
||||
"privateKeyHex": "2bfe58ab6d9fd575bdc3a624e4825dd2b375d64ac033fbc46ea79dbab4f69a3e",
|
||||
"version": 128
|
||||
},
|
||||
{
|
||||
"WIF": "KzrA86mCVMGWnLGBQu9yzQa32qbxb5dvSK4XhyjjGAWSBKYX4rHx",
|
||||
"compressed": true,
|
||||
"privateKeyHex": "6c4313b03f2e7324d75e642f0ab81b734b724e13fec930f309e222470236d66b",
|
||||
"version": 128
|
||||
},
|
||||
{
|
||||
"WIF": "5JdxzLtFPHNe7CAL8EBC6krdFv9pwPoRo4e3syMZEQT9srmK8hh",
|
||||
"compressed": false,
|
||||
"privateKeyHex": "6c4313b03f2e7324d75e642f0ab81b734b724e13fec930f309e222470236d66b",
|
||||
"version": 128
|
||||
},
|
||||
{
|
||||
"WIF": "cRD9b1m3vQxmwmjSoJy7Mj56f4uNFXjcWMCzpQCEmHASS4edEwXv",
|
||||
"compressed": true,
|
||||
"privateKeyHex": "6c4313b03f2e7324d75e642f0ab81b734b724e13fec930f309e222470236d66b",
|
||||
"version": 239
|
||||
},
|
||||
{
|
||||
"WIF": "92Qba5hnyWSn5Ffcka56yMQauaWY6ZLd91Vzxbi4a9CCetaHtYj",
|
||||
"compressed": false,
|
||||
"privateKeyHex": "6c4313b03f2e7324d75e642f0ab81b734b724e13fec930f309e222470236d66b",
|
||||
"version": 239
|
||||
},
|
||||
{
|
||||
"WIF": "L5oLkpV3aqBjhki6LmvChTCV6odsp4SXM6FfU2Gppt5kFLaHLuZ9",
|
||||
"compressed": true,
|
||||
"privateKeyHex": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140",
|
||||
"version": 128
|
||||
}
|
||||
],
|
||||
"invalid": {
|
||||
"decode": [
|
||||
{
|
||||
"exception": "Invalid network version",
|
||||
"version": 128,
|
||||
"WIF": "92Qba5hnyWSn5Ffcka56yMQauaWY6ZLd91Vzxbi4a9CCetaHtYj"
|
||||
},
|
||||
{
|
||||
"exception": "Invalid compression flag",
|
||||
"version": 128,
|
||||
"WIF": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sfZr2ym"
|
||||
},
|
||||
{
|
||||
"exception": "Invalid WIF length",
|
||||
"version": 128,
|
||||
"WIF": "3tq8Vmhh9SN5XhjTGSWgx8iKk59XbKG6UH4oqpejRuJhfYD"
|
||||
},
|
||||
{
|
||||
"exception": "Invalid WIF length",
|
||||
"version": 128,
|
||||
"WIF": "38uMpGARR2BJy5p4dNFKYg9UsWNoBtkpbdrXDjmfvz8krCtw3T1W92ZDSR"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
var wif = require('../')
|
||||
var fixtures = require('./fixtures')
|
||||
var tape = require('tape')
|
||||
|
||||
fixtures.valid.forEach(function (f) {
|
||||
tape('encode/encodeRaw returns ' + f.WIF + ' for ' + f.privateKeyHex.slice(0, 20) + '... (' + f.version + ')', function (t) {
|
||||
t.plan(1)
|
||||
|
||||
var privateKey = new Buffer(f.privateKeyHex, 'hex')
|
||||
var actual = wif.encode(f.version, privateKey, f.compressed)
|
||||
t.equal(actual, f.WIF)
|
||||
})
|
||||
})
|
||||
|
||||
fixtures.valid.forEach(function (f) {
|
||||
tape('decode/decodeRaw returns ' + f.privateKeyHex.slice(0, 20) + '... (' + f.version + ')' + ' for ' + f.WIF, function (t) {
|
||||
t.plan(3)
|
||||
|
||||
var actual = wif.decode(f.WIF, f.version)
|
||||
t.equal(actual.version, f.version)
|
||||
t.equal(actual.privateKey.toString('hex'), f.privateKeyHex)
|
||||
t.equal(actual.compressed, f.compressed)
|
||||
})
|
||||
})
|
||||
|
||||
fixtures.invalid.decode.forEach(function (f) {
|
||||
tape('throws ' + f.exception + ' for ' + f.WIF, function (t) {
|
||||
t.plan(1)
|
||||
t.throws(function () {
|
||||
wif.decode(f.WIF, f.version)
|
||||
}, new RegExp(f.exception))
|
||||
})
|
||||
})
|
||||
|
||||
fixtures.valid.forEach(function (f) {
|
||||
tape('decode/encode for ' + f.WIF, function (t) {
|
||||
t.plan(1)
|
||||
|
||||
var actual = wif.encode(wif.decode(f.WIF, f.version))
|
||||
t.equal(actual, f.WIF)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user