entropyxyz / entropy-core

Protocol and cryptography development.
https://docs.entropy.xyz/
GNU Affero General Public License v3.0
11 stars 2 forks source link

Add json output to test CLI command output #1164

Closed ameba23 closed 1 week ago

ameba23 commented 1 week ago

Closes https://github.com/entropyxyz/entropy-core/issues/1163

This adds optional JSON output to the test CLI.

This is activated with the --json or -j command line argument. Note that it must be given before the subcommand like this: entropy-test-cli --json status and not entropy-test-cli status --json.

Currently not all available information is given, for example the status command gives you the hashes of stored programs and account ids of registered accounts, but not the additional metadata you will see in the non-json output, such as which programs are associated with which account, who stored a program, how many times a program was used, etc. These fields could easily be added so please say if they would be useful.

Generally binary data is represented as hex.

Example output:

$ entropy-test-cli --json status
{
  "accounts": [
    "029d00ff44b70613228963e45a05fe743ea92beb8ece19d48fb6c60d66386fe03b",
    "024609b84f24cdf494c356e48390927df1fbd16cf893d21c9774561b9805a006ae",
    "03cecec00e1c008a1c16c8cbb175b8de3eb10ff03fd79ed82a04e4caa3d21c159d",
    "02c1fd5c996e716f4ad6a3651861ce0efc4db3da88bbff7b84328ecb772f4eddaf",
    "0229e6718ed1d9a1568b4d087f66a00612133b4652b9c03ae9ba62c1ead33dd5d4",
    "03dd0713f53f162e4ce01b064db664682a270ac3a17bccc47ea670ac2a1037f306",
    "037363e1634cd106a78027fa4b160e6430c52ddef27d60828f2483c8e203f56e86",
    "035c0e76a179f5340303e9b219a983a494ad80d2b16d422bd34173ee0281f2a08c",
    "032fc02cb38a2dc5f5bee6fb68600aa9b05f4e18b1b196c1f31d1fde21c06d1e3d",
    "036f424bacf0252ddc470dd797cd511ca5bd5c37845f7a0bef60c8afaedf176fd4",
    "03cc90e7fbcace6b2945ba6718a7e04dc296f76a3788eab223f9e666360829f385",
    "03d53398bf3efa12edebc212959b3b2442612b74c0f0c45c209ec9a664731dc66f",
    "038ad84e7bfdea764737319123f60019365616c48b4c1ef5230fd05e22390c702c",
    "03aee03ad9862e9f31d06f7d1b4b388ad1c66152ad17f919fc16fcc75929b08db3"
  ],
  "programs": [
    "3289435006003c3c83da5582d779d810c343f177fb1fecddbaca5d326ef9d9b3",
    "3a1d45fecdee990925286ccce71f78693ff2bb27eae62adf8cfb7d3d61e142aa",
    "0000000000000000000000000000000000000000000000000000000000000000"
  ]
}

$ entropy-test-cli --json register ../testing-utils/template_barebones.wasm -m //Peg
"035c0e76a179f5340303e9b219a983a494ad80d2b16d422bd34173ee0281f2a08c"

$ entropy-test-cli --json sign "035c0e76a179f5340303e9b219a983a494ad80d2b16d422bd34173ee0281f2a08c" "this is a message"
{
  "signature": "46C2821E3FD730D0B0030CBD89C7AE9265A31B7863C207E6AC971B349D52D5C65FEF136F6D80E375769D47C07CA6F6C8EC0344C7B1B9591DCFF0EE14875D495B",
  "recovery_id": 1
}

For commands which finish successfully with no output, an empty JSON object is displayed: {}. Im not sure if it would be more appropriate to just give no ouput.

Errors

Errors are currently not displayed as JSON. If a command fails, the exit status will be 1, and the original error message will be displayed to standard error. If needed we could wrap this in { "error": "error message" }, but actually serializing the errors to JSON is probably going to be tricky as i imagine they will not all implement Serialize.

ameba23 commented 1 week ago

Since this is a breaking change it is paired with a PR in programs to update the CLI for deploying programs, which has entropy-test-cli as a dependency. https://github.com/entropyxyz/programs/pull/96

cooldracula commented 1 week ago

This looks good to me, thank you!