@darkwolf/base58
Advanced tools
Comparing version 13.21.72 to 13.21.73
45
index.js
@@ -5,7 +5,14 @@ const constants = require('./constants') | ||
function base58(alphabet) { | ||
if (!(validator.isNonEmptyString(alphabet) && validator.isAlphabet(alphabet))) throw new Base58Error('invalid-alphabet', `"alphabet" argument must be a non-empty alphabetical string consisting of characters in the range: [${constants.BITCOIN_ALPHABET}]`) | ||
class Base58 { | ||
constructor(alphabet) { | ||
if (!validator.isAlphabet(alphabet)) throw new Base58Error('invalid-alphabet', `"alphabet" argument must be a non-empty alphabetical string consisting of characters in the range: [${constants.BITCOIN_ALPHABET}]`) | ||
this.alphabet = alphabet | ||
} | ||
function encode(input) { | ||
if (!(validator.isNonEmptyString(input) && validator.isASCII(input))) throw new Base58Error('invalid-input', '"input" argument must be a non-empty ASCII encoded string') | ||
get Base58() { | ||
return Base58 | ||
} | ||
encode(input) { | ||
if (!validator.isASCII(input)) throw new Base58Error('invalid-input', '"input" argument must be a non-empty ASCII-encoded string') | ||
return Buffer.from(input).reduce((bytes, carry, i) => { | ||
@@ -21,8 +28,8 @@ if (!(carry || bytes.length ^ i)) bytes.push(1) | ||
return bytes | ||
}, []).reverse().map(o => alphabet[o]).join('') | ||
}, []).reverse().map(o => this.alphabet[o]).join('') | ||
} | ||
function decode(input) { | ||
if (!(validator.isNonEmptyString(input) && validator.isBase58(input))) throw new Base58Error('invalid-input', '"input" argument must be a non-empty Base58 encoded string') | ||
return String.fromCharCode(...input.split('').map(o => alphabet.indexOf(o)).reduce((bytes, carry, i) => { | ||
decode(input) { | ||
if (!validator.isBase58(input)) throw new Base58Error('invalid-input', '"input" argument must be a non-empty Base58-encoded string') | ||
return String.fromCharCode(...input.split('').map(o => this.alphabet.indexOf(o)).reduce((bytes, carry, i) => { | ||
if (!(carry || bytes.length ^ i)) bytes.push(0) | ||
@@ -40,3 +47,3 @@ let j = 0 | ||
function encodeInt(input) { | ||
encodeInt(input) { | ||
if (!(validator.isSafeInteger(input) && validator.isNonNegative(input))) throw new Base58Error('invalid-input', '"input" argument must be a non-negative safe integer') | ||
@@ -48,22 +55,14 @@ let int = input | ||
mod = int % 58 | ||
str = alphabet[mod] + str | ||
str = this.alphabet[mod] + str | ||
int = int / 58 | 0 | ||
} | ||
return alphabet[int] + str | ||
return this.alphabet[int] + str | ||
} | ||
function decodeInt(input) { | ||
if (!(validator.isNonEmptyString(input) && validator.isBase58(input))) throw new Base58Error('invalid-input', '"input" argument must be a non-empty Base58 encoded string') | ||
return input.split('').reverse().reduce((int, char, i) => int + alphabet.indexOf(char) * Math.pow(58, i), 0) | ||
decodeInt(input) { | ||
if (!validator.isBase58(input)) throw new Base58Error('invalid-input', '"input" argument must be a non-empty Base58 encoded string') | ||
return input.split('').reverse().reduce((int, char, i) => int + this.alphabet.indexOf(char) * Math.pow(58, i), 0) | ||
} | ||
return { | ||
withAlphabet: base58, | ||
encode, | ||
decode, | ||
encodeInt, | ||
decodeInt | ||
} | ||
} | ||
module.exports = base58(constants.DARKWOLF_ALPHABET) | ||
module.exports = new Base58(constants.DARKWOLF_ALPHABET) |
{ | ||
"name": "@darkwolf/base58", | ||
"version": "13.21.72", | ||
"version": "13.21.73", | ||
"description": "Base58", | ||
@@ -26,4 +26,4 @@ "main": "index.js", | ||
"@darkwolf/code-error": "^13.21.69", | ||
"@darkwolf/validator": "^13.21.70" | ||
"@darkwolf/validator": "^13.21.80" | ||
} | ||
} |
@@ -9,2 +9,9 @@ # Base58 | ||
const base58 = require('@darkwolf/base58') | ||
const { | ||
DARKWOLF_ALPHABET, | ||
BITCOIN_ALPHABET, | ||
RIPPLE_ALPHABET, | ||
FLICKR_ALPHABET | ||
} = require('@darkwolf/base58/constants') | ||
const { Base58 } = base58 | ||
// default alphabet: «AveDarkwo1f23456789BCEFGHJKLMNPQRSTUVWXYZbcdghijmnpqstuxyz» | ||
@@ -21,9 +28,3 @@ | ||
// custom alphabet | ||
const { | ||
DARKWOLF_ALPHABET, | ||
BITCOIN_ALPHABET, | ||
RIPPLE_ALPHABET, | ||
FLICKR_ALPHABET | ||
} = require('@darkwolf/base58/constants') | ||
const btc58 = base58.withAlphabet(BITCOIN_ALPHABET) | ||
const btc58 = new Base58(BITCOIN_ALPHABET) | ||
@@ -33,4 +34,5 @@ const encodedBtc = btc58.encode('Ave, Bitcoin!') | ||
``` | ||
## Init | ||
### new Base58(alphabet) | ||
## Methods | ||
### withAlphabet(alphabet) | ||
### encode(input) | ||
@@ -37,0 +39,0 @@ ### decode(input) |
@@ -1,9 +0,14 @@ | ||
const validator = require('@darkwolf/validator') | ||
const { Validator } = require('@darkwolf/validator') | ||
const constants = require('./constants') | ||
const isAlphabet = value => constants.ALPHABET_REGEX.test(value) | ||
class Base58Validator extends Validator { | ||
constructor() { | ||
super() | ||
} | ||
module.exports = { | ||
...validator, | ||
isAlphabet | ||
isAlphabet(value) { | ||
return this.isString(value) && constants.ALPHABET_REGEX.test(value) | ||
} | ||
} | ||
module.exports = new Base58Validator() |
88
42
6203