"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.StorageNodeRegistry = void 0;
const utils_1 = require("@streamr/utils");
const tsyringe_1 = require("tsyringe");
const Authentication_1 = require("../Authentication");
const Config_1 = require("../Config");
const ContractFactory_1 = require("../ContractFactory");
const Ethereum_1 = require("../Ethereum");
const StreamrClientError_1 = require("../StreamrClientError");
const NodeRegistryAbi_json_1 = __importDefault(require("../ethereumArtifacts/NodeRegistryAbi.json"));
const contract_1 = require("../utils/contract");
/**
 * Store a mapping of storage node addresses <-> storage node URLs
 */
let StorageNodeRegistry = class StorageNodeRegistry {
    nodeRegistryContract;
    nodeRegistryContractsReadonly;
    contractFactory;
    config;
    authentication;
    constructor(contractFactory, config, authentication) {
        this.contractFactory = contractFactory;
        this.config = config;
        this.authentication = authentication;
        this.nodeRegistryContractsReadonly = (0, Ethereum_1.getStreamRegistryChainProviders)(config).map((provider) => {
            return this.contractFactory.createReadContract((0, utils_1.toEthereumAddress)(this.config.contracts.storageNodeRegistryChainAddress), NodeRegistryAbi_json_1.default, provider, 'storageNodeRegistry');
        });
    }
    async connectToContract() {
        if (!this.nodeRegistryContract) {
            const chainSigner = await this.authentication.getStreamRegistryChainSigner();
            this.nodeRegistryContract = this.contractFactory.createWriteContract((0, utils_1.toEthereumAddress)(this.config.contracts.storageNodeRegistryChainAddress), NodeRegistryAbi_json_1.default, chainSigner, 'storageNodeRegistry');
        }
    }
    async setStorageNodeMetadata(metadata) {
        await this.connectToContract();
        const ethersOverrides = (0, Ethereum_1.getEthersOverrides)(this.config);
        if (metadata !== undefined) {
            await (0, contract_1.waitForTx)(this.nodeRegistryContract.createOrUpdateNodeSelf(JSON.stringify(metadata), ethersOverrides));
        }
        else {
            await (0, contract_1.waitForTx)(this.nodeRegistryContract.removeNodeSelf(ethersOverrides));
        }
    }
    async getStorageNodeMetadata(nodeAddress) {
        const [resultNodeAddress, metadata] = await (0, contract_1.queryAllReadonlyContracts)((contract) => {
            return contract.getNode(nodeAddress);
        }, this.nodeRegistryContractsReadonly);
        const NODE_NOT_FOUND = '0x0000000000000000000000000000000000000000';
        if (resultNodeAddress !== NODE_NOT_FOUND) {
            return JSON.parse(metadata);
        }
        else {
            throw new StreamrClientError_1.StreamrClientError('Node not found, id: ' + nodeAddress, 'NODE_NOT_FOUND');
        }
    }
};
exports.StorageNodeRegistry = StorageNodeRegistry;
exports.StorageNodeRegistry = StorageNodeRegistry = __decorate([
    (0, tsyringe_1.scoped)(tsyringe_1.Lifecycle.ContainerScoped),
    __param(1, (0, tsyringe_1.inject)(Config_1.ConfigInjectionToken)),
    __param(2, (0, tsyringe_1.inject)(Authentication_1.AuthenticationInjectionToken)),
    __metadata("design:paramtypes", [ContractFactory_1.ContractFactory, Object, Object])
], StorageNodeRegistry);
//# sourceMappingURL=StorageNodeRegistry.js.map