bip151: refactor parser.

This commit is contained in:
Christopher Jeffrey 2016-07-27 02:25:21 -07:00
parent 01471d6e63
commit 3f6a93e180
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -77,9 +77,6 @@ function BIP151Stream(cipher, key) {
this.processed = 0; this.processed = 0;
this.lastRekey = 0; this.lastRekey = 0;
this.pendingHeader = [];
this.pendingHeaderTotal = 0;
this.hasHeader = false;
this.pending = []; this.pending = [];
this.pendingTotal = 0; this.pendingTotal = 0;
this.waiting = 0; this.waiting = 0;
@ -291,21 +288,21 @@ BIP151Stream.prototype.verify = function verify(tag) {
BIP151Stream.prototype.feed = function feed(data) { BIP151Stream.prototype.feed = function feed(data) {
var chunk, size, payload, tag, p, cmd, body; var chunk, size, payload, tag, p, cmd, body;
while (data) { while (data.length) {
if (!this.hasHeader) { if (!this.waiting) {
this.pendingHeaderTotal += data.length; this.pendingTotal += data.length;
this.pendingHeader.push(data); this.pending.push(data);
data = null;
if (this.pendingHeaderTotal < 4) if (this.pendingTotal < 4)
break; break;
chunk = concat(this.pendingHeader); chunk = concat(this.pending);
this.pendingHeaderTotal = 0; this.pendingTotal = 0;
this.pendingHeader.length = 0; this.pending.length = 0;
size = this.decryptSize(chunk); size = this.decryptSize(chunk);
data = chunk.slice(4);
// Allow 3 batched packets of max message size (12mb). // Allow 3 batched packets of max message size (12mb).
// Not technically standard, but this protects us // Not technically standard, but this protects us
@ -314,23 +311,17 @@ BIP151Stream.prototype.feed = function feed(data) {
// Note that 6 is the minimum size: // Note that 6 is the minimum size:
// cmd=varint(1) string(1) length(4) data(0) // cmd=varint(1) string(1) length(4) data(0)
if (size < 6 || size > constants.MAX_MESSAGE * 3) { if (size < 6 || size > constants.MAX_MESSAGE * 3) {
assert(this.waiting === 0);
this.emit('error', new Error('Bad packet size.')); this.emit('error', new Error('Bad packet size.'));
continue; continue;
} }
this.waiting = size + 16; this.waiting = size + 16;
this.hasHeader = true;
data = chunk.slice(4); continue;
if (data.length === 0)
break;
} }
this.pendingTotal += data.length; this.pendingTotal += data.length;
this.pending.push(data); this.pending.push(data);
data = null;
if (this.pendingTotal < this.waiting) if (this.pendingTotal < this.waiting)
break; break;
@ -340,12 +331,8 @@ BIP151Stream.prototype.feed = function feed(data) {
tag = chunk.slice(this.waiting - 16, this.waiting); tag = chunk.slice(this.waiting - 16, this.waiting);
data = chunk.slice(this.waiting); data = chunk.slice(this.waiting);
if (data.length === 0)
data = null;
this.pendingTotal = 0; this.pendingTotal = 0;
this.pending.length = 0; this.pending.length = 0;
this.hasHeader = false;
this.waiting = 0; this.waiting = 0;
// Authenticate payload before decrypting. // Authenticate payload before decrypting.
@ -355,6 +342,7 @@ BIP151Stream.prototype.feed = function feed(data) {
this.finish(); this.finish();
if (!this.verify(tag)) { if (!this.verify(tag)) {
this.sequence();
this.emit('error', new Error('Bad tag.')); this.emit('error', new Error('Bad tag.'));
continue; continue;
} }