|
@@ -9,7 +9,6 @@
|
|
*/
|
|
*/
|
|
var Base58 = {
|
|
var Base58 = {
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* @constant
|
|
* @constant
|
|
* @default
|
|
* @default
|
|
@@ -24,15 +23,12 @@ var Base58 = {
|
|
value: "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz",
|
|
value: "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz",
|
|
},
|
|
},
|
|
],
|
|
],
|
|
-
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* @constant
|
|
* @constant
|
|
* @default
|
|
* @default
|
|
*/
|
|
*/
|
|
REMOVE_NON_ALPH_CHARS: true,
|
|
REMOVE_NON_ALPH_CHARS: true,
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* To Base58 operation.
|
|
* To Base58 operation.
|
|
*
|
|
*
|
|
@@ -41,7 +37,10 @@ var Base58 = {
|
|
* @returns {string}
|
|
* @returns {string}
|
|
*/
|
|
*/
|
|
runTo: function(input, args) {
|
|
runTo: function(input, args) {
|
|
- var alphabet = args[0] || Base58.ALPHABET_OPTIONS[0].value;
|
|
|
|
|
|
+ var alphabet = args[0] || Base58.ALPHABET_OPTIONS[0].value,
|
|
|
|
+ result = [0];
|
|
|
|
+
|
|
|
|
+ alphabet = Utils.expandAlphRange(alphabet).join("");
|
|
|
|
|
|
if (alphabet.length !== 58 ||
|
|
if (alphabet.length !== 58 ||
|
|
[].unique.call(alphabet).length !== 58) {
|
|
[].unique.call(alphabet).length !== 58) {
|
|
@@ -50,8 +49,6 @@ var Base58 = {
|
|
|
|
|
|
if (input.length === 0) return "";
|
|
if (input.length === 0) return "";
|
|
|
|
|
|
- var result = [0];
|
|
|
|
-
|
|
|
|
input.forEach(function(b) {
|
|
input.forEach(function(b) {
|
|
var carry = (result[0] << 8) + b;
|
|
var carry = (result[0] << 8) + b;
|
|
result[0] = carry % 58;
|
|
result[0] = carry % 58;
|
|
@@ -89,21 +86,19 @@ var Base58 = {
|
|
* @returns {byteArray}
|
|
* @returns {byteArray}
|
|
*/
|
|
*/
|
|
runFrom: function(input, args) {
|
|
runFrom: function(input, args) {
|
|
- var alphabet = args[0] || Base58.ALPHABET_OPTIONS[0].value;
|
|
|
|
|
|
+ var alphabet = args[0] || Base58.ALPHABET_OPTIONS[0].value,
|
|
|
|
+ removeNonAlphaChars = args[1] === undefined ? true : args[1],
|
|
|
|
+ result = [0];
|
|
|
|
+
|
|
|
|
+ alphabet = Utils.expandAlphRange(alphabet).join("");
|
|
|
|
|
|
if (alphabet.length !== 58 ||
|
|
if (alphabet.length !== 58 ||
|
|
[].unique.call(alphabet).length !== 58) {
|
|
[].unique.call(alphabet).length !== 58) {
|
|
throw ("Alphabet must be of length 58");
|
|
throw ("Alphabet must be of length 58");
|
|
}
|
|
}
|
|
|
|
|
|
- var removeNonAlphaChars = args[1];
|
|
|
|
- if (removeNonAlphaChars === undefined)
|
|
|
|
- removeNonAlphaChars = true;
|
|
|
|
-
|
|
|
|
if (input.length === 0) return [];
|
|
if (input.length === 0) return [];
|
|
|
|
|
|
- var result = [0];
|
|
|
|
-
|
|
|
|
[].forEach.call(input, function(c, charIndex) {
|
|
[].forEach.call(input, function(c, charIndex) {
|
|
var index = alphabet.indexOf(c);
|
|
var index = alphabet.indexOf(c);
|
|
|
|
|
|
@@ -111,7 +106,7 @@ var Base58 = {
|
|
if (removeNonAlphaChars) {
|
|
if (removeNonAlphaChars) {
|
|
return;
|
|
return;
|
|
} else {
|
|
} else {
|
|
- throw ("Char " + c + " not in alphabet");
|
|
|
|
|
|
+ throw ("Char '" + c + "' at position " + charIndex + " not in alphabet");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -133,4 +128,5 @@ var Base58 = {
|
|
|
|
|
|
return result.reverse();
|
|
return result.reverse();
|
|
},
|
|
},
|
|
|
|
+
|
|
};
|
|
};
|