-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: the Kotlin wrapper to be in line with the Web wrapper
The Kotlin wrapper was at some point imported from the Kalium but for us it makes more sense to align all our wrapper to have a similar structure.
- Loading branch information
Showing
10 changed files
with
307 additions
and
1,224 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
crypto-ffi/bindings/jvm/src/main/kotlin/com/wire/crypto/client/CoreCrypto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.wire.crypto.client | ||
|
||
import com.wire.crypto.* | ||
import java.util.* | ||
import kotlin.reflect.typeOf | ||
|
||
typealias EnrollmentHandle = ByteArray | ||
|
||
/** | ||
* Initializes the logging inside Core Crypto. Not required to be called and by default there will be no logging. | ||
* | ||
* @param logger a callback to implement the platform specific logging. It will receive the string with the log text from Core Crypto | ||
**/ | ||
fun setLogger(logger: CoreCryptoLogger) { | ||
com.wire.crypto.setLoggerOnly(logger) | ||
} | ||
|
||
/** | ||
* Set maximum log level of logs which are forwarded to the [CoreCryptoLogger]. | ||
* | ||
* @param level the max level that should be logged, by default it will be WARN | ||
*/ | ||
fun setMaxLogLevel(level: CoreCryptoLogLevel) { | ||
com.wire.crypto.setMaxLogLevel(level) | ||
} | ||
|
||
class CoreCrypto(val cc: com.wire.crypto.CoreCrypto) { | ||
|
||
companion object { | ||
internal const val DEFAULT_NB_KEY_PACKAGE: UInt = 100U | ||
|
||
suspend operator fun invoke( | ||
keystore: String, | ||
databaseKey: String | ||
): CoreCrypto { | ||
val cc = coreCryptoDeferredInit(keystore, databaseKey) | ||
cc.setCallbacks(Callbacks()) | ||
return CoreCrypto(cc) | ||
} | ||
} | ||
|
||
internal fun lower() = cc | ||
|
||
/** | ||
* Starts a transaction in Core Crypto. If the callback succeeds, it will be committed, otherwise, every operation | ||
* performed with the context will be discarded. | ||
* | ||
* @param block the function to be executed within the transaction context. A [CoreCryptoContext] will be given as parameter to this function | ||
* | ||
* @return the return of the function passed as parameter | ||
*/ | ||
@Suppress("unchecked_cast") | ||
suspend fun <R> transaction(block: suspend (context: CoreCryptoContext) -> R): R { | ||
var result: R? = null | ||
var error: Throwable? = null | ||
try { | ||
this.cc.transaction(object : CoreCryptoCommand { | ||
override suspend fun execute(context: com.wire.crypto.CoreCryptoContext) { | ||
try { | ||
result = block(CoreCryptoContext(context)) | ||
} catch (e: Throwable) { | ||
// We want to catch the error before it gets wrapped by core crypto. | ||
error = e | ||
// This is to tell core crypto that there was an error inside the transaction. | ||
throw e | ||
} | ||
} | ||
}) | ||
// Catch the wrapped error, which we don't need, because we caught the original error above. | ||
} catch (_: Throwable) { } | ||
if (error != null) { | ||
throw error as Throwable | ||
} | ||
|
||
// Since we know that transaction will either run or throw it's safe to do unchecked cast here | ||
return result as R | ||
} | ||
|
||
suspend fun proteusInit() { | ||
cc.proteusInit() | ||
} | ||
|
||
/** | ||
* Dumps the PKI environment as PEM | ||
* | ||
* @return a struct with different fields representing the PKI environment as PEM strings | ||
*/ | ||
suspend fun e2eiDumpPKIEnv(): E2eiDumpedPkiEnv? { | ||
return cc.e2eiDumpPkiEnv() | ||
} | ||
|
||
/** | ||
* Returns whether the E2EI PKI environment is setup (i.e. Root CA, Intermediates, CRLs) | ||
*/ | ||
suspend fun e2eiIsPKIEnvSetup(): Boolean { | ||
return cc.e2eiIsPkiEnvSetup() | ||
} | ||
|
||
/** | ||
* Closes this [CoreCryptoCentral] instance and deallocates all loaded resources. | ||
* | ||
* **CAUTION**: This {@link CoreCrypto} instance won't be usable after a call to this method, but there's no way to express this requirement in Kotlin, so you'll get errors instead! | ||
*/ | ||
fun close() { | ||
cc.close() | ||
} | ||
} | ||
|
||
private class Callbacks : CoreCryptoCallbacks { | ||
|
||
override suspend fun authorize(conversationId: ByteArray, clientId: ByteArray): Boolean = true | ||
|
||
override suspend fun userAuthorize( | ||
conversationId: ByteArray, | ||
externalClientId: ByteArray, | ||
existingClients: List<ByteArray> | ||
): Boolean = true | ||
|
||
override suspend fun clientIsExistingGroupUser( | ||
conversationId: ByteArray, | ||
clientId: ByteArray, | ||
existingClients: List<ByteArray>, | ||
parentConversationClients: List<ByteArray>? | ||
): Boolean = true | ||
} |
Oops, something went wrong.