"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.relayerRefundHashFn = exports.readUInt256BE = void 0;
exports.calculateRelayHashUint8Array = calculateRelayHashUint8Array;
exports.calculateRelayEventHashUint8Array = calculateRelayEventHashUint8Array;
exports.hashNonEmptyMessage = hashNonEmptyMessage;
exports.calculateRelayerRefundLeafHashUint8Array = calculateRelayerRefundLeafHashUint8Array;
exports.slowFillHashFn = slowFillHashFn;
var anchor_1 = require("@coral-xyz/anchor");
var ethers_1 = require("ethers");
var borsh_1 = require("borsh");
function calculateRelayHashUint8Array(relayData, chainId) {
    var contentToHash = Buffer.concat([
        relayData.depositor.toBuffer(),
        relayData.recipient.toBuffer(),
        relayData.exclusiveRelayer.toBuffer(),
        relayData.inputToken.toBuffer(),
        relayData.outputToken.toBuffer(),
        Buffer.from(relayData.inputAmount),
        relayData.outputAmount.toArrayLike(Buffer, "le", 8),
        relayData.originChainId.toArrayLike(Buffer, "le", 8),
        Buffer.from(relayData.depositId),
        new anchor_1.BN(relayData.fillDeadline).toArrayLike(Buffer, "le", 4),
        new anchor_1.BN(relayData.exclusivityDeadline).toArrayLike(Buffer, "le", 4),
        hashNonEmptyMessage(relayData.message),
        chainId.toArrayLike(Buffer, "le", 8),
    ]);
    var relayHash = ethers_1.ethers.utils.keccak256(contentToHash);
    var relayHashBuffer = Buffer.from(relayHash.slice(2), "hex");
    return new Uint8Array(relayHashBuffer);
}
function calculateRelayEventHashUint8Array(relayEventData, chainId) {
    var contentToHash = Buffer.concat([
        relayEventData.depositor.toBuffer(),
        relayEventData.recipient.toBuffer(),
        relayEventData.exclusiveRelayer.toBuffer(),
        relayEventData.inputToken.toBuffer(),
        relayEventData.outputToken.toBuffer(),
        Buffer.from(relayEventData.inputAmount),
        relayEventData.outputAmount.toArrayLike(Buffer, "le", 8),
        relayEventData.originChainId.toArrayLike(Buffer, "le", 8),
        Buffer.from(relayEventData.depositId),
        new anchor_1.BN(relayEventData.fillDeadline).toArrayLike(Buffer, "le", 4),
        new anchor_1.BN(relayEventData.exclusivityDeadline).toArrayLike(Buffer, "le", 4),
        Buffer.from(relayEventData.messageHash),
        chainId.toArrayLike(Buffer, "le", 8),
    ]);
    var relayHash = ethers_1.ethers.utils.keccak256(contentToHash);
    var relayHashBuffer = Buffer.from(relayHash.slice(2), "hex");
    return new Uint8Array(relayHashBuffer);
}
var readUInt256BE = function (buffer) {
    var result = BigInt(0);
    for (var i = 0; i < buffer.length; i++) {
        result = (result << BigInt(8)) + BigInt(buffer[i]);
    }
    return result;
};
exports.readUInt256BE = readUInt256BE;
function hashNonEmptyMessage(message) {
    if (message.length > 0) {
        var hash = ethers_1.ethers.utils.keccak256(message);
        return Uint8Array.from(Buffer.from(hash.slice(2), "hex"));
    }
    return new Uint8Array(32);
}
var RelayData = (function () {
    function RelayData(properties) {
        Object.assign(this, properties);
    }
    return RelayData;
}());
var relayDataSchema = new Map([
    [
        RelayData,
        {
            kind: "struct",
            fields: [
                ["amountToReturn", "u64"],
                ["chainId", "u64"],
                ["refundAmounts", ["u64"]],
                ["leafId", "u32"],
                ["mintPublicKey", [32]],
                ["refundAddresses", [[32]]],
            ],
        },
    ],
]);
function calculateRelayerRefundLeafHashUint8Array(relayData) {
    var refundAddresses = relayData.refundAddresses.map(function (address) { return address.toBuffer(); });
    var data = new RelayData({
        amountToReturn: relayData.amountToReturn,
        chainId: relayData.chainId,
        refundAmounts: relayData.refundAmounts,
        leafId: relayData.leafId,
        mintPublicKey: relayData.mintPublicKey.toBuffer(),
        refundAddresses: refundAddresses,
    });
    var serializedData = (0, borsh_1.serialize)(relayDataSchema, data);
    var contentToHash = Buffer.concat([Buffer.alloc(64, 0), serializedData]);
    return ethers_1.ethers.utils.keccak256(contentToHash);
}
var relayerRefundHashFn = function (input) {
    if (!input.isSolana) {
        var abiCoder = new ethers_1.ethers.utils.AbiCoder();
        var encodedData = abiCoder.encode([
            "tuple( uint256 amountToReturn, uint256 chainId, uint256[] refundAmounts, uint256 leafId, address l2TokenAddress, address[] refundAddresses)",
        ], [
            {
                leafId: input.leafId,
                chainId: input.chainId,
                amountToReturn: input.amountToReturn,
                l2TokenAddress: input.l2TokenAddress,
                refundAddresses: input.refundAddresses,
                refundAmounts: input.refundAmounts,
            },
        ]);
        return ethers_1.ethers.utils.keccak256(encodedData);
    }
    else {
        return calculateRelayerRefundLeafHashUint8Array(input);
    }
};
exports.relayerRefundHashFn = relayerRefundHashFn;
var SlowFillData = (function () {
    function SlowFillData(properties) {
        Object.assign(this, properties);
    }
    return SlowFillData;
}());
var slowFillDataSchema = new Map([
    [
        SlowFillData,
        {
            kind: "struct",
            fields: [
                ["depositor", [32]],
                ["recipient", [32]],
                ["exclusiveRelayer", [32]],
                ["inputToken", [32]],
                ["outputToken", [32]],
                ["inputAmount", [32]],
                ["outputAmount", "u64"],
                ["originChainId", "u64"],
                ["depositId", [32]],
                ["fillDeadline", "u32"],
                ["exclusivityDeadline", "u32"],
                ["message", ["u8"]],
                ["chainId", "u64"],
                ["updatedOutputAmount", "u64"],
            ],
        },
    ],
]);
function slowFillHashFn(slowFillLeaf) {
    var data = new SlowFillData({
        depositor: Uint8Array.from(slowFillLeaf.relayData.depositor.toBuffer()),
        recipient: Uint8Array.from(slowFillLeaf.relayData.recipient.toBuffer()),
        exclusiveRelayer: Uint8Array.from(slowFillLeaf.relayData.exclusiveRelayer.toBuffer()),
        inputToken: Uint8Array.from(slowFillLeaf.relayData.inputToken.toBuffer()),
        outputToken: Uint8Array.from(slowFillLeaf.relayData.outputToken.toBuffer()),
        inputAmount: slowFillLeaf.relayData.inputAmount,
        outputAmount: slowFillLeaf.relayData.outputAmount,
        originChainId: slowFillLeaf.relayData.originChainId,
        depositId: Uint8Array.from(Buffer.from(slowFillLeaf.relayData.depositId)),
        fillDeadline: slowFillLeaf.relayData.fillDeadline,
        exclusivityDeadline: slowFillLeaf.relayData.exclusivityDeadline,
        message: Uint8Array.from(slowFillLeaf.relayData.message),
        chainId: slowFillLeaf.chainId,
        updatedOutputAmount: slowFillLeaf.updatedOutputAmount,
    });
    var serializedData = (0, borsh_1.serialize)(slowFillDataSchema, data);
    var contentToHash = Buffer.concat([Buffer.alloc(64, 0), serializedData]);
    return ethers_1.ethers.utils.keccak256(contentToHash);
}
//# sourceMappingURL=relayHashUtils.js.map