simple-hypercore-protocol
Advanced tools
Comparing version 1.2.1 to 1.3.0
49
index.js
@@ -14,3 +14,2 @@ const Handshake = require('./lib/handshake') | ||
const payload = { nonce: XOR.nonce(), userData: handlers.userData } | ||
const handshake = new Handshake(initiator, messages.NoisePayload.encode(payload), handlers, this._onhandshake.bind(this)) | ||
@@ -20,3 +19,3 @@ this.handlers = handlers || {} | ||
this.remotePublicKey = null | ||
this.publicKey = handshake.keyPair.publicKey | ||
this.publicKey = null | ||
this.destroyed = false | ||
@@ -27,10 +26,14 @@ | ||
this._initiator = initiator | ||
this._payload = payload | ||
this._pending = [] | ||
this._handshake = handshake | ||
this._handshake = null | ||
this._handshaking = true | ||
this._split = null | ||
this._encryption = null | ||
this._buffering = null | ||
this._messages = new SMC({ | ||
onmessage, | ||
onmissing, | ||
context: this, | ||
@@ -51,4 +54,28 @@ types: [ | ||
}) | ||
if (typeof this.handlers.keyPair !== 'function') { | ||
this._onkeypair(null, this.handlers.keyPair || null) | ||
} else { | ||
this._buffering = [] | ||
this.handlers.keyPair(this._onkeypair.bind(this)) | ||
} | ||
} | ||
_onkeypair (err, keyPair) { | ||
if (err) return this.destroy(err) | ||
if (this._handshake !== null) return | ||
this.handlers.keyPair = keyPair | ||
const handshake = new Handshake(this._initiator, messages.NoisePayload.encode(this._payload), this.handlers, this._onhandshake.bind(this)) | ||
this.publicKey = handshake.keyPair.publicKey | ||
this._handshake = handshake | ||
if (this._buffering) { | ||
while (this._buffering.length) this._recv(this._buffering.shift()) | ||
} | ||
this._buffering = null | ||
} | ||
open (ch, message) { | ||
@@ -108,3 +135,3 @@ return this._send(ch, 0, message) | ||
ping () { | ||
if (this._handshake || this._pending.length) return | ||
if (this._handshaking || this._pending.length) return | ||
@@ -132,2 +159,3 @@ let ping = Buffer.from([0]) | ||
this._handshake = null | ||
this._handshaking = false | ||
this._split = split | ||
@@ -151,3 +179,3 @@ this._encryption = this.handlers.encrypted === false | ||
_send (channel, type, message) { | ||
if (this._handshake || this._pending.length) { | ||
if (this._handshaking || this._pending.length) { | ||
this._pending.push([channel, type, message]) | ||
@@ -198,5 +226,10 @@ return false | ||
recv (data) { | ||
if (this._buffering !== null) this._buffering.push(data) | ||
else this._recv(data) | ||
} | ||
_recv (data) { | ||
if (this.destroyed) return | ||
if (this._handshake !== null) { | ||
if (this._handshaking) { | ||
this._handshake.recv(data) | ||
@@ -277,1 +310,5 @@ return | ||
} | ||
function onmissing (bytes, self) { | ||
if (self.handlers.onmissing) self.handlers.onmissing(bytes) | ||
} |
{ | ||
"name": "simple-hypercore-protocol", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Hypercore protocol state machine", | ||
@@ -10,3 +10,3 @@ "main": "index.js", | ||
"simple-handshake": "^1.3.1", | ||
"simple-message-channels": "^1.1.2", | ||
"simple-message-channels": "^1.2.1", | ||
"sodium-universal": "^2.0.0", | ||
@@ -13,0 +13,0 @@ "varint": "^5.0.0" |
30
test.js
@@ -206,1 +206,31 @@ const tape = require('tape') | ||
}) | ||
tape('set key pair later', function (t) { | ||
let later = null | ||
const a = new SHP(false, { | ||
keyPair (done) { | ||
setImmediate(function () { | ||
later = SHP.keyPair() | ||
done(null, later) | ||
}) | ||
}, | ||
send (data) { | ||
b.recv(data) | ||
} | ||
}) | ||
const b = new SHP(true, { | ||
send (data) { | ||
a.recv(data) | ||
}, | ||
onauthenticate (remotePublicKey, done) { | ||
t.same(remotePublicKey, later.publicKey) | ||
t.same(a.publicKey, later.publicKey) | ||
done() | ||
}, | ||
onhandshake () { | ||
t.end() | ||
} | ||
}) | ||
}) |
57692
1763