# ZoKrates ABI
# Spec
## Resolved type
We define `resolved(t)` for `t` a type as follows:
- if `t` is not a user defined struct, then `t`
- if `t` is a user defined struct, then `resolved(t)` is a struct where for each member `m` in the declaration of `t`, we add a member with the same key and as value `resolved(t.members[m])`
Informally, `resolve` recursively turns user defined structs into their canonical form which only contains basic types and deep nested structs.
Example:
```go
struct Bar {
field a
}
struct Foo {
field a,
Bar b
}
resolved(Bar) == struct { field a }
resolved(Foo) == struct { field a, struct { field a } }
```
## JSON-ABI
The JSON-ABI for a program `p` with arguments `args` and return types `ret` is a JSON object with two keys: `inputs` and `outputs`.
### Types
For a type `t` we define `json_type(t)` as
- if `resolved(t)` is a struct `s` with members `members`, then `json_type(t).type` is `"struct"` and `json_type(t).components` is a JSON *array* of JSON objects `json_member(m)` for `m` in `members` where `json_member(m)` is defined as `json_type(m.type)` merged with `{ "name" : m.name }`
- if `resolved(t)` is a static array of `n` elements of type `ty`, then `json_type(t).type` is `"array"` and `json_type(t).components` is a JSON object with keys `size` equal to `n` merged with `json_type(ty)`
- if `resolved(t)` is a basic type, then `json_type(t).type` is `string(t)`
### Inputs
`inputs` maps to a JSON array containing one element for each element in `args`.
For an argument `a`, we define `json_argument(a)`, a JSON object in the following way:
- `json_argument(a).name` is the name of the argument
- `json_argument(a).public` is true iff a is a public argument
Merged with `json_type(a.type)`
### Outputs
`outputs` maps to a JSON array containing one element for each element in `ret`.
For any type `t`, we define `json_return_type(t)` as `json_type(t)`
## JSON input format
The JSON input domain for a program `p` is the subset of all JSON values which are accepted by the JSON-ABI encoder of `p`.
Any element `j` of the input domain of `p` with arguments `args` must be a JSON array of `value(a.ty)` for each argument `a` in `args` where `value(t)` is defined as follows:
- if `t` is `ty[n]`, then a JSON array of `n` values `value(ty)`
- TODO make it clear what to do with `ty[p][n]`
- if `t` is `struct(members)`, then a JSON object where for each member `m` we have key `m.name` and value `value(m.ty)`
- if `t` is a basic type, then the JSON representation of `t` (ommited here)
# Examples
```go
struct Bar {
field a
}
struct Foo {
field a
Bar b
}
def main(private Foo foo, bool[2] bar, field num) -> (field):
return 42
```
JSON ABI for the code above:
```json
{
"inputs": [
{
"name": "foo",
"public": false,
"type": "struct",
"components": [
{
"name": "a",
"type": "field"
},
{
"name": "b",
"type": "struct",
"components": [
{
"name": "a",
"type": "field"
}
]
}
]
},
{
"name": "bar",
"public": "true",
"type": "array",
"components": {
"size": 2,
"type": "bool"
}
},
{
"name": "num",
"public": "true",
"type": "field"
}
],
"outputs": [
{
"type": "field"
}
]
}
```