// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

//! The Application Binary Interface (ABI) for the Dusk network.

pub use piecrust_uplink::{
    ARGBUF_LEN, CONTRACT_ID_BYTES, ContractError, ContractId, Event,
    StandardBufSerializer,
};

#[cfg(feature = "abi")]
pub use self::host_queries::*;

/// Enum storing the metadata identifiers.
pub enum Metadata {}

impl Metadata {
    /// The chain-id of the network.
    pub const CHAIN_ID: &'static str = "chain_id";
    /// The current block-height.
    pub const BLOCK_HEIGHT: &'static str = "block_height";
    /// The sender of the transaction, if the transaction is public.
    pub const PUBLIC_SENDER: &'static str = "public_sender";
}

/// Enum storing the available host-queries.
pub enum Query {}

impl Query {
    /// Host-function name to compute the hash of some input-data.
    pub const HASH: &'static str = "hash";
    /// Host-function name to compute the poseidon-hash of some input-data.
    pub const POSEIDON_HASH: &'static str = "poseidon_hash";
    /// Host-function name to verify a plonk-proof.
    pub const VERIFY_PLONK: &'static str = "verify_plonk";
    /// Host-function name to verify a groth16-bn254 proof.
    pub const VERIFY_GROTH16_BN254: &'static str = "verify_groth16_bn254";
    /// Host-function name to verify a schnorr-signature.
    pub const VERIFY_SCHNORR: &'static str = "verify_schnorr";
    /// Host-function name to verify a bls-signature.
    pub const VERIFY_BLS: &'static str = "verify_bls";
    /// Host-function name to verify a bls-multisig.
    pub const VERIFY_BLS_MULTISIG: &'static str = "verify_bls_multisig";
    /// Host-function name to compute the keccak256 hash.
    pub const KECCAK256: &'static str = "keccak256";
    /// Host-function name to compute the sha256 hash.
    pub const SHA256: &'static str = "sha256";
    /// Host-function name to verify a KZG point-evaluation proof.
    pub const VERIFY_KZG_PROOF: &'static str = "verify_kzg_proof";
    /// Host-function name to recover a secp256k1 public key.
    pub const SECP256K1_RECOVER: &'static str = "secp256k1_recover";
}

#[cfg(feature = "abi")]
pub(crate) mod host_queries {
    #[cfg(feature = "abi-debug")]
    pub use piecrust_uplink::debug as piecrust_debug;
    pub use piecrust_uplink::{
        call, call_raw, call_raw_with_limit, call_with_limit, caller,
        callstack, emit, emit_raw, feed, limit, self_id, spent, wrap_call,
        wrap_call_unchecked, /* maybe use for our Transaction in
                              * spend_and_execute */
    };

    use alloc::vec::Vec;

    use dusk_bytes::Serializable;
    use piecrust_uplink::{host_query, meta_data};

    use crate::BlsScalar;
    use crate::abi::{ContractId, Metadata, Query};
    use crate::signatures::bls::{
        MultisigSignature, PublicKey as BlsPublicKey, Signature as BlsSignature,
    };
    use crate::signatures::schnorr::{
        PublicKey as SchnorrPublicKey, Signature as SchnorrSignature,
    };

    /// Compute the blake2b hash of the given bytes, returning the resulting
    /// scalar. The output of the hasher is truncated (last nibble) to fit
    /// onto a scalar.
    #[must_use]
    pub fn hash(bytes: Vec<u8>) -> BlsScalar {
        host_query(Query::HASH, bytes)
    }

    /// Compute the poseidon hash of the given scalars
    #[must_use]
    pub fn poseidon_hash(scalars: Vec<BlsScalar>) -> BlsScalar {
        host_query(Query::POSEIDON_HASH, scalars)
    }

    /// Verify that a Groth16 proof in the BN254 pairing is valid for a given
    /// circuit and inputs.
    ///
    /// `proof` and `inputs` should be in compressed form, while `pvk`
    /// uncompressed.
    #[must_use]
    pub fn verify_groth16_bn254(
        pvk: Vec<u8>,
        proof: Vec<u8>,
        inputs: Vec<u8>,
    ) -> bool {
        host_query(Query::VERIFY_GROTH16_BN254, (pvk, proof, inputs))
    }

    /// Verify a Plonk proof is valid for a given circuit type and public inputs
    #[must_use]
    pub fn verify_plonk(
        verifier_data: Vec<u8>,
        proof: Vec<u8>,
        public_inputs: Vec<BlsScalar>,
    ) -> bool {
        host_query(Query::VERIFY_PLONK, (verifier_data, proof, public_inputs))
    }

    /// Verify a schnorr signature is valid for the given public key and message
    #[must_use]
    pub fn verify_schnorr(
        msg: BlsScalar,
        pk: SchnorrPublicKey,
        sig: SchnorrSignature,
    ) -> bool {
        host_query(Query::VERIFY_SCHNORR, (msg, pk, sig))
    }

    /// Verify a BLS signature is valid for the given public key and message
    #[must_use]
    pub fn verify_bls(
        msg: Vec<u8>,
        pk: BlsPublicKey,
        sig: BlsSignature,
    ) -> bool {
        host_query(Query::VERIFY_BLS, (msg, pk, sig))
    }

    /// Verify a BLS signature is valid for the given public key and message
    #[must_use]
    pub fn verify_bls_multisig(
        msg: Vec<u8>,
        keys: Vec<BlsPublicKey>,
        sig: MultisigSignature,
    ) -> bool {
        host_query(Query::VERIFY_BLS_MULTISIG, (msg, keys, sig))
    }

    /// Compute the keccak256 hash of the given bytes, returning the resulting
    /// bytes.
    #[must_use]
    pub fn keccak256(bytes: Vec<u8>) -> [u8; 32] {
        host_query(Query::KECCAK256, bytes)
    }

    /// Compute the sha256 hash of the given bytes, returning the resulting
    /// bytes.
    #[must_use]
    pub fn sha256(bytes: Vec<u8>) -> [u8; 32] {
        host_query(Query::SHA256, bytes)
    }

    /// Verify a KZG point-evaluation proof for a commitment and evaluation.
    #[must_use]
    pub fn verify_kzg_proof(
        commitment: [u8; 48],
        z: [u8; 32],
        y: [u8; 32],
        proof: [u8; 48],
    ) -> bool {
        host_query(Query::VERIFY_KZG_PROOF, (commitment, z, y, proof))
    }

    /// Recover a secp256k1 public key from a 32-byte message hash and
    /// signature.
    ///
    /// Signature is expected as r(32) || s(32) || v(1), with v in {0,1,27,28}.
    #[must_use]
    pub fn secp256k1_recover(
        msg_hash: [u8; 32],
        sig: [u8; 65],
    ) -> Option<[u8; 65]> {
        host_query(Query::SECP256K1_RECOVER, (msg_hash, sig))
    }

    /// Get the chain ID.
    ///
    /// # Panics
    /// Panics if the chain doesn't store a `u8` `CHAIN_ID` in the metadata.
    #[must_use]
    pub fn chain_id() -> u8 {
        meta_data(Metadata::CHAIN_ID).unwrap()
    }

    /// Get the current block height.
    ///
    /// # Panics
    /// Panics if the chain doesn't store a `u64` `BLOCK_HEIGHT` in the
    /// metadata.
    #[must_use]
    pub fn block_height() -> u64 {
        meta_data(Metadata::BLOCK_HEIGHT).unwrap()
    }

    /// Get the public sender of the ongoing tx. Returns `None` if the
    /// transaction is shielded.
    ///
    /// # Panics
    /// Panics if the chain doesn't store a `Option<BlsPublicKey>`
    /// `PUBLIC_SENDER` in the metadata.
    #[must_use]
    pub fn public_sender() -> Option<BlsPublicKey> {
        meta_data(Metadata::PUBLIC_SENDER)
            .expect("moonlight sender metadata to be set")
    }

    /// Query owner of a given contract.
    /// Returns none if contract is not found.
    ///
    /// # Panics
    /// Panics if owner is not a valid public key (should never happen).
    #[must_use]
    pub fn owner(contract: ContractId) -> Option<BlsPublicKey> {
        owner_raw(contract).map(|buf| {
            BlsPublicKey::from_bytes(&buf)
                .expect("Owner should deserialize correctly")
        })
    }

    /// Query self owner of a given contract.
    ///
    /// # Panics
    /// Panics if owner is not a valid public key (should never happen).
    #[must_use]
    pub fn self_owner() -> BlsPublicKey {
        let buf = self_owner_raw();
        BlsPublicKey::from_bytes(&buf)
            .expect("Owner should deserialize correctly")
    }

    /// Query raw `to_bytes` serialization of the owner of a given contract.
    #[must_use]
    pub fn owner_raw(contract: ContractId) -> Option<[u8; BlsPublicKey::SIZE]> {
        piecrust_uplink::owner(contract)
    }

    /// Query raw `to_bytes` serialization of the self owner.
    #[must_use]
    pub fn self_owner_raw() -> [u8; BlsPublicKey::SIZE] {
        piecrust_uplink::self_owner()
    }
}
