"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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ERC1271ContractFacade = exports.SUCCESS_MAGIC_VALUE = void 0;
const utils_1 = require("@streamr/utils");
const tsyringe_1 = require("tsyringe");
const RpcProviderSource_1 = require("../RpcProviderSource");
const IERC1271Abi_json_1 = __importDefault(require("../ethereumArtifacts/IERC1271Abi.json"));
const Mapping_1 = require("../utils/Mapping");
const ContractFactory_1 = require("./ContractFactory");
exports.SUCCESS_MAGIC_VALUE = '0x1626ba7e'; // Magic value for success as defined by ERC-1271
const CACHE_TTL = 10 * 60 * 1000; // 10 minutes
const signingUtil = new utils_1.EcdsaSecp256k1Evm();
function formCacheKey(contractAddress, signerUserId) {
    return `${contractAddress}_${signerUserId}`;
}
let ERC1271ContractFacade = class ERC1271ContractFacade {
    contractsByAddress;
    publisherCache = new utils_1.MapWithTtl(() => CACHE_TTL);
    constructor(contractFactory, rpcProviderSource) {
        this.contractsByAddress = (0, Mapping_1.createLazyMap)({
            valueFactory: async (address) => {
                return contractFactory.createReadContract(address, IERC1271Abi_json_1.default, rpcProviderSource.getProvider(), 'erc1271Contract');
            }
        });
    }
    async isValidSignature(contractAddress, payload, signature) {
        const recoveredSignerUserId = (0, utils_1.toUserId)(signingUtil.recoverSignerUserId(signature, payload));
        const cacheKey = formCacheKey(contractAddress, recoveredSignerUserId);
        const cachedValue = this.publisherCache.get(cacheKey);
        if (cachedValue !== undefined) {
            return cachedValue;
        }
        else {
            const contract = await this.contractsByAddress.get(contractAddress);
            const result = await contract.isValidSignature(signingUtil.keccakHash(payload), signature);
            const isValid = result === exports.SUCCESS_MAGIC_VALUE;
            this.publisherCache.set(cacheKey, isValid);
            return isValid;
        }
    }
};
exports.ERC1271ContractFacade = ERC1271ContractFacade;
exports.ERC1271ContractFacade = ERC1271ContractFacade = __decorate([
    (0, tsyringe_1.scoped)(tsyringe_1.Lifecycle.ContainerScoped),
    __metadata("design:paramtypes", [ContractFactory_1.ContractFactory,
        RpcProviderSource_1.RpcProviderSource])
], ERC1271ContractFacade);
//# sourceMappingURL=ERC1271ContractFacade.js.map