816 changes: 0 additions & 816 deletions lib/net/bip150.js

This file was deleted.

761 changes: 0 additions & 761 deletions lib/net/bip151.js

This file was deleted.

2 changes: 0 additions & 2 deletions lib/net/index.js
Expand Up @@ -10,8 +10,6 @@
* @module net
*/

exports.BIP150 = require('./bip150');
exports.BIP151 = require('./bip151');
exports.bip152 = require('./bip152');
exports.common = require('./common');
exports.Framer = require('./framer');
Expand Down
511 changes: 3 additions & 508 deletions lib/net/packets.js

Large diffs are not rendered by default.

274 changes: 4 additions & 270 deletions lib/net/peer.js
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
139 changes: 1 addition & 138 deletions lib/net/pool.js
Expand Up @@ -17,13 +17,10 @@ const UPNP = require('bupnp');
const socks = require('bsocks');
const List = require('blst');
const {BloomFilter, RollingFilter} = require('bfilter');
const secp256k1 = require('bcrypto/lib/secp256k1');
const util = require('../utils/util');
const common = require('./common');
const chainCommon = require('../blockchain/common');
const Address = require('../primitives/address');
const BIP150 = require('./bip150');
const BIP151 = require('./bip151');
const BIP152 = require('./bip152');
const Network = require('../protocol/network');
const Peer = require('./peer');
Expand Down Expand Up @@ -82,7 +79,6 @@ class Pool extends EventEmitter {
this.headerTip = null;

this.peers = new PeerList();
this.authdb = new BIP150.AuthDB(this.options);
this.hosts = new HostList(this.options);
this.id = 0;

Expand Down Expand Up @@ -182,12 +178,6 @@ class Pool extends EventEmitter {

this.logger.info('Pool loaded (maxpeers=%d).', this.options.maxOutbound);

if (this.options.bip150) {
const key = secp256k1.publicKeyCreate(this.options.identityKey, true);
this.logger.info('Identity public key: %s.', key.toString('hex'));
this.logger.info('Identity address: %s.', BIP150.address(key));
}

this.resetChain();
}

Expand Down Expand Up @@ -257,7 +247,6 @@ class Pool extends EventEmitter {
return;

await this.hosts.open();
await this.authdb.open();

await this.discoverGateway();
await this.discoverExternal();
Expand Down Expand Up @@ -324,7 +313,6 @@ class Pool extends EventEmitter {

this.stopTimer();

await this.authdb.close();
await this.hosts.close();

await this.unlisten();
Expand Down Expand Up @@ -1063,18 +1051,10 @@ class Pool extends EventEmitter {
*/

createOutbound(addr) {
const cipher = BIP151.ciphers.CHACHAPOLY;
const identity = this.options.identityKey;
const peer = Peer.fromOutbound(this.options, addr);

this.hosts.markAttempt(addr.hostname);

if (this.options.bip151)
peer.setCipher(cipher);

if (this.options.bip150)
peer.setAuth(this.authdb, identity);

this.bindPeer(peer);

this.logger.debug('Connecting to %s.', peer.hostname());
Expand All @@ -1092,16 +1072,8 @@ class Pool extends EventEmitter {
*/

createInbound(socket) {
const cipher = BIP151.ciphers.CHACHAPOLY;
const identity = this.options.identityKey;
const peer = Peer.fromInbound(this.options, socket);

if (this.options.bip151)
peer.setCipher(cipher);

if (this.options.bip150)
peer.setAuth(this.authdb, identity);

this.bindPeer(peer);

peer.tryOpen();
Expand Down Expand Up @@ -1255,21 +1227,6 @@ class Pool extends EventEmitter {
case packetTypes.BLOCKTXN:
await this.handleBlockTxn(peer, packet);
break;
case packetTypes.ENCINIT:
await this.handleEncinit(peer, packet);
break;
case packetTypes.ENCACK:
await this.handleEncack(peer, packet);
break;
case packetTypes.AUTHCHALLENGE:
await this.handleAuthChallenge(peer, packet);
break;
case packetTypes.AUTHREPLY:
await this.handleAuthReply(peer, packet);
break;
case packetTypes.AUTHPROPOSE:
await this.handleAuthPropose(peer, packet);
break;
case packetTypes.UNKNOWN:
await this.handleUnknown(peer, packet);
break;
Expand Down Expand Up @@ -1593,6 +1550,7 @@ class Pool extends EventEmitter {

const blocks = [];
const txs = [];

let unknown = -1;

for (const item of items) {
Expand Down Expand Up @@ -2915,66 +2873,6 @@ class Pool extends EventEmitter {
await this.addBlock(peer, block.toBlock(), flags);
}

/**
* Handle `encinit` packet.
* @method
* @private
* @param {Peer} peer
* @param {EncinitPacket} packet
*/

async handleEncinit(peer, packet) {
;
}

/**
* Handle `encack` packet.
* @method
* @private
* @param {Peer} peer
* @param {EncackPacket} packet
*/

async handleEncack(peer, packet) {
;
}

/**
* Handle `authchallenge` packet.
* @method
* @private
* @param {Peer} peer
* @param {AuthChallengePacket} packet
*/

async handleAuthChallenge(peer, packet) {
;
}

/**
* Handle `authreply` packet.
* @method
* @private
* @param {Peer} peer
* @param {AuthReplyPacket} packet
*/

async handleAuthReply(peer, packet) {
;
}

/**
* Handle `authpropose` packet.
* @method
* @private
* @param {Peer} peer
* @param {AuthProposePacket} packet
*/

async handleAuthPropose(peer, packet) {
;
}

/**
* Handle `unknown` packet.
* @method
Expand Down Expand Up @@ -3636,11 +3534,6 @@ class PoolOptions {
this.selfish = false;
this.version = common.PROTOCOL_VERSION;
this.agent = common.USER_AGENT;
this.bip151 = false;
this.bip150 = false;
this.authPeers = [];
this.knownPeers = {};
this.identityKey = secp256k1.generatePrivateKey();
this.banScore = common.BAN_SCORE;
this.banTime = common.BAN_TIME;
this.feeRate = -1;
Expand Down Expand Up @@ -3808,36 +3701,6 @@ class PoolOptions {
this.agent = options.agent;
}

if (options.bip151 != null) {
assert(typeof options.bip151 === 'boolean');
this.bip151 = options.bip151;
}

if (options.bip150 != null) {
assert(typeof options.bip150 === 'boolean');
assert(!options.bip150 || this.bip151,
'Cannot enable bip150 without bip151.');

if (options.knownPeers) {
assert(typeof options.knownPeers === 'object');
assert(!Array.isArray(options.knownPeers));
this.knownPeers = options.knownPeers;
}

if (options.authPeers) {
assert(Array.isArray(options.authPeers));
this.authPeers = options.authPeers;
}

if (options.identityKey) {
assert(Buffer.isBuffer(options.identityKey),
'Identity key must be a buffer.');
assert(secp256k1.privateKeyVerify(options.identityKey),
'Invalid identity key.');
this.identityKey = options.identityKey;
}
}

if (options.banScore != null) {
assert(typeof this.options.banScore === 'number');
this.banScore = this.options.banScore;
Expand Down
3 changes: 0 additions & 3 deletions lib/node/fullnode.js
Expand Up @@ -93,9 +93,6 @@ class FullNode extends Node {
selfish: this.config.bool('selfish'),
compact: this.config.bool('compact'),
bip37: this.config.bool('bip37'),
bip151: this.config.bool('bip151'),
bip150: this.config.bool('bip150'),
identityKey: this.config.buf('identity-key'),
maxOutbound: this.config.uint('max-outbound'),
maxInbound: this.config.uint('max-inbound'),
createSocket: this.config.func('create-socket'),
Expand Down
3 changes: 0 additions & 3 deletions lib/node/spvnode.js
Expand Up @@ -68,9 +68,6 @@ class SPVNode extends Node {
seeds: this.config.array('seeds'),
nodes: this.config.array('nodes'),
only: this.config.array('only'),
bip151: this.config.bool('bip151'),
bip150: this.config.bool('bip150'),
identityKey: this.config.buf('identity-key'),
maxOutbound: this.config.uint('max-outbound'),
createSocket: this.config.func('create-socket'),
memory: this.config.bool('memory'),
Expand Down
238 changes: 0 additions & 238 deletions test/bip150-test.js

This file was deleted.

213 changes: 0 additions & 213 deletions test/bip151-test.js

This file was deleted.