From 1e1ee18e09e77b2138ecdd0415a719866eec7efa Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 23 Aug 2016 05:33:53 -0700 Subject: [PATCH] bip150: consistency. --- lib/bcoin/bip150.js | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/bcoin/bip150.js b/lib/bcoin/bip150.js index 26dbef49..c1de195e 100644 --- a/lib/bcoin/bip150.js +++ b/lib/bcoin/bip150.js @@ -33,6 +33,10 @@ function BIP150(bip151, hostname, outbound, db, identity) { return new BIP150(bip151, hostname, outbound, db, identity); assert(bip151, 'BIP150 requires BIP151.'); + assert(typeof hostname === 'string', 'Hostname required.'); + assert(typeof outbound === 'boolean', 'Outbound flag required.'); + assert(db instanceof AuthDB, 'Auth DB required.'); + assert(Buffer.isBuffer(identity), 'Identity key required.'); EventEmitter.call(this); @@ -50,8 +54,8 @@ function BIP150(bip151, hostname, outbound, db, identity) { this.peerIdentity = this.db.getKnown(this.hostname); // Identity keypair - this.privateKey = identity || bcoin.ec.generatePrivateKey(); - this.publicKey = bcoin.ec.publicKeyCreate(this.privateKey, true); + this.privateKey = identity; + this.publicKey = bcoin.ec.publicKeyCreate(identity, true); this.challengeReceived = false; this.replyReceived = false; @@ -77,6 +81,7 @@ BIP150.prototype.challenge = function challenge(payload) { var type = this.outbound ? 'r' : 'i'; var msg, sig; + assert(this.bip151.handshake, 'No BIP151 handshake before challenge.'); assert(!this.challengeReceived, 'Peer challenged twice.'); this.challengeReceived = true; @@ -162,17 +167,18 @@ BIP150.prototype.propose = function propose(payload) { BIP150.prototype.toChallenge = function toChallenge(writer) { var p = new bcoin.writer(writer); - var hash; + var msg; + assert(this.bip151.handshake, 'No BIP151 handshake before challenge.'); assert(this.outbound, 'Cannot challenge an inbound connection.'); assert(this.peerIdentity, 'Cannot challenge without a peer identity.'); - hash = this.hash(this.output.sid, 'i', this.peerIdentity); + msg = this.hash(this.output.sid, 'i', this.peerIdentity); assert(!this.challengeSent, 'Cannot initiate challenge twice.'); this.challengeSent = true; - p.writeBytes(hash); + p.writeBytes(msg); if (!writer) p = p.render(); @@ -219,8 +225,8 @@ BIP150.prototype.findAuthorized = function findAuthorized(hash) { var i, key, msg; // Scary O(n) stuff. - for (i = 0; i < this.db.auth.length; i++) { - key = this.db.auth[i]; + for (i = 0; i < this.db.authorized.length; i++) { + key = this.db.authorized[i]; msg = this.hash(this.output.sid, 'p', key); // XXX Do we really need a constant @@ -285,7 +291,7 @@ function AuthDB() { return new AuthDB(); this.known = {}; - this.auth = []; + this.authorized = []; } AuthDB.prototype.addKnown = function addKnown(host, key) { @@ -295,6 +301,12 @@ AuthDB.prototype.addKnown = function addKnown(host, key) { this.known[host] = key; }; +AuthDB.prototype.addAuthorized = function addAuthorized(key) { + assert(Buffer.isBuffer(key) && key.length === 33, + 'Invalid public key for authorized peer.'); + this.authorized.push(key); +}; + AuthDB.prototype.setKnown = function setKnown(map) { var keys = Object.keys(map); var i, host, key; @@ -306,17 +318,11 @@ AuthDB.prototype.setKnown = function setKnown(map) { } }; -AuthDB.prototype.addAuthorized = function addAuthorized(key) { - assert(Buffer.isBuffer(key) && key.length === 33, - 'Invalid public key for authorized peer.'); - this.auth.push(key); -}; - -AuthDB.prototype.setAuthorized = function setAuthorized(auth) { +AuthDB.prototype.setAuthorized = function setAuthorized(keys) { var i, key; - for (i = 0; i < auth.length; i++) { - key = auth[i]; + for (i = 0; i < keys.length; i++) { + key = keys[i]; this.addAuthorized(key); } };