"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Web3Proxy = Web3Proxy;
const request_1 = __importDefault(require("request"));
const logger_1 = __importDefault(require("../../logger"));
const config_1 = require("../../services/config");
function Web3Proxy(req, res) {
    const { chain, network } = req.params;
    const chainConfig = config_1.Config.chainConfig({ chain, network });
    const provider = chainConfig.provider || (chainConfig.providers && chainConfig.providers[0]);
    if (provider && chainConfig.publicWeb3) {
        const { host, port } = provider;
        const url = `http://${host}:${port}`;
        let requestStream;
        if (req.body.jsonrpc) {
            const options = {
                uri: url,
                headers: { 'content-type': 'application/x-www-form-urlencoded' },
                method: req.method,
                body: JSON.stringify(req.body),
                json: true
            };
            requestStream = (0, request_1.default)(options);
        }
        else {
            requestStream = req.pipe((0, request_1.default)(url));
        }
        requestStream
            .on('error', (err) => {
            logger_1.default.error('Error streaming wallet utxos: %o', err.stack || err.message || err);
            res.status(500).send('An Error Has Occurred');
        })
            .pipe(res);
    }
    else {
        res.status(500).send(`This node is not configured with a web3 connection for ${chain} ${network} `);
    }
}
//# sourceMappingURL=index.js.map