From f4de4de8c17f4d45e7e1f9f764f8083525baf52e Mon Sep 17 00:00:00 2001 From: Brian Whitney Date: Sun, 21 Oct 2018 21:10:49 -0400 Subject: [PATCH] Fixing the babel, scrypt, and base58 issues --- .babelrc | 2 +- src/core/operations/FromBase58.mjs | 10 ++++++++++ src/core/operations/Scrypt.mjs | 2 +- src/core/operations/ToBase58.mjs | 7 ++++++- test/tests/operations/Base58.mjs | 22 ++++++++++++++++++++++ 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/.babelrc b/.babelrc index 4d90a44f..451186bd 100644 --- a/.babelrc +++ b/.babelrc @@ -8,7 +8,7 @@ "node": "6.5" }, "modules": false, - "useBuiltIns": "usage" + "useBuiltIns": "entry" }] ], "plugins": [ diff --git a/src/core/operations/FromBase58.mjs b/src/core/operations/FromBase58.mjs index 1e70aa5b..966a95be 100644 --- a/src/core/operations/FromBase58.mjs +++ b/src/core/operations/FromBase58.mjs @@ -71,6 +71,11 @@ class FromBase58 extends Operation { if (input.length === 0) return []; + let zeroPrefix = 0; + for (let i = 0; i < input.length && input[i] === alphabet[0]; i++) { + zeroPrefix++; + } + [].forEach.call(input, function(c, charIndex) { const index = alphabet.indexOf(c); @@ -98,6 +103,11 @@ class FromBase58 extends Operation { } }); + while (zeroPrefix > 0) { + result.push(0); + zeroPrefix--; + } + return result.reverse(); } diff --git a/src/core/operations/Scrypt.mjs b/src/core/operations/Scrypt.mjs index 029a1beb..ec5ce492 100644 --- a/src/core/operations/Scrypt.mjs +++ b/src/core/operations/Scrypt.mjs @@ -62,7 +62,7 @@ class Scrypt extends Operation { * @returns {string} */ run(input, args) { - const salt = Utils.convertToByteString(args[0].string || "", args[0].option), + const salt = Buffer.from(Utils.convertToByteArray(args[0].string || "", args[0].option)), iterations = args[1], memFactor = args[2], parallelFactor = args[3], diff --git a/src/core/operations/ToBase58.mjs b/src/core/operations/ToBase58.mjs index ac3d7267..3e2c6a60 100644 --- a/src/core/operations/ToBase58.mjs +++ b/src/core/operations/ToBase58.mjs @@ -53,6 +53,11 @@ class ToBase58 extends Operation { if (input.length === 0) return ""; + let zeroPrefix = 0; + for (let i = 0; i < input.length && input[i] === 0; i++) { + zeroPrefix++; + } + input.forEach(function(b) { let carry = (result[0] << 8) + b; result[0] = carry % 58; @@ -74,7 +79,7 @@ class ToBase58 extends Operation { return alphabet[b]; }).reverse().join(""); - while (result.length < input.length) { + while (zeroPrefix--) { result = alphabet[0] + result; } diff --git a/test/tests/operations/Base58.mjs b/test/tests/operations/Base58.mjs index ccb7a26c..3b284223 100644 --- a/test/tests/operations/Base58.mjs +++ b/test/tests/operations/Base58.mjs @@ -53,6 +53,28 @@ TestRegister.addTests([ }, ], }, + { + name: "To Base58 with null prefix and suffix", + input: "\0\0\0Hello\0\0\0", + expectedOutput: "111D7LMXYjHjTu", + recipeConfig: [ + { + op: "To Base58", + args: ["123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"], + }, + ], + }, + { + name: "From Base58 with null prefix and suffix", + input: "111D7LMXYjHjTu", + expectedOutput: "\0\0\0Hello\0\0\0", + recipeConfig: [ + { + op: "From Base58", + args: ["123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"], + }, + ], + }, { name: "From Base58 (Bitcoin): 'StV1DL6CwTryKyV'", input: "StV1DL6CwTryKyV",