bip150: consistency.
This commit is contained in:
parent
799d3ba55d
commit
1e1ee18e09
@ -33,6 +33,10 @@ function BIP150(bip151, hostname, outbound, db, identity) {
|
|||||||
return new BIP150(bip151, hostname, outbound, db, identity);
|
return new BIP150(bip151, hostname, outbound, db, identity);
|
||||||
|
|
||||||
assert(bip151, 'BIP150 requires BIP151.');
|
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);
|
EventEmitter.call(this);
|
||||||
|
|
||||||
@ -50,8 +54,8 @@ function BIP150(bip151, hostname, outbound, db, identity) {
|
|||||||
this.peerIdentity = this.db.getKnown(this.hostname);
|
this.peerIdentity = this.db.getKnown(this.hostname);
|
||||||
|
|
||||||
// Identity keypair
|
// Identity keypair
|
||||||
this.privateKey = identity || bcoin.ec.generatePrivateKey();
|
this.privateKey = identity;
|
||||||
this.publicKey = bcoin.ec.publicKeyCreate(this.privateKey, true);
|
this.publicKey = bcoin.ec.publicKeyCreate(identity, true);
|
||||||
|
|
||||||
this.challengeReceived = false;
|
this.challengeReceived = false;
|
||||||
this.replyReceived = false;
|
this.replyReceived = false;
|
||||||
@ -77,6 +81,7 @@ BIP150.prototype.challenge = function challenge(payload) {
|
|||||||
var type = this.outbound ? 'r' : 'i';
|
var type = this.outbound ? 'r' : 'i';
|
||||||
var msg, sig;
|
var msg, sig;
|
||||||
|
|
||||||
|
assert(this.bip151.handshake, 'No BIP151 handshake before challenge.');
|
||||||
assert(!this.challengeReceived, 'Peer challenged twice.');
|
assert(!this.challengeReceived, 'Peer challenged twice.');
|
||||||
this.challengeReceived = true;
|
this.challengeReceived = true;
|
||||||
|
|
||||||
@ -162,17 +167,18 @@ BIP150.prototype.propose = function propose(payload) {
|
|||||||
|
|
||||||
BIP150.prototype.toChallenge = function toChallenge(writer) {
|
BIP150.prototype.toChallenge = function toChallenge(writer) {
|
||||||
var p = new bcoin.writer(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.outbound, 'Cannot challenge an inbound connection.');
|
||||||
assert(this.peerIdentity, 'Cannot challenge without a peer identity.');
|
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.');
|
assert(!this.challengeSent, 'Cannot initiate challenge twice.');
|
||||||
this.challengeSent = true;
|
this.challengeSent = true;
|
||||||
|
|
||||||
p.writeBytes(hash);
|
p.writeBytes(msg);
|
||||||
|
|
||||||
if (!writer)
|
if (!writer)
|
||||||
p = p.render();
|
p = p.render();
|
||||||
@ -219,8 +225,8 @@ BIP150.prototype.findAuthorized = function findAuthorized(hash) {
|
|||||||
var i, key, msg;
|
var i, key, msg;
|
||||||
|
|
||||||
// Scary O(n) stuff.
|
// Scary O(n) stuff.
|
||||||
for (i = 0; i < this.db.auth.length; i++) {
|
for (i = 0; i < this.db.authorized.length; i++) {
|
||||||
key = this.db.auth[i];
|
key = this.db.authorized[i];
|
||||||
msg = this.hash(this.output.sid, 'p', key);
|
msg = this.hash(this.output.sid, 'p', key);
|
||||||
|
|
||||||
// XXX Do we really need a constant
|
// XXX Do we really need a constant
|
||||||
@ -285,7 +291,7 @@ function AuthDB() {
|
|||||||
return new AuthDB();
|
return new AuthDB();
|
||||||
|
|
||||||
this.known = {};
|
this.known = {};
|
||||||
this.auth = [];
|
this.authorized = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
AuthDB.prototype.addKnown = function addKnown(host, key) {
|
AuthDB.prototype.addKnown = function addKnown(host, key) {
|
||||||
@ -295,6 +301,12 @@ AuthDB.prototype.addKnown = function addKnown(host, key) {
|
|||||||
this.known[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) {
|
AuthDB.prototype.setKnown = function setKnown(map) {
|
||||||
var keys = Object.keys(map);
|
var keys = Object.keys(map);
|
||||||
var i, host, key;
|
var i, host, key;
|
||||||
@ -306,17 +318,11 @@ AuthDB.prototype.setKnown = function setKnown(map) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
AuthDB.prototype.addAuthorized = function addAuthorized(key) {
|
AuthDB.prototype.setAuthorized = function setAuthorized(keys) {
|
||||||
assert(Buffer.isBuffer(key) && key.length === 33,
|
|
||||||
'Invalid public key for authorized peer.');
|
|
||||||
this.auth.push(key);
|
|
||||||
};
|
|
||||||
|
|
||||||
AuthDB.prototype.setAuthorized = function setAuthorized(auth) {
|
|
||||||
var i, key;
|
var i, key;
|
||||||
|
|
||||||
for (i = 0; i < auth.length; i++) {
|
for (i = 0; i < keys.length; i++) {
|
||||||
key = auth[i];
|
key = keys[i];
|
||||||
this.addAuthorized(key);
|
this.addAuthorized(key);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user