Web3js and IPFS – How to Access Data from Blockchain

ipfsweb3js

I am using IPFS to store data [As per suggestion from an answer in ethereum stackexchange ,I couldnt find link to the question:( ]. The hash of file stored in ipfs is inserted into the data field of a transaction. But I couldnt find a way to access these datafields . Tried fetching transaction by using the transactionhash , but the retrieved result does not have a data field . Is there any function similar to eth.filter in web3 v1.0 ?.

function(req,res){
console.log(req.body)
web3.eth.getAccounts(function(e,accounts){

    web3.eth.personal.unlockAccount(accounts[0],req.body.pass, 1000000);
    web3.eth.sendTransaction({
        from:accounts[0],
        to:req.body.address,
        data: req.body.id
    }, function(error, hash){
       console.log(error)
       console.log(hash)
    });


})

}

Running the above code returns a transaction hash . req.body.id is fetched from an api endpoint which returns hash of the uploaded file . To list all transactions , I tried to use web3.eth.filter but it returned an error :

TypeError: web3.eth.filter is not a function

Then I console logged web3.eth and couldnt find any filter function in the log .Is there any function to list all the transactions from/to an address ?

Best Answer

Using web3 v1.0 I sent this transaction

eth.sendTransaction({
  from: account0,
  to: account1,
  value: 0,
  data: web3.utils.toHex("Hello World")
});

Now with the returned hash I can query the transaction (after it is mined) } eth.getTransaction("0x6c2135c..");`. And we get the resulting transaction

{
  blockHash: "0xb69212f..",
  blockNumber: 510,
  from: "0x9bc62f..",
  gas: 90000,
  gasPrice: 100000000000,
  hash: "0x6c2135c..",
  input: "0x48656c6c6f20576f726c64",
  nonce: 11,
  r: "0x49097a7..",
  s: "0x2b6ce95..",
  to: "0x7fe0197..",
  transactionIndex: 0,
  v: "0xec",
  value: 0
}

The field you have to look is input you can decode it web3.utils.toAscii("0x48656c6c6f20576f726c64"), it will return

"Hello World"

Related Topic