Skip to content
This repository was archived by the owner on Aug 23, 2019. It is now read-only.

Commit 732a8c5

Browse files
committed
fix: improve support for interop usage
1 parent c47dea1 commit 732a8c5

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"debug": "^3.1.0",
2929
"interface-connection": "^0.3.2",
3030
"pull-cat": "^1.1.11",
31+
"pull-defer": "^0.2.2",
3132
"pull-handshake": "^1.1.4",
3233
"pull-reader": "^1.2.9",
3334
"pull-stream": "^3.6.7",

src/crypto.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const debug = require('debug')
55
const Errors = require('./errors')
66
const xsalsa20 = require('xsalsa20')
77
const KEY_LENGTH = require('./key-generator').KEY_LENGTH
8-
const NONCE_LENGTH = require('./key-generator').NONCE_LENGTH
98

109
const log = debug('libp2p:pnet')
1110
log.trace = debug('libp2p:pnet:trace')
@@ -41,10 +40,6 @@ module.exports.createUnboxStream = (remote, psk) => {
4140
ensureBuffer(),
4241
pull.map((chunk) => {
4342
if (!xor) {
44-
if (!remote.nonce || remote.nonce.byteLength !== NONCE_LENGTH) {
45-
log.trace('No nonce was read, throwing an error')
46-
throw new Error(Errors.INVALID_PEER)
47-
}
4843
xor = xsalsa20(remote.nonce, psk)
4944
log.trace('Decryption enabled')
5045
}

src/state.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ const pair = require('pull-pair')
66
const Reader = require('pull-reader')
77
const cat = require('pull-cat')
88
const pull = require('pull-stream')
9+
const deferred = require('pull-defer')
910

1011
const cryptoStreams = require('./crypto')
1112
const NONCE_LENGTH = require('./key-generator').NONCE_LENGTH
1213

1314
const log = debug('libp2p:pnet')
1415
log.err = debug('libp2p:pnet:err')
16+
log.trace = debug('libp2p:pnet:trace')
1517

1618
/**
1719
* Keeps track of the state of a given connection, such as the local psk
@@ -67,19 +69,21 @@ class State {
6769
// The outer stream needs to be returned before we setup the
6870
// rest of the streams, so we're delaying the execution
6971
setTimeout(() => {
70-
// Read the nonce first, this queues up the read when data
71-
// is available, so we will have the nonce before any
72-
// decryption occurs
72+
// Read the nonce first, once we have it resolve the
73+
// deferred source, so we keep reading
74+
const deferredSource = deferred.source()
7375
this.rawReader.read(NONCE_LENGTH, (err, data) => {
7476
if (err) {
7577
log.err('There was an error attempting to read the nonce', err)
7678
}
79+
log.trace('remote nonce received')
7780
this.remote.nonce = data
81+
deferredSource.resolve(this.rawReader.read())
7882
})
7983

8084
this.innerRawStream = {
8185
sink: this.rawPairStream.sink,
82-
source: this.rawReader.read()
86+
source: deferredSource
8387
}
8488

8589
// Create the pull exchange between the two inner streams

0 commit comments

Comments
 (0)