allow eth_syncing to return false, support HTTP Basic Auth (#545)

* strict mode in formatters.js

* allow eth.syncing to return false

* HTTPProvider: support HTTP basic auth
This commit is contained in:
Jannis Redmann 2017-06-24 10:21:43 +02:00 committed by Fabian Vogelsteller
parent 91f6cb2cf5
commit 8fbd5f7c3c
4 changed files with 71 additions and 4 deletions

View File

@ -1,3 +1,5 @@
'use strict'
/*
This file is part of web3.js.
@ -190,11 +192,11 @@ var outputBlockFormatter = function(block) {
* @returns {Object} log
*/
var outputLogFormatter = function(log) {
if(log.blockNumber !== null)
if(log.blockNumber)
log.blockNumber = utils.toDecimal(log.blockNumber);
if(log.transactionIndex !== null)
if(log.transactionIndex)
log.transactionIndex = utils.toDecimal(log.transactionIndex);
if(log.logIndex !== null)
if(log.logIndex)
log.logIndex = utils.toDecimal(log.logIndex);
return log;
@ -273,6 +275,9 @@ var inputAddressFormatter = function (address) {
var outputSyncingFormatter = function(result) {
if (!result) {
return result;
}
result.startingBlock = utils.toDecimal(result.startingBlock);
result.currentBlock = utils.toDecimal(result.currentBlock);

View File

@ -36,13 +36,16 @@ if (typeof window !== 'undefined' && window.XMLHttpRequest) {
}
var XHR2 = require('xhr2'); // jshint ignore: line
var btoa = require('btoa');
/**
* HttpProvider should be used to send rpc calls over http
*/
var HttpProvider = function (host, timeout) {
var HttpProvider = function (host, timeout, user, password) {
this.host = host || 'http://localhost:8545';
this.timeout = timeout || 0;
this.user = user;
this.password = password;
};
/**
@ -63,6 +66,10 @@ HttpProvider.prototype.prepareRequest = function (async) {
}
request.open('POST', this.host, async);
if (this.user && this.password) {
var value = 'Basic ' + btoa(this.user + ':' + this.password);
request.setRequestHeader('Authorization', value);
}
request.setRequestHeader('Content-Type','application/json');
return request;
};

View File

@ -8,6 +8,7 @@
"lib": "./lib"
},
"dependencies": {
"btoa": "^1.1.2",
"bignumber.js": "git+https://github.com/frozeman/bignumber.js-nolookahead.git",
"crypto-js": "^3.1.4",
"utf8": "^2.1.1",

View File

@ -0,0 +1,54 @@
'use strict'
var chai = require('chai');
var Web3 = require('../index');
var assert = chai.assert;
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
describe('eth', function () {
describe('getSyncing', function () {
it('syncing object', function (done) {
// given
var provider = new FakeHttpProvider();
var web3 = new Web3(provider);
provider.injectResult({
startingBlock: '0xb',
currentBlock: '0xb',
highestBlock: '0xb'
});
provider.injectValidation(function(payload) {
assert.equal(payload.jsonrpc, '2.0', 'failed');
assert.equal(payload.method, 'eth_syncing');
});
// call
web3.eth.getSyncing(function(err, res){
assert.deepEqual(res, {
startingBlock: 11,
currentBlock: 11,
highestBlock: 11
});
done();
});
});
it('false', function (done) {
// given
var provider = new FakeHttpProvider();
var web3 = new Web3(provider);
provider.injectResult(false);
provider.injectValidation(function(payload) {
assert.equal(payload.jsonrpc, '2.0', 'failed');
assert.equal(payload.method, 'eth_syncing');
});
// call
web3.eth.getSyncing(function(err, res){
console.log('err', err, 'res', res)
assert.strictEqual(res, false);
done();
});
});
});
});