bip151: refactor.
This commit is contained in:
parent
6448cea1e7
commit
e709cbeae5
@ -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.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user