Skip to content

Commit

Permalink
fix: upgrade aegir to 38.1.7 (#292)
Browse files Browse the repository at this point in the history
chore: upgrade aegir to 38.1.7 + linting
  • Loading branch information
maschad authored Mar 22, 2023
1 parent 61a53e4 commit 62b2c91
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 15 additions & 5 deletions src/handshakes/abstract-handshake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
10 changes: 5 additions & 5 deletions src/handshakes/xx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')}`)
Expand Down
6 changes: 3 additions & 3 deletions src/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Metrics } from '@libp2p/interface-metrics'
import type { Counter, Metrics } from '@libp2p/interface-metrics'

export type MetricsRegistry = ReturnType<typeof registerMetrics>
export type MetricsRegistry = Record<string, Counter>

export function registerMetrics (metrics: Metrics) {
export function registerMetrics (metrics: Metrics): MetricsRegistry {
return {
xxHandshakeSuccesses: metrics.registerCounter(
'libp2p_noise_xxhandshake_successes_total', {
Expand Down
6 changes: 3 additions & 3 deletions test/handshakes/xx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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])
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof sinon.spy> {
return sinon.spy({
increment: () => {},
reset: () => {}
Expand Down
6 changes: 3 additions & 3 deletions test/interop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async function createGoPeer (options: SpawnOptions): Promise<Daemon> {
return {
client: createClient(apiAddr),
stop: async () => {
await proc.kill()
proc.kill()
}
}
}
Expand All @@ -82,7 +82,7 @@ async function createJsPeer (options: SpawnOptions): Promise<Daemon> {
}

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 {
Expand All @@ -105,7 +105,7 @@ async function main (): Promise<void> {
}
}

await connectInteropTests(factory)
connectInteropTests(factory)
}

main().catch(err => {
Expand Down
4 changes: 2 additions & 2 deletions test/noise.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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()
Expand Down

0 comments on commit 62b2c91

Please sign in to comment.