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

feat: adjust to comply with conn encryptor spec #153

Merged
merged 3 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 0 additions & 27 deletions .editorconfig

This file was deleted.

2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14
16
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
},
"dependencies": {
"@libp2p/crypto": "^0.22.9",
"@libp2p/interfaces": "^1.3.14",
"@libp2p/interfaces": "^2.0.1",
"@libp2p/logger": "^1.1.2",
"@libp2p/peer-collections": "^1.0.0",
"@libp2p/peer-id": "^1.1.8",
Expand All @@ -85,12 +85,13 @@
},
"devDependencies": {
"@libp2p/peer-id-factory": "^1.0.8",
"aegir": "^37.0.11",
"@libp2p/interface-compliance-tests": "^2.0.1",
"aegir": "^37.0.15",
"benchmark": "^2.1.4",
"events": "^3.3.0",
"microtime": "^3.0.0",
"protons": "^3.0.3",
"mkdirp": "^1.0.4",
"protons": "^3.0.3",
"sinon": "^14.0.0",
"util": "^0.12.4"
},
Expand Down
13 changes: 7 additions & 6 deletions src/handshake-xx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PeerId } from '@libp2p/interfaces/peer-id'
import { InvalidCryptoExchangeError, UnexpectedPeerError } from '@libp2p/interfaces/connection-encrypter/errors'
import type { ProtobufStream } from 'it-pb-stream'
import type { bytes, bytes32 } from './@types/basic.js'
import type { CipherState, NoiseSession } from './@types/handshake.js'
Expand Down Expand Up @@ -71,7 +72,7 @@ export class XXHandshake implements IHandshake {
const receivedMessageBuffer = decode0((await this.connection.readLP()).slice())
const { valid } = this.xx.recvMessage(this.session, receivedMessageBuffer)
if (!valid) {
throw new Error('xx handshake stage 0 validation fail')
throw new InvalidCryptoExchangeError('xx handshake stage 0 validation fail')
}
logger('Stage 0 - Responder received first message.')
logRemoteEphemeralKey(this.session.hs.re)
Expand All @@ -85,7 +86,7 @@ export class XXHandshake implements IHandshake {
const receivedMessageBuffer = decode1((await this.connection.readLP()).slice())
const { plaintext, valid } = this.xx.recvMessage(this.session, receivedMessageBuffer)
if (!valid) {
throw new Error('xx handshake stage 1 validation fail')
throw new InvalidCryptoExchangeError('xx handshake stage 1 validation fail')
}
logger('Stage 1 - Initiator received the message.')
logRemoteEphemeralKey(this.session.hs.re)
Expand All @@ -99,7 +100,7 @@ export class XXHandshake implements IHandshake {
this.setRemoteEarlyData(decodedPayload.data)
} catch (e) {
const err = e as Error
throw new Error(`Error occurred while verifying signed payload: ${err.message}`)
throw new UnexpectedPeerError(`Error occurred while verifying signed payload: ${err.message}`)
}
logger('All good with the signature!')
} else {
Expand All @@ -123,7 +124,7 @@ export class XXHandshake implements IHandshake {
const receivedMessageBuffer = decode2((await this.connection.readLP()).slice())
const { plaintext, valid } = this.xx.recvMessage(this.session, receivedMessageBuffer)
if (!valid) {
throw new Error('xx handshake stage 2 validation fail')
throw new InvalidCryptoExchangeError('xx handshake stage 2 validation fail')
}
logger('Stage 2 - Responder received the message, finished handshake.')

Expand All @@ -134,7 +135,7 @@ export class XXHandshake implements IHandshake {
this.setRemoteEarlyData(decodedPayload.data)
} catch (e) {
const err = e as Error
throw new Error(`Error occurred while verifying signed payload: ${err.message}`)
throw new UnexpectedPeerError(`Error occurred while verifying signed payload: ${err.message}`)
}
}
logCipherState(this.session)
Expand All @@ -158,7 +159,7 @@ export class XXHandshake implements IHandshake {

private getCS (session: NoiseSession, encryption = true): CipherState {
if (!session.cs1 || !session.cs2) {
throw new Error('Handshake not completed properly, cipher state does not exist.')
throw new InvalidCryptoExchangeError('Handshake not completed properly, cipher state does not exist.')
}

if (this.isInitiator) {
Expand Down
11 changes: 7 additions & 4 deletions src/noise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ export class Noise implements INoiseConnection {
await handshake.finish()
} catch (e) {
const err = e as Error
err.message = `Error occurred during XX Fallback handshake: ${err.message}`
logger(err)
throw new Error(`Error occurred during XX Fallback handshake: ${err.message}`)
throw err
}

return handshake
Expand Down Expand Up @@ -222,9 +223,11 @@ export class Noise implements INoiseConnection {
if (this.useNoisePipes && handshake.remotePeer) {
KeyCache.store(handshake.remotePeer, handshake.getRemoteStaticKey())
}
} catch (e) {
const err = e as Error
throw new Error(`Error occurred during XX handshake: ${err.message}`)
} catch (e: unknown) {
if (e instanceof Error) {
e.message = `Error occurred during XX handshake: ${e.message}`
throw e
}
}

return handshake
Expand Down
11 changes: 11 additions & 0 deletions test/compliance.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import tests from '@libp2p/interface-compliance-tests/connection-encrypter'
import { Noise } from '../src/index.js'

describe('spec compliance tests', function () {
tests({
async setup () {
return new Noise()
},
async teardown () {}
})
})