"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.recoverAttestation = exports.decodeAttestation = exports.encodeAttestation = exports.createAttestation = exports.getDomainSeparator = void 0;
const ethers_1 = require("ethers");
const eip712 = __importStar(require("./eip712"));
const { defaultAbiCoder: abi, arrayify, concat, hexlify, splitSignature, joinSignature, } = ethers_1.utils;
const SIG_SIZE_BYTES = 161;
const RECEIPT_SIZE_BYTES = 96;
const RECEIPT_TYPE_HASH = eip712.typeHash('Receipt(bytes32 requestCID,bytes32 responseCID,bytes32 subgraphDeploymentID)');
const SALT = '0xa070ffb1cd7409649bf77822cce74495468e06dbfaef09556838bf188679b9c2';
const encodeReceipt = (receipt) => eip712.hashStruct(RECEIPT_TYPE_HASH, ['bytes32', 'bytes32', 'bytes32'], [receipt.requestCID, receipt.responseCID, receipt.subgraphDeploymentID]);
exports.getDomainSeparator = (chainId, disputeManagerAddress) => {
    const domainSeparator = eip712.domainSeparator({
        name: 'Graph Protocol',
        version: '0',
        chainId,
        verifyingContract: disputeManagerAddress,
        salt: SALT,
    });
    return domainSeparator;
};
exports.createAttestation = (signer, chainId, disputeManagerAddress, receipt) => __awaiter(void 0, void 0, void 0, function* () {
    const domainSeparator = exports.getDomainSeparator(chainId, disputeManagerAddress);
    const encodedReceipt = encodeReceipt(receipt);
    const message = eip712.encode(domainSeparator, encodedReceipt);
    const messageHash = ethers_1.utils.keccak256(message);
    const signingKey = new ethers_1.utils.SigningKey(signer);
    const { r, s, v } = signingKey.signDigest(messageHash);
    return {
        requestCID: receipt.requestCID,
        responseCID: receipt.responseCID,
        subgraphDeploymentID: receipt.subgraphDeploymentID,
        v,
        r,
        s,
    };
});
exports.encodeAttestation = (attestation) => {
    const data = arrayify(abi.encode(['bytes32', 'bytes32', 'bytes32'], [attestation.requestCID, attestation.responseCID, attestation.subgraphDeploymentID]));
    const sig = joinSignature(attestation);
    return hexlify(concat([data, sig]));
};
exports.decodeAttestation = (attestationData) => {
    const attestationBytes = arrayify(attestationData);
    if (attestationBytes.length !== SIG_SIZE_BYTES) {
        throw new Error('Invalid signature length');
    }
    const [requestCID, responseCID, subgraphDeploymentID] = abi.decode(['bytes32', 'bytes32', 'bytes32'], attestationBytes);
    const sig = splitSignature(attestationBytes.slice(RECEIPT_SIZE_BYTES, RECEIPT_SIZE_BYTES + SIG_SIZE_BYTES));
    return {
        requestCID,
        responseCID,
        subgraphDeploymentID,
        v: sig.v,
        r: sig.r,
        s: sig.s,
    };
};
exports.recoverAttestation = (chainId, disputeManagerAddress, attestation) => {
    const domainSeparator = exports.getDomainSeparator(chainId, disputeManagerAddress);
    const receipt = {
        requestCID: attestation.requestCID,
        responseCID: attestation.responseCID,
        subgraphDeploymentID: attestation.subgraphDeploymentID,
    };
    const encodedReceipt = encodeReceipt(receipt);
    const message = eip712.encode(domainSeparator, encodedReceipt);
    const messageHash = ethers_1.utils.keccak256(message);
    return ethers_1.utils.recoverAddress(messageHash, joinSignature({ r: attestation.r, s: attestation.s, v: attestation.v }));
};
//# sourceMappingURL=attestations.js.map