ethereumjs / ethereumjs-abi

[DEPRECATED] Decoder and encoder for the Ethereum ABI
MIT License
300 stars 162 forks source link

Adding encoding aided by the JSON ABI #74

Closed pouladzade closed 5 years ago

pouladzade commented 5 years ago

1-Adding encoding aided by the JSON ABI (encode(json_abi, function_name, parameter_list) ) 2- Updating the readme 3- Adding tests regarding the new encoding method

Example:

var json_abi = [{"constant":false,"inputs":[{"name":"a","type":"int256"}],"name":"setA","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"a","type":"int256"},{"name":"b","type":"string"},{"name":"c","type":"bytes8"}],"name":"setABC","outputs":[{"name":"","type":"int256"},{"name":"","type":"string"},{"name":"","type":"bytes8"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"b","type":"string"}],"name":"setB","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"c","type":"bytes8"}],"name":"setC","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]

var encoded = abi.encode(json_abi, 'setABC', [12, 'salam', [1, 2, 3, 4, 5, 6, 7, 8]])
console.log(encoded.toString("hex"))

Output:

f77a13e1000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000600102030405060708000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000573616c616d000000000000000000000000000000000000000000000000000000
coveralls commented 5 years ago

Coverage Status

Coverage increased (+0.8%) to 87.671% when pulling c365fb6cbe191799ed44f7f54afc3407f174bccc on pouladzade:json-abi into 2863c40e0982acfc0b7163f0285d4c56427c7799 on ethereumjs:master.

holgerd77 commented 5 years ago

Hi @pouladzade, thanks, great that you are tackling this! 😄

Is this ready for review?

pouladzade commented 5 years ago

Hi @holgerd77 , You are welcome! Yes, I would be happy if you review the codes and let me know if I need to change it. I am also working on decode function which will send it by another pr.

holgerd77 commented 5 years ago

Hi Ahmad, sorry didn't find the time. I understand if you loose patience, but would also be a pity if all the work gets lost. If you would be willing to reopen I'll try to more quickly address.

pouladzade commented 5 years ago

Hi @holgerd77 Thank you so much for your kind comment. I have implemented the decode method as well, but I need to test it more and make sure that I have considered all the corner cases, then I will open a new pr for sure. I am providing encode and decode using JSON ABI for both Input and output parameters.