var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var blobstoreAbi = require('./blobstore.abi.json');
var blobstoreContract = web3.eth.contract(blobstoreAbi);
var blobstoreAddress = '0x3c531591cb807e01404574076f429d205f5ee981';
var blobstore = blobstoreContract.at(blobstoreAddress);

//Solidity version: 0.1.7-f86451cd/.-Emscripten/clang/int linked to libethereum-1.1.0-35b67881/.-Emscripten/clang/int

var storeBlob = function(blob) {
  var gas = 45000 + 80 * blob.length;
  blobstore.storeBlob('0x' + blob.toString('hex'), {gas: gas});
  return '0x' + web3.sha3(blob.toString('ascii'));
}

var getBlobBlock = function(hash) {
  return blobstore.getBlobBlock(hash).toFixed();
}

var getBlob = function(hash, callback) {

  var block = blobstore.getBlobBlock(hash).toFixed();
  var filter = web3.eth.filter({fromBlock: block, toBlock: block, address: blobstoreAddress, topics: [hash]});
  filter.get(function(error, result) {
    var length = parseInt(result[0].data.substr(66, 64), 16);
    callback(new Buffer(result[0].data.substr(130, length * 2), 'hex'));
  });
}

module.exports = {
  storeBlob: storeBlob,
  getBlobBlock: getBlobBlock,
  getBlob: getBlob
};

var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var blobstoreAbi = require('./blobstore.abi.json');
var blobstoreContract = web3.eth.contract(blobstoreAbi);
var blobstoreAddress = '0x3c531591cb807e01404574076f429d205f5ee981';
var blobstore = blobstoreContract.at(blobstoreAddress);

//Solidity version: 0.1.7-f86451cd/.-Emscripten/clang/int linked to libethereum-1.1.0-35b67881/.-Emscripten/clang/int

var storeBlob = function(blob) {
  var gas = 44800 + 78 * blob.length;
  blobstore.storeBlob('0x' + blob.toString('hex'), {gas: gas});
  return '0x' + web3.sha3(blob.toString('ascii'));
}

var getBlobBlock = function(hash) {
  return blobstore.getBlobBlock(hash, {}, 'latest').toFixed();
}

var getBlob = function(hash, callback) {

  var block = getBlobBlock(hash);
  var filter = web3.eth.filter({fromBlock: block, toBlock: block, address: blobstoreAddress, topics: [hash]});
  filter.get(function(error, result) {
    if (result != 0) {
      var length = parseInt(result[0].data.substr(66, 64), 16);
      callback(new Buffer(result[0].data.substr(130, length * 2), 'hex'));
    }
    else {
      
    }
  });
}

web3.eth.defaultAccount = web3.eth.accounts[0];
//var hash = storeBlob(new Buffer("hello again5"));
//console.log(hash);
var hash = "0x71cc9e8819a5c08ffd3f05ab192e7de94d0265905b2da039000e2aa0024424ac";
console.log(getBlobBlock(hash));

module.exports = {
  storeBlob: storeBlob,
  getBlobBlock: getBlobBlock,
  getBlob: getBlob
};

Reduce estimated gas requirement.