1 Commits

Author SHA1 Message Date
dependabot[bot]
c6443c2c27 Bump ajv from 6.5.4 to 6.12.6
Bumps [ajv](https://github.com/ajv-validator/ajv) from 6.5.4 to 6.12.6.
- [Release notes](https://github.com/ajv-validator/ajv/releases)
- [Commits](https://github.com/ajv-validator/ajv/compare/v6.5.4...v6.12.6)

---
updated-dependencies:
- dependency-name: ajv
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-02-11 12:53:32 +00:00
4 changed files with 571 additions and 677 deletions

22
package-lock.json generated
View File

@@ -40,15 +40,23 @@
}
},
"ajv": {
"version": "6.5.4",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.5.4.tgz",
"integrity": "sha512-4Wyjt8+t6YszqaXnLDfMmG/8AlO5Zbcsy3ATHncCzjW/NoPzAId8AK6749Ybjmdt+kUY1gP60fCu46oDxPv/mg==",
"version": "6.12.6",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
"integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==",
"dev": true,
"requires": {
"fast-deep-equal": "^2.0.1",
"fast-deep-equal": "^3.1.1",
"fast-json-stable-stringify": "^2.0.0",
"json-schema-traverse": "^0.4.1",
"uri-js": "^4.2.2"
},
"dependencies": {
"fast-deep-equal": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==",
"dev": true
}
}
},
"ajv-keywords": {
@@ -418,12 +426,6 @@
"tmp": "^0.0.33"
}
},
"fast-deep-equal": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz",
"integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=",
"dev": true
},
"fast-diff": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/fast-diff/-/fast-diff-1.1.2.tgz",

View File

@@ -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);
};

View File

@@ -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.";
}
);
});
});
});

File diff suppressed because it is too large Load Diff