This repository contains the Ethereum Attestation Service SDK, used to interact with the Ethereum Attestation Service Protocol.
To install the EAS SDK, run the following command within your project directory:
yarn add @ethereum-attestation-service/eas-sdk
OR
npm install @ethereum-attestation-service/eas-sdk
OR
pnpm add @ethereum-attestation-service/eas-sdk
Import and initialize the library:
import { EAS, Offchain, SchemaEncoder, SchemaRegistry } from '@ethereum-attestation-service/eas-sdk';
import { ethers } from 'ethers';
export const EASContractAddress = '0xC2679fBD37d54388Ce493F1DB75320D236e1815e'; // Sepolia v0.26
// Initialize the SDK with the address of the EAS Schema contract address
const eas = new EAS(EASContractAddress);
// Gets a default provider (in production use something else like infura/alchemy)
const provider = ethers.getDefaultProvider('sepolia');
// Connects an ethers style provider/signingProvider to perform read/write functions.
// MUST be a signer to do write operations!
eas.connect(provider);
The getAttestation function allows you to retrieve an on-chain attestation for a given UID. This function returns an attestation object containing information about the attestation, such as the schema, recipient, attester, and more.
import { EAS, NO_EXPIRATION } from '@ethereum-attestation-service/eas-sdk';
const eas = new EAS(EASContractAddress);
eas.connect(provider);
const uid = '0xff08bbf3d3e6e0992fc70ab9b9370416be59e87897c3d42b20549901d2cccc3e';
const attestation = await eas.getAttestation(uid);
console.log(attestation);
The getAttestation function returns an attestation object with the following properties:
uid: The unique identifier of the attestation.schema: The schema identifier associated with the attestation.refUID: The reference UID of the attestation, if any.time: The Unix timestamp when the attestation was created.expirationTime: The Unix timestamp when the attestation expires (0 for no expiration).revocationTime: The Unix timestamp when the attestation was revoked, if applicable.recipient: The Ethereum address of the recipient of the attestation.attester: The Ethereum address of the attester who created the attestation.revocable: A boolean indicating whether the attestation is revocable or not.data: The attestation data in bytes format.Example output:
{
uid: '0x5134f511e0533f997e569dac711952dde21daf14b316f3cce23835defc82c065',
schema: '0x27d06e3659317e9a4f8154d1e849eb53d43d91fb4f219884d1684f86d797804a',
refUID: '0x0000000000000000000000000000000000000000000000000000000000000000',
time: 1671219600,
expirationTime: NO_EXPIRATION,
revocationTime: 1671219636,
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
attester: '0x1e3de6aE412cA218FD2ae3379750388D414532dc',
revocable: true,
data: '0x0000000000000000000000000000000000000000000000000000000000000000'
}
The estimateGas method allows you to estimate the gas cost for any transaction before sending it. This is useful for determining the gas requirements and costs before executing transactions. The method is available on all transaction objects returned by EAS SDK methods.
import { EAS, NO_EXPIRATION, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
const eas = new EAS(EASContractAddress);
eas.connect(signer);
// Initialize SchemaEncoder with the schema string
const schemaEncoder = new SchemaEncoder('uint256 eventId, uint8 voteIndex');
const encodedData = schemaEncoder.encodeData([
{ name: 'eventId', value: 1, type: 'uint256' },
{ name: 'voteIndex', value: 1, type: 'uint8' }
]);
const schemaUID = '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995';
const transaction = await eas.attest({
schema: schemaUID,
data: {
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
expirationTime: NO_EXPIRATION,
revocable: true,
data: encodedData
}
});
// Estimate gas before sending the transaction
const estimatedGas = await transaction.estimateGas();
console.log('Estimated gas:', estimatedGas.toString());
// Now send the transaction
const newAttestationUID = await transaction.wait();
console.log('New attestation UID:', newAttestationUID);
const transaction = await eas.revoke({
schema: '0x85500e806cf1e74844d51a20a6d893fe1ed6f6b0738b50e43d774827d08eca61',
data: { uid: '0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a' }
});
// Estimate gas before sending the transaction
const estimatedGas = await transaction.estimateGas();
console.log('Estimated gas for revocation:', estimatedGas.toString());
// Now send the transaction
await transaction.wait();
The attest function allows you to create an on-chain attestation for a specific schema. This function takes an object with the following properties:
schema: The UID of the schema for which the attestation is being created.data: An object containing the following properties:recipient: The Ethereum address of the recipient of the attestation.expirationTime: A Unix timestamp representing the expiration time of the attestation. Use 0 for no expiration.revocable: A boolean indicating whether the attestation is revocable or not.refUID: (Optional) The UID of a referenced attestation. Use ZERO_BYTES32 if there is no reference.data: The encoded data for the attestation, which should be generated using the SchemaEncoder class.value: (Optional) The ETH value that is being sent with the attestation.The function returns a Promise that resolves to the UID of the newly created attestation.
import { EAS, NO_EXPIRATION, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
const eas = new EAS(EASContractAddress);
eas.connect(signer);
// Initialize SchemaEncoder with the schema string
const schemaEncoder = new SchemaEncoder('uint256 eventId, uint8 voteIndex');
const encodedData = schemaEncoder.encodeData([
{ name: 'eventId', value: 1, type: 'uint256' },
{ name: 'voteIndex', value: 1, type: 'uint8' }
]);
const schemaUID = '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995';
const transaction = await eas.attest({
schema: schemaUID,
data: {
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
expirationTime: NO_EXPIRATION,
revocable: true, // Be aware that if your schema is not revocable, this MUST be false
data: encodedData
}
});
// Estimate gas for the transaction before sending it
const estimatedGas = await transaction.estimateGas();
console.log('Estimated gas:', estimatedGas.toString());
const newAttestationUID = await transaction.wait();
console.log('New attestation UID:', newAttestationUID);
console.log('Transaction receipt:', transaction.receipt);
import { EAS, NO_EXPIRATION, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
const eas = new EAS(EASContractAddress);
eas.connect(signer);
// Initialize SchemaEncoder with the schema string
const schemaEncoder = new SchemaEncoder('uint256 eventId, uint8 voteIndex');
const encodedData = schemaEncoder.encodeData([
{ name: 'eventId', value: 1, type: 'uint256' },
{ name: 'voteIndex', value: 1, type: 'uint8' }
]);
const encodedData2 = schemaEncoder.encodeData([
{ name: 'eventId', value: 10, type: 'uint256' },
{ name: 'voteIndex', value: 2, type: 'uint8' }
]);
const schemaUID = '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995';
const transaction = await eas.multiAttest([
{
schema: schemaUID,
data: [
{
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
expirationTime: NO_EXPIRATION,
revocable: true, // Be aware that if your schema is not revocable, this MUST be false
data: encodedData
},
{
recipient: '0xA1207F3BBa224E2c9c3c6D5aF63D0eb1582Ce587',
expirationTime: NO_EXPIRATION,
revocable: false,
data: encodedData2
}
]
}
]);
const newAttestationUID = await transaction.wait();
console.log('New attestation UID:', newAttestationUID);
console.log('Transaction receipt:', transaction.receipt);
The revoke function allows you to revoke an on-chain attestation. This function takes an object with the following properties:
schema: The UID of the schema for which the attestation is being revoked.data: An object containing the following properties:uid: The UID of the attestation to revoke.value: (Optional) The ETH value that is being sent with the revocation.const transaction = await eas.revoke({
schema: '0x85500e806cf1e74844d51a20a6d893fe1ed6f6b0738b50e43d774827d08eca61',
data: { uid: '0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a' }
});
// Optional: Wait for transaction to be validated
await transaction.wait();
To create an offchain attestation, you can use the signOffchainAttestation function provided by the Offchain class in the EAS SDK. Here's an example:
import { EAS, NO_EXPIRATION, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
// Initialize EAS with the EAS contract address on whichever chain where your schema is defined
const eas = new EAS(EASContractAddress);
const offchain = await eas.getOffchain();
// Initialize SchemaEncoder with the schema string
// Note these values are sample values and should be filled with actual values
// Code samples can be found when viewing each schema on easscan.org
const schemaEncoder = new SchemaEncoder('uint256 eventId, uint8 voteIndex');
const encodedData = schemaEncoder.encodeData([
{ name: 'eventId', value: 1, type: 'uint256' },
{ name: 'voteIndex', value: 1, type: 'uint8' }
]);
// Signer is an ethers.js Signer instance
const signer = new ethers.Wallet(privateKey, provider);
const offchainAttestation = await offchain.signOffchainAttestation(
{
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
expirationTime: NO_EXPIRATION, // Unix timestamp of when attestation expires (0 for no expiration)
time: BigInt(Math.floor(Date.now() / 1000)), // Unix timestamp of current time
revocable: true, // Be aware that if your schema is not revocable, this MUST be false
schema: '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995',
refUID: '0x0000000000000000000000000000000000000000000000000000000000000000',
data: encodedData
},
signer
);
This function will return a signed offchain attestation object containing the UID, signature, and attestation data. You can then share this object with the intended recipient or store it for future use.
Since the offchain attestation protocol is constantly evolving a
$ claude mcp add eas-sdk \
-- python -m otcore.mcp_server <graph>