2

I am sending data with my transaction by using following way

var tx = {from: eth.coinbase, to:eth.accounts[1], value: web3.toWei(1, "ether"),data:web3.toHex("Test Data")}

personal.sendTransaction(tx, "password")

As we can see I am using simple string in data which is converted to hex, I have some questions around it

  1. Is there any recommended datatype to use it within data field
  2. Is there any standard way to put JSON data in that data field.

(note:currently I am sending JSON data by manually converting the data to JSON string by following way data:web3.toHex("{\"key1\":\"Data1\",\"key2\":\"Data2\"}"))

1 Answer 1

2

Encoding a JSON string does not seem like a good idea. See this.

Instead of supplying an object as data, you should define your contract function to take the relevant inputs (such as function f(string key1, uint key2){}) and use a transaction library such as ethereumjs-tx to compute the data according to your parameters. Here is an example with only Web3.

8
  • According to this the data is stored in contract storage, But I want to store data on blockchain as a transaction, eg. If we take example of office assets management, If I get new mouse from hardware guy, I should be stored on block chain, what is the best way to do that. Commented May 29, 2017 at 11:16
  • 1
    Storing data as a transaction is an interesting idea, but the standard way is to store it in a contract. It will be more easily accessible. You will only need the contract address and ABI. To restore data from a tx. hash (or receipt) you will probably need a more complex and specialized software.
    – jeff
    Commented May 29, 2017 at 14:17
  • Any special software was not needed, you can get a hash in input field of transaction data and convert it to ASCII to get the input fields, But the only major problem is, the data field in transaction will accept only string data There is example of transaction var tx = {from: eth.coinbase, to:eth.accounts[1], value: web3.toWei(10, "ether"),data:web3.toHex("D1:Testing Data")} Commented May 30, 2017 at 6:50
  • can you suggest me any good example of storing data in contract storage which suites the above asset management use case Commented May 30, 2017 at 7:03
  • The data field is usually the RLP encoding of the function name, parameter names and values. Any basic contract call example will do. You cannot just hex-encode "name: value" and put it as data. If you do, your contract needs to do a complex parsing, that's what I called special software.
    – jeff
    Commented May 30, 2017 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.