// Copyright 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import { callUtilsMethod } from '../bindings';
import { Address, Ed25519Address, } from '../types';
/** Utils class for utils. */
var Utils = /** @class */ (function () {
    function Utils() {
    }
    /**
     * Generate a new mnemonic.
     */
    Utils.generateMnemonic = function () {
        return callUtilsMethod({
            name: 'generateMnemonic'
        });
    };
    /**
     * Convert a mnemonic to a hex encoded seed.
     *
     * @param mnemonic A mnemonic string.
     * @returns The seed as hex-encoded string.
     */
    Utils.mnemonicToHexSeed = function (mnemonic) {
        return callUtilsMethod({
            name: 'mnemonicToHexSeed',
            data: {
                mnemonic: mnemonic
            }
        });
    };
    /**
     * Compute the account ID from a given account output ID.
     *
     * @param outputId The output ID as hex-encoded string.
     * @returns The account ID.
     */
    Utils.computeAccountId = function (outputId) {
        return callUtilsMethod({
            name: 'blake2b256Hash',
            data: {
                bytes: outputId
            }
        });
    };
    /**
     * Compute the delegation ID from a given delegation output ID.
     *
     * @param outputId The output ID as hex-encoded string.
     * @returns The delegation ID.
     */
    Utils.computeDelegationId = function (outputId) {
        return callUtilsMethod({
            name: 'blake2b256Hash',
            data: {
                bytes: outputId
            }
        });
    };
    /**
     * Compute the Foundry ID.
     *
     * @param accountId The account ID associated with the Foundry.
     * @param serialNumber The serial number of the Foundry.
     * @param tokenSchemeType The Token scheme type. Currently only a simple scheme is supported.
     * @returns The Foundry ID.
     */
    Utils.computeFoundryId = function (accountId, serialNumber, tokenSchemeType) {
        return callUtilsMethod({
            name: 'computeFoundryId',
            data: {
                accountId: accountId,
                serialNumber: serialNumber,
                tokenSchemeType: tokenSchemeType
            }
        });
    };
    /**
     * Compute the NFT ID from the given NFT output ID.
     *
     * @param outputId The output ID as hex-encoded string.
     * @returns The NFT ID.
     */
    Utils.computeNftId = function (outputId) {
        return callUtilsMethod({
            name: 'blake2b256Hash',
            data: {
                bytes: outputId
            }
        });
    };
    /**
     * Compute the output ID from transaction ID and output index.
     *
     * @param transactionId The ID of the transaction.
     * @param outputIndex The index of the output.
     * @returns The output ID.
     */
    Utils.computeOutputId = function (id, index) {
        return callUtilsMethod({
            name: 'computeOutputId',
            data: {
                id: id,
                index: index
            }
        });
    };
    /**
     * Compute the transaction ID from an output ID.
     *
     * @param outputId The output ID.
     * @returns The transaction ID of the transaction which created the output.
     */
    Utils.transactionIdFromOutputId = function (outputId) {
        return outputId.slice(0, 74);
    };
    /**
     * Compute the output index from an output ID.
     *
     * @param outputId The output ID.
     * @returns The output index.
     */
    Utils.outputIndexFromOutputId = function (outputId) {
        var numberString = outputId.slice(-4);
        var chunks = [];
        for (var i = 0, charsLength = numberString.length; i < charsLength; i += 2) {
            chunks.push(numberString.substring(i, i + 2));
        }
        var separated = chunks.map(function (n) { return parseInt(n, 16); });
        var buf = Uint8Array.from(separated).buffer;
        var view = new DataView(buf);
        return view.getUint16(0, true);
    };
    /**
     * Compute the required storage deposit of an output.
     *
     * @param output The output.
     * @param storageScoreParameters Storage score of objects which take node resources.
     * @returns The required storage deposit.
     */
    Utils.computeMinimumOutputAmount = function (output, storageScoreParameters) {
        var minStorageDepositAmount = callUtilsMethod({
            name: 'computeMinimumOutputAmount',
            data: {
                output: output,
                storageScoreParameters: storageScoreParameters
            }
        });
        return BigInt(minStorageDepositAmount);
    };
    /**
     * Computes a tokenId from the account ID, serial number and token scheme type.
     * @param accountId The account that controls the foundry.
     * @param serialNumber The serial number of the foundry.
     * @param tokenSchemeType The tokenSchemeType of the foundry.
     * @returns The tokenId.
     */
    Utils.computeTokenId = function (accountId, serialNumber, tokenSchemeType) {
        return callUtilsMethod({
            name: 'computeTokenId',
            data: {
                accountId: accountId,
                serialNumber: serialNumber,
                tokenSchemeType: tokenSchemeType
            }
        });
    };
    /**
     * Parse a Bech32 address from a string.
     *
     * @param address A Bech32 address as string.
     * @returns A Bech32 address.
     */
    Utils.parseBech32Address = function (address) {
        var addr = callUtilsMethod({
            name: 'parseBech32Address',
            data: {
                address: address
            }
        });
        return Address.parse(addr);
    };
    /**
     * Compute the block ID (Blake2b256 hash of the block bytes) of a block.
     *
     * @param block A block.
     * @param protocolParameters The network protocol parameters.
     * @returns The corresponding block ID.
     */
    Utils.blockId = function (block, protocolParameters) {
        return callUtilsMethod({
            name: 'blockId',
            data: {
                block: block,
                protocolParameters: protocolParameters
            }
        });
    };
    /**
     * Compute the transaction ID (Blake2b256 hash of the provided transaction payload) of a transaction payload.
     *
     * @param payload A transaction payload.
     * @returns The transaction ID.
     */
    Utils.transactionId = function (payload) {
        return callUtilsMethod({
            name: 'transactionId',
            data: {
                payload: payload
            }
        });
    };
    /**
     * Converts an address to its bech32 representation.
     *
     * @param address An address.
     * @param bech32Hrp The Bech32 HRP (human readable part) to use.
     * @returns The Bech32-encoded address string.
     */
    Utils.addressToBech32 = function (address, bech32Hrp) {
        return callUtilsMethod({
            name: 'addressToBech32',
            data: {
                address: address,
                bech32Hrp: bech32Hrp
            }
        });
    };
    /**
     * Hashes a hex encoded public key with Blake2b256.
     *
     * @param hex The hexadecimal string representation of a public key.
     * @returns The Ed25519 address with the hashed public key.
     */
    Utils.publicKeyHash = function (hex) {
        return new Ed25519Address(callUtilsMethod({
            name: 'blake2b256Hash',
            data: {
                bytes: hex
            }
        }));
    };
    /**
     * Checks whether an address string is a valid Bech32-encoded address.
     *
     * @param address An address string.
     */
    Utils.isAddressValid = function (address) {
        return callUtilsMethod({
            name: 'isAddressValid',
            data: {
                address: address
            }
        });
    };
    /**
     * Compute the hash of an instance of ProtocolParameters.
     *
     * @param protocolParameters A ProtocolParameters instance.
     * @returns The hash of the protocol parameters as a hex-encoded string.
     */
    Utils.protocolParametersHash = function (protocolParameters) {
        return callUtilsMethod({
            name: 'protocolParametersHash',
            data: {
                protocolParameters: protocolParameters
            }
        });
    };
    /**
     * Compute the signing hash of a transaction.
     *
     * @param transaction A transaction.
     * @returns The signing hash of the transaction as a hex-encoded string.
     */
    Utils.transactionSigningHash = function (transaction) {
        return callUtilsMethod({
            name: 'transactionSigningHash',
            data: {
                transaction: transaction
            }
        });
    };
    /**
     * Verify an Ed25519 signature against a message.
     *
     * @param signature An Ed25519 signature.
     * @param message A hex-encoded message.
     */
    Utils.verifyEd25519Signature = function (signature, message) {
        return callUtilsMethod({
            name: 'verifyEd25519Signature',
            data: {
                signature: signature,
                message: message
            }
        });
    };
    /**
     * Verify a Secp256k1Ecdsa signature against a message.
     * @param publicKey A hex-encoded public key.
     * @param signature A hex-encoded signature.
     * @param message A hex-encoded message.
     */
    Utils.verifySecp256k1EcdsaSignature = function (publicKey, signature, message) {
        return callUtilsMethod({
            name: 'verifySecp256k1EcdsaSignature',
            data: {
                publicKey: publicKey,
                signature: signature,
                message: message
            }
        });
    };
    /**
     * Verify if a mnemonic is a valid BIP39 mnemonic.
     *
     * @param mnemonic A mnemonic string.
     */
    Utils.verifyMnemonic = function (mnemonic) {
        return callUtilsMethod({
            name: 'verifyMnemonic',
            data: { mnemonic: mnemonic }
        });
    };
    /**
     * Computes a slotIndex from a block, transaction or slotCommitment Id.
     * @param id The block, transaction or slotCommitment Id.
     * @returns The slotIndex.
     */
    Utils.computeSlotIndex = function (id) {
        var numberString = id.slice(-8);
        var chunks = [];
        for (var i = 0, charsLength = numberString.length; i < charsLength; i += 2) {
            chunks.push(numberString.substring(i, i + 2));
        }
        var separated = chunks.map(function (n) { return parseInt(n, 16); });
        var buf = Uint8Array.from(separated).buffer;
        var view = new DataView(buf);
        return view.getUint32(0, true);
    };
    /**
     * Derives the `SlotCommitmentId` of the `SlotCommitment`.
     */
    Utils.computeSlotCommitmentId = function (slotCommitment) {
        return callUtilsMethod({
            name: 'computeSlotCommitmentId',
            data: {
                slotCommitment: {
                    protocolVersion: slotCommitment.protocolVersion,
                    slot: slotCommitment.slot,
                    previousCommitmentId: slotCommitment.previousCommitmentId,
                    rootsId: slotCommitment.rootsId,
                    cumulativeWeight: slotCommitment.cumulativeWeight.toString(10),
                    referenceManaCost: slotCommitment.referenceManaCost.toString(10)
                }
            }
        });
    };
    /**
     * Returns the hex representation of the serialized output bytes.
     *
     * @param output The output.
     * @returns The hex representation of the serialized output bytes.
     */
    Utils.outputHexBytes = function (output) {
        var hexBytes = callUtilsMethod({
            name: 'outputHexBytes',
            data: {
                output: output
            }
        });
        return hexBytes;
    };
    /**
     * Verifies the semantic of a transaction.
     *
     * @param transaction The transaction payload.
     * @param inputs The inputs data.
     * @param protocolParameters The protocol parameters.
     * @param unlocks The unlocks.
     * @param manaRewards The total mana rewards claimed in the transaction.
     *
     * @returns void.
     */
    Utils.verifyTransactionSemantic = function (transaction, inputs, protocolParameters, unlocks, manaRewards) {
        return callUtilsMethod({
            name: 'verifyTransactionSemantic',
            data: {
                transaction: transaction,
                inputs: inputs,
                protocolParameters: protocolParameters,
                unlocks: unlocks,
                manaRewards: manaRewards
            }
        });
    };
    /**
     * Applies mana decay to the given mana.
     *
     * @param mana The mana to decay.
     * @param slotIndexCreated The slot index at which the provided mana starts to decay.
     * @param slotIndexTarget The slot index at which the provided mana stops to decay.
     * @param protocolParameters The ProtocolParameters.
     *
     * @returns The decayed mana.
     */
    Utils.manaWithDecay = function (mana, slotIndexCreated, slotIndexTarget, protocolParameters) {
        var decayedMana = callUtilsMethod({
            name: 'manaWithDecay',
            data: {
                mana: mana.toString(10),
                slotIndexCreated: slotIndexCreated,
                slotIndexTarget: slotIndexTarget,
                protocolParameters: protocolParameters
            }
        });
        return BigInt(decayedMana);
    };
    /**
     * Calculates the potential mana that is generated by holding `amount` tokens from `slotIndexCreated` to
     * `slotIndexTarget` and applies the decay to the result.
     *
     * @param amount The amount that generates mana.
     * @param slotIndexCreated The slot index at which the provided amount starts to generate mana.
     * @param slotIndexTarget The slot index at which the provided mana stops to generate mana.
     * @param protocolParameters The ProtocolParameters.
     *
     * @returns The decayed potential mana.
     */
    Utils.generateManaWithDecay = function (amount, slotIndexCreated, slotIndexTarget, protocolParameters) {
        var decayedMana = callUtilsMethod({
            name: 'generateManaWithDecay',
            data: {
                amount: amount.toString(10),
                slotIndexCreated: slotIndexCreated,
                slotIndexTarget: slotIndexTarget,
                protocolParameters: protocolParameters
            }
        });
        return BigInt(decayedMana);
    };
    /**
     * Applies mana decay to the output mana and calculates the potential mana that is generated.
     *
     * @param output The output.
     * @param slotIndexCreated The slot index at which the provided output was created.
     * @param slotIndexTarget The slot index for which to calculate the mana values.
     * @param protocolParameters The ProtocolParameters.
     *
     * @returns The decayed stored and potential mana.
     */
    Utils.outputManaWithDecay = function (output, slotIndexCreated, slotIndexTarget, protocolParameters) {
        var decayedMana = callUtilsMethod({
            name: 'outputManaWithDecay',
            data: {
                output: output,
                slotIndexCreated: slotIndexCreated,
                slotIndexTarget: slotIndexTarget,
                protocolParameters: protocolParameters
            }
        });
        return {
            stored: BigInt(decayedMana.stored),
            potential: BigInt(decayedMana.potential)
        };
    };
    /**
     * Verifies the syntax of a transaction.
     *
     * @param transaction The transaction payload.
     * @param protocolParameters The protocol parameters used for the validation.
     * @returns void.
     */
    Utils.verifyTransactionSyntax = function (transaction, protocolParameters) {
        return callUtilsMethod({
            name: 'verifyTransactionSyntax',
            data: {
                transaction: transaction,
                protocolParameters: protocolParameters
            }
        });
    };
    /**
     * Returns the serialized bytes of a block.
     *
     * @param block The block.
     * @returns The block bytes.
     */
    Utils.blockBytes = function (block) {
        var blockBytes = callUtilsMethod({
            name: 'blockBytes',
            data: {
                block: block
            }
        });
        return new Uint8Array(blockBytes);
    };
    Utils.iotaMainnetProtocolParameters = function () {
        var params = callUtilsMethod({
            name: 'iotaMainnetProtocolParameters'
        });
        return params;
    };
    Utils.shimmerMainnetProtocolParameters = function () {
        var params = callUtilsMethod({
            name: 'shimmerMainnetProtocolParameters'
        });
        return params;
    };
    return Utils;
}());
export { Utils };
//# sourceMappingURL=utils.js.map