flow-cli icon indicating copy to clipboard operation
flow-cli copied to clipboard

CLI command to transform contract code to hex with imports replaced

Open sisyphusSmiling opened this issue 1 year ago • 3 comments

Issue To Be Solved

I'm working on contract to aid in delegated contract updates, and this includes storing contract code in onchain resources. The easiest way to pass around contract code is as a hex string and convert it to a byte array in the transaction. However, hexifying the .cdc file contents with non-native utils means I need to explicitly state the import addresses.

It would be really helpful if I could tap into Flow CLI's import syntax replacement to handle this for me.

Suggest A Solution

Add a flow command that takes a contract file location and returns the code as a hex string, something like

flow hexify ./contracts/Foo.cdc --network emulator # returns 70756...0a7d

Where the CLI replaces import addresses in Foo according to the aliases set for the specified network and returns the resulting contract code as hex.

Context

Mentioned above

sisyphusSmiling avatar Aug 08 '23 20:08 sisyphusSmiling

I would suggest having this command a bit differently, since this seems a bit of a special use case at least for now I would avoid adding it to the top level since we are trying to have as few as possible to keep it concise and readable. I think since everything that has to do with using flow.json to resolve the code is in the project domain it would be a nice fit to put it there. Furthermore, the output encoding could be achieved using the already existing output flags --output which was made just for the purpose like these (It's more of a helper flag tbh because in true nature of CLIs you should pipe the output in a command that converts the input to hex but that's another convo 😄). My suggestion is: Have the command as:

flow project build {optional name of contract}

That would build all the contracts or just the one specified. You can then leverage the ouput flag and save flag if you want to save to file. So the whole command would be:

flow project build Foo --output hex

Would that work for you? I'm just changing proposal to keep things consistent with other commands.

devbugging avatar Aug 09 '23 11:08 devbugging

That all makes sense! Agree that it's better to put the feature behind a flag instead of a top-level command.

sisyphusSmiling avatar Aug 09 '23 12:08 sisyphusSmiling

I have this code in overflow.

bjartek avatar Sep 26 '23 20:09 bjartek