"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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ERC1271ContractFacade = exports.createNewInstantiateContractsFn = exports.InstantiateERC1271ContractsToken = exports.SUCCESS_MAGIC_VALUE = void 0;
const Ethereum_1 = require("../Ethereum");
const utils_1 = require("@streamr/utils");
const IERC1271Abi_json_1 = __importDefault(require("../ethereumArtifacts/IERC1271Abi.json"));
const contract_1 = require("../utils/contract");
const Mapping_1 = require("../utils/Mapping");
const tsyringe_1 = require("tsyringe");
const utils_2 = require("@streamr/utils");
exports.SUCCESS_MAGIC_VALUE = '0x1626ba7e'; // Magic value for success as defined by ERC-1271
const CACHE_TTL = 10 * 60 * 1000; // 10 minutes
function formCacheKey(contractAddress, clientWalletAddress) {
    return `${contractAddress}_${clientWalletAddress}`;
}
exports.InstantiateERC1271ContractsToken = Symbol('InstantiateERC1271ContractsToken');
function createNewInstantiateContractsFn(contractFactory, config) {
    return (address) => (0, Ethereum_1.getStreamRegistryChainProviders)(config).map((provider) => {
        return contractFactory.createReadContract(address, IERC1271Abi_json_1.default, provider, 'erc1271Contract');
    });
}
exports.createNewInstantiateContractsFn = createNewInstantiateContractsFn;
let ERC1271ContractFacade = class ERC1271ContractFacade {
    contractsByAddress;
    publisherCache = new utils_1.MapWithTtl(() => CACHE_TTL);
    constructor(instantiateContracts) {
        this.contractsByAddress = new Mapping_1.Mapping(async (address) => {
            return instantiateContracts(address);
        });
    }
    async isValidSignature(contractAddress, payload, signature) {
        const clientWalletAddress = (0, utils_1.toEthereumAddress)((0, utils_2.recoverAddress)(signature, payload));
        const cacheKey = formCacheKey(contractAddress, clientWalletAddress);
        const cachedValue = this.publisherCache.get(cacheKey);
        if (cachedValue !== undefined) {
            return cachedValue;
        }
        else {
            const contracts = await this.contractsByAddress.get(contractAddress);
            const result = await (0, contract_1.queryAllReadonlyContracts)((contract) => {
                return contract.isValidSignature((0, utils_2.hash)(payload), signature);
            }, contracts);
            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),
    __param(0, (0, tsyringe_1.inject)(exports.InstantiateERC1271ContractsToken)),
    __metadata("design:paramtypes", [Function])
], ERC1271ContractFacade);
//# sourceMappingURL=ERC1271ContractFacade.js.map