UNPKG

@graphprotocol/toolshed

Version:

A collection of tools and utilities for the Graph Protocol Typescript components

101 lines 4.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EIP712_ATTESTATION_PROOF_TYPES = exports.EIP712_ATTESTATION_PROOF_TYPEHASH = exports.EIP712_DISPUTE_MANAGER_DOMAIN_SALT = void 0; exports.generateAttestationData = generateAttestationData; exports.verifyAttestationSigner = verifyAttestationSigner; const ethers_1 = require("ethers"); exports.EIP712_DISPUTE_MANAGER_DOMAIN_SALT = ethers_1.ethers.getBytes('0xa070ffb1cd7409649bf77822cce74495468e06dbfaef09556838bf188679b9c2'); exports.EIP712_ATTESTATION_PROOF_TYPEHASH = (0, ethers_1.id)('Receipt(bytes32 requestCID,bytes32 responseCID,bytes32 subgraphDeploymentID)'); exports.EIP712_ATTESTATION_PROOF_TYPES = { Receipt: [ { name: 'requestCID', type: 'bytes32' }, { name: 'responseCID', type: 'bytes32' }, { name: 'subgraphDeploymentID', type: 'bytes32' }, ], }; /** * Creates an attestation data for a given request and response CIDs. * @param requestCID The request CID. * @param responseCID The response CID. * @param signerPrivateKey The private key of the signer. * @param subgraphDeploymentId The subgraph deployment ID. * @param disputeManagerAddress The address of the dispute manager contract. * @param chainId The chain ID. * @returns The attestation data. */ async function generateAttestationData(requestCID, responseCID, subgraphDeploymentId, signerPrivateKey, disputeManagerAddress, chainId) { // Create the domain for the EIP712 signature const domain = { name: 'Graph Protocol', version: '0', chainId: chainId, verifyingContract: disputeManagerAddress, salt: exports.EIP712_DISPUTE_MANAGER_DOMAIN_SALT, }; // Create receipt struct const receipt = { requestCID: ethers_1.ethers.hexlify(ethers_1.ethers.getBytes(requestCID)), responseCID: ethers_1.ethers.hexlify(ethers_1.ethers.getBytes(responseCID)), subgraphDeploymentID: ethers_1.ethers.hexlify(ethers_1.ethers.getBytes(subgraphDeploymentId)), }; // Sign the receipt hash with the allocation private key const signer = new ethers_1.Wallet(signerPrivateKey); const signature = await signer.signTypedData(domain, exports.EIP712_ATTESTATION_PROOF_TYPES, receipt); const sig = ethers_1.ethers.Signature.from(signature); // Concatenate the bytes directly return ethers_1.ethers.concat([ ethers_1.ethers.getBytes(requestCID), ethers_1.ethers.getBytes(responseCID), ethers_1.ethers.getBytes(subgraphDeploymentId), ethers_1.ethers.getBytes(sig.r), ethers_1.ethers.getBytes(sig.s), new Uint8Array([sig.v]), ]); } /** * Verifies the signer of an attestation data. * @param attestationData The attestation data to verify. * @param expectedSigner The expected signer address. * @param disputeManagerAddress The address of the dispute manager contract. * @param chainId The chain ID. * @returns True if the signer matches the expected signer, false otherwise. */ function verifyAttestationSigner(attestationData, expectedSigner, disputeManagerAddress, chainId) { try { // Extract components from attestation data const data = ethers_1.ethers.getBytes(attestationData); // Each CID and deployment ID is 32 bytes const requestCID = ethers_1.ethers.hexlify(data.slice(0, 32)); const responseCID = ethers_1.ethers.hexlify(data.slice(32, 64)); const subgraphDeploymentID = ethers_1.ethers.hexlify(data.slice(64, 96)); // Extract signature components const r = ethers_1.ethers.hexlify(data.slice(96, 128)); const s = ethers_1.ethers.hexlify(data.slice(128, 160)); const v = data[160]; // Create the domain for the EIP712 signature const domain = { name: 'Graph Protocol', version: '0', chainId: chainId, verifyingContract: disputeManagerAddress, salt: exports.EIP712_DISPUTE_MANAGER_DOMAIN_SALT, }; // Create receipt struct const receipt = { requestCID, responseCID, subgraphDeploymentID, }; // Recover the signer address const signature = ethers_1.ethers.Signature.from({ r, s, v }); const recoveredAddress = ethers_1.ethers.verifyTypedData(domain, exports.EIP712_ATTESTATION_PROOF_TYPES, receipt, signature); console.log(`recoveredAddress: ${recoveredAddress}`); // Compare addresses (case-insensitive) return recoveredAddress.toLowerCase() === expectedSigner.toLowerCase(); } catch (error) { console.log(error); return false; } } //# sourceMappingURL=attestations.js.map