"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 StreamMessage_1 = require("../protocol/StreamMessage");
const StreamrClientError_1 = require("../StreamrClientError");
const createLegacySignaturePayload_1 = require("./createLegacySignaturePayload");
const createSignaturePayload_1 = require("./createSignaturePayload");
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) {
        switch (streamMessage.signatureType) {
            case StreamMessage_1.SignatureType.LEGACY_SECP256K1:
                return (0, utils_1.verifySignature)((0, utils_1.toUserIdRaw)(streamMessage.getPublisherId()), (0, createLegacySignaturePayload_1.createLegacySignaturePayload)(streamMessage), streamMessage.signature);
            case StreamMessage_1.SignatureType.SECP256K1:
                return (0, utils_1.verifySignature)((0, utils_1.toUserIdRaw)(streamMessage.getPublisherId()), (0, createSignaturePayload_1.createSignaturePayload)(streamMessage), streamMessage.signature);
            case StreamMessage_1.SignatureType.ERC_1271:
                return this.erc1271ContractFacade.isValidSignature((0, utils_1.toEthereumAddress)(streamMessage.getPublisherId()), (0, createSignaturePayload_1.createSignaturePayload)(streamMessage), streamMessage.signature);
            default:
                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