2020-05-13 14:53:54 +00:00
|
|
|
.. _metadata:
|
|
|
|
|
2017-09-05 13:20:53 +00:00
|
|
|
#################
|
|
|
|
Contract Metadata
|
|
|
|
#################
|
|
|
|
|
|
|
|
.. index:: metadata, contract verification
|
|
|
|
|
2018-08-22 10:53:39 +00:00
|
|
|
The Solidity compiler automatically generates a JSON file, the contract
|
2019-12-12 15:28:05 +00:00
|
|
|
metadata, that contains information about the compiled contract. You can use
|
2018-08-22 10:53:39 +00:00
|
|
|
this file to query the compiler version, the sources used, the ABI and NatSpec
|
|
|
|
documentation to more safely interact with the contract and verify its source
|
|
|
|
code.
|
2017-09-05 13:20:53 +00:00
|
|
|
|
2019-09-06 17:11:07 +00:00
|
|
|
The compiler appends by default the IPFS hash of the metadata file to the end
|
|
|
|
of the bytecode (for details, see below) of each contract, so that you can
|
|
|
|
retrieve the file in an authenticated way without having to resort to a
|
|
|
|
centralized data provider. The other available options are the Swarm hash and
|
|
|
|
not appending the metadata hash to the bytecode. These can be configured via
|
|
|
|
the :ref:`Standard JSON Interface<compiler-api>`.
|
|
|
|
|
|
|
|
You have to publish the metadata file to IPFS, Swarm, or another service so
|
|
|
|
that others can access it. You create the file by using the ``solc --metadata``
|
2018-08-22 10:53:39 +00:00
|
|
|
command that generates a file called ``ContractName_meta.json``. It contains
|
2019-09-06 17:11:07 +00:00
|
|
|
IPFS and Swarm references to the source code, so you have to upload all source
|
|
|
|
files and the metadata file.
|
2017-09-05 13:20:53 +00:00
|
|
|
|
|
|
|
The metadata file has the following format. The example below is presented in a
|
|
|
|
human-readable way. Properly formatted metadata should use quotes correctly,
|
|
|
|
reduce whitespace to a minimum and sort the keys of all objects to arrive at a
|
2018-08-22 10:53:39 +00:00
|
|
|
unique formatting. Comments are not permitted and used here only for
|
|
|
|
explanatory purposes.
|
2017-09-05 13:20:53 +00:00
|
|
|
|
2021-07-14 16:53:15 +00:00
|
|
|
.. code-block:: javascript
|
2017-09-05 13:20:53 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Required: The version of the metadata format
|
2021-07-14 16:53:15 +00:00
|
|
|
"version": "1",
|
2017-09-05 13:20:53 +00:00
|
|
|
// Required: Source code language, basically selects a "sub-version"
|
|
|
|
// of the specification
|
2021-07-14 16:53:15 +00:00
|
|
|
"language": "Solidity",
|
2017-09-05 13:20:53 +00:00
|
|
|
// Required: Details about the compiler, contents are specific
|
|
|
|
// to the language.
|
2021-07-14 16:53:15 +00:00
|
|
|
"compiler": {
|
2017-09-05 13:20:53 +00:00
|
|
|
// Required for Solidity: Version of the compiler
|
2021-07-14 16:53:15 +00:00
|
|
|
"version": "0.4.6+commit.2dabbdf0.Emscripten.clang",
|
2017-09-05 13:20:53 +00:00
|
|
|
// Optional: Hash of the compiler binary which produced this output
|
2021-07-14 16:53:15 +00:00
|
|
|
"keccak256": "0x123..."
|
2017-09-05 13:20:53 +00:00
|
|
|
},
|
|
|
|
// Required: Compilation source files/source units, keys are file names
|
2021-07-14 16:53:15 +00:00
|
|
|
"sources":
|
2017-09-05 13:20:53 +00:00
|
|
|
{
|
|
|
|
"myFile.sol": {
|
|
|
|
// Required: keccak256 hash of the source file
|
|
|
|
"keccak256": "0x123...",
|
|
|
|
// Required (unless "content" is used, see below): Sorted URL(s)
|
|
|
|
// to the source file, protocol is more or less arbitrary, but a
|
|
|
|
// Swarm URL is recommended
|
2020-05-13 14:53:54 +00:00
|
|
|
"urls": [ "bzzr://56ab..." ],
|
|
|
|
// Optional: SPDX license identifier as given in the source file
|
|
|
|
"license": "MIT"
|
2017-09-05 13:20:53 +00:00
|
|
|
},
|
2020-01-08 08:17:59 +00:00
|
|
|
"destructible": {
|
2017-09-05 13:20:53 +00:00
|
|
|
// Required: keccak256 hash of the source file
|
|
|
|
"keccak256": "0x234...",
|
|
|
|
// Required (unless "url" is used): literal contents of the source file
|
2020-01-08 08:17:59 +00:00
|
|
|
"content": "contract destructible is owned { function destroy() { if (msg.sender == owner) selfdestruct(owner); } }"
|
2017-09-05 13:20:53 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
// Required: Compiler settings
|
2021-07-14 16:53:15 +00:00
|
|
|
"settings":
|
2017-09-05 13:20:53 +00:00
|
|
|
{
|
|
|
|
// Required for Solidity: Sorted list of remappings
|
2021-07-14 16:53:15 +00:00
|
|
|
"remappings": [ ":g=/dir" ],
|
2017-07-17 11:12:00 +00:00
|
|
|
// Optional: Optimizer settings. The fields "enabled" and "runs" are deprecated
|
|
|
|
// and are only given for backwards-compatibility.
|
2021-07-14 16:53:15 +00:00
|
|
|
"optimizer": {
|
|
|
|
"enabled": true,
|
|
|
|
"runs": 500,
|
|
|
|
"details": {
|
2017-07-17 11:12:00 +00:00
|
|
|
// peephole defaults to "true"
|
2021-07-14 16:53:15 +00:00
|
|
|
"peephole": true,
|
2021-01-14 12:02:14 +00:00
|
|
|
// inliner defaults to "true"
|
2021-07-14 16:53:15 +00:00
|
|
|
"inliner": true,
|
2017-07-17 11:12:00 +00:00
|
|
|
// jumpdestRemover defaults to "true"
|
2021-07-14 16:53:15 +00:00
|
|
|
"jumpdestRemover": true,
|
|
|
|
"orderLiterals": false,
|
|
|
|
"deduplicate": false,
|
|
|
|
"cse": false,
|
|
|
|
"constantOptimizer": false,
|
|
|
|
"yul": true,
|
2020-05-04 13:45:44 +00:00
|
|
|
// Optional: Only present if "yul" is "true"
|
2021-07-14 16:53:15 +00:00
|
|
|
"yulDetails": {
|
|
|
|
"stackAllocation": false,
|
|
|
|
"optimizerSteps": "dhfoDgvulfnTUtnIf..."
|
2020-05-04 13:45:44 +00:00
|
|
|
}
|
2017-07-17 11:12:00 +00:00
|
|
|
}
|
2017-09-05 13:20:53 +00:00
|
|
|
},
|
2021-07-14 16:53:15 +00:00
|
|
|
"metadata": {
|
2019-04-23 19:53:24 +00:00
|
|
|
// Reflects the setting used in the input json, defaults to false
|
2021-07-14 16:53:15 +00:00
|
|
|
"useLiteralContent": true,
|
2019-09-06 17:11:07 +00:00
|
|
|
// Reflects the setting used in the input json, defaults to "ipfs"
|
2021-07-14 16:53:15 +00:00
|
|
|
"bytecodeHash": "ipfs"
|
|
|
|
},
|
2017-09-05 13:20:53 +00:00
|
|
|
// Required for Solidity: File and name of the contract or library this
|
|
|
|
// metadata is created for.
|
2021-07-14 16:53:15 +00:00
|
|
|
"compilationTarget": {
|
2017-09-05 13:20:53 +00:00
|
|
|
"myFile.sol": "MyContract"
|
|
|
|
},
|
|
|
|
// Required for Solidity: Addresses for libraries used
|
2021-07-14 16:53:15 +00:00
|
|
|
"libraries": {
|
2017-09-05 13:20:53 +00:00
|
|
|
"MyLib": "0x123123..."
|
2019-04-23 19:53:24 +00:00
|
|
|
}
|
2017-09-05 13:20:53 +00:00
|
|
|
},
|
|
|
|
// Required: Generated information about the contract.
|
2021-07-14 16:53:15 +00:00
|
|
|
"output":
|
2017-09-05 13:20:53 +00:00
|
|
|
{
|
|
|
|
// Required: ABI definition of the contract
|
2021-07-14 16:53:15 +00:00
|
|
|
"abi": [/* ... */],
|
2017-09-05 13:20:53 +00:00
|
|
|
// Required: NatSpec user documentation of the contract
|
2021-07-14 16:53:15 +00:00
|
|
|
"userdoc": [/* ... */],
|
2017-09-05 13:20:53 +00:00
|
|
|
// Required: NatSpec developer documentation of the contract
|
2021-07-14 16:53:15 +00:00
|
|
|
"devdoc": [/* ... */]
|
2017-09-05 13:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 16:32:16 +00:00
|
|
|
.. warning::
|
2019-09-06 17:11:07 +00:00
|
|
|
Since the bytecode of the resulting contract contains the metadata hash by default, any
|
|
|
|
change to the metadata might result in a change of the bytecode. This includes
|
2018-09-26 16:32:16 +00:00
|
|
|
changes to a filename or path, and since the metadata includes a hash of all the
|
|
|
|
sources used, a single whitespace change results in different metadata, and
|
|
|
|
different bytecode.
|
|
|
|
|
2017-09-05 13:20:53 +00:00
|
|
|
.. note::
|
2019-12-12 15:28:05 +00:00
|
|
|
The ABI definition above has no fixed order. It can change with compiler versions.
|
|
|
|
Starting from Solidity version 0.5.12, though, the array maintains a certain
|
|
|
|
order.
|
2017-09-05 13:20:53 +00:00
|
|
|
|
2019-12-16 21:25:51 +00:00
|
|
|
.. _encoding-of-the-metadata-hash-in-the-bytecode:
|
|
|
|
|
2017-09-05 13:20:53 +00:00
|
|
|
Encoding of the Metadata Hash in the Bytecode
|
|
|
|
=============================================
|
|
|
|
|
|
|
|
Because we might support other ways to retrieve the metadata file in the future,
|
2019-09-06 17:11:07 +00:00
|
|
|
the mapping ``{"ipfs": <IPFS hash>, "solc": <compiler version>}`` is stored
|
2019-04-17 08:40:26 +00:00
|
|
|
`CBOR <https://tools.ietf.org/html/rfc7049>`_-encoded. Since the mapping might
|
|
|
|
contain more keys (see below) and the beginning of that
|
2017-09-05 13:20:53 +00:00
|
|
|
encoding is not easy to find, its length is added in a two-byte big-endian
|
2019-04-17 08:40:26 +00:00
|
|
|
encoding. The current version of the Solidity compiler usually adds the following
|
2021-07-14 16:52:39 +00:00
|
|
|
to the end of the deployed bytecode
|
|
|
|
|
|
|
|
.. code-block:: text
|
2017-09-05 13:20:53 +00:00
|
|
|
|
2019-05-08 14:12:54 +00:00
|
|
|
0xa2
|
2019-09-06 17:11:07 +00:00
|
|
|
0x64 'i' 'p' 'f' 's' 0x58 0x22 <34 bytes IPFS hash>
|
2019-05-08 14:12:54 +00:00
|
|
|
0x64 's' 'o' 'l' 'c' 0x43 <3 byte version encoding>
|
2020-01-20 14:13:40 +00:00
|
|
|
0x00 0x33
|
2017-09-05 13:20:53 +00:00
|
|
|
|
|
|
|
So in order to retrieve the data, the end of the deployed bytecode can be checked
|
2019-09-06 17:11:07 +00:00
|
|
|
to match that pattern and use the IPFS hash to retrieve the file.
|
2017-09-05 13:20:53 +00:00
|
|
|
|
2019-05-08 14:12:54 +00:00
|
|
|
Whereas release builds of solc use a 3 byte encoding of the version as shown
|
|
|
|
above (one byte each for major, minor and patch version number), prerelease builds
|
|
|
|
will instead use a complete version string including commit hash and build date.
|
|
|
|
|
2019-04-17 08:40:26 +00:00
|
|
|
.. note::
|
|
|
|
The CBOR mapping can also contain other keys, so it is better to fully
|
2019-09-06 17:11:07 +00:00
|
|
|
decode the data instead of relying on it starting with ``0xa264``.
|
2019-04-17 08:40:26 +00:00
|
|
|
For example, if any experimental features that affect code generation
|
|
|
|
are used, the mapping will also contain ``"experimental": true``.
|
|
|
|
|
2018-09-25 16:49:49 +00:00
|
|
|
.. note::
|
2019-09-06 17:11:07 +00:00
|
|
|
The compiler currently uses the IPFS hash of the metadata by default, but
|
|
|
|
it may also use the bzzr1 hash or some other hash in the future, so do
|
|
|
|
not rely on this sequence to start with ``0xa2 0x64 'i' 'p' 'f' 's'``. We
|
|
|
|
might also add additional data to this CBOR structure, so the best option
|
|
|
|
is to use a proper CBOR parser.
|
2018-09-25 16:49:49 +00:00
|
|
|
|
|
|
|
|
2017-09-05 13:20:53 +00:00
|
|
|
Usage for Automatic Interface Generation and NatSpec
|
|
|
|
====================================================
|
|
|
|
|
|
|
|
The metadata is used in the following way: A component that wants to interact
|
2019-09-06 17:11:07 +00:00
|
|
|
with a contract (e.g. Mist or any wallet) retrieves the code of the contract,
|
|
|
|
from that the IPFS/Swarm hash of a file which is then retrieved. That file
|
|
|
|
is JSON-decoded into a structure like above.
|
2017-09-05 13:20:53 +00:00
|
|
|
|
|
|
|
The component can then use the ABI to automatically generate a rudimentary
|
|
|
|
user interface for the contract.
|
|
|
|
|
2018-09-25 16:49:49 +00:00
|
|
|
Furthermore, the wallet can use the NatSpec user documentation to display a confirmation message to the user
|
|
|
|
whenever they interact with the contract, together with requesting
|
|
|
|
authorization for the transaction signature.
|
2017-09-05 13:20:53 +00:00
|
|
|
|
2019-02-27 03:34:33 +00:00
|
|
|
For additional information, read :doc:`Ethereum Natural Language Specification (NatSpec) format <natspec-format>`.
|
2017-12-01 19:00:44 +00:00
|
|
|
|
2017-09-05 13:20:53 +00:00
|
|
|
Usage for Source Code Verification
|
|
|
|
==================================
|
|
|
|
|
2019-09-06 17:11:07 +00:00
|
|
|
In order to verify the compilation, sources can be retrieved from IPFS/Swarm
|
2017-09-05 13:20:53 +00:00
|
|
|
via the link in the metadata file.
|
|
|
|
The compiler of the correct version (which is checked to be part of the "official" compilers)
|
|
|
|
is invoked on that input with the specified settings. The resulting
|
|
|
|
bytecode is compared to the data of the creation transaction or ``CREATE`` opcode data.
|
|
|
|
This automatically verifies the metadata since its hash is part of the bytecode.
|
|
|
|
Excess data corresponds to the constructor input data, which should be decoded
|
|
|
|
according to the interface and presented to the user.
|
2019-04-29 07:14:32 +00:00
|
|
|
|
2020-08-06 12:37:22 +00:00
|
|
|
In the repository `sourcify <https://github.com/ethereum/sourcify>`_
|
2019-07-01 14:03:12 +00:00
|
|
|
(`npm package <https://www.npmjs.com/package/source-verify>`_) you can see
|
2019-04-29 07:14:32 +00:00
|
|
|
example code that shows how to use this feature.
|