Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

remove buffer-dep, replace with bufferFrom #445

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,31 @@
},
"homepage": "https://github.com/SkynetLabs/skynet-js",
"dependencies": {
"@skynetlabs/tus-js-client": "^2.3.1",
"async-mutex": "^0.3.2",
"axios": "^0.27.2",
"base32-decode": "^1.0.0",
"base32-encode": "^1.1.1",
"base64-js": "^1.3.1",
"blakejs": "^1.1.0",
"buffer": "^6.0.1",
"buffer-from": "^1.1.2",
"mime": "^3.0.0",
"path-browserify": "^1.0.1",
"pbkdf2": "^3.1.2",
"post-me": "^0.4.5",
"randombytes": "^2.1.0",
"sjcl": "^1.0.8",
"skynet-mysky-utils": "^0.3.0",
"@skynetlabs/tus-js-client": "^2.3.0",
"tweetnacl": "^1.0.3",
"url-join": "^4.0.1",
"url-parse": "^1.5.1"
},
"devDependencies": {
"@types/base64-js": "^1.3.0",
"@types/buffer-from": "^1.1.0",
"@types/jest": "^27.0.1",
"@types/mime": "^2.0.3",
"@types/minimist": "^1.2.2",
"@types/node": "^15.0.1",
"@types/randombytes": "^2.0.0",
"@types/sjcl": "^1.0.29",
"@types/pbkdf2": "^3.1.0",
"@types/url-join": "^4.0.0",
"@types/url-parse": "^1.4.3",
"@typescript-eslint/eslint-plugin": "^4.3.0",
Expand Down
14 changes: 6 additions & 8 deletions src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { misc, codec } from "sjcl";
import { Buffer } from "buffer";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As weird as it looks, having this line in this file, though unused, was preventing a bug in the browser. This should be tested before we merge it in - I can do that by running the test-skapp tests when I have time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With latest commits, I think this is fixed.

import { blake2bFinal, blake2bInit, blake2bUpdate } from "blakejs";
import randomBytes from "randombytes";
import { hash, sign } from "tweetnacl";
import { hash, sign, randomBytes } from "tweetnacl";
import bufferFrom from "buffer-from";
import { pbkdf2Sync } from "pbkdf2";

import { RegistryEntry } from "./registry";
import { hexToUint8Array, stringToUint8ArrayUtf8, toHexString } from "./utils/string";
Expand Down Expand Up @@ -87,9 +86,8 @@ export function genKeyPairFromSeed(seed: string): KeyPair {
validateString("seed", seed, "parameter");

// Get a 32-byte key.
const derivedKey = misc.pbkdf2(seed, "", 1000, 32 * 8);
const derivedKeyHex = codec.hex.fromBits(derivedKey);
const { publicKey, secretKey } = sign.keyPair.fromSeed(hexToUint8Array(derivedKeyHex));
const derivedKey = pbkdf2Sync(seed, "", 1000, 32, "sha256");
kwypchlo marked this conversation as resolved.
Show resolved Hide resolved
const { publicKey, secretKey } = sign.keyPair.fromSeed(Uint8Array.from(derivedKey));

return { publicKey: toHexString(publicKey), privateKey: toHexString(secretKey) };
}
Expand Down Expand Up @@ -160,6 +158,6 @@ export function sha512(message: Uint8Array | string): Uint8Array {
function genRandomSeed(length: number): string {
// Cryptographically-secure random number generator. It should use the
// built-in crypto.getRandomValues in the browser.
const array = randomBytes(length);
const array = bufferFrom(randomBytes(length));
return toHexString(array);
}
5 changes: 2 additions & 3 deletions src/mysky/encrypted_files.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import randomBytes from "randombytes";
import { sanitizePath } from "skynet-mysky-utils";
import { secretbox } from "tweetnacl";
import { secretbox, randomBytes } from "tweetnacl";

import { HASH_LENGTH, sha512 } from "../crypto";
import { hexToUint8Array, stringToUint8ArrayUtf8, toHexString, uint8ArrayToStringUtf8 } from "../utils/string";
Expand Down Expand Up @@ -154,7 +153,7 @@ export function encryptJSONFile(json: JsonData, metadata: EncryptedFileMetadata,
data = new Uint8Array([...data, ...new Uint8Array(finalSize - data.length)]);

// Generate a random nonce.
const nonce = new Uint8Array(randomBytes(ENCRYPTION_NONCE_LENGTH));
const nonce = randomBytes(ENCRYPTION_NONCE_LENGTH);

// Encrypt the data.
const encryptedBytes = secretbox(data, nonce, key);
Expand Down
4 changes: 2 additions & 2 deletions src/registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AxiosResponse } from "axios";
import { Buffer } from "buffer";
import bufferFrom from "buffer-from";
import { sign } from "tweetnacl";

import { SkynetClient } from "./client";
Expand Down Expand Up @@ -203,7 +203,7 @@ export async function getEntry(

// Convert the revision from a string to bigint.
const revision = BigInt(response.data.revision);
const signature = Buffer.from(hexToUint8Array(response.data.signature));
const signature = bufferFrom(hexToUint8Array(response.data.signature));
// Use empty array if the data is empty.
let data = new Uint8Array([]);
if (response.data.data) {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Buffer } from "buffer";
import bufferFrom from "buffer-from";

import { validateHexString, validateString, validationError } from "./validation";

Expand Down Expand Up @@ -103,7 +103,7 @@ export function trimUriPrefix(str: string, prefix: string): string {
export function stringToUint8ArrayUtf8(str: string): Uint8Array {
validateString("str", str, "parameter");

return Uint8Array.from(Buffer.from(str, "utf-8"));
return Uint8Array.from(bufferFrom(str, "utf-8"));
}

/**
Expand All @@ -113,7 +113,7 @@ export function stringToUint8ArrayUtf8(str: string): Uint8Array {
* @returns - The string.
*/
export function uint8ArrayToStringUtf8(array: Uint8Array): string {
return Buffer.from(array).toString("utf-8");
return bufferFrom(array).toString("utf-8");
}

/**
Expand Down
Loading