Json Api
The JsonApi
attestation type enables data collection from an arbitrary Web2 source.
You can learn more about it in the official specification repo.
We will now demonstrate how the FDC protocol can be used to collect the data of a given Star Wars API request.
The request we will be making is https://swapi.dev/api/peaople/3/
.
The same procedure works for all public APIs.
In this guide, we will follow the steps outlined in the FDC overview.
We will define a scripts/fdcExample/JsonApi.ts
file that will encapsulate the whole process.
import { run, web3 } from "hardhat";
import { TransferEventListenerInstance } from "../../typechain-types";
import {
prepareAttestationRequestBase,
submitAttestationRequest,
retrieveDataAndProofBase,
} from "./Base";
const JsonApi = artifacts.require("TransferEventListener");
const { VERIFIER_URL_TESTNET, VERIFIER_API_KEY_TESTNET, COSTON2_DA_LAYER_URL } =
process.env;
...
async function main() {
const data = await prepareAttestationRequest(
apiUrl,
postprocessJq,
abiSignature
);
console.log("Data:", data, "\n");
const abiEncodedRequest = data.abiEncodedRequest;
const roundId = await submitAttestationRequest(abiEncodedRequest);
const proof = await retrieveDataAndProof(abiEncodedRequest, roundId);
const characterList: StarWarsCharacterListInstance =
await deployAndVerifyContract();
await interactWithContract(characterList, proof);
}
main().then((data) => {
process.exit(0);
});
The function names mostly mirror the steps described in the FDC guide.
Prepare request
In this guide we will demonstrate how to prepare an attestation request through a verifier server. At the end of the section we will provide a breakdown of the abi encoded request; thus we will demonstrate how it can be constructed manually.
To use the verifier server, we send a request to its prepareRequest
endpoint.
A JSON request to the verifier follows the same structure for all attestation types, with field values varying between types.
Required Fields
attestationType
is the UTF8 hex string encoding of the attestation type name, zero-padded to 32 bytes.sourceId
is the UTF8 hex string encoding of the data source identifier name, zero-padded to 32 bytes.requestBody
is different for each attestation type.
In the case of JsonApi
, requestBody
is a JSON containing the fields:
url
: url of the data source; asstring
postprocessJq
: JQ filter to postprocess the json data received from the URL; asstring
abi_signature
: ABI signature of the Solidity struct that will be used to decode the data; asstring
Reference Documentation
Example Values
url
: the above addresshttps://swapi.dev/api/people/3/
postprocessJq
:{name: .name, height: .height, mass: .mass, numberOfFilms: .films | length, uid: (.url | split(\\"/\\") | .[-2] | tonumber)}
abi_signature
:
{\\"components\\": [
{\\"internalType\\": \\"string\\", \\"name\\": \\"name\\", \\"type\\": \\"string\\"},
{\\"internalType\\": \\"uint256\\", \\"name\\": \\"height\\", \\"type\\": \\"uint256\\"},
{\\"internalType\\": \\"uint256\\", \\"name\\": \\"mass\\", \\"type\\": \\"uint256\\"},
{\\"internalType\\": \\"uint256\\", \\"name\\": \\"numberOfFilms\\", \\"type\\": \\"uint256\\"},
{\\"internalType\\": \\"uint256\\", \\"name\\": \\"uid\\", \\"type\\": \\"uint256\\"}
],
\\"name\\": \\"task\\",\\"type\\": \\"tuple\\"}
// Request data
const apiUrl = "https://swapi.dev/api/people/3/";
const postprocessJq = `{name: .name, height: .height, mass: .mass, numberOfFilms: .films | length, uid: (.url | split(\"/\") | .[-2] | tonumber)}`;
const abiSignature = `{\"components\": [{\"internalType\": \"string\", \"name\": \"name\", \"type\": \"string\"},{\"internalType\": \"uint256\", \"name\": \"height\", \"type\": \"uint256\"},{\"internalType\": \"uint256\", \"name\": \"mass\", \"type\": \"uint256\"},{\"internalType\": \"uint256\", \"name\": \"numberOfFilms\", \"type\": \"uint256\"},{\"internalType\": \"uint256\", \"name\": \"uid\", \"type\": \"uint256\"}],\"name\": \"task\",\"type\": \"tuple\"}`;
// Configuration constants
const attestationTypeBase = "IJsonApi";
const sourceIdBase = "WEB2";
const verifierUrlBase = JQ_VERIFIER_URL_TESTNET;
Encoding Functions
To encode values into UTF8 hex:
toUtf8HexString
: Converts a string to UTF8 hex.toHex
: Zero-right-pads the string to 32 bytes.
These functions are included in the Base library within the example repository, but they can also be defined locally in your contract or script.
function toHex(data: string) {
var result = "";
for (var i = 0; i < data.length; i++) {
result += data.charCodeAt(i).toString(16);
}
return result.padEnd(64, "0");
}
function toUtf8HexString(data: string) {
return "0x" + toHex(data);
}
Because of the console.log
commands it will produce JSON strings that represent valid requests; we can then pass this to the interactive verifier to check the response.
The process of posting a request to a verifier server is identical for all attestation types.
It differs only in values used.
For that reason we define a base function that the prepareAttestationRequest
function will call.
The prepareAttestationRequestBase
function formulates a request for the verifier server, and posts it to the given URL.
async function prepareAttestationRequestBase(
url: string,
apiKey: string,
attestationTypeBase: string,
sourceIdBase: string,
requestBody: any,
) {
console.log("Url:", url, "\n");
const attestationType = toUtf8HexString(attestationTypeBase);
const sourceId = toUtf8HexString(sourceIdBase);
const request = {
attestationType: attestationType,
sourceId: sourceId,
requestBody: requestBody,
};
console.log("Prepared request:\n", request, "\n");
const response = await fetch(url, {
method: "POST",
headers: {
"X-API-KEY": apiKey,
"Content-Type": "application/json",
},
body: JSON.stringify(request),
});
if (response.status != 200) {
throw new Error(
`Response status is not OK, status ${response.status} ${response.statusText}\n`,
);
}
console.log("Response status is OK\n");
return await response.json();
}
In the example repository, it is once again included within the Base library file.
We construct the URL by appending to the verifier address https://fdc-verifiers-testnet.flare.network/
the path verifier/btc/JsonApi/prepareRequest
.
If we were using another source, we would replace the string eth
with sgb
or flr
accordingly (we would also have to replace testETH
with testSGB
or testFLR
).
Thus, the function that prepares the verifier request looks like:
async function prepareAttestationRequest(
apiUrl: string,
postprocessJq: string,
abiSignature: string,
) {
const requestBody = {
url: apiUrl,
postprocessJq: postprocessJq,
abi_signature: abiSignature,
};
const url = `${verifierUrlBase}JsonApi/prepareRequest`;
const apiKey = JQ_VERIFIER_API_KEY_TESTNET!;
return await prepareAttestationRequestBase(
url,
apiKey,
attestationTypeBase,
sourceIdBase,
requestBody,
);
}
Details
Understanding the abiEncodedRequest
.
If everything went right, the abiEncodedRequest
should look something like this (minus the line breaks - we split it after the 0x
symbol and then after every 64 characters (32 bytes), for the sake of clarity).0x
494a736f6e417069000000000000000000000000000000000000000000000000
5745423200000000000000000000000000000000000000000000000000000000
0b62b2fe7066a5b56cd4cc859f4c802a02e2a0f84b5ad12893ef5a90651e588c
0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000060
00000000000000000000000000000000000000000000000000000000000000a0
0000000000000000000000000000000000000000000000000000000000000140
000000000000000000000000000000000000000000000000000000000000001f
68747470733a2f2f73776170692e6465762f6170692f70656f706c652f332f00
0000000000000000000000000000000000000000000000000000000000000078
7b6e616d653a202e6e616d652c206865696768743a202e6865696768742c206d
6173733a202e6d6173732c206e756d6265724f6646696c6d733a202e66696c6d
73207c206c656e6774682c207569643a20282e75726c207c2073706c69742822
2f2229207c202e5b2d325d207c20746f6e756d626572297d0000000000000000
0000000000000000000000000000000000000000000000000000000000000173
7b22636f6d706f6e656e7473223a205b7b22696e7465726e616c54797065223a
2022737472696e67222c20226e616d65223a20226e616d65222c202274797065
223a2022737472696e67227d2c7b22696e7465726e616c54797065223a202275
696e74323536222c20226e616d65223a2022686569676874222c202274797065
223a202275696e74323536227d2c7b22696e7465726e616c54797065223a2022
75696e74323536222c20226e616d65223a20226d617373222c20227479706522
3a202275696e74323536227d2c7b22696e7465726e616c54797065223a202275
696e74323536222c20226e616d65223a20226e756d6265724f6646696c6d7322
2c202274797065223a202275696e74323536227d2c7b22696e7465726e616c54
797065223a202275696e74323536222c20226e616d65223a2022756964222c20
2274797065223a202275696e74323536227d5d2c226e616d65223a2022746173
6b222c2274797065223a20227475706c65227d00000000000000000000000000
Let's break it down line by line:
- First line:
toUtf8HexString("JsonApi")
- Second line:
toUtf8HexString("testETH")
- Third line: message integrity code (MIC), a hash of the whole response salted with a string
"Flare"
, ensures the integrity of the attestation - Remaining lines: ABI encoded
JsonApi.RequestBody
Solidity struct
What this demonstrates is that, with some effort, the abiEncodedRequest
can be constructed manually.
Submit request to FDC
This step transitions from off-chain request preparation to on-chain interaction with the FDC protocol.
We will submit the validated request to the blockchain using deployed official Flare smart contracts.
To streamline the process of accessing these, the Flare smart contracts periphery package is shipped with the ContractRegistry
library.
The ContractRegistry
library allows us to
We define a Helpers
contract that will give us access to the following contracts:
FdcHub
: for posting the request toFdcRequestFeeConfigurations
: calculates the fee of the requestFlareSystemsManager
: for calculating the round IDRelay
: confirms the round has finalized
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;
import {console} from "hardhat/console.sol";
import {ContractRegistry} from "@flarenetwork/flare-periphery-contracts/coston2/ContractRegistry.sol";
import {IFdcHub} from "@flarenetwork/flare-periphery-contracts/coston2/IFdcHub.sol";
import {IFdcRequestFeeConfigurations} from
"@flarenetwork/flare-periphery-contracts/coston2/IFdcRequestFeeConfigurations.sol";
import {IFlareSystemsManager} from "@flarenetwork/flare-periphery-contracts/coston2/IFlareSystemsManager.sol";
import {IRelay} from "@flarenetwork/flare-periphery-contracts/coston2/IRelay.sol";
contract Helpers {
function getFdcHub() public view returns (IFdcHub) {
return ContractRegistry.getFdcHub();
}
function getFdcRequestFeeConfigurations() public view returns (IFdcRequestFeeConfigurations) {
return ContractRegistry.getFdcRequestFeeConfigurations();
}
function getFlareSystemsManager() public view returns (IFlareSystemsManager) {
return ContractRegistry.getFlareSystemsManager();
}
function getRelay() public view returns (IRelay) {
return ContractRegistry.getRelay();
}
}
We expose the Helpers
contract through the following function.
async function getHelpers() {
const helpers: HelpersInstance = await Helpers.new();
return helpers;
}
To submit the attestation request, we first access the deployed FdcHub
contract.
We determine the fee for our attestation type, and then request the attestation of the FDC, paying the required fee.
Lastly, we calculate the voting round Id from the transaction that carried the attestation request; we will need it to query the data and proof.
async function submitAttestationRequest(abiEncodedRequest: string) {
const fdcHub = await getFdcHub();
const requestFee = await getFdcRequestFee(abiEncodedRequest);
const transaction = await fdcHub.requestAttestation(abiEncodedRequest, {
value: requestFee,
});
console.log("Submitted request:", transaction.tx, "\n");
const roundId = await calculateRoundId(transaction);
console.log(
`Check round progress at: https://${hre.network.name}-systems-explorer.flare.rocks/voting-epoch/${roundId}?tab=fdc\n`,
);
return roundId;
}
async function getFdcHub() {
const helpers: HelpersInstance = await getHelpers();
const fdcHubAddress: string = await helpers.getFdcHub();
return await FdcHub.at(fdcHubAddress);
}
The request fee is obtained from the fdcRequestFeeConfigurations
contract.
We once again connect to the fdcRequestFeeConfigurations
contract through the ContractRegistry
library.
async function getFdcRequestFee(abiEncodedRequest: string) {
const helpers: HelpersInstance = await getHelpers();
const fdcRequestFeeConfigurationsAddress: string =
await helpers.getFdcRequestFeeConfigurations();
const fdcRequestFeeConfigurations: IFdcRequestFeeConfigurationsInstance =
await FdcRequestFeeConfigurations.at(fdcRequestFeeConfigurationsAddress);
return await fdcRequestFeeConfigurations.getRequestFee(abiEncodedRequest);
}
The round ID is calculate from the timestamp of the block, containing the transaction requesting attestation.
We first subtract from the block timestamp the timestamp of the first voting epoch.
Then, we divide the number by the duration of the voting epoch (90 seconds).
Instead of hard-coding them, we retrieve these values from another official Flare contract, the flareSystemsManager
.
async function calculateRoundId(transaction: any) {
const blockNumber = transaction.receipt.blockNumber;
const block = await ethers.provider.getBlock(blockNumber);
const blockTimestamp = BigInt(block!.timestamp);
const flareSystemsManager: IFlareSystemsManagerInstance =
await getFlareSystemsManager();
const firsVotingRoundStartTs = BigInt(
await flareSystemsManager.firstVotingRoundStartTs(),
);
const votingEpochDurationSeconds = BigInt(
await flareSystemsManager.votingEpochDurationSeconds(),
);
console.log("Block timestamp:", blockTimestamp, "\n");
console.log("First voting round start ts:", firsVotingRoundStartTs, "\n");
console.log(
"Voting epoch duration seconds:",
votingEpochDurationSeconds,
"\n",
);
const roundId = Number(
(blockTimestamp - firsVotingRoundStartTs) / votingEpochDurationSeconds,
);
console.log("Calculated round id:", roundId, "\n");
console.log(
"Received round id:",
Number(await flareSystemsManager.getCurrentVotingEpochId()),
"\n",
);
return roundId;
}
We obtain the flareSystemsManager
contract through the ContractRegistry
library and the previously defined Helpers
contract as well.
async function getFlareSystemsManager() {
const helpers: HelpersInstance = await getHelpers();
const flareSystemsManagerAddress: string =
await helpers.getFlareSystemsManager();
return await FlareSystemsManager.at(flareSystemsManagerAddress);
}
Retrieve data and proof
To retrieve the data and proof, we must first wait for the voting round in which the attestation request was submitted to finalize; this takes no more than 180 seconds, but is on average much less. After the round has been finalized, we post a request to a DA Layer provider.
We can check if the request was submitted successfully on the AttestationRequests page on the Flare Systems Explorer website. To check if the round has been finalized, go to Finalizations page.
If you want to learn more about how the FDC protocol works, check here.
Because the process includes waiting for the voting round to finalize, we prepare a sleep
function.
The function pauses the execution of the script for a given number of milliseconds.
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
The only difference between the retrieveDataAndProof
functions of all six attestation types is the URL of the DA Layer server.
For that reason, we will define as separate retrieveDataAndProofBase
function that will handle most of the logic.
The function waits for the round to finalize - rechecking every 10 seconds if necessary.
Then, it prepares a proof request, and posts it to the DA Layer server.
Because it might take a few seconds for the server to generate the proof, the function ensures that the response actually contains a sufficient response, and retries otherwise.
async function retrieveDataAndProofBase(
url: string,
abiEncodedRequest: string,
roundId: number,
) {
console.log("Waiting for the round to finalize...");
// We check every 10 seconds if the round is finalized
const relay: IRelayInstance = await getRelay();
while (!(await relay.isFinalized(200, roundId))) {
await sleep(10000);
}
console.log("Round finalized!\n");
const request = {
votingRoundId: roundId,
requestBytes: abiEncodedRequest,
};
console.log("Prepared request:\n", request, "\n");
await sleep(10000);
var proof = await postRequestToDALayer(url, request, true);
console.log("Waiting for the DA Layer to generate the proof...");
while (proof.response_hex == undefined) {
await sleep(5000);
proof = await postRequestToDALayer(url, request, false);
}
console.log("Proof generated!\n");
console.log("Proof:", proof, "\n");
return proof;
}
We access the Flare's official Relay
contract with a helper function.
async function getRelay() {
const helpers: HelpersInstance = await getHelpers();
const relayAddress: string = await helpers.getRelay();
return await IRelay.at(relayAddress);
}
The following function posts a proof request to the DA Layer.
async function postRequestToDALayer(
url: string,
request: any,
watchStatus: boolean = false,
) {
const response = await fetch(url, {
method: "POST",
headers: {
// "X-API-KEY": "",
"Content-Type": "application/json",
},
body: JSON.stringify(request),
});
if (watchStatus && response.status != 200) {
throw new Error(
`Response status is not OK, status ${response.status} ${response.statusText}\n`,
);
} else if (watchStatus) {
console.log("Response status is OK\n");
}
return await response.json();
}
The main prepare the URL of the DA Layer's proof-by-request-raw
endpoint.
We contact this specific endpoint, because it return the abi encoded IJsonApi.Response
struct, and is thus unambiguous.
async function retrieveDataAndProof(
abiEncodedRequest: string,
roundId: number,
) {
const url = `${COSTON2_DA_LAYER_URL}api/v1/fdc/proof-by-request-round-raw`;
console.log("Url:", url, "\n");
return await retrieveDataAndProofBase(url, abiEncodedRequest, roundId);
}
The response the DA Layer server returns has the following fields:
- The field
attestationType
holds the UTF8 encoded hex string of the attestation type name, padded to 32 bytes. Thus, it should match the value of theattestationType
parameter in the Prepare the request step. In our case, that value is0x4164647265737356616c69646974790000000000000000000000000000000000
. - The array
proofs
holds the Merkle proofs of our attestation request. - Lastly,
responseHex
is the ABI encoding of the chosen attestation type response struct. In this case, it is theIJsonApi.Response
struct.
We can ascertain the form of the proof request, as well as examine the response in advance, trough the interactive documentation of the DA Layer server.
An example complete proof response and decoded IJsonApi.Response
.
An example DA Layer response for a request using the data provided in this example is:
{
response_hex: '0x
0000000000000000000000000000000000000000000000000000000000000020
494a736f6e417069000000000000000000000000000000000000000000000000
5745423200000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000e6c41
0000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000c0
00000000000000000000000000000000000000000000000000000000000003a0
0000000000000000000000000000000000000000000000000000000000000060
00000000000000000000000000000000000000000000000000000000000000a0
0000000000000000000000000000000000000000000000000000000000000140
000000000000000000000000000000000000000000000000000000000000001f
68747470733a2f2f73776170692e6465762f6170692f70656f706c652f332f00
0000000000000000000000000000000000000000000000000000000000000078
7b6e616d653a202e6e616d652c206865696768743a202e6865696768742c206d
6173733a202e6d6173732c206e756d6265724f6646696c6d733a202e66696c6d
73207c206c656e6774682c207569643a20282e75726c207c2073706c69742822
2f2229207c202e5b2d325d207c20746f6e756d626572297d0000000000000000
0000000000000000000000000000000000000000000000000000000000000173
7b22636f6d706f6e656e7473223a205b7b22696e7465726e616c54797065223a
2022737472696e67222c20226e616d65223a20226e616d65222c202274797065
223a2022737472696e67227d2c7b22696e7465726e616c54797065223a202275
696e74323536222c20226e616d65223a2022686569676874222c202274797065
223a202275696e74323536227d2c7b22696e7465726e616c54797065223a2022
75696e74323536222c20226e616d65223a20226d617373222c20227479706522
3a202275696e74323536227d2c7b22696e7465726e616c54797065223a202275
696e74323536222c20226e616d65223a20226e756d6265724f6646696c6d7322
2c202274797065223a202275696e74323536227d2c7b22696e7465726e616c54
797065223a202275696e74323536222c20226e616d65223a2022756964222c20
2274797065223a202275696e74323536227d5d2c226e616d65223a2022746173
6b222c2274797065223a20227475706c65227d00000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000100
0000000000000000000000000000000000000000000000000000000000000020
00000000000000000000000000000000000000000000000000000000000000a0
0000000000000000000000000000000000000000000000000000000000000060
0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000006
0000000000000000000000000000000000000000000000000000000000000003
0000000000000000000000000000000000000000000000000000000000000005
52322d4432000000000000000000000000000000000000000000000000000000',
attestation_type: '0x494a736f6e417069000000000000000000000000000000000000000000000000',
proof: [
'0xab2384609341b65b4686cf9accd981f8c7f58e47aa41bc49ec60f655c8d99840',
'0xc30b0590a4ea59adc7aa5486a9ece81bdcc756ac4d22e09dbe171bf8b50e53b5',
'0x4a5fc6d814dd3c52e199396de4d48f7c18274bbf72f9065c1e68306f4fd22c34'
]
}
The proof
field is dependent on the round in which the attestation request was submitted;
it contains proofs for all of the requests submitted in that round.
In the case of a single attestation request it is an empty list []
(the proof is the merkle root itself).
The decoded IJsonApi.Response
struct is:
[
attestationType: '0x494a736f6e417069000000000000000000000000000000000000000000000000',
sourceId: '0x5745423200000000000000000000000000000000000000000000000000000000',
votingRound: '945217',
lowestUsedTimestamp: '0',
requestBody: [
'https://swapi.dev/api/people/3/',
'{name: .name, height: .height, mass: .mass, numberOfFilms: .films | length, uid: (.url | split("/") | .[-2] | tonumber)}',
'{"components": [{"internalType": "string", "name": "name", "type": "string"},{"internalType": "uint256", "name": "height", "type": "uint256"},{"internalType": "uint256", "name": "mass", "type": "uint256"},{"internalType": "uint256", "name": "numberOfFilms", "type": "uint256"},{"internalType": "uint256", "name": "uid", "type": "uint256"}],"name": "task","type": "tuple"}',
url: 'https://swapi.dev/api/people/3/',
postprocessJq: '{name: .name, height: .height, mass: .mass, numberOfFilms: .films | length, uid: (.url | split("/") | .[-2] | tonumber)}',
abi_signature: '{"components": [{"internalType": "string", "name": "name", "type": "string"},{"internalType": "uint256", "name": "height", "type": "uint256"},{"internalType": "uint256", "name": "mass", "type": "uint256"},{"internalType": "uint256", "name": "numberOfFilms", "type": "uint256"},{"internalType": "uint256", "name": "uid", "type": "uint256"}],"name": "task","type": "tuple"}'
],
responseBody: [
'0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000552322d4432000000000000000000000000000000000000000000000000000000',
abi_encoded_data: '0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000552322d4432000000000000000000000000000000000000000000000000000000'
]
]
Use the data
We will now define a simple contract, that will demonstrate how the data can be used onchain.
The contract will receive character data from the Star Wars API, and store it in a StarWarsCharacter
struct.
It will do so only if the proof is valid.
struct StarWarsCharacter {
string name;
uint256 numberOfMovies;
uint256 apiUid;
uint256 bmi;
}
We will also need a DataTransportObject
struct, that will allow us to decode the data.
struct DataTransportObject {
string name;
uint256 height;
uint256 mass;
uint256 numberOfMovies;
uint256 apiUid;
}
The code of the contract is as follows.
contract StarWarsCharacterList {
mapping(uint256 => StarWarsCharacter) public characters;
uint256[] public characterIds;
function isJsonApiProofValid(
IJsonApi.Proof calldata _proof
) private view returns (bool) {
// Inline the check for now until we have an official contract deployed
return
ContractRegistry.auxiliaryGetIJsonApiVerification().verifyJsonApi(
_proof
);
}
function addCharacter(IJsonApi.Proof calldata data) public {
require(isJsonApiProofValid(data), "Invalid proof");
DataTransportObject memory dto = abi.decode(
data.data.responseBody.abi_encoded_data,
(DataTransportObject)
);
require(characters[dto.apiUid].apiUid == 0, "Character already exists");
StarWarsCharacter memory character = StarWarsCharacter({
name: dto.name,
numberOfMovies: dto.numberOfMovies,
apiUid: dto.apiUid,
bmi: (dto.mass * 100 * 100) / (dto.height * dto.height)
});
characters[dto.apiUid] = character;
characterIds.push(dto.apiUid);
}
function getAllCharacters()
public
view
returns (StarWarsCharacter[] memory)
{
StarWarsCharacter[] memory result = new StarWarsCharacter[](
characterIds.length
);
for (uint256 i = 0; i < characterIds.length; i++) {
result[i] = characters[characterIds[i]];
}
return result;
}
}
Verify proof
FDC optimizes on-chain storage costs by implementing a hybrid data verification system. Instead of storing complete datasets on-chain, it stores only Merkle proofs, while maintaining the actual data through trusted off-chain providers. This approach significantly reduces gas costs while preserving data integrity.
When requested, data providers supply the original data along with its corresponding Merkle proof. The protocol verifies data authenticity by comparing the provided Merkle proof against the on-chain Merkle root. A successful match confirms the data's integrity and authenticity within the FDC system.
While data verification is optional if you trust your data provider, FDC ensures transparency by making verification possible at any time. This capability is crucial for maintaining system integrity and allowing users to independently verify data when needed, particularly in production environments.
FDC provides verification functionality through the FdcVerification
contract.
If we proof is valid, the function verifyEVMTransaction
will return true
, otherwise false
.
We deploy and verify this contract with the deployAndVerifyContract
function in the scripts/fdcExample/JsonApi.ts
file.
async function deployAndVerifyContract() {
const args: any[] = [];
const characterList: StarWarsCharacterListInstance =
await StarWarsCharacterList.new(...args);
try {
await run("verify:verify", {
address: characterList.address,
constructorArguments: args,
});
} catch (e: any) {
console.log(e);
}
console.log("StarWarsCharacterList deployed to", characterList.address, "\n");
return characterList;
}
Interact with contract
We define an additional function that allows us to interact with the just deployed contract.
The interactWithContract
function also takes the proof retrieved in the previous step as an argument.
It abi decodes the response_hex
value to an IJsonApi.Response
struct.
From that and the array of proofs, it constructs an IJsonApi.Proof
object, on which it call the registerAddress
function of the AddressRegistry
contract deployed above.
The contract verifies the address, and the script prints it to the console.
async function interactWithContract(
characterList: StarWarsCharacterListInstance,
proof: any,
) {
console.log("Proof hex:", proof.response_hex, "\n");
// A piece of black magic that allows us to read the response type from an artifact
const IJsonApiVerification = await artifacts.require("IJsonApiVerification");
const responseType =
IJsonApiVerification._json.abi[0].inputs[0].components[1];
console.log("Response type:", responseType, "\n");
const decodedResponse = web3.eth.abi.decodeParameter(
responseType,
proof.response_hex,
);
console.log("Decoded proof:", decodedResponse, "\n");
const transaction = await characterList.addCharacter({
merkleProof: proof.proof,
data: decodedResponse,
});
console.log("Transaction:", transaction.tx, "\n");
console.log(
"Star Wars Characters:\n",
await characterList.getAllCharacters(),
"\n",
);
}
We can run the whole script by calling the following console command.
yarn hardhat run scripts/fdcExample/JsonApi.ts