mirror of
https://github.com/jimeh/node-base58.git
synced 2026-02-19 07:36:40 +00:00
Initial support for bitcoin and ripple alphabets
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
const alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
|
||||
const base = alphabet.length;
|
||||
const alphabets = {
|
||||
flickr: "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ",
|
||||
bitcoin: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",
|
||||
ripple: "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz"
|
||||
};
|
||||
const base = alphabets.flickr.length;
|
||||
|
||||
// Create a lookup table to fetch character index
|
||||
const alphabetLookup = [...alphabet].reduce((lookup, char, index) => {
|
||||
lookup[char] = index;
|
||||
const alphabetLookups = Object.keys(alphabets).reduce((lookup, name) => {
|
||||
lookup[name] = [...alphabets[name]].reduce((memo, char, index) => {
|
||||
memo[char] = index;
|
||||
return memo;
|
||||
}, {});
|
||||
|
||||
return lookup;
|
||||
}, {});
|
||||
|
||||
@@ -25,13 +33,13 @@ function assertString(str) {
|
||||
}
|
||||
}
|
||||
|
||||
function assertBase58Character(character) {
|
||||
if (alphabetLookup[character] === undefined) {
|
||||
function assertBase58Character(character, alphabet) {
|
||||
if (alphabetLookups[alphabet][character] === undefined) {
|
||||
throw new Error("Value passed is not a valid Base58 string.");
|
||||
}
|
||||
}
|
||||
|
||||
exports.int_to_base58 = exports.encode = function(num) {
|
||||
exports.int_to_base58 = exports.encode = function(num, alphabet = "flickr") {
|
||||
let str = "";
|
||||
let modulus;
|
||||
|
||||
@@ -41,18 +49,18 @@ exports.int_to_base58 = exports.encode = function(num) {
|
||||
|
||||
while (num >= base) {
|
||||
modulus = num % base;
|
||||
str = alphabet[modulus] + str;
|
||||
str = alphabets[alphabet][modulus] + str;
|
||||
num = Math.floor(num / base);
|
||||
}
|
||||
|
||||
return alphabet[num] + str;
|
||||
return alphabets[alphabet][num] + str;
|
||||
};
|
||||
|
||||
exports.base58_to_int = exports.decode = function(str) {
|
||||
exports.base58_to_int = exports.decode = function(str, alphabet = "flickr") {
|
||||
assertString(str);
|
||||
|
||||
return [...str].reverse().reduce((num, character, index) => {
|
||||
assertBase58Character(character);
|
||||
return num + alphabetLookup[character] * Math.pow(base, index);
|
||||
assertBase58Character(character, alphabet);
|
||||
return num + alphabetLookups[alphabet][character] * Math.pow(base, index);
|
||||
}, 0);
|
||||
};
|
||||
|
||||
@@ -2,9 +2,9 @@ const assert = require("assert");
|
||||
const examples = require("./examples");
|
||||
const base58 = require("..");
|
||||
|
||||
function exampleRunner(callback) {
|
||||
Object.keys(examples).forEach(function(str) {
|
||||
callback(str, examples[str]);
|
||||
function exampleRunner(alphabet, callback) {
|
||||
Object.keys(alphabet).forEach(function(str) {
|
||||
callback(str, alphabet[str]);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -13,18 +13,20 @@ describe("Base58", function() {
|
||||
var valid = true;
|
||||
var count = 0;
|
||||
|
||||
exampleRunner(function(str, num) {
|
||||
count++;
|
||||
if (typeof str !== "string") {
|
||||
valid = false;
|
||||
}
|
||||
if (typeof num !== "number") {
|
||||
valid = false;
|
||||
}
|
||||
});
|
||||
for (var alphabetName in examples.alphabets) {
|
||||
exampleRunner(examples.alphabets[alphabetName], function(str, num) {
|
||||
count++;
|
||||
if (typeof str !== "string") {
|
||||
valid = false;
|
||||
}
|
||||
if (typeof num !== "number") {
|
||||
valid = false;
|
||||
}
|
||||
});
|
||||
|
||||
assert.strictEqual(count > 0, true, "Expected there to be examples");
|
||||
assert.strictEqual(valid, true, "Expected the examples to be valid");
|
||||
assert.strictEqual(count > 0, true, "Expected there to be examples");
|
||||
assert.strictEqual(valid, true, "Expected the examples to be valid");
|
||||
}
|
||||
});
|
||||
|
||||
describe("backwards compatibility", function() {
|
||||
@@ -38,17 +40,49 @@ describe("Base58", function() {
|
||||
});
|
||||
|
||||
describe(".int_to_base58()", function() {
|
||||
it("encodes number to Base58 string", function() {
|
||||
exampleRunner(function(str, num) {
|
||||
assert.strictEqual(base58.int_to_base58(num), str);
|
||||
});
|
||||
});
|
||||
for (var alphabetName in examples.alphabets) {
|
||||
describe("when using " + alphabetName + " alphabet", function() {
|
||||
it("encodes number to Base58 string", function() {
|
||||
exampleRunner(examples.alphabets[alphabetName], function(str, num) {
|
||||
let result = base58.int_to_base58(num, alphabetName);
|
||||
|
||||
describe("when passed a string only containing numbers", function() {
|
||||
it("encodes string after first converting it to an integer", function() {
|
||||
exampleRunner(function(str, num) {
|
||||
assert.strictEqual(base58.int_to_base58(num.toString()), str);
|
||||
assert.strictEqual(result, str);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when passed a string only containing numbers", function() {
|
||||
it("encodes string after first converting it to an integer", function() {
|
||||
exampleRunner(examples.alphabets[alphabetName], function(str, num) {
|
||||
let result = base58.int_to_base58(num.toString(), alphabetName);
|
||||
|
||||
assert.strictEqual(result, str);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe("when alphabet is not specified", function() {
|
||||
it("encodes number to Base58 string with flickr alphabet", function() {
|
||||
exampleRunner(examples.alphabets.flickr, function(str, num) {
|
||||
let result = base58.int_to_base58(num);
|
||||
|
||||
assert.strictEqual(result, str);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when passed a string only containing numbers", function() {
|
||||
it(
|
||||
"encodes string with flickr alphabet " +
|
||||
"after first converting it to an integer",
|
||||
function() {
|
||||
exampleRunner(examples.alphabets.flickr, function(str, num) {
|
||||
let result = base58.int_to_base58(num.toString());
|
||||
|
||||
assert.strictEqual(result, str);
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -114,35 +148,81 @@ describe("Base58", function() {
|
||||
});
|
||||
|
||||
describe(".base58_to_int()", function() {
|
||||
it("decodes base58 string to number", function() {
|
||||
exampleRunner(function(str, num) {
|
||||
assert.strictEqual(base58.base58_to_int(str), num);
|
||||
});
|
||||
});
|
||||
for (var alphabetName in examples.alphabets) {
|
||||
describe("when using " + alphabetName + " alphabet", function() {
|
||||
it("decodes base58 string to number", function() {
|
||||
exampleRunner(examples.alphabets[alphabetName], function(str, num) {
|
||||
let result = base58.base58_to_int(str, alphabetName);
|
||||
|
||||
describe("when passed a non string", function() {
|
||||
it("throws an error", function() {
|
||||
assert.throws(
|
||||
function() {
|
||||
base58.base58_to_int(123);
|
||||
},
|
||||
function(err) {
|
||||
return err.message === "Value passed is not a string.";
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
assert.strictEqual(result, num);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when passed a non base58 string", function() {
|
||||
it("throws an error", function() {
|
||||
assert.throws(
|
||||
function() {
|
||||
base58.base58_to_int(">_<");
|
||||
},
|
||||
function(err) {
|
||||
return err.message === "Value passed is not a valid Base58 string.";
|
||||
}
|
||||
);
|
||||
describe("when passed a non string", function() {
|
||||
it("throws an error", function() {
|
||||
assert.throws(
|
||||
function() {
|
||||
base58.base58_to_int(123, alphabetName);
|
||||
},
|
||||
function(err) {
|
||||
return err.message === "Value passed is not a string.";
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when passed a non base58 string", function() {
|
||||
it("throws an error", function() {
|
||||
assert.throws(
|
||||
function() {
|
||||
base58.base58_to_int(">_<", alphabetName);
|
||||
},
|
||||
function(err) {
|
||||
return (
|
||||
err.message === "Value passed is not a valid Base58 string."
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe("when alphabet is not specified", function() {
|
||||
it("decodes base58 string to number with flickr alphabet", function() {
|
||||
exampleRunner(examples.alphabets.flickr, function(str, num) {
|
||||
let result = base58.base58_to_int(str);
|
||||
|
||||
assert.strictEqual(result, num);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when passed a non string", function() {
|
||||
it("throws an error", function() {
|
||||
assert.throws(
|
||||
function() {
|
||||
base58.base58_to_int(123);
|
||||
},
|
||||
function(err) {
|
||||
return err.message === "Value passed is not a string.";
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when passed a non base58 string", function() {
|
||||
it("throws an error", function() {
|
||||
assert.throws(
|
||||
function() {
|
||||
base58.base58_to_int(">_<");
|
||||
},
|
||||
function(err) {
|
||||
return (
|
||||
err.message === "Value passed is not a valid Base58 string."
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
1020
test/examples.js
1020
test/examples.js
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user