Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #205 from bookchin/master
Browse files Browse the repository at this point in the history
[v1.3.1]
  • Loading branch information
bookchin authored Jun 22, 2016
2 parents 1634377 + d5f5d60 commit 8784b9f
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 23 deletions.
35 changes: 35 additions & 0 deletions bin/storj.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ function getKeyRing(callback) {
});
}

function getNewPassword(callback) {
prompt.start();
prompt.get({
properties: {
password: {
description: 'Enter your new desired password',
required: true,
replace: '*',
hidden: true
}
}
}, callback);
}

function getCredentials(callback) {
prompt.start();
prompt.get({
Expand Down Expand Up @@ -233,6 +247,22 @@ var ACTIONS = {
log('info', 'This device has been successfully unpaired.');
});
},
resetpassword: function resetpassword(email) {
getNewPassword(function(err, result) {
PublicClient().resetPassword({
email: email,
password: result.password
}, function(err) {
if (err) {
return log('error', 'Failed to request password reset, reason: %s', [
err.message
]);
}

log('info', 'Password reset request processed, check your email to continue.');
});
});
},
listkeys: function listkeys() {
PrivateClient().getPublicKeys(function(err, keys) {
if (err) {
Expand Down Expand Up @@ -842,6 +872,11 @@ program
.description('revoke this device\'s access your storj api account')
.action(ACTIONS.logout);

program
.command('reset-password <email>')
.description('request an account password reset email')
.action(ACTIONS.resetpassword);

program
.command('list-keys')
.description('list your registered public keys')
Expand Down
6 changes: 3 additions & 3 deletions lib/bridgeclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ BridgeClient.prototype.getContactByNodeId = function(nodeId, callback) {
BridgeClient.prototype.createUser = function(options, callback) {
return this._request('POST', '/users', {
email: options.email,
password: utils.sha256(options.password),
password: utils.sha256(options.password, 'utf8'),
redirect: options.redirect,
pubkey: options.pubkey
}, callback);
Expand Down Expand Up @@ -123,7 +123,7 @@ BridgeClient.prototype.destroyUser = function(options, callback) {
*/
BridgeClient.prototype.resetPassword = function(options, callback) {
return this._request('PATCH', '/users/' + options.email, {
password: utils.sha256(options.password),
password: utils.sha256(options.password, 'utf8'),
redirect: options.redirect
}, callback);
};
Expand Down Expand Up @@ -546,7 +546,7 @@ BridgeClient.prototype._authenticate = function(opts) {
} else if (this._options.basicauth) {
opts.auth = {
user: this._options.basicauth.email,
pass: utils.sha256(this._options.basicauth.password)
pass: utils.sha256(this._options.basicauth.password, 'utf8')
};
}

Expand Down
3 changes: 1 addition & 2 deletions lib/datachannel/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ DataChannelServer.prototype._handleConsignStream = function(socket, token) {

passthrough.on('end', function() {
if (utils.rmd160(hasher.digest('hex')) !== hash) {
self._log.warn('Calculated hash does not match the expected result');

self._log.warn('calculated hash does not match the expected result');
socket.close(400, 'Calculated hash does not match the expected result');
return self.reject(token);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/interfaces/farmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ FarmerInterface.prototype._negotiateContract = function(contract) {

if (!renter) {
self._removeContractFromPendingList(contract);
return self._logger.warn('Could not locate renter for offer');
return self._logger.warn('could not locate renter for offer');
}

self._sendOfferForContract(contract, renter);
Expand Down Expand Up @@ -253,11 +253,11 @@ FarmerInterface.prototype._handleOfferRes = function(res, contract, renter) {
try {
final = Contract.fromObject(res.result.contract);
} catch (err) {
return self._logger.error('Renter responded with invalid contract');
return self._logger.error('renter responded with invalid contract');
}

if (!final.verify('renter', contract.get('renter_id'))) {
return self._logger.error('Renter signature is invalid');
return self._logger.error('renter signature is invalid');
}

self._manager.load(contract.get('data_hash'), function(err, item) {
Expand Down
9 changes: 7 additions & 2 deletions lib/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,8 @@ Network.prototype._listenForTunnelers = function() {
this._pubsub.subscribe(
Buffer.concat([prefix, available]).toString('hex'),
function(contact) {
if (self._tunnelers.getSize() < kad.constants.K) {
if (!self._tunnelers.addContact(Contact(contact))) {
self._tunnelers.removeContact(self._tunnelers.getContact(0));
self._tunnelers.addContact(Contact(contact));
}
}
Expand Down Expand Up @@ -602,7 +603,7 @@ Network.prototype._findTunnel = function(neighbors, callback) {

// NB: If we are going to be tunneled, we better not accept any tunnel
// NB: connections from other nodes, so let's override our maxTunnels.
this._transport._tunserver._options.maxTunnels = 0;
this._options.tunnels = this._transport._tunserver._options.maxTunnels = 0;

if (!neighbors.length) {
return callback(
Expand All @@ -617,6 +618,10 @@ Network.prototype._findTunnel = function(neighbors, callback) {
return callback(false);
}

if (!resp.result.tunnels.length) {
return callback(false);
}

tunnelers = tunnelers.concat(resp.result.tunnels);

callback(true);
Expand Down
18 changes: 12 additions & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,34 @@ var base58 = bitcore.deps.bs58;
/**
* Returns the SHA-256 hash of the input
* @param {String|Buffer} input - Data to hash
* @param {String} encoding - The encoding type of the data
* @returns {String}
*/
module.exports.sha256 = function(input) {
return crypto.createHash('sha256').update(input).digest('hex');
module.exports.sha256 = function(input, encoding) {
return crypto.createHash('sha256').update(input, encoding).digest('hex');
};

/**
* Returns the RIPEMD-160 hash of the input
* @param {String|Buffer} input - Data to hash
* @param {String} encoding - The encoding type of the data
* @returns {String}
*/
module.exports.rmd160 = function(input) {
return crypto.createHash('rmd160').update(input).digest('hex');
module.exports.rmd160 = function(input, encoding) {
return crypto.createHash('rmd160').update(input, encoding).digest('hex');
};

/**
* Returns the RIPEMD-160 SHA-256 hash of this input
* @param {String|Buffer} input - Data to hash
* @param {String} encoding - The encoding type of the data
* @returns {String}
*/
module.exports.rmd160sha256 = function(input) {
return module.exports.rmd160(module.exports.sha256(input));
module.exports.rmd160sha256 = function(input, encoding) {
return module.exports.rmd160(
module.exports.sha256(input, encoding),
encoding
);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "storj",
"version": "1.3.0",
"version": "1.3.1",
"description": "implementation of the storj protocol for node.js and the browser",
"main": "index.js",
"directories": {
Expand Down
4 changes: 2 additions & 2 deletions test/interfaces/farmer.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ describe('FarmerInterface', function() {
setImmediate(function() {
_error.restore();
expect(
_error.calledWith('Renter responded with invalid contract')
_error.calledWith('renter responded with invalid contract')
).to.equal(true);
done();
});
Expand All @@ -399,7 +399,7 @@ describe('FarmerInterface', function() {
setImmediate(function() {
_error.restore();
expect(
_error.calledWith('Renter signature is invalid')
_error.calledWith('renter signature is invalid')
).to.equal(true);
done();
});
Expand Down
51 changes: 47 additions & 4 deletions test/network/index.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ describe('Network (private)', function() {
});
});

it('should not add contact when publish received and full', function(done) {
it('should remove the oldest tunneler to add the new one', function(done) {
var net = Network({
keypair: KeyPair(),
manager: Manager(RAMStorageAdapter()),
Expand All @@ -347,8 +347,24 @@ describe('Network (private)', function() {
noforward: true,
tunnels: 0
});
var _acCallCount = 0;
var _getSize = sinon.stub(net._tunnelers, 'getSize').returns(20);
var _addContact = sinon.stub(net._tunnelers, 'addContact');
var _addContact = sinon.stub(net._tunnelers, 'addContact', function() {
_acCallCount++;

if (_acCallCount === 1) {
return false;
}

return true;
});
var _removeContact = sinon.stub(net._tunnelers, 'removeContact');
var _indexOf = sinon.stub(net._tunnelers, 'getContact').returns(Contact({
address: '127.0.0.1',
port: 1338,
nodeID: utils.rmd160('nodeid1'),
protocol: version.protocol
}));
var _subscribe = sinon.stub(net._pubsub, 'subscribe', function(t, cb) {
if (t === '0e01') {
cb({
Expand All @@ -364,8 +380,9 @@ describe('Network (private)', function() {
_getSize.restore();
_addContact.restore();
_subscribe.restore();
expect(_getSize.called).to.equal(true);
expect(_addContact.called).to.equal(false);
_indexOf.restore();
expect(_removeContact.called).to.equal(true);
expect(_addContact.callCount).to.equal(2);
done();
});
});
Expand Down Expand Up @@ -574,6 +591,32 @@ describe('Network (private)', function() {
});
});

it('should try all neighbors for tunnel list', function(done) {
var net = Network({
keypair: KeyPair(),
manager: Manager(RAMStorageAdapter()),
logger: kad.Logger(0),
seeds: [],
address: '127.0.0.1',
port: 0,
noforward: true
});
var _send = sinon.stub(net._transport, 'send').callsArgWith(
2,
null,
{ result: { tunnels: [] } }
);
var contact = { address: '127.0.0.1', port: 1337 };
net._findTunnel([contact, contact], function(err) {
_send.restore();
expect(_send.callCount).to.equal(2);
expect(err.message).to.equal(
'Failed to find tunnels from neighbors'
);
done();
});
});

it('should callback error if send fails', function(done) {
var net = Network({
keypair: KeyPair(),
Expand Down
3 changes: 3 additions & 0 deletions test/tunnel/server.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ describe('TunnelServer', function() {
});
var options = [55000, 55001, 55002];
ts.createGateway(function(err, gw1) {
expect(err).to.equal(null);
options.splice(options.indexOf(gw1.getEntranceAddress().port), 1);
ts.createGateway(function(err, gw2) {
expect(err).to.equal(null);
options.splice(options.indexOf(gw2.getEntranceAddress().port), 1);
ts.createGateway(function(err, gw3) {
expect(err).to.equal(null);
options.splice(options.indexOf(gw3.getEntranceAddress().port), 1);
expect(options).to.have.lengthOf(0);
ts.createGateway(function(err) {
Expand Down

0 comments on commit 8784b9f

Please sign in to comment.