{"id":3569,"date":"2016-06-02T17:58:35","date_gmt":"2016-06-02T23:58:35","guid":{"rendered":"http:\/\/benincosa.com\/?p=3569"},"modified":"2016-06-02T17:58:35","modified_gmt":"2016-06-02T23:58:35","slug":"ethereum-contracts","status":"publish","type":"post","link":"https:\/\/benincosa.com\/?p=3569","title":{"rendered":"Ethereum Contracts"},"content":{"rendered":"<p>I&#8217;ve been working with Eris to manipulate &#8220;smart contracts&#8221; (smart contracts are neither smart, nor are they contracts. \u00a0They&#8217;re just code written to the blockchain that stores values that can be changed with the right permissions).<\/p>\n<p>Eris has a pretty good <a href=\"https:\/\/github.com\/eris-ltd\/eris-by-example\/blob\/master\/eris_by_curl\/example.sh\">&#8216;eris-by example&#8217; script<\/a> that shows how you can do a lot of it with curl. \u00a0My object was to not use the Eris tools but make it a bit more generic for use with any Ethereum contract.<\/p>\n<p>I&#8217;ll gloss over creating the contract, but essentially, I just created a quick contract called &#8216;echo&#8217; and compiled it into the <a href=\"https:\/\/chriseth.github.io\/browser-solidity\/#version=soljson-latest.js\">solidity compiler<\/a>. (Yes I know it moved by the new link didn&#8217;t work for me )<\/p>\n<p>My code is pretty simple and borrowed from the millions of other echo &#8220;hello world&#8221; like contracts where it stores a string and an integer:<\/p>\n<pre class=\"lang:js decode:true \">contract echo {\r\n    string s;\r\n    uint64 amount; \r\n    \r\n    function echo(uint64 a, string d_s) {\r\n        s = d_s;\r\n        amount = a;\r\n    }   \r\n\r\n    function set_s(string new_s) {\r\n        s = new_s;\r\n    }   \r\n\r\n    function get_s() returns (string) {\r\n        return s;\r\n    }   \r\n    \r\n    function getAmount() returns(uint) {\r\n        return amount;\r\n    }\r\n}<\/pre>\n<p>After submitting the transaction I get addresses as to where it was submitted. \u00a0I&#8217;ll gloss over that detail as I&#8217;m interested in the other part of this and that is how to interact with the data.<\/p>\n<p>The solidity compiler shows that when it compiles the functions to javascript byte code it creates hashes of the function names themselves. \u00a0<a href=\"https:\/\/github.com\/ethereum\/wiki\/wiki\/Ethereum-Contract-ABI#function-selector-and-argument-encoding\">The documentation<\/a> shows that to call a function you do it by calling its name &#8216;derived as the first 4 bytes of the Keccak hash of the ASCII form of the signature&#8217;. \u00a0Luckily the solidity compiler gives this value to you.<\/p>\n<p>Now you just need to find the address of where the function lives.<\/p>\n<p>In my app I can now call:<\/p>\n<pre class=\"lang:sh decode:true\">curl 'http:\/\/&lt;eris-node&gt;:46657\/call?fromAddress=\"E881F77A3DDDD7AFA33F0C05BBF738852482CB9F\"&amp;toAddress=\"E3BFA7CE2BCD196BBB483BEE2E558C1BEE290FBD\"&amp;data=\"d321fe29\"'<\/pre>\n<p>Let&#8217;s break this down. \u00a0The from Address is one of the validators that I created. \u00a0The &#8216;toAddress&#8217; is the address of where the contract lives. \u00a0To get the value amount from this we found from the solidity compiler that the function was called by entering in &#8216;d321fe29&#8217;. \u00a0That then returns the following:<\/p>\n<pre class=\"lang:js decode:true \">{\r\n  \"jsonrpc\": \"2.0\",\r\n  \"id\": \"\",\r\n  \"result\": [\r\n    2,\r\n    {\r\n      \"return\": \"0000000000000000000000000000000000000000000000000000000000000064\",\r\n      \"gas_used\": 0\r\n    }\r\n  ],\r\n  \"error\": \"\"\r\n}<\/pre>\n<p>Our value is encoded in hex, and everyone knows that 64 is actually just 100.<\/p>\n<p>We can do the same thing to get the string value calling the get_s function:<\/p>\n<p>Using the above command but subsituting in 75d74f39 we get the following:<\/p>\n<pre class=\"lang:sh decode:true\">{\r\n  \"jsonrpc\": \"2.0\",\r\n  \"id\": \"\",\r\n  \"result\": [\r\n    2,\r\n    {\r\n      \"return\": \"0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001E416C6C206C617A7920646F6773206861766520676F6F6420616C776179730000\",\r\n      \"gas_used\": 0\r\n    }\r\n  ],\r\n  \"error\": \"\"\r\n}<\/pre>\n<p>To convert the returned hex into string we look at the output. \u00a0The first 32 bit string shows a 2. \u00a0This means that the data comprises the next 2 32 byte strings. \u00a0Decoding the last part:<\/p>\n<pre class=\"lang:sh decode:true\">echo 1E416C6C206C617A7920646F6773206861766520676F6F6420616C776179730000 | xxd -r -p\r\nAll lazy dogs have good always<\/pre>\n<p>Nice message.<\/p>\n<p>Now how to change this value? \u00a0If we had a set_amount function we would just add the value after the function. \u00a0Since we only have a set string we have to do it dynamically. \u00a0In this case figure out what our string will be then pad it with enough 0s. \u00a0<a href=\"https:\/\/github.com\/ethereum\/wiki\/wiki\/Ethereum-Contract-ABI#use-of-dynamic-types\">Strings are &#8216;dynamic types&#8217; as specified in the documentation<\/a>. \u00a0So we have to add a few more fields. \u00a0First the function:<\/p>\n<pre class=\"lang:sh decode:true\">e7aab290 set_s(string)<\/pre>\n<p>Let&#8217;s first make a string:<\/p>\n<pre class=\"lang:sh decode:true \">echo \"hello world\" | xxd -p -c 32\r\n68656c6c6f20776f726c640a<\/pre>\n<p>This will fit in one 64 byte string so we add the extra 0s to the end and add the 64 byte string to tell us the offset for it. \u00a0The argument to the function then looks as follows:<\/p>\n<pre class=\"lang:sh decode:true\">0000000000000000000000000000000000000000000000000000000000000002\r\n68656c6c6f20776f726c640a0000000000000000000000000000000000000000<\/pre>\n<p>Adding this to the one command:<\/p>\n<pre class=\"lang:sh decode:true\">curl 'http:\/\/&lt;eris-node&gt;:46657\/call?fromAddress=\"E881F77A3DDDD7AFA33F0C05BBF738852482CB9F\"&amp;toAddress=\"E3BFA7CE2BCD196BBB483BEE2E558C1BEE290FBD\"&amp;data=\"e7aab290000000000000000000000000000000000000000000000000000000000000000268656c6c6f20776f726c640a0000000000000000000000000000000000000000\"'<\/pre>\n<p>The output will have changed nothing. \u00a0Why? \u00a0From the <a href=\"https:\/\/github.com\/eris-ltd\/eris-by-example\/blob\/master\/eris_by_curl\/example.sh\">README<\/a> we\u00a0read that:<\/p>\n<p>&#8221; It is possible to &#8220;query&#8221; contracts using the \/call endpoint. \u00a0Such queries are only &#8220;simulated calls&#8221;, in that there is no transaction (or signature) required, and hence they have no effect on the blockchain state.&#8221;<\/p>\n<p>So to change this for reals we need to actually sign our call. \u00a0I tried several methods at present but eris only returns garbage. \u00a0For example here&#8217;s a command I ran:<\/p>\n<pre class=\"lang:sh decode:true \">curl -X POST -H \"Accept: application\/json\" &lt;eris-node&gt;:46656 --data '{\"jsonrpc\":\"2.0\",\"value\":\"3\",\"method\":\"eth_call\",\"params\":[{\"from\":\"E881F77A3DDDD7AFA33F0C05BBF738852482CB9F\", \"to\":\"E3BFA7CE2BCD196BBB483BEE2E558C1BEE290FBD\", \"data\":\"e7aab290000000000000000000000000000000000000000000000000000000000000000268656c6c6f20776f726c640a0000000000000000000000000000000000000000\"}]}'<\/pre>\n<p>The output was garbled guck. \u00a0At this point I&#8217;m stuck on this effort but thought I would at least show where I am. \u00a0Using the JSON RPC is perhaps too low level and ideally you would use the Eris javascript library (<a href=\"https:\/\/docs.erisindustries.com\/documentation\/eris-contracts-js\/\">eris-contracts<\/a>) \u00a0My hope instead was to use the RPC API to accomplish this. \u00a0Perhaps more to come!<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working with Eris to manipulate &#8220;smart contracts&#8221; (smart contracts are neither smart, nor are they contracts. \u00a0They&#8217;re just code written to the blockchain that stores values that can be changed with the right permissions). Eris has a pretty good &#8216;eris-by example&#8217; script that shows how you can do a lot of it with&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[826,825],"tags":[828,827,832],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3569"}],"collection":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3569"}],"version-history":[{"count":1,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3569\/revisions"}],"predecessor-version":[{"id":3570,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3569\/revisions\/3570"}],"wp:attachment":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}