Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ org.gradle.configuration-cache=false
#Kotlin
kotlin.code.style=official
kotlin.js.compiler=ir
#kotlin.js.ir.output.granularity=per-file
#MPP
kotlin.mpp.enableCInteropCommonization=true
#Android
android.useAndroidX=true
android.nonTransitiveRClass=true
android.nonTransitiveRClass=true
10 changes: 5 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[versions]
agp = "8.5.0"
kotlin = "2.2.0"
agp = "8.6.1"
kotlin = "2.2.20"
kotest = "6.0.0.M1"
kotlin-serialization = "1.7.1"
kotlin-coroutines = "1.9.0"
kotlin-serialization = "1.9.0"
kotlin-coroutines = "1.10.2"
nexus-publish = "2.0.0"
android-minSdk = "24"
android-minSdk = "26"
android-compileSdk = "35"
kotlin-bignum = "0.3.10"

Expand Down
810 changes: 488 additions & 322 deletions kotlin-js-store/yarn.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ licenseReport {

group = "com.icure"

val version = "1.1.6"
val version = "1.1.7"
project.version = version

kotlin {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.icure.kerberus

import kotlinx.coroutines.yield

internal actual suspend fun doYield() =
yield()
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal class ChallengePieceResolver(
return score(prefixHash, nonce)
}

suspend fun resolve(onProgress: (Double) -> Unit): ProofOfWork {
suspend fun resolve(onProgress: suspend (Double) -> Unit): ProofOfWork {
val prefixHash = sha256(prefix)

val probabilityOfNotSuccessOnEachNonce = difficulty.doubleValue(false) / MAX_UINT_128_AS_DOUBLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public suspend fun resolveChallenge(config: Challenge, serializedInput: String,
if (progress - lastSentProgress > 0.01) {
lastSentProgress = progress
onProgress(progress)
doYield()
}
}
}.map { it.nonce }.toList()
Expand Down
3 changes: 3 additions & 0 deletions library/src/commonMain/kotlin/com/icure/kerberus/Yield.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.icure.kerberus

internal expect suspend fun doYield()
37 changes: 15 additions & 22 deletions library/src/jsMain/kotlin/com/icure/kerberus/Kerberus.js.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,32 @@ import com.icure.kryptom.crypto.external.XCryptoService
import com.icure.kryptom.crypto.external.adaptExternalCryptoService
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.promise
import kotlin.js.Promise
import kotlin.js.json

@OptIn(DelicateCoroutinesApi::class, ExperimentalJsExport::class)
@JsExport
@JsName("resolveChallenge")
public fun resolveChallengeJs(config: ChallengeJs, serializedInput: String, cryptoService: XCryptoService? = null, onProgress: (Double) -> Unit = {}): Promise<SolutionJs> = Promise { resolve, _ ->
GlobalScope.launch {
resolve(
resolveChallenge(
config = config.toConfig(),
serializedInput = serializedInput,
cryptoService = cryptoService ?.let { adaptExternalCryptoService(it) } ?: defaultCryptoService,
onProgress = onProgress
).toSolutionJs()
)
}
public fun resolveChallengeJs(config: ChallengeJs, serializedInput: String, cryptoService: XCryptoService? = null, onProgress: (Double) -> Unit = {}): Promise<SolutionJs> = GlobalScope.promise {
resolveChallenge(
config = config.toConfig(),
serializedInput = serializedInput,
cryptoService = cryptoService ?.let { adaptExternalCryptoService(it) } ?: defaultCryptoService,
onProgress = onProgress
).toSolutionJs()
}

@OptIn(DelicateCoroutinesApi::class, ExperimentalJsExport::class)
@JsExport
@JsName("validateSolution")
public fun validateSolutionJs(config: ChallengeJs, result: SolutionJs, serializedInput: String, cryptoService: XCryptoService? = null): Promise<Boolean> = Promise { resolve, _ ->
GlobalScope.launch {
resolve(
validateSolution(
config = config.toConfig(),
result = result.toSolution(),
serializedInput = serializedInput,
cryptoService = cryptoService ?.let { adaptExternalCryptoService(it) } ?: defaultCryptoService)
)
}
public fun validateSolutionJs(config: ChallengeJs, result: SolutionJs, serializedInput: String, cryptoService: XCryptoService? = null): Promise<Boolean> = GlobalScope.promise {
validateSolution(
config = config.toConfig(),
result = result.toSolution(),
serializedInput = serializedInput,
cryptoService = cryptoService ?.let { adaptExternalCryptoService(it) } ?: defaultCryptoService
)
}

private fun SolutionJs.toSolution(): Solution = Solution(
Expand Down
14 changes: 14 additions & 0 deletions library/src/jsMain/kotlin/com/icure/kerberus/Yield.js.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.icure.kerberus

import kotlinx.coroutines.await
import kotlin.js.Promise

internal actual suspend fun doYield() =
yieldingPromise().await()

private fun yieldingPromise(): Promise<Unit> =
Promise { resolve, _ ->
setTimeout({ resolve(Unit) }, 0)
}

private external fun setTimeout(callback: () -> Unit, delay: Int)
6 changes: 6 additions & 0 deletions library/src/jvmMain/kotlin/com/icure/kerberus/Yield.jvm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.icure.kerberus

import kotlinx.coroutines.yield

internal actual suspend fun doYield() =
yield()
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.icure.kerberus

import kotlinx.coroutines.yield

internal actual suspend fun doYield() =
yield()