/*
 * Copyright (c) 2026 QPQ IaaS AG <info@qpq.swiss>. All rights reserved.
 * Project: Gajumaru Core Java Libraries <gajumaru.io>
 *
 * This program is dual-licensed:
 * 1) Under the GNU Affero General Public License as published by the Free
 *    Software Foundation, either version 3 of the License, or (at your option)
 *    any later version (AGPL-3.0-or-later).
 *
 * 2) Under a commercial/proprietary license available directly from QPQ IaaS AG.
 *    If you wish to use this software outside the strict constraints of the
 *    AGPLv3 (e.g. within a closed-source or proprietary product), you must
 *    purchase a commercial license from QPQ IaaS AG.
 *
 * Authors:
 *   - Craig Everett <craigeverett@qpq.swiss>
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later OR LicenseRef-QPQ-Commercial
 */

package swiss.qpq.gajumobile.security

object GajuNative {
    var loadError: String? = null
        private set

    init {
        try {
            System.loadLibrary("gaju-native")
            // Register as the default signing provider for TransactionService
            swiss.qpq.gajumaru.core.tools.TransactionService.setProvider(NativeSigningProvider())
        } catch (e: Throwable) {
            loadError = e.message ?: e.toString()
        }
    }

    /**
     * Zeros out the provided byte array using native memset.
     */
    @JvmStatic
    external fun memzero(array: ByteArray)

    /**
     * Generates an Ed25519 keypair from a 32-byte seed.
     * @param seed 32-byte seed.
     * @return 96-byte array containing public key (32 bytes) followed by secret key (64 bytes).
     */
    @JvmStatic
    external fun cryptoSignSeedKeypair(seed: ByteArray): ByteArray?

    /**
     * Signs a message using Ed25519.
     * @param message The message to sign.
     * @param secretKey 64-byte secret key.
     * @return 64-byte detached signature.
     */
    @JvmStatic
    external fun cryptoSignDetached(message: ByteArray, secretKey: ByteArray): ByteArray?

    /**
     * Verifies an Ed25519 detached signature.
     * @param signature 64-byte signature.
     * @param message The message.
     * @param publicKey 32-byte public key.
     * @return true if valid, false otherwise.
     */
    @JvmStatic
    external fun cryptoSignVerifyDetached(signature: ByteArray, message: ByteArray, publicKey: ByteArray): Boolean
}
