Skip to content

Commit 0fd6a63

Browse files
authored
feat: adjust to comply with conn encryptor spec (#153)
* update node version in nvmrc * chore: adjust to comply with compliance spec * fix lint
1 parent 6272d74 commit 0fd6a63

File tree

6 files changed

+30
-41
lines changed

6 files changed

+30
-41
lines changed

.editorconfig

Lines changed: 0 additions & 27 deletions
This file was deleted.

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
14
1+
16

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
},
6868
"dependencies": {
6969
"@libp2p/crypto": "^0.22.9",
70-
"@libp2p/interfaces": "^1.3.14",
70+
"@libp2p/interfaces": "^2.0.1",
7171
"@libp2p/logger": "^1.1.2",
7272
"@libp2p/peer-collections": "^1.0.0",
7373
"@libp2p/peer-id": "^1.1.8",
@@ -85,12 +85,13 @@
8585
},
8686
"devDependencies": {
8787
"@libp2p/peer-id-factory": "^1.0.8",
88-
"aegir": "^37.0.11",
88+
"@libp2p/interface-compliance-tests": "^2.0.1",
89+
"aegir": "^37.0.15",
8990
"benchmark": "^2.1.4",
9091
"events": "^3.3.0",
9192
"microtime": "^3.0.0",
92-
"protons": "^3.0.3",
9393
"mkdirp": "^1.0.4",
94+
"protons": "^3.0.3",
9495
"sinon": "^14.0.0",
9596
"util": "^0.12.4"
9697
},

src/handshake-xx.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { PeerId } from '@libp2p/interfaces/peer-id'
2+
import { InvalidCryptoExchangeError, UnexpectedPeerError } from '@libp2p/interfaces/connection-encrypter/errors'
23
import type { ProtobufStream } from 'it-pb-stream'
34
import type { bytes, bytes32 } from './@types/basic.js'
45
import type { CipherState, NoiseSession } from './@types/handshake.js'
@@ -71,7 +72,7 @@ export class XXHandshake implements IHandshake {
7172
const receivedMessageBuffer = decode0((await this.connection.readLP()).slice())
7273
const { valid } = this.xx.recvMessage(this.session, receivedMessageBuffer)
7374
if (!valid) {
74-
throw new Error('xx handshake stage 0 validation fail')
75+
throw new InvalidCryptoExchangeError('xx handshake stage 0 validation fail')
7576
}
7677
logger('Stage 0 - Responder received first message.')
7778
logRemoteEphemeralKey(this.session.hs.re)
@@ -85,7 +86,7 @@ export class XXHandshake implements IHandshake {
8586
const receivedMessageBuffer = decode1((await this.connection.readLP()).slice())
8687
const { plaintext, valid } = this.xx.recvMessage(this.session, receivedMessageBuffer)
8788
if (!valid) {
88-
throw new Error('xx handshake stage 1 validation fail')
89+
throw new InvalidCryptoExchangeError('xx handshake stage 1 validation fail')
8990
}
9091
logger('Stage 1 - Initiator received the message.')
9192
logRemoteEphemeralKey(this.session.hs.re)
@@ -99,7 +100,7 @@ export class XXHandshake implements IHandshake {
99100
this.setRemoteEarlyData(decodedPayload.data)
100101
} catch (e) {
101102
const err = e as Error
102-
throw new Error(`Error occurred while verifying signed payload: ${err.message}`)
103+
throw new UnexpectedPeerError(`Error occurred while verifying signed payload: ${err.message}`)
103104
}
104105
logger('All good with the signature!')
105106
} else {
@@ -123,7 +124,7 @@ export class XXHandshake implements IHandshake {
123124
const receivedMessageBuffer = decode2((await this.connection.readLP()).slice())
124125
const { plaintext, valid } = this.xx.recvMessage(this.session, receivedMessageBuffer)
125126
if (!valid) {
126-
throw new Error('xx handshake stage 2 validation fail')
127+
throw new InvalidCryptoExchangeError('xx handshake stage 2 validation fail')
127128
}
128129
logger('Stage 2 - Responder received the message, finished handshake.')
129130

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

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

164165
if (this.isInitiator) {

src/noise.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ export class Noise implements INoiseConnection {
192192
await handshake.finish()
193193
} catch (e) {
194194
const err = e as Error
195+
err.message = `Error occurred during XX Fallback handshake: ${err.message}`
195196
logger(err)
196-
throw new Error(`Error occurred during XX Fallback handshake: ${err.message}`)
197+
throw err
197198
}
198199

199200
return handshake
@@ -222,9 +223,11 @@ export class Noise implements INoiseConnection {
222223
if (this.useNoisePipes && handshake.remotePeer) {
223224
KeyCache.store(handshake.remotePeer, handshake.getRemoteStaticKey())
224225
}
225-
} catch (e) {
226-
const err = e as Error
227-
throw new Error(`Error occurred during XX handshake: ${err.message}`)
226+
} catch (e: unknown) {
227+
if (e instanceof Error) {
228+
e.message = `Error occurred during XX handshake: ${e.message}`
229+
throw e
230+
}
228231
}
229232

230233
return handshake

test/compliance.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import tests from '@libp2p/interface-compliance-tests/connection-encrypter'
2+
import { Noise } from '../src/index.js'
3+
4+
describe('spec compliance tests', function () {
5+
tests({
6+
async setup () {
7+
return new Noise()
8+
},
9+
async teardown () {}
10+
})
11+
})

0 commit comments

Comments
 (0)