"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addressOrBase58ToBytes32 = exports.fromBytes32ToAddress = exports.fromBase58ToBytes32 = exports.publicKeyToEvmAddress = exports.evmAddressToPublicKey = void 0;
exports.intToU8Array32 = intToU8Array32;
exports.u8Array32ToInt = u8Array32ToInt;
exports.u8Array32ToBigNumber = u8Array32ToBigNumber;
exports.strPublicKey = strPublicKey;
var anchor_1 = require("@coral-xyz/anchor");
var web3_js_1 = require("@solana/web3.js");
var ethers_1 = require("ethers");
function intToU8Array32(num) {
    var bigIntValue = BigInt(num instanceof anchor_1.BN ? num.toString() : num);
    if (bigIntValue < 0)
        throw new Error("Input must be a non-negative integer or BN");
    var hexString = bigIntValue.toString(16).padStart(64, "0");
    var u8Array = Array.from(Buffer.from(hexString, "hex"));
    return u8Array;
}
function u8Array32ToInt(u8Array) {
    var isValidArray = function (arr) { return Array.isArray(arr) && arr.every(Number.isInteger); };
    if ((u8Array instanceof Uint8Array || isValidArray(u8Array)) && u8Array.length === 32) {
        return Array.from(u8Array).reduce(function (num, byte) { return (num << BigInt(8)) | BigInt(byte); }, BigInt(0));
    }
    throw new Error("Input must be a Uint8Array or an array of 32 numbers.");
}
function u8Array32ToBigNumber(u8Array) {
    var isValidArray = function (arr) { return Array.isArray(arr) && arr.every(Number.isInteger); };
    if ((u8Array instanceof Uint8Array || isValidArray(u8Array)) && u8Array.length === 32) {
        var hexString = "0x" + Buffer.from(u8Array).toString("hex");
        return ethers_1.BigNumber.from(hexString);
    }
    throw new Error("Input must be a Uint8Array or an array of 32 numbers.");
}
function strPublicKey(publicKey) {
    return new web3_js_1.PublicKey(publicKey).toString();
}
var evmAddressToPublicKey = function (address) {
    var bytes32Address = "0x000000000000000000000000".concat(address.replace("0x", ""));
    return new web3_js_1.PublicKey(ethers_1.ethers.utils.arrayify(bytes32Address));
};
exports.evmAddressToPublicKey = evmAddressToPublicKey;
var publicKeyToEvmAddress = function (publicKey) {
    var pubKeyBuffer = typeof publicKey === "string" ? new web3_js_1.PublicKey(publicKey).toBuffer() : publicKey.toBuffer();
    var addressBuffer = pubKeyBuffer.slice(-20);
    return "0x".concat(addressBuffer.toString("hex"));
};
exports.publicKeyToEvmAddress = publicKeyToEvmAddress;
var fromBase58ToBytes32 = function (input) {
    var decodedBytes = anchor_1.utils.bytes.bs58.decode(input);
    return "0x" + Buffer.from(decodedBytes).toString("hex");
};
exports.fromBase58ToBytes32 = fromBase58ToBytes32;
var fromBytes32ToAddress = function (input) {
    var hexString = input.startsWith("0x") ? input.slice(2) : input;
    if (hexString.length !== 64) {
        throw new Error("Invalid bytes32 string");
    }
    var address = hexString.slice(-40);
    return "0x" + address;
};
exports.fromBytes32ToAddress = fromBytes32ToAddress;
var addressOrBase58ToBytes32 = function (input) {
    if (ethers_1.ethers.utils.isAddress(input)) {
        return ethers_1.ethers.utils.hexZeroPad(input, 32);
    }
    else if (ethers_1.ethers.utils.isHexString(input, 32)) {
        return input;
    }
    else {
        return (0, exports.fromBase58ToBytes32)(input);
    }
};
exports.addressOrBase58ToBytes32 = addressOrBase58ToBytes32;
//# sourceMappingURL=conversionUtils.js.map