Skip to content

Commit

Permalink
Merge pull request #1763 from zb3/fix-base58
Browse files Browse the repository at this point in the history
  • Loading branch information
a3957273 committed Apr 1, 2024
2 parents 866c9a9 + 4c6200f commit c46660a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
8 changes: 3 additions & 5 deletions src/core/operations/FromBase58.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class FromBase58 extends Operation {
run(input, args) {
let alphabet = args[0] || ALPHABET_OPTIONS[0].value;
const removeNonAlphaChars = args[1] === undefined ? true : args[1],
result = [0];
result = [];

alphabet = Utils.expandAlphRange(alphabet).join("");

Expand All @@ -87,11 +87,9 @@ class FromBase58 extends Operation {
}
}

let carry = result[0] * 58 + index;
result[0] = carry & 0xFF;
carry = carry >> 8;
let carry = index;

for (let i = 1; i < result.length; i++) {
for (let i = 0; i < result.length; i++) {
carry += result[i] * 58;
result[i] = carry & 0xFF;
carry = carry >> 8;
Expand Down
8 changes: 3 additions & 5 deletions src/core/operations/ToBase58.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ToBase58 extends Operation {
run(input, args) {
input = new Uint8Array(input);
let alphabet = args[0] || ALPHABET_OPTIONS[0].value,
result = [0];
result = [];

alphabet = Utils.expandAlphRange(alphabet).join("");

Expand All @@ -60,11 +60,9 @@ class ToBase58 extends Operation {
}

input.forEach(function(b) {
let carry = (result[0] << 8) + b;
result[0] = carry % 58;
carry = (carry / 58) | 0;
let carry = b;

for (let i = 1; i < result.length; i++) {
for (let i = 0; i < result.length; i++) {
carry += result[i] << 8;
result[i] = carry % 58;
carry = (carry / 58) | 0;
Expand Down
22 changes: 22 additions & 0 deletions tests/operations/tests/Base58.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ TestRegister.addTests([
},
],
},
{
name: "To Base58 all null",
input: "\0\0\0\0\0\0",
expectedOutput: "111111",
recipeConfig: [
{
op: "To Base58",
args: ["123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"],
},
],
},
{
name: "From Base58 all null",
input: "111111",
expectedOutput: "\0\0\0\0\0\0",
recipeConfig: [
{
op: "From Base58",
args: ["123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"],
},
],
},
{
name: "To Base58 with null prefix and suffix",
input: "\0\0\0Hello\0\0\0",
Expand Down

0 comments on commit c46660a

Please sign in to comment.