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

Fix lint errors and tests #67

Merged
merged 11 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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 .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12.4.0
12
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
script:
- yarn aegir dep-check
- yarn run lint
- yarn run build

- stage: test
name: chrome
Expand Down
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"name": "libp2p-noise",
"version": "1.1.2",
"main": "dist/src/index.js",
"types": "dist/src/dist/index.d.ts",
mpetrunic marked this conversation as resolved.
Show resolved Hide resolved
"files": [
"dist"
"dist",
"src"
mpetrunic marked this conversation as resolved.
Show resolved Hide resolved
],
"repository": "[email protected]:NodeFactoryIo/js-libp2p-noise.git",
"author": "NodeFactory <[email protected]>",
Expand All @@ -16,6 +18,7 @@
"scripts": {
"build": "aegir build --ts",
"lint": "aegir lint --ts",
"lint:fix": "aegir lint --ts --fix",
"test": "aegir test --ts",
"test:node": "aegir test -t node --ts",
"test:browser": "aegir test -t browser --ts",
Expand All @@ -27,6 +30,7 @@
"@types/mocha": "^5.2.7",
"aegir": "ipfs/aegir#feat/cosmiconfig",
"chai": "^4.2.0",
"karma-mocha-webworker": "^1.3.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

You should not need this, karma has a weird issue with this module and fails to load it if the package doesnt come from npm. So when we release aegir properly this can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

oh, did not know that, failed locally so ive added it. Will remove once we update aegir version

Copy link
Member Author

Choose a reason for hiding this comment

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

Even with released aegir it still says that it's missing

"mocha": "^6.2.2",
"sinon": "^8.1.0"
},
Expand All @@ -47,6 +51,12 @@
"bn.js": "4.4.0"
},
"eslintConfig": {
"extends": "./node_modules/aegir/src/config/eslintrc-ts.js"
"extends": "./node_modules/aegir/src/config/eslintrc-ts.js",
"rules": {
"@typescript-eslint/no-unused-vars": "error"
},
"ignorePatterns": [
"src/proto/payload.js"
]
}
}
13 changes: 6 additions & 7 deletions src/@types/it-length-prefixed/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
declare module "it-length-prefixed" {
/* eslint-disable @typescript-eslint/interface-name-prefix */
import BufferList from "bl";
import {Buffer} from "buffer"
declare module 'it-length-prefixed' {
import BufferList from 'bl'
import { Buffer } from 'buffer'

interface LengthDecoderFunction {
(data: Buffer | BufferList): number;
bytes: number;
}

interface LengthEncoderFunction {
(value: Buffer, target: number, offset: number): number|Buffer;
(value: number, target: Buffer, offset: number): number|Buffer;
bytes: number;
}

Expand All @@ -33,7 +32,7 @@ declare module "it-length-prefixed" {
MAX_DATA_LENGTH: number;
}

export const encode: Encoder;
export const decode: Decoder;
export const encode: Encoder
export const decode: Decoder

}
8 changes: 3 additions & 5 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export const NOISE_MSG_MAX_LENGTH_BYTES = 65535;
export const NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG = NOISE_MSG_MAX_LENGTH_BYTES - 16;

export const DUMP_SESSION_KEYS = process.env.DUMP_SESSION_KEYS;

export const NOISE_MSG_MAX_LENGTH_BYTES = 65535
export const NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG = NOISE_MSG_MAX_LENGTH_BYTES - 16

export const DUMP_SESSION_KEYS = process.env.DUMP_SESSION_KEYS
37 changes: 18 additions & 19 deletions src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
import { Buffer } from "buffer";
import {IHandshake} from "./@types/handshake-interface";
import {NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG} from "./constants";
import { Buffer } from 'buffer'
import { IHandshake } from './@types/handshake-interface'
import { NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } from './constants'

interface IReturnEncryptionWrapper {
(source: Iterable<Uint8Array>): AsyncIterableIterator<Uint8Array>;
}

// Returns generator that encrypts payload from the user
export function encryptStream(handshake: IHandshake): IReturnEncryptionWrapper {
export function encryptStream (handshake: IHandshake): IReturnEncryptionWrapper {
return async function * (source) {
for await (const chunk of source) {
const chunkBuffer = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.length);
const chunkBuffer = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.length)

for (let i = 0; i < chunkBuffer.length; i += NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG) {
let end = i + NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG;
let end = i + NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG
if (end > chunkBuffer.length) {
end = chunkBuffer.length;
end = chunkBuffer.length
}

const data = handshake.encrypt(chunkBuffer.slice(i, end), handshake.session);
yield data;
const data = handshake.encrypt(chunkBuffer.slice(i, end), handshake.session)
yield data
}
}
}
}


// Decrypt received payload to the user
export function decryptStream(handshake: IHandshake): IReturnEncryptionWrapper {
export function decryptStream (handshake: IHandshake): IReturnEncryptionWrapper {
return async function * (source) {
for await (const chunk of source) {
const chunkBuffer = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.length);
const chunkBuffer = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.length)

for (let i = 0; i < chunkBuffer.length; i += NOISE_MSG_MAX_LENGTH_BYTES) {
let end = i + NOISE_MSG_MAX_LENGTH_BYTES;
let end = i + NOISE_MSG_MAX_LENGTH_BYTES
if (end > chunkBuffer.length) {
end = chunkBuffer.length;
end = chunkBuffer.length
}

const chunk = chunkBuffer.slice(i, end);
const {plaintext: decrypted, valid} = await handshake.decrypt(chunk, handshake.session);
if(!valid) {
throw new Error("Failed to validate decrypted chunk");
const chunk = chunkBuffer.slice(i, end)
const { plaintext: decrypted, valid } = await handshake.decrypt(chunk, handshake.session)
if (!valid) {
throw new Error('Failed to validate decrypted chunk')
}
yield decrypted;
yield decrypted
}
}
}
Expand Down
59 changes: 30 additions & 29 deletions src/encoder.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@
import {Buffer} from "buffer";
import {bytes} from "./@types/basic";
import {MessageBuffer} from "./@types/handshake";
import { Buffer } from 'buffer'
import { bytes } from './@types/basic'
import { MessageBuffer } from './@types/handshake'
import BufferList from 'bl'

export const uint16BEEncode = (value, target, offset) => {
target = target || Buffer.allocUnsafe(2);
target.writeUInt16BE(value, offset);
return target;
};
uint16BEEncode.bytes = 2;
export const uint16BEEncode = (value: number, target: Buffer, offset: number): Buffer => {
target = target || Buffer.allocUnsafe(2)
target.writeUInt16BE(value, offset)
return target
}
uint16BEEncode.bytes = 2

export const uint16BEDecode = data => {
if (data.length < 2) throw RangeError('Could not decode int16BE');
return data.readUInt16BE(0);
};
uint16BEDecode.bytes = 2;
export const uint16BEDecode = (data: Buffer | BufferList): number => {
if (data.length < 2) throw RangeError('Could not decode int16BE')
return data.readUInt16BE(0)
}
uint16BEDecode.bytes = 2

// Note: IK and XX encoder usage is opposite (XX uses in stages encode0 where IK uses encode1)

export function encode0(message: MessageBuffer): bytes {
return Buffer.concat([message.ne, message.ciphertext]);
export function encode0 (message: MessageBuffer): bytes {
return Buffer.concat([message.ne, message.ciphertext])
}

export function encode1(message: MessageBuffer): bytes {
return Buffer.concat([message.ne, message.ns, message.ciphertext]);
export function encode1 (message: MessageBuffer): bytes {
return Buffer.concat([message.ne, message.ns, message.ciphertext])
}

export function encode2(message: MessageBuffer): bytes {
return Buffer.concat([message.ns, message.ciphertext]);
export function encode2 (message: MessageBuffer): bytes {
return Buffer.concat([message.ns, message.ciphertext])
}

export function decode0(input: bytes): MessageBuffer {
export function decode0 (input: bytes): MessageBuffer {
if (input.length < 32) {
throw new Error("Cannot decode stage 0 MessageBuffer: length less than 32 bytes.");
throw new Error('Cannot decode stage 0 MessageBuffer: length less than 32 bytes.')
}

return {
ne: input.slice(0, 32),
ciphertext: input.slice(32, input.length),
ns: Buffer.alloc(0),
ns: Buffer.alloc(0)
}
}

export function decode1(input: bytes): MessageBuffer {
export function decode1 (input: bytes): MessageBuffer {
if (input.length < 80) {
throw new Error("Cannot decode stage 1 MessageBuffer: length less than 80 bytes.");
throw new Error('Cannot decode stage 1 MessageBuffer: length less than 80 bytes.')
}

return {
ne: input.slice(0, 32),
ns: input.slice(32, 80),
ciphertext: input.slice(80, input.length),
ciphertext: input.slice(80, input.length)
}
}

export function decode2(input: bytes): MessageBuffer {
export function decode2 (input: bytes): MessageBuffer {
if (input.length < 48) {
throw new Error("Cannot decode stage 2 MessageBuffer: length less than 48 bytes.");
throw new Error('Cannot decode stage 2 MessageBuffer: length less than 48 bytes.')
}

return {
ne: Buffer.alloc(0),
ns: input.slice(0, 48),
ciphertext: input.slice(48, input.length),
ciphertext: input.slice(48, input.length)
}
}
14 changes: 8 additions & 6 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import BufferList from 'bl'

export class FailedIKError extends Error {
public initialMsg;
public initialMsg: string|BufferList|Buffer;

constructor(initialMsg, message?: string) {
super(message);
constructor (initialMsg: string|BufferList|Buffer, message?: string) {
super(message)

this.initialMsg = initialMsg;
this.name = "FailedIKhandshake";
this.initialMsg = initialMsg
this.name = 'FailedIKhandshake'
}
};
}
Loading