I am designing a smart contract challenge/enigma where I expect participants to run a given function of the contract to find the "secret treasure".

I use truffle to compile the contract, then truffle migrate, then run a truffle console, and participants get a prompt in that console.

The problem is that, currently, they can directly view the source code of my contract doing mycontract.source, and hence they read the source code and immediately find the treasure.

I would like to remove the source code. (It's okay if they see other information, but source code really spoils it too much).

Attempt #1. When I compile the source code, compiled artificats go to ./build/contracts/mycontract.json. I have edited this file to remove:

"source": "pragma soldity ^0.4.18;\n\ncontract mycontract{...}"

Unfortunately, that modification is not taken into account, and I continue to see the full source code for my contract :(

Attempt #2. In another attempt, I have tried to modify the mycontract.sol after compilation in case source code was read from there, but no, same, my source code continues to be fully visible.

How can I do this?

  • Blockchain is supposed to be completely public. Taking this into account, source code must be public, or it defeats the purpose of blockchain. Jun 11 '18 at 14:11
  • Encryption? Not necessarily the best way to use a contract though. Jun 11 '18 at 14:22

You can use web3 to deploy contract. It will take ABI and bytecode as an argument. There will be no need to specify source code to deploy it.

Refer web3 documentation: https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#deploy

  • I have no web3.eth.Contract.deploy() ... truffle(development)> web3.eth.contract. web3.eth.contract.propertyIsEnumerable web3.eth.contract.toLocaleString web3.eth.contract.toString web3.eth.contract.valueOf web3.eth.contract.apply web3.eth.contract.arguments web3.eth.contract.bind web3.eth.contract.call web3.eth.contract.caller web3.eth.contract.length web3.eth.contract.name web3.eth.contract.prototype
    – user369353
    Jun 15 '18 at 10:18

The solution I found consists in adding a script to run at the end of migrations (./migrations/9_source.js), where this script edits the .source field of my smart contract:

let mycontract = artifacts.require("./mycontract.sol");

module.exports = function(deployer) {
    mycontract.source = web3.toHex(mycountract.source);

Then, run truffle migrate and truffle console and mycontract.source will have encoded value for source code instead of "readable" source code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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