From e709cbeae5d5115482a6cac0f2b4d1458966a9d1 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 27 Jul 2016 17:23:15 -0700 Subject: [PATCH] bip151: refactor. --- lib/bcoin/bip151.js | 129 ++++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 63 deletions(-) diff --git a/lib/bcoin/bip151.js b/lib/bcoin/bip151.js index e63e0e00..42ac49bb 100644 --- a/lib/bcoin/bip151.js +++ b/lib/bcoin/bip151.js @@ -515,11 +515,77 @@ BIP151.prototype.toEncinit = function toEncinit(writer) { if (!writer) p = p.render(); + assert(!this.initSent, 'Cannot init twice.'); this.initSent = true; return p; }; +/** + * Render `encack` packet. Contains the + * output stream public key. + * @returns {Buffer} + */ + +BIP151.prototype.toEncack = function toEncack(writer) { + var p = bcoin.writer(writer); + + assert(this.output.prk, 'Cannot ack before init.'); + + p.writeBytes(this.output.getPublicKey()); + + if (!writer) + p = p.render(); + + assert(!this.ackSent, 'Cannot ack twice.'); + this.ackSent = true; + + if (this.isReady()) { + this.handshake = true; + this.emit('handshake'); + } + + return p; +}; + +/** + * Render `encack` packet with an all + * zero public key, notifying of a rekey + * for the output stream. + * @returns {Buffer} + */ + +BIP151.prototype.toRekey = function toRekey(writer) { + var p = bcoin.writer(writer); + + assert(this.handshake, 'Cannot rekey before handshake.'); + + p.writeBytes(constants.ZERO_KEY); + + if (!writer) + p = p.render(); + + return p; +}; + +/** + * Handle `encinit` from remote peer. + * @param {Buffer} + */ + +BIP151.prototype.encinit = function encinit(data) { + var p = bcoin.reader(data); + var publicKey = p.readBytes(33); + var cipher = p.readU8(); + + assert(!this.initReceived, 'Already initialized.'); + this.initReceived = true; + + assert(cipher === this.output.cipher, 'Cipher mismatch.'); + + this.output.init(publicKey); +}; + /** * Handle `encack` from remote peer. * @param {Buffer} data @@ -548,69 +614,6 @@ BIP151.prototype.encack = function encack(data) { } }; -/** - * Handle `encinit` from remote peer. - * @param {Buffer} - */ - -BIP151.prototype.encinit = function encinit(data) { - var p = bcoin.reader(data); - var publicKey = p.readBytes(33); - var cipher = p.readU8(); - - assert(!this.initReceived, 'Already initialized.'); - this.initReceived = true; - - assert(cipher === this.output.cipher, 'Cipher mismatch.'); - - this.output.init(publicKey); -}; - -/** - * Render `encack` packet. Contains the - * output stream public key. - * @returns {Buffer} - */ - -BIP151.prototype.toEncack = function toEncack(writer) { - var p = bcoin.writer(writer); - - assert(this.output, 'Cannot ack before init.'); - - p.writeBytes(this.output.getPublicKey()); - - if (!writer) - p = p.render(); - - if (!this.ackSent) { - this.ackSent = true; - if (this.isReady()) { - this.handshake = true; - this.emit('handshake'); - } - } - - return p; -}; - -/** - * Render `encack` packet with an all - * zero public key, notifying of a rekey - * for the output stream. - * @returns {Buffer} - */ - -BIP151.prototype.toRekey = function toRekey(writer) { - var p = bcoin.writer(writer); - - p.writeBytes(constants.ZERO_KEY); - - if (!writer) - p = p.render(); - - return p; -}; - /** * Complete the timeout for handshake, * possibly with an error.