MCPcopy Index your code
hub / github.com/ethereum-attestation-service/eas-sdk

github.com/ethereum-attestation-service/eas-sdk @v2.9.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.9.0 ↗ · + Follow
303 symbols 651 edges 47 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Ethereum Attestation Service - TypeScript/JavaScript SDK

Docs NPM Package Test

This repository contains the Ethereum Attestation Service SDK, used to interact with the Ethereum Attestation Service Protocol.

Table of Contents

Installing the EAS SDK

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

Using the 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);

Getting an Attestation

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.

Usage

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);

Output

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'
}

Estimating Gas for Transactions

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.

Example: Estimating Gas for an 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,
    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);

Example: Estimating Gas for a Revocation

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();

Creating Onchain Attestations

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.

Example: Creating Onchain Attestations

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);

Example: Creating Multi Onchain Attestations

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);

Revoking Onchain Attestations

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.

Example: Revoking Onchain Attestations

const transaction = await eas.revoke({
  schema: '0x85500e806cf1e74844d51a20a6d893fe1ed6f6b0738b50e43d774827d08eca61',
  data: { uid: '0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a' }
});

// Optional: Wait for transaction to be validated
await transaction.wait();

Creating Offchain Attestations

To create an offchain attestation, you can use the signOffchainAttestation function provided by the Offchain class in the EAS SDK. Here's an example:

Example: Creating Offchain Attestations

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.

Versioning

Since the offchain attestation protocol is constantly evolving a

Extension points exported contracts — how you extend this code

ContractBuilder (Interface)
(no doc)
test/components/Contracts.ts
Spec (Interface)
(no doc)
test/test/offchain-utils.ts
MerkleMultiProof (Interface)
(no doc)
src/private-data.ts
RegisterSchemaParams (Interface)
(no doc)
src/schema-registry.ts
IndexerOptions (Interface)
(no doc)
src/indexer.ts
AttestationRequestData (Interface)
(no doc)
src/request.ts
TransactionProvider (Interface)
(no doc)
src/transaction.ts
EIP712ProxyOptions (Interface)
(no doc)
src/eip712-proxy.ts

Core symbols most depended-on inside this repo

wait
called by 37
src/transaction.ts
latest
called by 29
test/test/helpers/time.ts
populateTransaction
called by 23
src/legacy/typechain/common.ts
connect
called by 22
src/legacy/typechain/contracts/EAS.ts
deploy
called by 18
test/components/Contracts.ts
verifyOffchainAttestationSignature
called by 17
src/offchain/offchain.ts
signOffchainAttestation
called by 12
src/offchain/offchain.ts
register
called by 11
src/schema-registry.ts

Shape

Method 143
Interface 74
Function 44
Class 35
Enum 7

Languages

TypeScript100%

Modules by API surface

src/eas.ts39 symbols
src/indexer.ts29 symbols
src/offchain/typed-data-handler.ts25 symbols
src/eip712-proxy.ts18 symbols
src/schema-encoder.ts17 symbols
src/legacy/typechain/contracts/EAS.ts16 symbols
src/transaction.ts13 symbols
src/private-data.ts13 symbols
src/offchain/offchain.ts12 symbols
src/offchain/delegated.ts12 symbols
src/legacy/typechain/common.ts12 symbols
src/request.ts11 symbols

For agents

$ claude mcp add eas-sdk \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page