Created
June 6, 2024 20:08
-
-
Save moaazsidat/6aa02999d731562cb4d341e51f33730e to your computer and use it in GitHub Desktop.
EAS SDK extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
AttestationRequest, | |
EAS, | |
MultiAttestationRequest, | |
NO_EXPIRATION, | |
Offchain, | |
SchemaEncoder, | |
SchemaRegistry, | |
Transaction, | |
ZERO_ADDRESS, | |
ZERO_BYTES32, | |
getUIDsFromAttestReceipt, | |
} from '@ethereum-attestation-service/eas-sdk' | |
import { Overrides, TransactionReceipt, ethers } from 'ethers' | |
export const EASContractAddress = '0x4200000000000000000000000000000000000021' // Optimism | |
// '0xC2679fBD37d54388Ce493F1DB75320D236e1815e' // Sepolia v0.26 | |
/** | |
* This is to extend the EAS SDK to support | |
* returning transaction hash as part of the response | |
* | |
* attest and multiAttest are copied from the EAS SDK | |
* and modified to return the transaction hash | |
*/ | |
interface AttestationResponse { | |
hash: string | |
uid: string | |
} | |
interface AttestationsResponse { | |
hash: string | |
uids: string[] | |
} | |
class EASExtended extends EAS { | |
public async attestWithHash( | |
{ | |
schema, | |
data: { | |
recipient = ZERO_ADDRESS, | |
data, | |
expirationTime = NO_EXPIRATION, | |
revocable = true, | |
refUID = ZERO_BYTES32, | |
value = BigInt(0), | |
}, | |
}: AttestationRequest, | |
overrides?: Overrides | |
): Promise<Transaction<AttestationResponse>> { | |
if (!this.signer) { | |
throw new Error('Invalid signer') | |
} | |
return new Transaction( | |
await this.contract.attest.populateTransaction( | |
{ | |
schema, | |
data: { recipient, expirationTime, revocable, refUID, data, value }, | |
}, | |
{ value, ...overrides } | |
), | |
this.signer, | |
// eslint-disable-next-line require-await | |
async (receipt: TransactionReceipt) => ({ | |
hash: receipt.hash, | |
uid: getUIDsFromAttestReceipt(receipt)[0], | |
}) | |
) | |
} | |
public async multiAttestWithHash( | |
requests: MultiAttestationRequest[], | |
overrides?: Overrides | |
): Promise<Transaction<AttestationsResponse>> { | |
if (!this.signer) { | |
throw new Error('Invalid signer') | |
} | |
const multiAttestationRequests = requests.map((r) => ({ | |
schema: r.schema, | |
data: r.data.map((d) => ({ | |
recipient: d.recipient ?? ZERO_ADDRESS, | |
expirationTime: d.expirationTime ?? NO_EXPIRATION, | |
revocable: d.revocable ?? true, | |
refUID: d.refUID ?? ZERO_BYTES32, | |
data: d.data ?? ZERO_BYTES32, | |
value: d.value ?? BigInt(0), | |
})), | |
})) | |
const requestedValue = multiAttestationRequests.reduce((res, { data }) => { | |
const total = data.reduce((res, r) => res + r.value, BigInt(0)) | |
return res + total | |
}, BigInt(0)) | |
return new Transaction( | |
await this.contract.multiAttest.populateTransaction( | |
multiAttestationRequests, | |
{ | |
value: requestedValue, | |
...overrides, | |
} | |
), | |
this.signer, | |
// eslint-disable-next-line require-await | |
async (receipt: TransactionReceipt) => ({ | |
hash: receipt.hash, | |
uids: getUIDsFromAttestReceipt(receipt), | |
}) | |
) | |
} | |
} | |
const eas = new EASExtended(EASContractAddress) | |
// const { hash, uid } = await eas.attestWithHash(...) | |
// const { hash, uids } = await eas.multiAttestWithHash(...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment