Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions update-eas-attestations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const { EAS, SchemaEncoder } = require("@ethereum-attestation-service/eas-sdk");
const { Wallet, JsonRpcProvider } = require("ethers");

const WALLET_PK = process.env.WALLET_PK;
const SCHEMA_UID = process.env.SCHEMA_UID;
const JSON_RPC_URL = process.env.JSON_RPC_URL;
const easContractAddress = "0x4200000000000000000000000000000000000021";

exports.handler = async (event) => {
try {
const signer = new Wallet(WALLET_PK, new JsonRpcProvider(JSON_RPC_URL));
const eas = new EAS(easContractAddress);
// Signer must be an ethers-like signer.
eas.connect(signer);
if (event.action === "create_attestations") {
if (event.wallet_address === undefined) {
return {
statusCode: 400,
body: { errorId: 1, error: "No wallet id provided" },
};
}
// Initialize SchemaEncoder with the schema string
const schemaEncoder = new SchemaEncoder("bytes32 credential");
const requests = [];

for (let i = 0; i < event.credentials.length; i++) {
const encodedData = schemaEncoder.encodeData([
{
name: "credential",
value: event.credentials[i],
type: "bytes32",
},
]);
requests.push({
schema: SCHEMA_UID,
data: [
{
recipient: event.wallet_address,
expirationTime: 0,
revocable: true,
data: encodedData,
},
],
});
}

const tx = await eas.multiAttest(requests);
const uids = await tx.wait();

const promises = uids.map(async (uid) => {
const { data } = await eas.getAttestation(uid);
const decodedData = schemaEncoder.decodeData(data);

return {
uid: uid,
credential: decodedData[0].value.value,
};
});

const result = await Promise.all(promises);

return {
statusCode: 200,
body: result,
};
} else if (event.action === "destroy_attestations") {
const requests = [];

for (let i = 0; i < event.uids.length; i++) {
requests.push({
schema: SCHEMA_UID,
data: [
{
uid: event.uids[i],
value: BigInt(0),
},
],
});
}

const tx = await eas.multiRevoke(requests);
await tx.wait();

return {
statusCode: 200,
body: {},
};
} else {
return {
statusCode: 400,
body: { errorId: 1, error: "invalid action" },
};
}
} catch (error) {
return {
statusCode: 500,
body: { error: error },
};
}
};
16 changes: 16 additions & 0 deletions update-eas-attestations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "update-eas-attestations",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@ethereum-attestation-service/eas-sdk": "2.1.4",
"ethers": "6.12.1"
}
}
Loading