Comparing version 1.1.3 to 1.1.4
63
index.js
@@ -27,3 +27,3 @@ 'use strict' | ||
var c = prefix.charCodeAt(i) | ||
if (c < 33 || c > 126) throw new Error('Invalid prefix (' + prefix + ')') | ||
if (c < 33 || c > 126) return 'Invalid prefix (' + prefix + ')' | ||
@@ -49,2 +49,4 @@ chk = polymodStep(chk) ^ (c >> 5) | ||
var chk = prefixChk(prefix) | ||
if (typeof chk === 'string') throw new Error(chk) | ||
var result = prefix + '1' | ||
@@ -72,6 +74,6 @@ for (var i = 0; i < words.length; ++i) { | ||
function decode (str, LIMIT) { | ||
function __decode (str, LIMIT) { | ||
LIMIT = LIMIT || 90 | ||
if (str.length < 8) throw new TypeError(str + ' too short') | ||
if (str.length > LIMIT) throw new TypeError('Exceeds length limit') | ||
if (str.length < 8) return str + ' too short' | ||
if (str.length > LIMIT) return 'Exceeds length limit' | ||
@@ -81,14 +83,16 @@ // don't allow mixed case | ||
var uppered = str.toUpperCase() | ||
if (str !== lowered && str !== uppered) throw new Error('Mixed-case string ' + str) | ||
if (str !== lowered && str !== uppered) return 'Mixed-case string ' + str | ||
str = lowered | ||
var split = str.lastIndexOf('1') | ||
if (split === -1) throw new Error('No separator character for ' + str) | ||
if (split === 0) throw new Error('Missing prefix for ' + str) | ||
if (split === -1) return 'No separator character for ' + str | ||
if (split === 0) return 'Missing prefix for ' + str | ||
var prefix = str.slice(0, split) | ||
var wordChars = str.slice(split + 1) | ||
if (wordChars.length < 6) throw new Error('Data too short') | ||
if (wordChars.length < 6) return 'Data too short' | ||
var chk = prefixChk(prefix) | ||
if (typeof chk === 'string') return chk | ||
var words = [] | ||
@@ -98,3 +102,3 @@ for (var i = 0; i < wordChars.length; ++i) { | ||
var v = ALPHABET_MAP[c] | ||
if (v === undefined) throw new Error('Unknown character ' + c) | ||
if (v === undefined) return 'Unknown character ' + c | ||
chk = polymodStep(chk) ^ v | ||
@@ -107,6 +111,18 @@ | ||
if (chk !== 1) throw new Error('Invalid checksum for ' + str) | ||
if (chk !== 1) return 'Invalid checksum for ' + str | ||
return { prefix: prefix, words: words } | ||
} | ||
function decodeUnsafe () { | ||
var res = __decode.apply(null, arguments) | ||
if (typeof res === 'object') return res | ||
} | ||
function decode (str) { | ||
var res = __decode.apply(null, arguments) | ||
if (typeof res === 'object') return res | ||
throw new Error(res) | ||
} | ||
function convert (data, inBits, outBits, pad) { | ||
@@ -133,4 +149,4 @@ var value = 0 | ||
} else { | ||
if (bits >= inBits) throw new Error('Excess padding') | ||
if ((value << (outBits - bits)) & maxV) throw new Error('Non-zero padding') | ||
if (bits >= inBits) return 'Excess padding' | ||
if ((value << (outBits - bits)) & maxV) return 'Non-zero padding' | ||
} | ||
@@ -141,15 +157,34 @@ | ||
function toWordsUnsafe (bytes) { | ||
var res = convert(bytes, 8, 5, true) | ||
if (Array.isArray(res)) return res | ||
} | ||
function toWords (bytes) { | ||
return convert(bytes, 8, 5, true) | ||
var res = convert(bytes, 8, 5, true) | ||
if (Array.isArray(res)) return res | ||
throw new Error(res) | ||
} | ||
function fromWordsUnsafe (words) { | ||
var res = convert(words, 5, 8, false) | ||
if (Array.isArray(res)) return res | ||
} | ||
function fromWords (words) { | ||
return convert(words, 5, 8, false) | ||
var res = convert(words, 5, 8, false) | ||
if (Array.isArray(res)) return res | ||
throw new Error(res) | ||
} | ||
module.exports = { | ||
decodeUnsafe: decodeUnsafe, | ||
decode: decode, | ||
encode: encode, | ||
toWordsUnsafe: toWordsUnsafe, | ||
toWords: toWords, | ||
fromWordsUnsafe: fromWordsUnsafe, | ||
fromWords: fromWords | ||
} |
{ | ||
"name": "bech32", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "Bech32 encoding / decoding", | ||
@@ -17,4 +17,6 @@ "keywords": [ | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
@@ -24,6 +26,6 @@ "license": "MIT", | ||
"devDependencies": { | ||
"nyc": "^10.2.0", | ||
"standard": "*", | ||
"tape": "^4.6.3", | ||
"tap-dot": "*" | ||
"nyc": "^15.0.0", | ||
"standard": "^14.3.3", | ||
"tap-dot": "*", | ||
"tape": "^4.13.2" | ||
}, | ||
@@ -30,0 +32,0 @@ "repository": { |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
9984
5
218
1