Foundations(iOS Sourceset): Provide implementation for LibSodiumInstance.ios.kt, using the native library(shipped in project). Generate cinterop definition file with custom Gradle task. Defines bindings for use in actual code(may be streamlined later). Adds NIP44 tests and makes sure they pass.

This commit is contained in:
KotlinGeekDev
2026-03-12 15:13:32 +01:00
parent 548ce0a69d
commit 8f02ea0cf6
83 changed files with 6649 additions and 4 deletions
+74
View File
@@ -5,6 +5,7 @@ import io.github.frankois944.spmForKmp.swiftPackageConfig
import io.github.frankois944.spmForKmp.utils.ExperimentalSpmForKmpFeature
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
plugins {
alias(libs.plugins.kotlinMultiplatform)
@@ -83,19 +84,71 @@ kotlin {
}
}
val libsodiumPath = project.file("src/nativeInterop/libsodium").toString()
iosX64 {
compilations.getByName("main") {
val headerFilesPath = "$libsodiumPath/include/sodium"
val Clibsodium by cinterops.creating {
definitionFile.set(project.file("Clibsodium.def"))
includeDirs("$libsodiumPath/include/sodium")
headers(
"$headerFilesPath/crypto_aead_xchacha20poly1305.h",
"$headerFilesPath/crypto_core_hchacha20.h",
"$headerFilesPath/crypto_stream_chacha20.h"
)
}
}
binaries.all {
//linkerOpts("-L${libsodiumPath}/ios/lib", "-llibsodium")
}
binaries.framework {
baseName = xcfName
}
}
iosArm64 {
compilations.getByName("main") {
val headerFilesPath = "$libsodiumPath/include/sodium"
val Clibsodium by cinterops.creating {
definitionFile.set(project.file("Clibsodium.def"))
includeDirs("$libsodiumPath/include/sodium")
headers(
"$headerFilesPath/crypto_aead_xchacha20poly1305.h",
"$headerFilesPath/crypto_core_hchacha20.h",
"$headerFilesPath/crypto_stream_chacha20.h"
)
}
}
binaries.all {
//linkerOpts("-L${libsodiumPath}/ios/lib", "-llibsodium")
}
binaries.framework {
baseName = xcfName
}
}
iosSimulatorArm64 {
compilations.getByName("main") {
val headerFilesPath = "$libsodiumPath/include/sodium"
val Clibsodium by cinterops.creating {
definitionFile.set(project.file("Clibsodium.def"))
includeDirs("${libsodiumPath}/ios-simulators/lib")
//compilerOpts("-I${libsodiumPath}/ios-simulators/lib")
headers(
"$headerFilesPath/crypto_aead_xchacha20poly1305.h",
"$headerFilesPath/crypto_core_hchacha20.h",
"$headerFilesPath/crypto_stream_chacha20.h"
)
}
}
binaries.all {
//linkerOpts("-L${libsodiumPath}/ios-simulators/lib", "-llibsodium-simulator")
}
binaries.framework {
baseName = xcfName
}
@@ -239,6 +292,8 @@ kotlin {
implementation(libs.charlietap.cachemap)
implementation(libs.net.thauvin.erik.urlencoder.lib)
implementation(libs.dev.whyoleg.cryptography.provider.apple.optimal)
// implementation("io.github.andreypfau:kotlinx-crypto-hmac:0.0.4")
// implementation("io.github.andreypfau:kotlinx-crypto-sha2:0.0.4")
}
}
@@ -320,3 +375,22 @@ mavenPublishing {
}
}
}
tasks.register<Exec>("GenerateSodiumCinteropFile") {
val libsodiumPath = project.file("src/nativeInterop/libsodium").toString()
val definitionFile = project.file("src/nativeInterop/cinterop/Clibsodium.def")
definitionFile.writeText("package = Clibsodium")
definitionFile.appendText("\n" + "staticLibraries = libsodium.a libsodium-simulator.a")
definitionFile.appendText("\n" + "libraryPaths = ${libsodiumPath}/ios/lib ${libsodiumPath}/ios-simulators/lib")
doLast {
executionResult.get().assertNormalExitValue()
}
}
tasks.withType<KotlinNativeCompile>().configureEach {
val fileDefinition = project.file("src/nativeInterop/cinterop/Clibsodium.def")
if (!fileDefinition.exists()) {
dependsOn("GenerateSodiumCinteropFile")
}
}