4

I am wondering that is there any way to find the byte-code of the Smart Contract which is published on a blockchain just by having only idea about it's contract address using JSON RPC running at 8545 port?

I was thinking to use eth_getStorageAt and bruteforce the value of the block. But, it is not working well.

Is there any other way to do it?

1 Answer 1

3

What you are looking for is eth_getCode:

Returns code at a given address.

Example:

$ curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getCode","params":["0x0289eC8BDFBF5f6d90102E752e674F18f7a8Ed03", "latest"],"id":1}' http://127.0.0.1:8545

{"jsonrpc":"2.0","id":1,"result":"0x608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680637bd703e81461005c57806390b98a11146100c1578063f8b2cb4f14610134575b600080fd5b34801561006857600080fd5b506100ab6004803603602081101561007f57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610199565b6040518082815260200191505060405180910390f35b3480156100cd57600080fd5b5061011a600480360360408110156100e457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610259565b604051808215151515815260200191505060405180910390f35b34801561014057600080fd5b506101836004803603602081101561015757600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506103b2565b6040518082815260200191505060405180910390f35b6000730d062b196b537fa208692cc6b53cdfd2f11c487f6396e4ee3d6101be846103b2565b60026040518363ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401808381526020018281526020019250505060206040518083038186803b15801561021757600080fd5b505af415801561022b573d6000803e3d6000fd5b505050506040513d602081101561024157600080fd5b81019080805190602001909291905050509050919050565b6000816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156102aa57600090506103ac565b816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190505b92915050565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905091905056fea165627a7a723058209ef44d5916f44b6e9c0da9a389164ff76784a11fd1e82df271f8529ca2238b280029"}

Example with Geth:

> eth.getCode("0x0289eC8BDFBF5f6d90102E752e674F18f7a8Ed03")
"0x608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680637bd703e81461005c57806390b98a11146100c1578063f8b2cb4f14610134575b600080fd5b34801561006857600080fd5b506100ab6004803603602081101561007f57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610199565b6040518082815260200191505060405180910390f35b3480156100cd57600080fd5b5061011a600480360360408110156100e457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610259565b604051808215151515815260200191505060405180910390f35b34801561014057600080fd5b506101836004803603602081101561015757600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506103b2565b6040518082815260200191505060405180910390f35b6000730d062b196b537fa208692cc6b53cdfd2f11c487f6396e4ee3d6101be846103b2565b60026040518363ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401808381526020018281526020019250505060206040518083038186803b15801561021757600080fd5b505af415801561022b573d6000803e3d6000fd5b505050506040513d602081101561024157600080fd5b81019080805190602001909291905050509050919050565b6000816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156102aa57600090506103ac565b816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190505b92915050565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905091905056fea165627a7a723058209ef44d5916f44b6e9c0da9a389164ff76784a11fd1e82df271f8529ca2238b280029"

https://rinkeby.etherscan.io/address/0x0289eC8BDFBF5f6d90102E752e674F18f7a8Ed03#contracts

5
  • Do you need to supply Block-number to it. or web3 will just handle it by itself?
    – Stefan
    Jun 13, 2019 at 18:09
  • Hm @alberto, It seems that latest will only check whether the contract exist on the latest block, isn't it? If I have a blockchain which is mining block based on transaction which don't contain that contract. Will it still be able to find it using latest keyword because I think it wont be included in latest.
    – Stefan
    Jun 13, 2019 at 18:33
  • latest determines the height of the block. If you prefer to use eth_getCode with a block number, you need to provide a block number after the contract creation. I can't prove that because my node is not running in syncmode full.
    – alberto
    Jun 13, 2019 at 18:56
  • I see, so the JSON RPC will find relevant block by itself and fetch the contract data from the input address right? @alberto
    – Stefan
    Jun 13, 2019 at 19:03
  • @Stefan Yes, if you provide a contract address, JSON RPC will find it with the block parameter you provided. You can read about the block parameter here: github.com/ethereum/wiki/wiki/…
    – alberto
    Jun 13, 2019 at 19:05

Your Answer

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

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