From 486e89e3900a30caf9e93310924b05555e29398a Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 21 Mar 2023 13:03:43 -0500 Subject: [PATCH] chore: upgrade aegir to 38.1.7 + linting --- package.json | 2 +- src/handshakes/abstract-handshake.ts | 20 +++++++++++++++----- src/handshakes/xx.ts | 10 +++++----- src/logger.ts | 2 +- src/metrics.ts | 6 +++--- test/handshakes/xx.spec.ts | 6 +++--- test/index.spec.ts | 2 +- test/interop.ts | 6 +++--- test/noise.spec.ts | 4 ++-- 9 files changed, 34 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 07771f3..0ecdc2e 100644 --- a/package.json +++ b/package.json @@ -96,7 +96,7 @@ "@libp2p/peer-id-factory": "^2.0.0", "@libp2p/tcp": "^6.0.2", "@multiformats/multiaddr": "^11.0.3", - "aegir": "^37.3.0", + "aegir": "^38.1.7", "benchmark": "^2.1.4", "execa": "^7.0.0", "go-libp2p": "^0.0.6", diff --git a/src/handshakes/abstract-handshake.ts b/src/handshakes/abstract-handshake.ts index c2ab3df..e929511 100644 --- a/src/handshakes/abstract-handshake.ts +++ b/src/handshakes/abstract-handshake.ts @@ -7,6 +7,16 @@ import type { ICryptoInterface } from '../crypto.js' import { logger } from '../logger.js' import { Nonce } from '../nonce.js' +export interface DecryptedResult { + plaintext: bytes + valid: boolean +} + +export interface SplitState { + cs1: CipherState + cs2: CipherState +} + export abstract class AbstractHandshake { public crypto: ICryptoInterface @@ -21,7 +31,7 @@ export abstract class AbstractHandshake { return e } - public decryptWithAd (cs: CipherState, ad: Uint8Array, ciphertext: Uint8Array, dst?: Uint8Array): {plaintext: bytes, valid: boolean} { + public decryptWithAd (cs: CipherState, ad: Uint8Array, ciphertext: Uint8Array, dst?: Uint8Array): DecryptedResult { const { plaintext, valid } = this.decrypt(cs.k, cs.n, ad, ciphertext, dst) if (valid) cs.n.increment() @@ -60,7 +70,7 @@ export abstract class AbstractHandshake { return ciphertext } - protected decrypt (k: bytes32, n: Nonce, ad: bytes, ciphertext: bytes, dst?: Uint8Array): {plaintext: bytes, valid: boolean} { + protected decrypt (k: bytes32, n: Nonce, ad: bytes, ciphertext: bytes, dst?: Uint8Array): DecryptedResult { n.assertValue() const encryptedMessage = this.crypto.chaCha20Poly1305Decrypt(ciphertext, n.getBytes(), ad, k, dst) @@ -78,7 +88,7 @@ export abstract class AbstractHandshake { } } - protected decryptAndHash (ss: SymmetricState, ciphertext: bytes): {plaintext: bytes, valid: boolean} { + protected decryptAndHash (ss: SymmetricState, ciphertext: bytes): DecryptedResult { let plaintext: bytes; let valid = true if (this.hasKey(ss.cs)) { ({ plaintext, valid } = this.decryptWithAd(ss.cs, ss.h, ciphertext)) @@ -148,7 +158,7 @@ export abstract class AbstractHandshake { } } - protected split (ss: SymmetricState): {cs1: CipherState, cs2: CipherState} { + protected split (ss: SymmetricState): SplitState { const [tempk1, tempk2] = this.crypto.getHKDF(ss.ck, new Uint8Array(0)) const cs1 = this.initializeKey(tempk1) const cs2 = this.initializeKey(tempk2) @@ -164,7 +174,7 @@ export abstract class AbstractHandshake { return { ne, ns, ciphertext } } - protected readMessageRegular (cs: CipherState, message: MessageBuffer): {plaintext: bytes, valid: boolean} { + protected readMessageRegular (cs: CipherState, message: MessageBuffer): DecryptedResult { return this.decryptWithAd(cs, new Uint8Array(0), message.ciphertext) } } diff --git a/src/handshakes/xx.ts b/src/handshakes/xx.ts index 3499d4c..3138a13 100644 --- a/src/handshakes/xx.ts +++ b/src/handshakes/xx.ts @@ -2,7 +2,7 @@ import type { bytes32, bytes } from '../@types/basic.js' import type { KeyPair } from '../@types/libp2p.js' import { isValidPublicKey } from '../utils.js' import type { CipherState, HandshakeState, MessageBuffer, NoiseSession } from '../@types/handshake.js' -import { AbstractHandshake } from './abstract-handshake.js' +import { AbstractHandshake, DecryptedResult } from './abstract-handshake.js' export class XX extends AbstractHandshake { private initializeInitiator (prologue: bytes32, s: KeyPair, rs: bytes32, psk: bytes32): HandshakeState { @@ -67,7 +67,7 @@ export class XX extends AbstractHandshake { return { h: hs.ss.h, messageBuffer, cs1, cs2 } } - private readMessageA (hs: HandshakeState, message: MessageBuffer): {plaintext: bytes, valid: boolean} { + private readMessageA (hs: HandshakeState, message: MessageBuffer): DecryptedResult { if (isValidPublicKey(message.ne)) { hs.re = message.ne } @@ -76,7 +76,7 @@ export class XX extends AbstractHandshake { return this.decryptAndHash(hs.ss, message.ciphertext) } - private readMessageB (hs: HandshakeState, message: MessageBuffer): {plaintext: bytes, valid: boolean} { + private readMessageB (hs: HandshakeState, message: MessageBuffer): DecryptedResult { if (isValidPublicKey(message.ne)) { hs.re = message.ne } @@ -95,7 +95,7 @@ export class XX extends AbstractHandshake { return { plaintext, valid: (valid1 && valid2) } } - private readMessageC (hs: HandshakeState, message: MessageBuffer): {h: bytes, plaintext: bytes, valid: boolean, cs1: CipherState, cs2: CipherState} { + private readMessageC (hs: HandshakeState, message: MessageBuffer): { h: bytes, plaintext: bytes, valid: boolean, cs1: CipherState, cs2: CipherState } { const { plaintext: ns, valid: valid1 } = this.decryptAndHash(hs.ss, message.ns) if (valid1 && isValidPublicKey(ns)) { hs.rs = ns @@ -163,7 +163,7 @@ export class XX extends AbstractHandshake { return messageBuffer } - public recvMessage (session: NoiseSession, message: MessageBuffer): {plaintext: bytes, valid: boolean} { + public recvMessage (session: NoiseSession, message: MessageBuffer): DecryptedResult { let plaintext: bytes = new Uint8Array(0) let valid = false if (session.mc === 0) { diff --git a/src/logger.ts b/src/logger.ts index 74386fc..4cac511 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -24,7 +24,7 @@ export function logLocalStaticKeys (s: KeyPair): void { keyLogger(`LOCAL_STATIC_PRIVATE_KEY ${uint8ArrayToString(s.privateKey, 'hex')}`) } -export function logLocalEphemeralKeys (e: KeyPair|undefined): void { +export function logLocalEphemeralKeys (e: KeyPair | undefined): void { if (e) { keyLogger(`LOCAL_PUBLIC_EPHEMERAL_KEY ${uint8ArrayToString(e.publicKey, 'hex')}`) keyLogger(`LOCAL_PRIVATE_EPHEMERAL_KEY ${uint8ArrayToString(e.privateKey, 'hex')}`) diff --git a/src/metrics.ts b/src/metrics.ts index 998a70a..ee8bb67 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -1,8 +1,8 @@ -import type { Metrics } from '@libp2p/interface-metrics' +import type { Counter, Metrics } from '@libp2p/interface-metrics' -export type MetricsRegistry = ReturnType +export type MetricsRegistry = Record -export function registerMetrics (metrics: Metrics) { +export function registerMetrics (metrics: Metrics): MetricsRegistry { return { xxHandshakeSuccesses: metrics.registerCounter( 'libp2p_noise_xxhandshake_successes_total', { diff --git a/test/handshakes/xx.spec.ts b/test/handshakes/xx.spec.ts index a1a5d26..7c0b3fa 100644 --- a/test/handshakes/xx.spec.ts +++ b/test/handshakes/xx.spec.ts @@ -18,7 +18,7 @@ describe('XX Handshake', () => { const kpInitiator: KeyPair = stablelib.generateX25519KeyPair() - await xx.initSession(true, prologue, kpInitiator) + xx.initSession(true, prologue, kpInitiator) } catch (e) { const err = e as Error assert(false, err.message) @@ -60,7 +60,7 @@ describe('XX Handshake', () => { libp2pInitKeys.marshal().slice(0, 32) const libp2pInitPubKey = libp2pInitKeys.marshal().slice(32, 64) - const payloadInitEnc = await createHandshakePayload(libp2pInitPubKey, initSignedPayload) + const payloadInitEnc = createHandshakePayload(libp2pInitPubKey, initSignedPayload) // initiator sends message const message = Buffer.concat([Buffer.alloc(0), payloadInitEnc]) @@ -76,7 +76,7 @@ describe('XX Handshake', () => { // responder creates payload libp2pRespKeys.marshal().slice(0, 32) const libp2pRespPubKey = libp2pRespKeys.marshal().slice(32, 64) - const payloadRespEnc = await createHandshakePayload(libp2pRespPubKey, respSignedPayload) + const payloadRespEnc = createHandshakePayload(libp2pRespPubKey, respSignedPayload) const message1 = Buffer.concat([message, payloadRespEnc]) const messageBuffer2 = xx.sendMessage(nsResp, message1) diff --git a/test/index.spec.ts b/test/index.spec.ts index f625539..84af2f9 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -8,7 +8,7 @@ import { Noise } from '../src/noise.js' import { createPeerIdsFromFixtures } from './fixtures/peer.js' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -function createCounterSpy () { +function createCounterSpy (): ReturnType { return sinon.spy({ increment: () => {}, reset: () => {} diff --git a/test/interop.ts b/test/interop.ts index e2050f6..ad18fe9 100644 --- a/test/interop.ts +++ b/test/interop.ts @@ -57,7 +57,7 @@ async function createGoPeer (options: SpawnOptions): Promise { return { client: createClient(apiAddr), stop: async () => { - await proc.kill() + proc.kill() } } } @@ -82,7 +82,7 @@ async function createJsPeer (options: SpawnOptions): Promise { } const node = await createLibp2p(opts) - const server = await createServer(multiaddr('/ip4/0.0.0.0/tcp/0'), node as any) + const server = createServer(multiaddr('/ip4/0.0.0.0/tcp/0'), node as any) await server.start() return { @@ -105,7 +105,7 @@ async function main (): Promise { } } - await connectInteropTests(factory) + connectInteropTests(factory) } main().catch(err => { diff --git a/test/noise.spec.ts b/test/noise.spec.ts index 4fe24e1..e309b6e 100644 --- a/test/noise.spec.ts +++ b/test/noise.spec.ts @@ -82,7 +82,7 @@ describe('Noise', () => { // Stage 1 const { publicKey: libp2pPubKey } = getKeyPairFromPeerId(remotePeer) const signedPayload = await signPayload(remotePeer, getHandshakePayload(staticKeys.publicKey)) - const handshakePayload = await createHandshakePayload(libp2pPubKey, signedPayload) + const handshakePayload = createHandshakePayload(libp2pPubKey, signedPayload) const messageBuffer = xx.sendMessage(handshake.session, handshakePayload) wrapped.writeLP(encode1(messageBuffer)) @@ -98,7 +98,7 @@ describe('Noise', () => { wrappedOutbound.write(uint8ArrayFromString('test')) // Check that noise message is prefixed with 16-bit big-endian unsigned integer - const data = await (await wrapped.readLP()).slice() + const data = (await wrapped.readLP()).slice() const { plaintext: decrypted, valid } = handshake.decrypt(data, handshake.session) // Decrypted data should match expect(uint8ArrayEquals(decrypted, uint8ArrayFromString('test'))).to.be.true()