lint fixes

This commit is contained in:
Marin Petrunić 2020-06-19 13:06:31 +02:00
parent 8327a60356
commit ca39bc5d99
No known key found for this signature in database
GPG Key ID: 834D07135E110DA5
16 changed files with 55 additions and 61 deletions

View File

@ -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"
]
}
}

View File

@ -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)
}

View File

@ -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

View File

@ -32,6 +32,7 @@ export class XXFallbackHandshake extends XXHandshake {
}
// stage 0
// eslint-disable-next-line require-await
public async propose (): Promise<void> {
if (this.isInitiator) {
this.xx.sendMessage(this.session, Buffer.alloc(0), this.ephemeralKeys)

View File

@ -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)

View File

@ -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')

View File

@ -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<SecureOutbound>}
*/
@ -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<SecureOutbound>}
*/
@ -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<IHandshake> {
const payload = await getPayload(params.localPeer, this.staticKeys.publicKey, this.earlyData)

View File

@ -36,11 +36,11 @@ export async function getPayload (
)
}
export async function createHandshakePayload (
export function createHandshakePayload (
libp2pPublicKey: bytes,
signedPayload: bytes,
earlyData?: bytes
): Promise<bytes> {
): 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<bytes> {
return peerId.privKey.sign(payload)
return await peerId.privKey.sign(payload)
}
export async function getPeerIdFromPayload (payload: pb.INoiseHandshakePayload): Promise<PeerId> {
return await PeerId.createFromPubKey(Buffer.from(payload.identityKey as Uint8Array))
}
export async function decodePayload (payload: bytes|Uint8Array): Promise<pb.INoiseHandshakePayload> {
export function decodePayload (payload: bytes|Uint8Array): pb.INoiseHandshakePayload {
return NoiseHandshakePayloadProto.toObject(
NoiseHandshakePayloadProto.decode(Buffer.from(payload))
) as INoisePayload

10
test/fixtures/peer.ts vendored
View File

@ -19,16 +19,16 @@ const peers = [{
pubKey: 'CAESIMbnikZaPciAMZhUXqDRVCs7VFOBtmlIk26g0GgOotDA'
}]
export async function createPeerIdsFromFixtures (length) {
return Promise.all(
export async function createPeerIdsFromFixtures (length:number): Promise<PeerId[]> {
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<PeerId[]> {
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)
}

View File

@ -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)
}
})

View File

@ -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)

View File

@ -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)
}
})

View File

@ -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}`)
}
})

View File

@ -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)
}
})

View File

@ -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<PrivateKey> {
return await keys.generateKeyPair('Ed25519', 32)
}
export function getKeyPairFromPeerId (peerId: PeerId): KeyPair {

View File

@ -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)
}
})