"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../constants");
const managers_1 = require("../managers");
const base58_1 = require("./base58");
exports.Base58 = base58_1.Base58;
const bignum_1 = require("./bignum");
exports.BigNumber = bignum_1.BigNumber;
const block_time_calculator_1 = require("./block-time-calculator");
exports.calculateBlockTime = block_time_calculator_1.calculateBlockTime;
exports.isNewBlockTime = block_time_calculator_1.isNewBlockTime;
const byte_buffer_1 = require("./byte-buffer");
exports.ByteBuffer = byte_buffer_1.ByteBuffer;
const is_valid_peer_1 = require("./is-valid-peer");
exports.isLocalHost = is_valid_peer_1.isLocalHost;
exports.isValidPeer = is_valid_peer_1.isValidPeer;
let genesisTransactions;
let whitelistedBlockAndTransactionIds;
let currentNetwork;
/**
 * Get human readable string from satoshis
 */
exports.formatSatoshi = (amount) => {
    const localeString = (+amount / constants_1.SATOSHI).toLocaleString("en", {
        minimumFractionDigits: 0,
        maximumFractionDigits: 8,
    });
    return `${localeString} ${managers_1.configManager.get("network.client.symbol")}`;
};
/**
 * Check if the given block or transaction id is an exception.
 */
exports.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.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.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.numberToHex = (num, padding = 2) => {
    const indexHex = Number(num).toString(16);
    return "0".repeat(padding - indexHex.length) + indexHex;
};
exports.maxVendorFieldLength = (height) => managers_1.configManager.getMilestone(height).vendorFieldLength;
exports.isSupportedTransactionVersion = (version) => {
    const aip11 = managers_1.configManager.getMilestone().aip11;
    if (aip11 && version !== 2) {
        return false;
    }
    if (!aip11 && version !== 1) {
        return false;
    }
    return true;
};
//# sourceMappingURL=index.js.map