"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isNewBlockTime = exports.calculateBlockTime = exports.isLocalHost = exports.isValidPeer = exports.ByteBuffer = exports.BigNumber = exports.Base58 = exports.isSupportedTransactionVersion = exports.maxVendorFieldLength = exports.numberToHex = exports.isGenesisTransaction = exports.isException = exports.isIdException = exports.formatSatoshi = void 0;
const constants_1 = require("../constants");
const managers_1 = require("../managers");
const base58_1 = require("./base58");
Object.defineProperty(exports, "Base58", { enumerable: true, get: function () { return base58_1.Base58; } });
const bignum_1 = require("./bignum");
Object.defineProperty(exports, "BigNumber", { enumerable: true, get: function () { return bignum_1.BigNumber; } });
const block_time_calculator_1 = require("./block-time-calculator");
Object.defineProperty(exports, "calculateBlockTime", { enumerable: true, get: function () { return block_time_calculator_1.calculateBlockTime; } });
Object.defineProperty(exports, "isNewBlockTime", { enumerable: true, get: function () { return block_time_calculator_1.isNewBlockTime; } });
const byte_buffer_1 = require("./byte-buffer");
Object.defineProperty(exports, "ByteBuffer", { enumerable: true, get: function () { return byte_buffer_1.ByteBuffer; } });
const is_valid_peer_1 = require("./is-valid-peer");
Object.defineProperty(exports, "isLocalHost", { enumerable: true, get: function () { return is_valid_peer_1.isLocalHost; } });
Object.defineProperty(exports, "isValidPeer", { enumerable: true, get: function () { return is_valid_peer_1.isValidPeer; } });
let genesisTransactions;
let whitelistedBlockAndTransactionIds;
let currentNetwork;
/**
 * Get human readable string from satoshis
 */
const formatSatoshi = (amount) => {
    const localeString = (+amount / constants_1.SATOSHI).toLocaleString("en", {
        minimumFractionDigits: 0,
        maximumFractionDigits: 8,
    });
    return `${localeString} ${managers_1.configManager.get("network.client.symbol")}`;
};
exports.formatSatoshi = formatSatoshi;
/**
 * Check if the given block or transaction id is an exception.
 */
const isIdException = (id) => {
    if (!id) {
        return false;
    }
    const network = managers_1.configManager.get("network.pubKeyHash");
    if (!whitelistedBlockAndTransactionIds || currentNetwork !== network) {
        currentNetwork = network;
        whitelistedBlockAndTransactionIds = [
            ...(managers_1.configManager.get("exceptions.blocks") || []),
            ...(managers_1.configManager.get("exceptions.transactions") || []),
        ].reduce((acc, curr) => Object.assign(acc, { [curr]: true }), {});
    }
    return !!whitelistedBlockAndTransactionIds[id];
};
exports.isIdException = isIdException;
const isException = (blockOrTransaction) => {
    if (typeof blockOrTransaction.id !== "string") {
        return false;
    }
    if (blockOrTransaction.id.length < 64) {
        // old block ids, we check that the transactions inside the block are correct
        const blockExceptionTxIds = (managers_1.configManager.get("exceptions.blocksTransactions") || {})[blockOrTransaction.id];
        const blockTransactions = blockOrTransaction.transactions || [];
        if (!blockExceptionTxIds || blockExceptionTxIds.length !== blockTransactions.length) {
            return false;
        }
        blockExceptionTxIds.sort();
        const blockToCheckTxIds = blockTransactions.map((tx) => tx.id).sort();
        for (let i = 0; i < blockExceptionTxIds.length; i++) {
            if (blockToCheckTxIds[i] !== blockExceptionTxIds[i]) {
                return false;
            }
        }
    }
    return exports.isIdException(blockOrTransaction.id);
};
exports.isException = isException;
const isGenesisTransaction = (id) => {
    const network = managers_1.configManager.get("network.pubKeyHash");
    if (!genesisTransactions || currentNetwork !== network) {
        currentNetwork = network;
        genesisTransactions = managers_1.configManager
            .get("genesisBlock.transactions")
            .reduce((acc, curr) => Object.assign(acc, { [curr.id]: true }), {});
    }
    return genesisTransactions[id];
};
exports.isGenesisTransaction = isGenesisTransaction;
const numberToHex = (num, padding = 2) => {
    const indexHex = Number(num).toString(16);
    return "0".repeat(padding - indexHex.length) + indexHex;
};
exports.numberToHex = numberToHex;
const maxVendorFieldLength = (height) => managers_1.configManager.getMilestone(height).vendorFieldLength;
exports.maxVendorFieldLength = maxVendorFieldLength;
const isSupportedTransactionVersion = (version) => {
    const aip11 = managers_1.configManager.getMilestone().aip11;
    if (aip11 && version !== 2) {
        return false;
    }
    if (!aip11 && version !== 1) {
        return false;
    }
    return true;
};
exports.isSupportedTransactionVersion = isSupportedTransactionVersion;
//# sourceMappingURL=index.js.map