import { EAS, Offchain, SignedOffchainAttestation, TypedDataSigner, SchemaEncoder, getSchemaUID, decodeBase64ZippedBase64, zipAndEncodeToBase64, OFFCHAIN_ATTESTATION_TYPES } from "@ethereum-attestation-service/eas-sdk"; import {BigNumber, ethers, Signer} from "ethers"; import {AttestationCrypto} from "../libs/AttestationCrypto"; import {AsnParser, AsnProp, AsnPropTypes, AsnSerializer} from "@peculiar/asn1-schema"; import {defaultAbiCoder, joinSignature} from "ethers/lib/utils"; import {hexStringToUint8, uint8tohex} from "../libs/utils"; import {OffchainAttestationParams} from "@ethereum-attestation-service/eas-sdk/dist/offchain/offchain"; import {Attestable} from "../libs/Attestable"; import {AttestableObject} from "../libs/AttestableObject"; import {KeyPair, KeysArray} from "../libs/KeyPair"; import {EIP712DomainTypedData} from "@ethereum-attestation-service/eas-sdk/dist/offchain/typed-data-handler"; import * as pako from "pako"; export {SignedOffchainAttestation, decodeBase64ZippedBase64, zipAndEncodeToBase64}; export enum AbiFieldTypes { bool = 'bool', uint8 = 'uint8', uint16 = 'uint16', uint32 = 'uint32', uint64 = 'uint64', uint128 = 'uint128', uint256 = 'uint256', address = 'address', string = 'string', bytes = 'bytes', bytes32 = 'bytes32' } export interface SchemaField { name: string, type: AbiFieldTypes|string, isCommitment?: boolean } export interface TicketSchema { fields: SchemaField[] } export interface EasTicketCreationOptions { recipient?: string, schema?: string, revocable?: boolean, refUID?: string, validity?: {from: number, to: number} } export interface EasJson { sig: SignedOffchainAttestation, signer: string, secret?: string } export class EasAsnEmbeddedSchema { @AsnProp({ type: AsnPropTypes.OctetString }) public attestation: Uint8Array @AsnProp({ type: AsnPropTypes.BitString }) public signature: Uint8Array; @AsnProp({ type: AsnPropTypes.OctetString, optional: true }) public domain: Uint8Array } export class EasTicketAttestation extends AttestableObject implements Attestable { private static DEFAULT_OFFCHAIN_VERSION = 1; private signedAttestation: SignedOffchainAttestation; private signerAddress: string; private signerPublicKey: string; private decodedData?: {[fieldName: string]: any}; private commitmentSecret?: bigint; private conferenceKeys?: KeyPair[]; private crypto = new AttestationCrypto(); /** * * @param schema The schema for the attestation. This must always be provided. * @param signingConfig The EAS SDK config and signer - required only to create & revoke attestations. * @param rpcMap A map of RPC nodes required for revocation checks * @param issuerKeys A map of valid issuer keys for signature validation purposes */ constructor( private schema: TicketSchema, private signingConfig?: { EASconfig: { address: string // EAS resolver contract address version: string chainId: number }, signer: Signer, offChainVersion?: number }, private rpcMap?: {[chainId: number]: string}, private issuerKeys?: KeysArray ) { super(); } private getEasSchema(){ return this.schema.fields.map((field) => { return field.type + " " + field.name }).join(","); } private checkAttestationIsLoaded(){ if (!this.signedAttestation) throw new Error("Signed attestation must be loaded to call this function"); } /** * Issue a new offchain attestation for the provided ID * @param data * @param options * @param commitmentType */ async createEasAttestation(data: {[key: string]: string|number}, options: EasTicketCreationOptions = {}, commitmentType = 'mail'){ if (!this.signingConfig) throw new Error("Please provide valid signing config for this function."); this.signerAddress = await this.signingConfig.signer.getAddress(); if (!this.signerAddress) new Error("Failed to get signer address"); // New secret generated for each attestation this.commitmentSecret = undefined; const fieldData = this.schema.fields.map((field) => { if (!data[field.name]) throw new Error("Value for field " + field.name + " was not provided"); let value = data[field.name] if (field.isCommitment){ // If there are multiple commitment fields, the same secret is used for all fields if (!this.commitmentSecret) this.commitmentSecret = this.crypto.makeSecret(); value = this.createCommitment(value as string, commitmentType, this.commitmentSecret); } return { name: field.name, value, type: field.type } }); const offChainVersion = this.signingConfig?.offChainVersion ?? EasTicketAttestation.DEFAULT_OFFCHAIN_VERSION; const offchain = new Offchain(this.signingConfig.EASconfig, offChainVersion); const schemaEncoder = new SchemaEncoder(this.getEasSchema()); const encodedData = schemaEncoder.encodeData(fieldData); // Automatically calculate schema address (may be used for AW) // TODO: Add option for resolver address if (!options.schema) options.schema = getSchemaUID(this.getEasSchema(), "0x0000000000000000000000000000000000000000", options.revocable !== false) const newAttestation = await offchain.signOffchainAttestation({ version: offChainVersion, recipient: options.recipient ?? '0x0000000000000000000000000000000000000000', // Unix timestamp of when attestation expires. (0 for no expiration) expirationTime: options.validity?.to ?? 0, // Unix timestamp of current time time: options.validity?.from ?? Math.round(Date.now() / 1000), nonce: 0, schema: options.schema, revocable: options.revocable !== false, refUID: options.refUID ?? "0x0000000000000000000000000000000000000000000000000000000000000000", data: encodedData, }, this.signingConfig.signer as unknown as TypedDataSigner); const valid = offchain.verifyOffchainAttestationSignature(this.signerAddress, newAttestation) if (!valid) throw new Error("Attestation signature check failed!"); this.signedAttestation = newAttestation; this.recoverSignerInfo(); return this.getEasJson(); } private createCommitment(commitmentValue: string, commitmentType: string, commitmentSecret: bigint){ return "0x" + uint8tohex(this.crypto.makeCommitment(commitmentValue, this.crypto.getType(commitmentType), commitmentSecret)) } getEasJson(){ this.checkAttestationIsLoaded(); const data: EasJson = { sig: this.signedAttestation, signer: this.signerAddress, }; if (this.commitmentSecret) data.secret = "0x" + this.commitmentSecret.toString(16) return data; } getEasUid(message?: OffchainAttestationParams){ if (!message) this.checkAttestationIsLoaded(); return Offchain.getOffchainUID(message ?? this.signedAttestation.message) } getEncoded(){ return zipAndEncodeToBase64(this.getEasJson()); } getAttestationData(){ this.checkAttestationIsLoaded(); if (!this.decodedData) { const schemaEncoder = new SchemaEncoder(this.getEasSchema()); const dataArr = schemaEncoder.decodeData(this.signedAttestation.message.data); this.decodedData = {}; let index = 0 for (const value of this.schema.fields){ this.decodedData[value.name] = dataArr[index].value.value; index++; } } return this.decodedData; } getAttestationField(fieldName: string){ const data = this.getAttestationData(); if (!data[fieldName]) { throw new Error("The attestation does not contain data field '" + fieldName + "'"); } return data[fieldName] } verifyIdCommitment(commitValue: string, commitSecret?: bigint, commitmentType = 'mail'){ if (commitSecret) this.commitmentSecret = commitSecret; if (!this.commitmentSecret) throw new Error("Commitment secret required."); const calced = this.createCommitment(commitValue, commitmentType, this.commitmentSecret); const commit = this.getAttestationField("commitment"); if (calced !== commit) throw new Error("Commitment verification failed."); } async validateEasAttestation(skipRevocationCheck = false){ this.checkAttestationIsLoaded(); // Signature check this.verify(); // Expiry check this.checkValidity(); if(skipRevocationCheck || !this.signedAttestation.message.revocable) return; // EAS registry check to make sure attestation is not revoked await this.checkRevocation() } private async checkRevocation(uid?: string){ if (!uid) uid = this.getEasUid(); const chainId = this.signedAttestation.domain.chainId; if (!this.rpcMap?.[chainId]) throw new Error("RPC not provided for chain " + chainId); const eas = new EAS(this.signedAttestation.domain.verifyingContract, {signerOrProvider: new ethers.providers.StaticJsonRpcProvider(this.rpcMap[chainId])}); const revoked = await eas.getRevocationOffchain(this.signerAddress, uid); if (BigNumber.from(revoked).gt(0)) { const msg = "Attestation has been revoked :-("; //alert(msg); throw new Error(msg); } } async revokeEasAttestation(uid?: string){ if (!uid) uid = this.getEasUid(); if (!this.signingConfig) throw new Error("Please provide a valid signer"); const eas = new EAS(this.signingConfig.EASconfig.address, {signerOrProvider: this.signingConfig.signer}); const tx = await eas.revokeOffchain(uid); await tx.wait(); return tx.tx; } async bulkRevokeEasAttestations(uids: string[]){ if (!this.signingConfig) throw new Error("Please provide a valid signer"); const eas = new EAS(this.signingConfig.EASconfig.address, {signerOrProvider: this.signingConfig.signer}); const tx = await eas.multiRevokeOffchain(uids); await tx.wait(); return tx.tx; } loadEasAttestation(attestation: SignedOffchainAttestation, keys?: KeysArray, commitmentSecret?: string){ this.decodedData = undefined; this.commitmentSecret = commitmentSecret ? BigInt(commitmentSecret) : undefined; this.signedAttestation = attestation; this.processKeysParam(keys); } loadFromEncoded( base64: string, keys?: KeysArray, commitmentSecret?: string){ const decoded = decodeBase64ZippedBase64(base64); this.loadEasAttestation(decoded.sig as SignedOffchainAttestation, keys, commitmentSecret) } public getAbiEncodedParts(){ const domain = defaultAbiCoder.encode( ['string', "address", "uint256"], [ this.signedAttestation.domain.version, this.signedAttestation.domain.verifyingContract, this.signedAttestation.domain.chainId ] ); const attestation = defaultAbiCoder.encode( this.signedAttestation.types.Attest.map((field) => field.type), this.signedAttestation.types.Attest.map((field) => this.signedAttestation.message[field.name]) ); const signature = joinSignature(this.signedAttestation.signature ?? this.signedAttestation); return {domain, attestation, signature}; } getBinaryEncoded(format: "abi"|"asn", compressed = false){ return format === "abi" ? this.getAbiEncoded(compressed) : this.getAsnEncoded(compressed); } getAsnEncoded(compressed = false){ const encodedParts = this.getAbiEncodedParts(); const asnEmbedded = new EasAsnEmbeddedSchema(); asnEmbedded.domain = hexStringToUint8(encodedParts.domain); asnEmbedded.attestation = hexStringToUint8(encodedParts.attestation); asnEmbedded.signature = hexStringToUint8(encodedParts.signature); const data = AsnSerializer.serialize(asnEmbedded); if (!compressed) return data; return pako.deflate(data, {level: 9}); } getAbiEncoded(compressed = false){ const encodedParts = this.getAbiEncodedParts(); const encoded = defaultAbiCoder.encode( ['bytes', "bytes", "bytes"], [ encodedParts.domain, encodedParts.attestation, encodedParts.signature ] ) const data = hexStringToUint8(encoded); if (!compressed) return data; return pako.deflate(data, {level: 9}); } loadBinaryEncoded(format: "abi"|"asn", bytes: ArrayBuffer|Uint8Array, keys?: KeysArray, compressed = false){ return format === "abi" ? this.loadAbiEncoded(bytes, keys, compressed) : this.loadAsnEncoded(bytes, keys, compressed) } loadAbiEncoded(bytes: ArrayBuffer|Uint8Array, keys?: KeysArray, compressed = false){ if (compressed) bytes = pako.inflate(bytes); const abiEmbedded = defaultAbiCoder.decode( ['bytes', "bytes", "bytes"], new Uint8Array(bytes) ); this.loadAbiEncodedParts({ domain: abiEmbedded[0], attestation: abiEmbedded[1], signature: abiEmbedded[2] }, keys); } loadAsnEncoded(bytes: ArrayBuffer|Uint8Array, keys?: KeysArray, compressed = false){ if (compressed) bytes = pako.inflate(bytes); const asnEmbedded = AsnParser.parse(bytes, EasAsnEmbeddedSchema); this.loadAbiEncodedParts(asnEmbedded, keys) } loadAbiEncodedParts( parts: {domain: string|Uint8Array, attestation: string|Uint8Array, signature: string|Uint8Array}, keys?: KeysArray ){ this.decodedData = undefined; this.commitmentSecret = undefined; const domainDecoded = defaultAbiCoder.decode( ['string', "address", "uint256"], parts.domain ); // console.log("ABI Encoded bytes: ", "0x" + uint8tohex(new Uint8Array(asnEmbedded.easAttestation))); const firstElem = defaultAbiCoder.decode(['uint256'], parts.attestation); const version = firstElem[0] > 50n ? 0 : 1; const abiDecoded = defaultAbiCoder.decode( OFFCHAIN_ATTESTATION_TYPES[version].types.map((field) => field.type), parts.attestation ); //console.log("Abi decoded: ", abiDecoded); const mappedDecodedValues: {[name: string]: any} = {}; for (const [index, value] of OFFCHAIN_ATTESTATION_TYPES[version].types.entries()){ mappedDecodedValues[value.name] = abiDecoded[index]; } if (version === 0) mappedDecodedValues.version = 0; // console.log("EAS version: ", version); const splitSignature = ethers.utils.splitSignature(parts.signature); const domain: EIP712DomainTypedData = { name: OFFCHAIN_ATTESTATION_TYPES[version].domainName, version: domainDecoded[0], verifyingContract: domainDecoded[1], chainId: domainDecoded[2] } this.signedAttestation = { domain: domain, message: mappedDecodedValues as OffchainAttestationParams, types: {Attest: OFFCHAIN_ATTESTATION_TYPES[version].types}, primaryType: OFFCHAIN_ATTESTATION_TYPES[version].primaryType, signature: { r: splitSignature.r, s: splitSignature.s, v: splitSignature.v, }, uid: this.getEasUid(mappedDecodedValues as OffchainAttestationParams), }; this.processKeysParam(keys); } checkValidity(): boolean { const now = Math.round(Date.now() / 1000); if (now < this.signedAttestation.message.time) throw new Error("Attestation not yet valid."); if (this.signedAttestation.message.expirationTime > 0 && now > this.signedAttestation.message.expirationTime) throw new Error("Attestation has expired."); return true; } private processKeysParam(keys?: KeysArray){ const data = this.getAttestationData(); let conferenceId = data.eventId ?? data.devconId ?? ""; if (!keys){ if (!this.issuerKeys){ throw new Error("No signing keys are defined"); } keys = this.issuerKeys; } if (!keys[conferenceId]){ if (!conferenceId || (conferenceId && !keys[""])){ throw new Error(conferenceId ? "No key set for conference ID " + conferenceId : "No default key set"); } conferenceId = ""; // Use default key as fallback when no keys for the provided conference ID are set } const keyArray = keys[conferenceId]; if (Array.isArray(keyArray)){ this.conferenceKeys = keyArray; } else { this.conferenceKeys = [keyArray]; } this.recoverSignerInfo(); } private recoverSignerInfo(){ const config = { version: this.signedAttestation.domain.version, address: this.signedAttestation.domain.verifyingContract, chainId: this.signedAttestation.domain.chainId }; const offchain = new Offchain(config, this.signedAttestation.message.version ?? 0); const hash = ethers.utils._TypedDataEncoder.hash(offchain.getDomainTypedData(), {Attest: OFFCHAIN_ATTESTATION_TYPES[this.signedAttestation.message.version].types}, this.signedAttestation.message); this.signerPublicKey = ethers.utils.recoverPublicKey(hash, this.signedAttestation.signature); this.signerAddress = ethers.utils.recoverAddress(hash, this.signedAttestation.signature) } verify(): boolean { if (!this.conferenceKeys){ throw new Error("Issuer keys are not defined"); } for (const key of this.conferenceKeys){ if (this.signerPublicKey.substring(2) === key.getPublicKeyAsHexStr()) { return true; } } throw new Error("Ticket signature is invalid"); } getSignerKeyPair(){ return KeyPair.fromPublicHex(this.signerPublicKey); } getSignerKey(){ return this.signerPublicKey; } getSignerAddress(){ return this.signerAddress; } getCommitment(): Uint8Array { return hexStringToUint8(this.getAttestationField("commitment")); } getDerEncoding(): string { return uint8tohex(new Uint8Array(this.getAsnEncoded(false))); } protected commitment: Uint8Array; protected encoded: string; fromBytes(bytes: ArrayBuffer|Uint8Array, keys: KeysArray){ this.loadAsnEncoded(bytes, keys); } }