"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.textToUint8Array = exports.textToHexString = exports.chunks = exports.uint8ArrayToNative = exports.nativeToHexString = exports.hexToNativeAssetString = exports.hexToNativeString = exports.hexToUint8Array = exports.uint8ArrayToHex = exports.nativeTerraHexToDenom = exports.isHexNativeTerra = exports.isEVMChain = void 0;
var bytes_1 = require("@ethersproject/bytes");
var web3_js_1 = require("@solana/web3.js");
var utils_1 = require("ethers/lib/utils");
var algorand_1 = require("../algorand");
var terra_1 = require("../terra");
var consts_1 = require("./consts");
var isEVMChain = function (chainId) {
    return (chainId === consts_1.CHAIN_ID_ETH ||
        chainId === consts_1.CHAIN_ID_BSC ||
        chainId === consts_1.CHAIN_ID_ETHEREUM_ROPSTEN ||
        chainId === consts_1.CHAIN_ID_AVAX ||
        chainId === consts_1.CHAIN_ID_POLYGON ||
        chainId === consts_1.CHAIN_ID_OASIS ||
        chainId === consts_1.CHAIN_ID_AURORA ||
        chainId === consts_1.CHAIN_ID_FANTOM ||
        chainId === consts_1.CHAIN_ID_KARURA ||
        chainId === consts_1.CHAIN_ID_ACALA ||
        chainId === consts_1.CHAIN_ID_KLAYTN ||
        chainId === consts_1.CHAIN_ID_CELO);
};
exports.isEVMChain = isEVMChain;
var isHexNativeTerra = function (h) { return h.startsWith("01"); };
exports.isHexNativeTerra = isHexNativeTerra;
var nativeTerraHexToDenom = function (h) {
    return Buffer.from(utils_1.stripZeros(exports.hexToUint8Array(h.substr(2)))).toString("ascii");
};
exports.nativeTerraHexToDenom = nativeTerraHexToDenom;
var uint8ArrayToHex = function (a) {
    return Buffer.from(a).toString("hex");
};
exports.uint8ArrayToHex = uint8ArrayToHex;
var hexToUint8Array = function (h) {
    return new Uint8Array(Buffer.from(h, "hex"));
};
exports.hexToUint8Array = hexToUint8Array;
var hexToNativeString = function (h, c) {
    try {
        return !h
            ? undefined
            : c === consts_1.CHAIN_ID_SOLANA
                ? new web3_js_1.PublicKey(exports.hexToUint8Array(h)).toString()
                : exports.isEVMChain(c)
                    ? utils_1.hexZeroPad(utils_1.hexValue(exports.hexToUint8Array(h)), 20)
                    : c === consts_1.CHAIN_ID_TERRA
                        ? exports.isHexNativeTerra(h)
                            ? exports.nativeTerraHexToDenom(h)
                            : terra_1.humanAddress(exports.hexToUint8Array(h.substr(24))) // terra expects 20 bytes, not 32
                        : c === consts_1.CHAIN_ID_ALGORAND
                            ? algorand_1.hexToNativeStringAlgorand(h)
                            : h;
    }
    catch (e) { }
    return undefined;
};
exports.hexToNativeString = hexToNativeString;
var hexToNativeAssetString = function (h, c) {
    try {
        return !h
            ? undefined
            : // Algorand assets are represented by their asset ids, not an address
                c === consts_1.CHAIN_ID_ALGORAND
                    ? algorand_1.hexToNativeAssetStringAlgorand(h)
                    : exports.hexToNativeString(h, c);
    }
    catch (e) {
        return undefined;
    }
};
exports.hexToNativeAssetString = hexToNativeAssetString;
var nativeToHexString = function (address, chain) {
    if (!address || !chain) {
        return null;
    }
    if (exports.isEVMChain(chain)) {
        return exports.uint8ArrayToHex(bytes_1.zeroPad(bytes_1.arrayify(address), 32));
    }
    else if (chain === consts_1.CHAIN_ID_SOLANA) {
        return exports.uint8ArrayToHex(bytes_1.zeroPad(new web3_js_1.PublicKey(address).toBytes(), 32));
    }
    else if (chain === consts_1.CHAIN_ID_TERRA) {
        if (terra_1.isNativeDenom(address)) {
            return ("01" +
                exports.uint8ArrayToHex(bytes_1.zeroPad(new Uint8Array(Buffer.from(address, "ascii")), 31)));
        }
        else {
            return exports.uint8ArrayToHex(bytes_1.zeroPad(terra_1.canonicalAddress(address), 32));
        }
    }
    else if (chain === consts_1.CHAIN_ID_ALGORAND) {
        return algorand_1.nativeStringToHexAlgorand(address);
    }
    else {
        return null;
    }
};
exports.nativeToHexString = nativeToHexString;
var uint8ArrayToNative = function (a, chainId) {
    return exports.hexToNativeString(exports.uint8ArrayToHex(a), chainId);
};
exports.uint8ArrayToNative = uint8ArrayToNative;
function chunks(array, size) {
    return Array.apply(0, new Array(Math.ceil(array.length / size))).map(function (_, index) { return array.slice(index * size, (index + 1) * size); });
}
exports.chunks = chunks;
function textToHexString(name) {
    return Buffer.from(name, "binary").toString("hex");
}
exports.textToHexString = textToHexString;
function textToUint8Array(name) {
    return new Uint8Array(Buffer.from(name, "binary"));
}
exports.textToUint8Array = textToUint8Array;
