Expand Up
@@ -22,8 +22,6 @@ const packets = require('./packets');
const consensus = require ( '../protocol/consensus' ) ;
const common = require ( './common' ) ;
const InvItem = require ( '../primitives/invitem' ) ;
const BIP151 = require ( './bip151' ) ;
const BIP150 = require ( './bip150' ) ;
const BIP152 = require ( './bip152' ) ;
const Block = require ( '../primitives/block' ) ;
const TX = require ( '../primitives/tx' ) ;
Expand Down
Expand Up
@@ -114,8 +112,6 @@ class Peer extends EventEmitter {
this . hashContinue = null ;
this . spvFilter = null ;
this . feeRate = - 1 ;
this . bip151 = null ;
this . bip150 = null ;
this . compactMode = - 1 ;
this . compactWitness = false ;
this . merkleBlock = null ;
Expand Down
Expand Up
@@ -228,8 +224,6 @@ class Peer extends EventEmitter {
*/
framePacket ( cmd , payload , checksum ) {
if ( this . bip151 && this . bip151 . handshake )
return this . bip151 . packet ( cmd , payload ) ;
return this . framer . packet ( cmd , payload , checksum ) ;
}
Expand All
@@ -239,68 +233,9 @@ class Peer extends EventEmitter {
*/
feedParser ( data ) {
if ( this . bip151 && this . bip151 . handshake )
return this . bip151 . feed ( data ) ;
return this . parser . feed ( data ) ;
}
/**
* Set BIP151 cipher type.
* @param {Number } cipher
*/
setCipher ( cipher ) {
assert ( ! this . bip151 , 'BIP151 already set.' ) ;
assert ( this . socket , 'Peer must be initialized with a socket.' ) ;
assert ( ! this . opened , 'Cannot set cipher after open.' ) ;
this . bip151 = new BIP151 ( cipher ) ;
this . bip151 . on ( 'error' , ( err ) => {
this . error ( err ) ;
this . destroy ( ) ;
} ) ;
this . bip151 . on ( 'rekey' , ( ) => {
if ( this . destroyed )
return ;
this . logger . debug ( 'Rekeying with peer (%s).' , this . hostname ( ) ) ;
this . send ( this . bip151 . toRekey ( ) ) ;
} ) ;
this . bip151 . on ( 'packet' , ( cmd , body ) => {
let payload = null ;
try {
payload = this . parser . parsePayload ( cmd , body ) ;
} catch ( e ) {
this . parser . error ( e ) ;
return ;
}
this . parser . emit ( 'packet' , payload ) ;
} ) ;
}
/**
* Set BIP150 auth.
* @param {AuthDB } db
* @param {Buffer } key
*/
setAuth ( db , key ) {
const bip151 = this . bip151 ;
const hostname = this . hostname ( ) ;
const outbound = this . outbound ;
assert ( this . bip151 , 'BIP151 not set.' ) ;
assert ( ! this . bip150 , 'BIP150 already set.' ) ;
assert ( this . socket , 'Peer must be initialized with a socket.' ) ;
assert ( ! this . opened , 'Cannot set auth after open.' ) ;
this . bip150 = new BIP150 ( bip151 , hostname , outbound , db , key ) ;
this . bip151 . bip150 = this . bip150 ;
}
/**
* Bind to socket.
* @param {net.Socket } socket
Expand Down
Expand Up
@@ -437,8 +372,6 @@ class Peer extends EventEmitter {
// Connect to peer.
await this . initConnect ( ) ;
await this . initStall ( ) ;
await this . initBIP151 ( ) ;
await this . initBIP150 ( ) ;
await this . initVersion ( ) ;
await this . finalize ( ) ;
Expand Down
Expand Up
@@ -510,83 +443,6 @@ class Peer extends EventEmitter {
return Promise . resolve ( ) ;
}
/**
* Handle `connect` event (called immediately
* if a socket was passed into peer).
* @method
* @private
* @returns {Promise }
*/
async initBIP151 ( ) {
assert ( ! this . destroyed ) ;
// Send encinit. Wait for handshake to complete.
if ( ! this . bip151 )
return ;
assert ( ! this . bip151 . completed ) ;
this . logger . info ( 'Attempting BIP151 handshake (%s).' , this . hostname ( ) ) ;
this . send ( this . bip151 . toEncinit ( ) ) ;
try {
await this . bip151 . wait ( 3000 ) ;
} catch ( err ) {
this . error ( err ) ;
}
if ( this . destroyed )
throw new Error ( 'Peer was destroyed during BIP151 handshake.' ) ;
assert ( this . bip151 . completed ) ;
if ( this . bip151 . handshake ) {
this . logger . info ( 'BIP151 handshake complete (%s).' , this . hostname ( ) ) ;
this . logger . info ( 'Connection is encrypted (%s).' , this . hostname ( ) ) ;
}
}
/**
* Handle post bip151-handshake.
* @method
* @private
* @returns {Promise }
*/
async initBIP150 ( ) {
assert ( ! this . destroyed ) ;
if ( ! this . bip150 )
return ;
assert ( this . bip151 ) ;
assert ( ! this . bip150 . completed ) ;
if ( ! this . bip151 . handshake )
throw new Error ( 'BIP151 handshake was not completed for BIP150.' ) ;
this . logger . info ( 'Attempting BIP150 handshake (%s).' , this . hostname ( ) ) ;
if ( this . bip150 . outbound ) {
if ( ! this . bip150 . peerIdentity )
throw new Error ( 'No known identity for peer.' ) ;
this . send ( this . bip150 . toChallenge ( ) ) ;
}
await this . bip150 . wait ( 3000 ) ;
assert ( ! this . destroyed ) ;
assert ( this . bip150 . completed ) ;
if ( this . bip150 . auth ) {
this . logger . info ( 'BIP150 handshake complete (%s).' , this . hostname ( ) ) ;
this . logger . info ( 'Peer is authed (%s): %s.' ,
this . hostname ( ) , this . bip150 . getAddress ( ) ) ;
}
}
/**
* Handle post handshake.
* @method
Expand Down
Expand Up
@@ -981,12 +837,6 @@ class Peer extends EventEmitter {
this . socket . destroy ( ) ;
this . socket = null ;
if ( this . bip151 )
this . bip151 . destroy ( ) ;
if ( this . bip150 )
this . bip150 . destroy ( ) ;
if ( this . pingTimer != null ) {
clearInterval ( this . pingTimer ) ;
this . pingTimer = null ;
Expand Down
Expand Up
@@ -1298,7 +1148,7 @@ class Peer extends EventEmitter {
if ( this . responseMap . size >= common . MAX_REQUEST ) {
this . destroy ( ) ;
return ;
return null ;
}
}
Expand Down
Expand Up
@@ -1335,13 +1185,13 @@ class Peer extends EventEmitter {
wait ( type , timeout ) {
return new Promise ( ( resolve , reject ) => {
if ( this . destroyed ) {
const entry = this . request ( type ) ;
if ( ! entry ) {
reject ( new Error ( 'Peer is destroyed (request).' ) ) ;
return ;
}
const entry = this . request ( type ) ;
entry . setTimeout ( timeout ) ;
entry . addJob ( resolve , reject ) ;
} ) ;
Expand Down
Expand Up
@@ -1486,11 +1336,6 @@ class Peer extends EventEmitter {
// is handled at a low level. They
// must be handled immediately.
switch ( packet . type ) {
case packetTypes . ENCINIT :
case packetTypes . ENCACK :
case packetTypes . AUTHCHALLENGE :
case packetTypes . AUTHREPLY :
case packetTypes . AUTHPROPOSE :
case packetTypes . PONG : {
try {
this . socket . pause ( ) ;
Expand Down
Expand Up
@@ -1527,23 +1372,6 @@ class Peer extends EventEmitter {
if ( this . destroyed )
throw new Error ( 'Destroyed peer sent a packet.' ) ;
if ( this . bip151
&& this . bip151 . job
&& ! this . bip151 . completed
&& packet . type !== packetTypes . ENCINIT
&& packet . type !== packetTypes . ENCACK ) {
this . bip151 . reject ( new Error ( 'Message before BIP151 handshake.' ) ) ;
}
if ( this . bip150
&& this . bip150 . job
&& ! this . bip150 . completed
&& packet . type !== packetTypes . AUTHCHALLENGE
&& packet . type !== packetTypes . AUTHREPLY
&& packet . type !== packetTypes . AUTHPROPOSE ) {
this . bip150 . reject ( new Error ( 'Message before BIP150 auth.' ) ) ;
}
const entry = this . fulfill ( packet ) ;
switch ( packet . type ) {
Expand Down
Expand Up
@@ -1577,21 +1405,6 @@ class Peer extends EventEmitter {
case packetTypes . SENDCMPCT :
await this . handleSendCmpct ( packet ) ;
break ;
case packetTypes . ENCINIT :
await this . handleEncinit ( packet ) ;
break ;
case packetTypes . ENCACK :
await this . handleEncack ( packet ) ;
break ;
case packetTypes . AUTHCHALLENGE :
await this . handleAuthChallenge ( packet ) ;
break ;
case packetTypes . AUTHREPLY :
await this . handleAuthReply ( packet ) ;
break ;
case packetTypes . AUTHPROPOSE :
await this . handleAuthPropose ( packet ) ;
break ;
}
if ( this . onPacket )
Expand Down
Expand Up
@@ -1858,85 +1671,6 @@ class Peer extends EventEmitter {
this . compactWitness = packet . version === 2 ;
}
/**
* Handle `encinit` packet.
* @method
* @private
* @param {EncinitPacket } packet
*/
async handleEncinit ( packet ) {
if ( ! this . bip151 )
return ;
this . bip151 . encinit ( packet . publicKey , packet . cipher ) ;
this . send ( this . bip151 . toEncack ( ) ) ;
}
/**
* Handle `encack` packet.
* @method
* @private
* @param {EncackPacket } packet
*/
async handleEncack ( packet ) {
if ( ! this . bip151 )
return ;
this . bip151 . encack ( packet . publicKey ) ;
}
/**
* Handle `authchallenge` packet.
* @method
* @private
* @param {AuthChallengePacket } packet
*/
async handleAuthChallenge ( packet ) {
if ( ! this . bip150 )
return ;
const sig = this . bip150 . challenge ( packet . hash ) ;
this . send ( new packets . AuthReplyPacket ( sig ) ) ;
}
/**
* Handle `authreply` packet.
* @method
* @private
* @param {AuthReplyPacket } packet
*/
async handleAuthReply ( packet ) {
if ( ! this . bip150 )
return ;
const hash = this . bip150 . reply ( packet . signature ) ;
if ( hash )
this . send ( new packets . AuthProposePacket ( hash ) ) ;
}
/**
* Handle `authpropose` packet.
* @method
* @private
* @param {AuthProposePacket } packet
*/
async handleAuthPropose ( packet ) {
if ( ! this . bip150 )
return ;
const hash = this . bip150 . propose ( packet . hash ) ;
this . send ( new packets . AuthChallengePacket ( hash ) ) ;
}
/**
* Send `getheaders` to peer. Note that unlike
* `getblocks`, `getheaders` can have a null locator.
Expand Down