From ca39bc5d99fcae28e3aeb0b7e309382679836ba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Fri, 19 Jun 2020 13:06:31 +0200 Subject: [PATCH] lint fixes --- package.json | 8 +++++++- src/encoder.ts | 4 ++-- src/errors.ts | 4 ++-- src/handshake-xx-fallback.ts | 1 + src/handshakes/abstract-handshake.ts | 2 +- src/logger.ts | 2 +- src/noise.ts | 10 +++++----- src/utils.ts | 8 ++++---- test/fixtures/peer.ts | 10 +++++----- test/handshakes/ik.spec.ts | 13 ++++++------- test/handshakes/xx.spec.ts | 10 +++++----- test/ik-handshake.spec.ts | 5 ++--- test/keycache.spec.ts | 10 ++++------ test/noise.spec.ts | 18 +++++------------- test/utils.ts | 6 +++--- test/xx-fallback-handshake.spec.ts | 5 ++--- 16 files changed, 55 insertions(+), 61 deletions(-) diff --git a/package.json b/package.json index 920a0b8..e1e2eb2 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,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" + ] } } diff --git a/src/encoder.ts b/src/encoder.ts index 08877f3..f60ea3a 100644 --- a/src/encoder.ts +++ b/src/encoder.ts @@ -2,14 +2,14 @@ import { Buffer } from 'buffer' import { bytes } from './@types/basic' import { MessageBuffer } from './@types/handshake' -export const uint16BEEncode = (value, target, offset) => { +export const uint16BEEncode = (value: number, target: bytes, offset: number): bytes => { target = target || Buffer.allocUnsafe(2) target.writeUInt16BE(value, offset) return target } uint16BEEncode.bytes = 2 -export const uint16BEDecode = data => { +export const uint16BEDecode = (data: bytes): number => { if (data.length < 2) throw RangeError('Could not decode int16BE') return data.readUInt16BE(0) } diff --git a/src/errors.ts b/src/errors.ts index 8a52f23..e04d520 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -1,7 +1,7 @@ export class FailedIKError extends Error { - public initialMsg; + public initialMsg: string; - constructor (initialMsg, message?: string) { + constructor (initialMsg: string, message?: string) { super(message) this.initialMsg = initialMsg diff --git a/src/handshake-xx-fallback.ts b/src/handshake-xx-fallback.ts index 4c19a21..10a08a1 100644 --- a/src/handshake-xx-fallback.ts +++ b/src/handshake-xx-fallback.ts @@ -32,6 +32,7 @@ export class XXFallbackHandshake extends XXHandshake { } // stage 0 + // eslint-disable-next-line require-await public async propose (): Promise { if (this.isInitiator) { this.xx.sendMessage(this.session, Buffer.alloc(0), this.ephemeralKeys) diff --git a/src/handshakes/abstract-handshake.ts b/src/handshakes/abstract-handshake.ts index 83a5d53..275152b 100644 --- a/src/handshakes/abstract-handshake.ts +++ b/src/handshakes/abstract-handshake.ts @@ -157,7 +157,7 @@ export abstract class AbstractHandshake { } } - protected split (ss: SymmetricState) { + protected split (ss: SymmetricState): {cs1: CipherState, cs2: CipherState} { const [tempk1, tempk2] = getHkdf(ss.ck, Buffer.alloc(0)) const cs1 = this.initializeKey(tempk1) const cs2 = this.initializeKey(tempk2) diff --git a/src/logger.ts b/src/logger.ts index 2cd1025..eda9715 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -1,7 +1,7 @@ import debug from 'debug' import { DUMP_SESSION_KEYS } from './constants' import { KeyPair } from './@types/libp2p' -import { NoiseSession, SymmetricState } from './@types/handshake' +import { NoiseSession } from './@types/handshake' export const logger = debug('libp2p:noise') diff --git a/src/noise.ts b/src/noise.ts index 1c97c0a..24d94b6 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -40,8 +40,8 @@ export class Noise implements INoiseConnection { /** * - * @param staticNoiseKey x25519 private key, reuse for faster handshakes - * @param earlyData + * @param {bytes} staticNoiseKey x25519 private key, reuse for faster handshakes + * @param {bytes} earlyData */ constructor (staticNoiseKey?: bytes, earlyData?: bytes) { this.earlyData = earlyData || Buffer.alloc(0) @@ -62,7 +62,7 @@ export class Noise implements INoiseConnection { /** * Encrypt outgoing data to the remote party (handshake as initiator) * @param {PeerId} localPeer - PeerId of the receiving peer - * @param connection - streaming iterable duplex that will be encrypted + * @param {any} connection - streaming iterable duplex that will be encrypted * @param {PeerId} remotePeer - PeerId of the remote peer. Used to validate the integrity of the remote peer. * @returns {Promise} */ @@ -93,7 +93,7 @@ export class Noise implements INoiseConnection { /** * Decrypt incoming data (handshake as responder). * @param {PeerId} localPeer - PeerId of the receiving peer. - * @param connection - streaming iterable duplex that will be encryption. + * @param {any} connection - streaming iterable duplex that will be encryption. * @param {PeerId} remotePeer - optional PeerId of the initiating peer, if known. This may only exist during transport upgrades. * @returns {Promise} */ @@ -124,7 +124,7 @@ export class Noise implements INoiseConnection { /** * If Noise pipes supported, tries IK handshake first with XX as fallback if it fails. * If noise pipes disabled or remote peer static key is unknown, use XX. - * @param params + * @param {HandshakeParams} params */ private async performHandshake (params: HandshakeParams): Promise { const payload = await getPayload(params.localPeer, this.staticKeys.publicKey, this.earlyData) diff --git a/src/utils.ts b/src/utils.ts index 144b4b3..b39b07f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -36,11 +36,11 @@ export async function getPayload ( ) } -export async function createHandshakePayload ( +export function createHandshakePayload ( libp2pPublicKey: bytes, signedPayload: bytes, earlyData?: bytes -): Promise { +): bytes { const payloadInit = NoiseHandshakePayloadProto.create({ identityKey: libp2pPublicKey, identitySig: signedPayload, @@ -51,14 +51,14 @@ export async function createHandshakePayload ( } export async function signPayload (peerId: PeerId, payload: bytes): Promise { - return peerId.privKey.sign(payload) + return await peerId.privKey.sign(payload) } export async function getPeerIdFromPayload (payload: pb.INoiseHandshakePayload): Promise { return await PeerId.createFromPubKey(Buffer.from(payload.identityKey as Uint8Array)) } -export async function decodePayload (payload: bytes|Uint8Array): Promise { +export function decodePayload (payload: bytes|Uint8Array): pb.INoiseHandshakePayload { return NoiseHandshakePayloadProto.toObject( NoiseHandshakePayloadProto.decode(Buffer.from(payload)) ) as INoisePayload diff --git a/test/fixtures/peer.ts b/test/fixtures/peer.ts index 9b7b0fb..15f394a 100644 --- a/test/fixtures/peer.ts +++ b/test/fixtures/peer.ts @@ -19,16 +19,16 @@ const peers = [{ pubKey: 'CAESIMbnikZaPciAMZhUXqDRVCs7VFOBtmlIk26g0GgOotDA' }] -export async function createPeerIdsFromFixtures (length) { - return Promise.all( +export async function createPeerIdsFromFixtures (length:number): Promise { + return await Promise.all( Array.from({ length }).map((_, i) => PeerId.createFromJSON(peers[i])) ) } -export async function createPeerIds (length) { - const peerIds: any[] = [] +export async function createPeerIds (length: number): Promise { + const peerIds: PeerId[] = [] for (let i = 0; i < length; i++) { - const id = await PeerId.create({ keyType: 'ed25519', bits: 256 }) + const id = await PeerId.create({ keyType: 'Ed25519', bits: 256 }) peerIds.push(id) } diff --git a/test/handshakes/ik.spec.ts b/test/handshakes/ik.spec.ts index 07b1b3a..f33eb54 100644 --- a/test/handshakes/ik.spec.ts +++ b/test/handshakes/ik.spec.ts @@ -29,7 +29,7 @@ describe('IK handshake', () => { // initiator creates payload const initSignedPayload = await libp2pInitKeys.sign(getHandshakePayload(kpInitiator.publicKey)) - const libp2pInitPrivKey = libp2pInitKeys.marshal().slice(0, 32) + libp2pInitKeys.marshal().slice(0, 32) const libp2pInitPubKey = libp2pInitKeys.marshal().slice(32, 64) const payloadInitEnc = await createHandshakePayload(libp2pInitPubKey, initSignedPayload) @@ -40,12 +40,12 @@ describe('IK handshake', () => { expect(messageBuffer.ne.length).not.equal(0) // responder receives message - const plaintext = ikR.recvMessage(responderSession, messageBuffer) + ikR.recvMessage(responderSession, messageBuffer) /* Stage 1 */ // responder creates payload - const libp2pRespPrivKey = libp2pRespKeys.marshal().slice(0, 32) + libp2pRespKeys.marshal().slice(0, 32) const libp2pRespPubKey = libp2pRespKeys.marshal().slice(32, 64) const respSignedPayload = await libp2pRespKeys.sign(getHandshakePayload(kpResponder.publicKey)) const payloadRespEnc = await createHandshakePayload(libp2pRespPubKey, respSignedPayload) @@ -54,12 +54,11 @@ describe('IK handshake', () => { const messageBuffer2 = ikR.sendMessage(responderSession, message1) // initiator receives message - const plaintext2 = ikI.recvMessage(initiatorSession, messageBuffer2) + ikI.recvMessage(initiatorSession, messageBuffer2) - assert(initiatorSession.cs1.k.equals(responderSession.cs1.k)) - assert(initiatorSession.cs2.k.equals(responderSession.cs2.k)) + assert(initiatorSession?.cs1?.k.equals(responderSession?.cs1?.k || new Uint8Array())) + assert(initiatorSession?.cs2?.k.equals(responderSession?.cs2?.k || new Uint8Array())) } catch (e) { - console.error(e) return assert(false, e.message) } }) diff --git a/test/handshakes/xx.spec.ts b/test/handshakes/xx.spec.ts index 9b3836e..c2d345c 100644 --- a/test/handshakes/xx.spec.ts +++ b/test/handshakes/xx.spec.ts @@ -14,15 +14,15 @@ describe('XX Handshake', () => { const xx = new XX() const kpInitiator: KeyPair = await generateKeypair() - const kpResponder: KeyPair = await generateKeypair() + await generateKeypair() - const session = await xx.initSession(true, prologue, kpInitiator) + await xx.initSession(true, prologue, kpInitiator) } catch (e) { assert(false, e.message) } }) - it('Test get HKDF', async () => { + it('Test get HKDF', () => { const ckBytes = Buffer.from('4e6f6973655f58585f32353531395f58436861436861506f6c795f53484132353600000000000000000000000000000000000000000000000000000000000000', 'hex') const ikm = Buffer.from('a3eae50ea37a47e8a7aa0c7cd8e16528670536dcd538cebfd724fb68ce44f1910ad898860666227d4e8dd50d22a9a64d1c0a6f47ace092510161e9e442953da3', 'hex') const ck = Buffer.alloc(32) @@ -54,7 +54,7 @@ describe('XX Handshake', () => { /* STAGE 0 */ // initiator creates payload - const libp2pInitPrivKey = libp2pInitKeys.marshal().slice(0, 32) + libp2pInitKeys.marshal().slice(0, 32) const libp2pInitPubKey = libp2pInitKeys.marshal().slice(32, 64) const payloadInitEnc = await createHandshakePayload(libp2pInitPubKey, initSignedPayload) @@ -71,7 +71,7 @@ describe('XX Handshake', () => { /* STAGE 1 */ // responder creates payload - const libp2pRespPrivKey = libp2pRespKeys.marshal().slice(0, 32) + libp2pRespKeys.marshal().slice(0, 32) const libp2pRespPubKey = libp2pRespKeys.marshal().slice(32, 64) const payloadRespEnc = await createHandshakePayload(libp2pRespPubKey, respSignedPayload) diff --git a/test/ik-handshake.spec.ts b/test/ik-handshake.spec.ts index 0a8b43f..3388e84 100644 --- a/test/ik-handshake.spec.ts +++ b/test/ik-handshake.spec.ts @@ -8,10 +8,10 @@ import { generateKeypair, getPayload } from '../src/utils' import { IKHandshake } from '../src/handshake-ik' describe('IK Handshake', () => { - let peerA, peerB, fakePeer + let peerA, peerB before(async () => { - [peerA, peerB, fakePeer] = await createPeerIdsFromFixtures(3) + [peerA, peerB] = await createPeerIdsFromFixtures(3) }) it('should finish both stages as initiator and responder', async () => { @@ -49,7 +49,6 @@ describe('IK Handshake', () => { const { plaintext: decrypted } = handshakeResp.decrypt(encrypted, handshakeResp.session) assert(decrypted.equals(Buffer.from('encryptthis'))) } catch (e) { - console.error(e) assert(false, e.message) } }) diff --git a/test/keycache.spec.ts b/test/keycache.spec.ts index 487a446..6c175eb 100644 --- a/test/keycache.spec.ts +++ b/test/keycache.spec.ts @@ -1,12 +1,12 @@ -import { expect, assert } from 'chai' +import { assert } from 'chai' import { KeyCache } from '../src/keycache' import { createPeerIds, createPeerIdsFromFixtures } from './fixtures/peer' describe('KeyCache', () => { - let peerA, peerB + let peerA before(async () => { - [peerA, peerB] = await createPeerIdsFromFixtures(2) + [peerA] = await createPeerIdsFromFixtures(2) }) it('should store and load same key successfully', async () => { @@ -14,9 +14,8 @@ describe('KeyCache', () => { const key = Buffer.from('this is id 007') await KeyCache.store(peerA, key) const result = await KeyCache.load(peerA) - assert(result.equals(key), 'Stored and loaded key are not the same') + assert(result?.equals(key), 'Stored and loaded key are not the same') } catch (e) { - console.error(e) assert(false, `Test failed - ${e.message}`) } }) @@ -27,7 +26,6 @@ describe('KeyCache', () => { const result = await KeyCache.load(newPeer) assert(!result) } catch (e) { - console.error(e) assert(false, `Test failed - ${e.message}`) } }) diff --git a/test/noise.spec.ts b/test/noise.spec.ts index dae1c0d..e0c7fe8 100644 --- a/test/noise.spec.ts +++ b/test/noise.spec.ts @@ -30,8 +30,8 @@ describe('Noise', () => { it('should communicate through encrypted streams without noise pipes', async () => { try { - const noiseInit = new Noise(undefined, undefined, false) - const noiseResp = new Noise(undefined, undefined, false) + const noiseInit = new Noise(undefined, undefined) + const noiseResp = new Noise(undefined, undefined) const [inboundConnection, outboundConnection] = DuplexPair() const [outbound, inbound] = await Promise.all([ @@ -50,7 +50,7 @@ describe('Noise', () => { }) it('should test that secureOutbound is spec compliant', async () => { - const noiseInit = new Noise(undefined, undefined, false) + const noiseInit = new Noise(undefined, undefined) const [inboundConnection, outboundConnection] = DuplexPair() const [outbound, { wrapped, handshake }] = await Promise.all([ @@ -111,8 +111,8 @@ describe('Noise', () => { it('should test large payloads', async function () { this.timeout(10000) try { - const noiseInit = new Noise(undefined, undefined, false) - const noiseResp = new Noise(undefined, undefined, false) + const noiseInit = new Noise(undefined, undefined) + const noiseResp = new Noise(undefined, undefined) const [inboundConnection, outboundConnection] = DuplexPair() const [outbound, inbound] = await Promise.all([ @@ -128,7 +128,6 @@ describe('Noise', () => { expect(response.length).equals(largePlaintext.length) } catch (e) { - console.log(e) assert(false, e.message) } }) @@ -162,7 +161,6 @@ describe('Noise', () => { assert(xxSpy.notCalled) assert(xxFallbackSpy.notCalled) } catch (e) { - console.error(e) assert(false, e.message) } }) @@ -194,7 +192,6 @@ describe('Noise', () => { assert(xxSpy.calledOnce, 'XX Fallback method was never called.') } catch (e) { - console.error(e) assert(false, e.message) } }) @@ -228,7 +225,6 @@ describe('Noise', () => { assert(xxSpy.calledOnce, 'XX Fallback method was never called.') } catch (e) { - console.error(e) assert(false, e.message) } }) @@ -263,7 +259,6 @@ describe('Noise', () => { assert(xxInitSpy.calledOnce, 'XX method was never called.') assert(xxRespSpy.calledOnce, 'XX Fallback method was never called.') } catch (e) { - console.error(e) assert(false, e.message) } }) @@ -301,7 +296,6 @@ describe('Noise', () => { assert(ikRespSpy.calledOnce, 'IK handshake was not called.') assert(xxFallbackInitSpy.notCalled, 'XX Fallback method was called.') } catch (e) { - console.error(e) assert(false, e.message) } }) @@ -332,7 +326,6 @@ describe('Noise', () => { assert(inbound.remotePeer.marshalPubKey().equals(localPeer.marshalPubKey())) assert(outbound.remotePeer.marshalPubKey().equals(remotePeer.marshalPubKey())) } catch (e) { - console.error(e) assert(false, e.message) } }) @@ -358,7 +351,6 @@ describe('Noise', () => { assert(inbound.remoteEarlyData.equals(localPeerEarlyData)) assert(outbound.remoteEarlyData.equals(Buffer.alloc(0))) } catch (e) { - console.error(e) assert(false, e.message) } }) diff --git a/test/utils.ts b/test/utils.ts index af3cf4e..4042ef9 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -1,9 +1,9 @@ -import { keys } from 'libp2p-crypto' +import { keys, PrivateKey } from 'libp2p-crypto' import { KeyPair } from '../src/@types/libp2p' import PeerId from 'peer-id' -export async function generateEd25519Keys () { - return await keys.generateKeyPair('ed25519') +export async function generateEd25519Keys (): Promise { + return await keys.generateKeyPair('Ed25519', 32) } export function getKeyPairFromPeerId (peerId: PeerId): KeyPair { diff --git a/test/xx-fallback-handshake.spec.ts b/test/xx-fallback-handshake.spec.ts index 0fff966..6dabb02 100644 --- a/test/xx-fallback-handshake.spec.ts +++ b/test/xx-fallback-handshake.spec.ts @@ -9,10 +9,10 @@ import { import { XXFallbackHandshake } from '../src/handshake-xx-fallback' import { createPeerIdsFromFixtures } from './fixtures/peer' import { assert } from 'chai' -import { decode1, encode0, encode1 } from '../src/encoder' +import { encode0 } from '../src/encoder' describe('XX Fallback Handshake', () => { - let peerA, peerB, fakePeer + let peerA, peerB before(async () => { [peerA, peerB] = await createPeerIdsFromFixtures(2) @@ -67,7 +67,6 @@ describe('XX Fallback Handshake', () => { assert(false) } } catch (e) { - console.error(e) assert(false, e.message) } })