"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SignatureValidator = void 0;
const utils_1 = require("@streamr/utils");
const tsyringe_1 = require("tsyringe");
const ERC1271ContractFacade_1 = require("../contracts/ERC1271ContractFacade");
const StreamrClientError_1 = require("../StreamrClientError");
const createLegacySignaturePayload_1 = require("./createLegacySignaturePayload");
const createSignaturePayload_1 = require("./createSignaturePayload");
const trackerless_network_1 = require("@streamr/trackerless-network");
const IdentityMapping_1 = require("../identity/IdentityMapping");
// Lookup structure SignatureType -> SigningUtil
const signingUtilBySignatureType = Object.fromEntries(Object.values(IdentityMapping_1.IdentityMapping).map((config) => [config.signatureType, config.signingUtil]));
let SignatureValidator = class SignatureValidator {
    erc1271ContractFacade;
    constructor(erc1271ContractFacade) {
        this.erc1271ContractFacade = erc1271ContractFacade;
    }
    /**
     * Checks that the signature in the given StreamMessage is cryptographically valid.
     * Resolves if valid, rejects otherwise.
     */
    async assertSignatureIsValid(streamMessage) {
        let success;
        try {
            success = await this.validate(streamMessage);
        }
        catch (err) {
            // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
            throw new StreamrClientError_1.StreamrClientError(`An error occurred during address recovery from signature: ${err}`, 'INVALID_SIGNATURE', streamMessage);
        }
        if (!success) {
            throw new StreamrClientError_1.StreamrClientError('Signature validation failed', 'INVALID_SIGNATURE', streamMessage);
        }
    }
    async validate(streamMessage) {
        const signingUtil = signingUtilBySignatureType[streamMessage.signatureType];
        // Common case
        if (signingUtil) {
            return signingUtil.verifySignature((0, utils_1.toUserIdRaw)(streamMessage.getPublisherId()), (0, createSignaturePayload_1.createSignaturePayload)(streamMessage), streamMessage.signature);
        }
        // Special handling: different payload computation, same SigningUtil
        if (streamMessage.signatureType === trackerless_network_1.SignatureType.ECDSA_SECP256K1_LEGACY) {
            return utils_1.ECDSA_SECP256K1_EVM.verifySignature(
            // publisherId is hex encoded Ethereum address string
            (0, utils_1.toUserIdRaw)(streamMessage.getPublisherId()), (0, createLegacySignaturePayload_1.createLegacySignaturePayload)(streamMessage), streamMessage.signature);
        }
        // Special handling: check signature with ERC-1271 contract facade
        if (streamMessage.signatureType === trackerless_network_1.SignatureType.ERC_1271) {
            return this.erc1271ContractFacade.isValidSignature((0, utils_1.toEthereumAddress)(streamMessage.getPublisherId()), (0, createSignaturePayload_1.createSignaturePayload)(streamMessage), streamMessage.signature);
        }
        throw new Error(`Cannot validate message signature, unsupported signatureType: "${streamMessage.signatureType}"`);
    }
};
exports.SignatureValidator = SignatureValidator;
exports.SignatureValidator = SignatureValidator = __decorate([
    (0, tsyringe_1.scoped)(tsyringe_1.Lifecycle.ContainerScoped),
    __metadata("design:paramtypes", [ERC1271ContractFacade_1.ERC1271ContractFacade])
], SignatureValidator);
//# sourceMappingURL=SignatureValidator.js.map