On Ropsten testnet I have deployed a contract like this:
// creation of contract object
var aContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"lastIdMessage","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"readYourLastMessage","outputs":[{"name":"message","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"idMessage","type":"uint256"}],"name":"readYourMessageById","outputs":[{"name":"message","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"idMessage","type":"uint256"}],"name":"readYourMessageMetadataById","outputs":[{"name":"_idMessage","type":"uint256"},{"name":"_idPrevious","type":"uint256"},{"name":"_timestamp","type":"uint256"},{"name":"_from","type":"address"},{"name":"_message","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"timestamp","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"uint256"}],"name":"messages","outputs":[{"name":"idMessage","type":"uint256"},{"name":"idPrevious","type":"uint256"},{"name":"timestamp","type":"uint256"},{"name":"from","type":"address"},{"name":"message","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"countMessage","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"whoAmI","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"message","type":"string"}],"name":"sendMessage","outputs":[],"payable":false,"type":"function"},{"payable":false,"type":"fallback"}]);
// initiate contract for an address
var sc = aContract.at('0x59dbff7e055b09f02d5508a7c42f09b4683b2934');
This contract has a method I am trying to invoke but first I need to undertand how to encode properly the string parameter:
// invoke contract method sendMessage(address,string)
sc.sendMessage.sendTransaction("0xa7e3c7c227c72a60e5a2f9912448fb1c21078769", "Hi Juan, this is marketpay sending first blockchain ever message!", {from:"0xf28dafbfeb41bf32869c9d498da0d651d0206ed4", gas:1000000});
"0xe1a5d54c5b2ed440d55405ab73516245e909a2e345c734438fc9878801365c56"
https://testnet.etherscan.io/tx/0xe1a5d54c5b2ed440d55405ab73516245e909a2e345c734438fc9878801365c56
According to this previous transaction I can explore how string data was serialized:
// hex header
0x
// 8 chars for method hash
de6f24bb
// 64 chars for first parameter, an account address
000000000000000000000000a7e3c7c227c72a60e5a2f9912448fb1c21078769
// 320 chars for encoding a string
000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000414869204a75616e2c2074686973206973206d61726b65747061792073656e64696e6720666972737420626c6f636b636861696e2065766572206d6573736167652100000000000000000000000000000000000000000000000000000000000000
Well, the question is how can I go from this string:
"Hi Juan, this is marketpay sending first blockchain ever message!"
to this hex value ? :
000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000414869204a75616e2c2074686973206973206d61726b65747061792073656e64696e6720666972737420626c6f636b636861696e2065766572206d6573736167652100000000000000000000000000000000000000000000000000000000000000
Where can I find a doc explaining string serialization on Ethereum? Thx!