mirror of
https://github.com/jimeh/node-base58.git
synced 2026-02-19 07:36:40 +00:00
Compare commits
1 Commits
add-bitcoi
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d3210d989 |
19
package-lock.json
generated
19
package-lock.json
generated
@@ -350,10 +350,21 @@
|
||||
}
|
||||
},
|
||||
"eslint-utils": {
|
||||
"version": "1.3.1",
|
||||
"resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-1.3.1.tgz",
|
||||
"integrity": "sha512-Z7YjnIldX+2XMcjr7ZkgEsOj/bREONV60qYeB/bjMAqqqZ4zxKyWX+BOUkdmRmA9riiIPVvo5x86m5elviOk0Q==",
|
||||
"dev": true
|
||||
"version": "1.4.3",
|
||||
"resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-1.4.3.tgz",
|
||||
"integrity": "sha512-fbBN5W2xdY45KulGXmLHZ3c3FHfVYmKg0IrAKGOkT/464PQsx2UeIzfz1RmEci+KLm1bBaAzZAh8+/E+XAeZ8Q==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"eslint-visitor-keys": "^1.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"eslint-visitor-keys": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-1.1.0.tgz",
|
||||
"integrity": "sha512-8y9YjtM1JBJU/A9Kc+SbaOV4y29sSWckBwMHa+FGtVj5gN/sbnKDf6xJUl+8g7FAij9LVaP8C24DUiH/f/2Z9A==",
|
||||
"dev": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"eslint-visitor-keys": {
|
||||
"version": "1.0.0",
|
||||
|
||||
@@ -1,17 +1,9 @@
|
||||
const alphabets = {
|
||||
flickr: "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ",
|
||||
bitcoin: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",
|
||||
ripple: "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz"
|
||||
};
|
||||
const base = alphabets.flickr.length;
|
||||
const alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
|
||||
const base = alphabet.length;
|
||||
|
||||
// Create a lookup table to fetch character index
|
||||
const alphabetLookups = Object.keys(alphabets).reduce((lookup, name) => {
|
||||
lookup[name] = [...alphabets[name]].reduce((memo, char, index) => {
|
||||
memo[char] = index;
|
||||
return memo;
|
||||
}, {});
|
||||
|
||||
const alphabetLookup = [...alphabet].reduce((lookup, char, index) => {
|
||||
lookup[char] = index;
|
||||
return lookup;
|
||||
}, {});
|
||||
|
||||
@@ -33,13 +25,13 @@ function assertString(str) {
|
||||
}
|
||||
}
|
||||
|
||||
function assertBase58Character(character, alphabet) {
|
||||
if (alphabetLookups[alphabet][character] === undefined) {
|
||||
function assertBase58Character(character) {
|
||||
if (alphabetLookup[character] === undefined) {
|
||||
throw new Error("Value passed is not a valid Base58 string.");
|
||||
}
|
||||
}
|
||||
|
||||
exports.int_to_base58 = exports.encode = function(num, alphabet = "flickr") {
|
||||
exports.int_to_base58 = exports.encode = function(num) {
|
||||
let str = "";
|
||||
let modulus;
|
||||
|
||||
@@ -49,18 +41,18 @@ exports.int_to_base58 = exports.encode = function(num, alphabet = "flickr") {
|
||||
|
||||
while (num >= base) {
|
||||
modulus = num % base;
|
||||
str = alphabets[alphabet][modulus] + str;
|
||||
str = alphabet[modulus] + str;
|
||||
num = Math.floor(num / base);
|
||||
}
|
||||
|
||||
return alphabets[alphabet][num] + str;
|
||||
return alphabet[num] + str;
|
||||
};
|
||||
|
||||
exports.base58_to_int = exports.decode = function(str, alphabet = "flickr") {
|
||||
exports.base58_to_int = exports.decode = function(str) {
|
||||
assertString(str);
|
||||
|
||||
return [...str].reverse().reduce((num, character, index) => {
|
||||
assertBase58Character(character, alphabet);
|
||||
return num + alphabetLookups[alphabet][character] * Math.pow(base, index);
|
||||
assertBase58Character(character);
|
||||
return num + alphabetLookup[character] * Math.pow(base, index);
|
||||
}, 0);
|
||||
};
|
||||
|
||||
@@ -2,9 +2,9 @@ const assert = require("assert");
|
||||
const examples = require("./examples");
|
||||
const base58 = require("..");
|
||||
|
||||
function exampleRunner(alphabet, callback) {
|
||||
Object.keys(alphabet).forEach(function(str) {
|
||||
callback(str, alphabet[str]);
|
||||
function exampleRunner(callback) {
|
||||
Object.keys(examples).forEach(function(str) {
|
||||
callback(str, examples[str]);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -13,20 +13,18 @@ describe("Base58", function() {
|
||||
var valid = true;
|
||||
var count = 0;
|
||||
|
||||
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;
|
||||
}
|
||||
});
|
||||
exampleRunner(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() {
|
||||
@@ -40,50 +38,18 @@ describe("Base58", function() {
|
||||
});
|
||||
|
||||
describe(".int_to_base58()", function() {
|
||||
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);
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
it("encodes number to Base58 string", function() {
|
||||
exampleRunner(function(str, num) {
|
||||
assert.strictEqual(base58.int_to_base58(num), 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 after first converting it to an integer", function() {
|
||||
exampleRunner(function(str, num) {
|
||||
assert.strictEqual(base58.int_to_base58(num.toString()), 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);
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when passed a non number", function() {
|
||||
@@ -148,81 +114,35 @@ describe("Base58", function() {
|
||||
});
|
||||
|
||||
describe(".base58_to_int()", function() {
|
||||
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);
|
||||
|
||||
assert.strictEqual(result, num);
|
||||
});
|
||||
});
|
||||
|
||||
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."
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
it("decodes base58 string to number", function() {
|
||||
exampleRunner(function(str, num) {
|
||||
assert.strictEqual(base58.base58_to_int(str), num);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
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 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."
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
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