The AttestationStation smart contract contains a public attestations
mapping that anyone can write to and read from. For more context on the AttestationStation visit the overview in our developer documentation.
In this tutorial you learn how to read, interpret, and write those attestations.
The contract we'll be using is on the Optimism Goerli network, at address 0x3Ca8c0B5608AE3E4D3b4d29b2699C5fCc0e67f3d
.
- You have Node.js running on your computer, as well as yarn.
- There is network connectivity to a provider on the Optimism Goerli network, and to the npm package registry.
-
Use
yarn
to download the packages you needyarn
-
Copy
.env.example
to.env
and modify the parameters:-
MNEMONIC
is the mnemonic to an account that has enough ETH to pay for the transaction. -
ALCHEMY_API_KEY
is the API key for an Optimism Goerli app on Alchemy, our preferred provider. -
OPTIMISM_GOERLI_URL
is the URL for Optimism Goerli, if you use a different node provider.
-
-
Enter the hardhat console:
yarn hardhat console --network optimism-goerli
-
Attach to the contract on the Optimism Goerli network:
AttestationStation = await ethers.getContractFactory("AttestationStation"); attestationStation = AttestationStation.attach( "0x3Ca8c0B5608AE3E4D3b4d29b2699C5fCc0e67f3d" );
-
Create the attestation.
goatAddr = "0x00000000000000000000000000000000000060A7"; attendedKey = ethers.utils.keccak256( ethers.utils.toUtf8Bytes("animal-farm.school.attended") ); attestation = { about: goatAddr, key: attendedKey, val: 1, // for true };
-
Send the attestation. Note that
attestationStation.attest
accepts an array as parameter, so you'll be able to attest to many facts in a single transaction.tx = await attestationStation.attest([attestation]); rcpt = await tx.wait();
To read an attestation you need to know three things:
- Creator address (who attested this)
- Subject address (who is being attested about)
- Key
-
Read the value for the attestation you just created.
creatorAddr = (await ethers.getSigner()).address( (await attestationStation.attestations( creatorAddr, goatAddr, attendedKey )) != "0x" );
-
Check the attestation for a different address to see that the default is false
notGoatAddr = "0x000000000000000000000000000000000000BEEF"( (await attestationStation.attestations( creatorAddr, notGoatAddr, attendedKey )) != "0x" );
-
Read an attestation created by a different user (this one is a grade, so it's text)
historyKey = ethers.utils.keccak256( ethers.utils.toUtf8Bytes("animal-farm.school.grades.history") ); hex = await attestationStation.attestations( "0xBCf86Fd70a0183433763ab0c14E7a760194f3a9F", goatAddr, historyKey ); ethers.utils.toUtf8String(hex);
Note: To create the attestation with an ascii value I used this data structure:
attestation = { about: goatAddr, key: historyKey, val: ethers.utils.toUtf8Bytes("A+"), };