Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the babel, scrypt, and base58 issues #403

Merged
merged 2 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"node": "6.5"
},
"modules": false,
"useBuiltIns": "usage"
"useBuiltIns": "entry"
}]
],
"plugins": [
Expand Down
9 changes: 9 additions & 0 deletions src/core/operations/FromBase58.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -98,6 +103,10 @@ class FromBase58 extends Operation {
}
});

while (zeroPrefix--) {
result.push(0);
}

return result.reverse();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/operations/Scrypt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
7 changes: 6 additions & 1 deletion src/core/operations/ToBase58.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -74,7 +79,7 @@ class ToBase58 extends Operation {
return alphabet[b];
}).reverse().join("");

while (result.length < input.length) {
while (zeroPrefix--) {
result = alphabet[0] + result;
}

Expand Down
22 changes: 22 additions & 0 deletions test/tests/operations/Base58.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down