base58check
Advanced tools
Comparing version 1.0.1 to 2.0.0
26
index.js
/* | ||
* @Author: zyc | ||
* @Date: 2016-09-11 23:36:05 | ||
* @Last Modified by: zyc | ||
* @Last Modified time: 2016-09-11 23:38:29 | ||
* @Last Modified by: unrealce | ||
* @Last Modified time: 2016-09-13 01:26:13 | ||
*/ | ||
@@ -12,3 +12,4 @@ 'use strict' | ||
module.exports = (data, prefix = '00', encoding = 'hex') => { | ||
module.exports.encode = (data, prefix = '00', encoding = 'hex') => { | ||
if (typeof data === 'string') { | ||
@@ -28,2 +29,21 @@ data = new Buffer(data, encoding) | ||
return base58.encode(hash) | ||
} | ||
module.exports.decode = (string, encoding) => { | ||
const buffer = new Buffer(base58.decode(string)) | ||
let prefix = buffer.slice(0, 1) | ||
let data = buffer.slice(1, -4) | ||
let hash = Buffer.concat([prefix, data]) | ||
hash = crypto.createHash('sha256').update(hash).digest() | ||
hash = crypto.createHash('sha256').update(hash).digest() | ||
buffer.slice(-4).forEach((check, index) => { | ||
if (check !== hash[index]) { | ||
throw new Error('Invalid checksum') | ||
} | ||
}) | ||
if (encoding) { | ||
prefix = prefix.toString(encoding) | ||
data = data.toString(encoding) | ||
} | ||
return { prefix, data } | ||
} |
{ | ||
"name": "base58check", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Base58Check encoding", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -16,3 +16,3 @@ base58check | ||
### string base58check(data, prefix = '00', encoding = 'hex') | ||
### string encode(data, prefix = '00', encoding = 'hex') | ||
@@ -26,6 +26,24 @@ `data` must be a [Buffer](http://nodejs.org/api/buffer.html) or a `string`. It returns a `string`. | ||
let data = 'f5f2d624cfb5c3f66d06123d0829d1c9cebf770e' | ||
console.log(base58check(data)) // => 1PRTTaJesdNovgne6Ehcdu1fpEdX7913CK | ||
const data = '086eaa677895f92d4a6c5ef740c168932b5e3f44' | ||
console.log(base58check.encode(data)) // => 1mayif3H2JDC62S4N3rLNtBNRAiUUP99k | ||
``` | ||
### { prefix, data } decode(string[, encoding]) | ||
`string` must be a base 58 check encoded string. Returns a `Object` for prefix & data. | ||
**example**: | ||
```js | ||
const base58check = require('base58check') | ||
const address = '1mayif3H2JDC62S4N3rLNtBNRAiUUP99k' | ||
console.log(base58check.decode(address)) | ||
// => { prefix: <Buffer 00>, data: <Buffer 08 6e aa 67 78 95 f9 2d 4a 6c 5e f7 40 c1 68 93 2b 5e 3f 44> } | ||
// if using encoding | ||
console.log(base58check.decode(address, 'hex')) | ||
// => { prefix: '00', data: '086eaa677895f92d4a6c5ef740c168932b5e3f44' } | ||
``` | ||
Hack / Test | ||
@@ -32,0 +50,0 @@ ----------- |
49
test.js
/* | ||
* @Author: zyc | ||
* @Date: 2016-09-11 23:36:23 | ||
* @Last Modified by: zyc | ||
* @Last Modified time: 2016-09-11 23:59:12 | ||
* @Last Modified by: unrealce | ||
* @Last Modified time: 2016-09-13 01:51:11 | ||
*/ | ||
@@ -10,5 +10,8 @@ 'use strict' | ||
const base58check = require('./index') | ||
const { encode, decode } = base58check | ||
let data = 'f5f2d624cfb5c3f66d06123d0829d1c9cebf770e' | ||
console.log(base58check(data)) // => 1PRTTaJesdNovgne6Ehcdu1fpEdX7913CK | ||
let string = encode(data) | ||
console.log(string) // => 1PRTTaJesdNovgne6Ehcdu1fpEdX7913CK | ||
console.log(decode(string, 'hex')) // => { prefix: '00', data: 'f5f2d624cfb5c3f66d06123d0829d1c9cebf770e' } | ||
@@ -18,3 +21,5 @@ const prefix = '80' | ||
data = '1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD' | ||
console.log(base58check(data, prefix)) // => 5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn | ||
string = encode(data , prefix) | ||
console.log(string) // => 5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn | ||
console.log(decode(string, 'hex')) // => { prefix: '80', data: '1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd' } | ||
@@ -24,11 +29,39 @@ const encoding = 'hex' | ||
data = '1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD01' | ||
console.log(base58check(data, prefix, encoding)) // => KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ | ||
string = encode(data , prefix, encoding) | ||
console.log(string) // => KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ | ||
console.log(decode(string, 'hex')) // => { prefix: '80', data: '1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd01' } | ||
data = new Buffer('27b5891b01da2db74cde1689a97a2acbe23d5fb1', encoding) | ||
console.log(base58check(data)) // => 14cxpo3MBCYYWCgF74SWTdcmxipnGUsPw3 | ||
string = encode(data) | ||
console.log(string) // => 14cxpo3MBCYYWCgF74SWTdcmxipnGUsPw3 | ||
console.log(decode(string, 'hex')) // => { prefix: '00', data: '27b5891b01da2db74cde1689a97a2acbe23d5fb1' } | ||
data = new Buffer('3aba4162c7251c891207b747840551a71939b0de081f85c4e44cf7c13e41daa6', encoding) | ||
console.log(base58check(data , prefix)) // => 5JG9hT3beGTJuUAmCQEmNaxAuMacCTfXuw1R3FCXig23RQHMr4K | ||
string = encode(data , prefix) | ||
console.log(string) // => 5JG9hT3beGTJuUAmCQEmNaxAuMacCTfXuw1R3FCXig23RQHMr4K | ||
console.log(decode(string, 'hex')) // => { prefix: '80', data: '3aba4162c7251c891207b747840551a71939b0de081f85c4e44cf7c13e41daa6' } | ||
data = new Buffer('3aba4162c7251c891207b747840551a71939b0de081f85c4e44cf7c13e41daa601', encoding) | ||
console.log(base58check(data , prefix, encoding)) // => KyBsPXxTuVD82av65KZkrGrWi5qLMah5SdNq6uftawDbgKa2wv6S | ||
string = encode(data , prefix, encoding) | ||
console.log(string) // => KyBsPXxTuVD82av65KZkrGrWi5qLMah5SdNq6uftawDbgKa2wv6S | ||
console.log(decode(string, 'hex')) // => { prefix: '80', data: '3aba4162c7251c891207b747840551a71939b0de081f85c4e44cf7c13e41daa601' } | ||
data = '086eaa677895f92d4a6c5ef740c168932b5e3f44' | ||
console.log(encode(data)) // => 1mayif3H2JDC62S4N3rLNtBNRAiUUP99k | ||
const address = '1mayif3H2JDC62S4N3rLNtBNRAiUUP99k' | ||
console.log(decode(address)) | ||
// => { prefix: <Buffer 00>, data: <Buffer 08 6e aa 67 78 95 f9 2d 4a 6c 5e f7 40 c1 68 93 2b 5e 3f 44> } | ||
console.log(decode(address, 'hex')) | ||
// => { prefix: '00', data: '086eaa677895f92d4a6c5ef740c168932b5e3f44' } | ||
const privateKeyHex = 'eddbdc1168f1daeadbd3e44c1e3f8f5a284c2029f78ad26af98583a499de5b19' | ||
console.log(encode(privateKeyHex, prefix)) // => 5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr | ||
const privateKeyWIF = '5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr' | ||
console.log(decode(privateKeyWIF)) | ||
// => { prefix: <Buffer 80>, data: <Buffer ed db dc 11 68 f1 da ea db d3 e4 4c 1e 3f 8f 5a 28 4c 20 29 f7 8a d2 6a f9 85 83 a4 99 de 5b 19> } | ||
console.log(decode(privateKeyWIF, 'hex')) | ||
// => { prefix: '80', data: 'eddbdc1168f1daeadbd3e44c1e3f8f5a284c2029f78ad26af98583a499de5b19' } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6896
90
68