Initial support for bitcoin and ripple alphabets

This commit is contained in:
2018-11-19 00:46:33 +00:00
parent ddfa8632e7
commit 071a882c3d
3 changed files with 670 additions and 562 deletions

View File

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