Skip to content

Commit

Permalink
docs: Inline Code Documentations
Browse files Browse the repository at this point in the history
- Wrote a lot of documentation to improve docs overall
- Improved Custom Exceptions to show errors
- Added missing @JvmStatic
- Added missing @jvmoverloads

Signed-off-by: Ahmed Moussa <ahmed.moussa@iohk.io>
  • Loading branch information
hamada147 committed Dec 13, 2023
1 parent 0f487bd commit 0077e5a
Show file tree
Hide file tree
Showing 136 changed files with 4,692 additions and 498 deletions.
2 changes: 1 addition & 1 deletion atala-prism-sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ tasks.withType<DokkaTask>().configureEach {
}
}

val buildProtoLibsGen by tasks.creating {
val buildProtoLibsGen: Task by tasks.creating {
group = "build"
this.dependsOn(":protosLib:generateProto")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package io.iohk.atala.prism.walletsdk.domain.models
import io.ktor.client.HttpClientConfig
import io.ktor.client.engine.okhttp.OkHttp

/**
* Creates an HTTP client with the specified configuration.
*
* @param config The configuration block for the HTTP client.
* @return The created HTTP client.
*/
actual fun httpClient(config: HttpClientConfig<*>.() -> Unit) = io.ktor.client.HttpClient(OkHttp) {
config(this)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
package io.iohk.atala.prism.walletsdk.domain.models

/**
* The `Platform` object represents the platform on which the code is running.
*/
actual object Platform {
/**
* This variable represents the operating system on which the code is currently running.
*
* On Android, it returns a string with the Android version followed by the SDK level.
* For example: "Android 10"
*
* @return The operating system of the device.
*/
actual val OS: String
get() = "Android"
get() = "Android ${android.os.Build.VERSION.SDK_INT}"

/**
* Represents the platform type.
*
* This actual property represents the current platform type. It is used to determine the type of the platform on which
* the application is being executed. The possible platform types are JVM, ANDROID, IOS, and WEB.
*
* This property is read-only and can be accessed using the `type` property of the `PlatformType` class.
*
* Example usage:
* ```
* val platformType = PlatformType.ANDROID
* ```
*
* @see PlatformType
*/
actual val type: PlatformType
get() = PlatformType.ANDROID
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import com.squareup.sqldelight.db.SqlDriver
import io.iohk.atala.prism.walletsdk.PrismPlutoDb
import io.iohk.atala.prism.walletsdk.domain.models.PlutoError

/**
* DbConnection class represents a connection to the database.
*/
actual class DbConnection actual constructor() {
actual var driver: SqlDriver? = null
actual suspend fun connectDb(context: Any?): PrismPlutoDb {
Expand All @@ -16,5 +19,14 @@ actual class DbConnection actual constructor() {
}
}

/**
* Represents the connection status of an SQL driver.
*/
actual val SqlDriver.isConnected: Boolean
get() = true
get() {
try {
return this.executeQuery(null, "SELECT 1", 0).next()
} catch (ex: Exception) {
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,32 @@ class ApolloImpl : Apollo {
)
}

/**
* Creates a private key based on the provided properties.
*
* @param properties A map of properties used to create the private key. The map should contain the following keys:
* - "type" (String): The type of the key ("EC" or "Curve25519").
* - "curve" (String): The curve of the key.
* - "rawKey" (ByteArray): The raw key data (optional).
* - "index" (Int): The index for the key (only applicable for EC keys with curve "secp256k1").
* - "derivationPath" (String): The derivation path for the key (only applicable for EC keys with curve "secp256k1").
* - "seed" (String): The seed for the key (only applicable for EC keys with curve "secp256k1").
*
* @return The created private key.
*
* @throws ApolloError.InvalidKeyType If the provided key type is invalid.
* @throws ApolloError.InvalidKeyCurve If the provided key curve is invalid.
* @throws ApolloError.InvalidRawData If the provided raw key data is invalid.
* @throws ApolloError.InvalidIndex If the provided index is invalid.
* @throws ApolloError.InvalidDerivationPath If the provided derivation path is invalid.
* @throws ApolloError.InvalidSeed If the provided seed is invalid.
*/
override fun createPrivateKey(properties: Map<String, Any>): PrivateKey {
if (!properties.containsKey(TypeKey().property)) {
throw ApolloError.InvalidKeyType(TypeKey().property, KeyTypes.values().map { it.type }.toTypedArray())
throw ApolloError.InvalidKeyType(TypeKey().property)
}
if (!properties.containsKey(CurveKey().property)) {
throw ApolloError.InvalidKeyCurve(CurveKey().property, Curve.values().map { it.value }.toTypedArray())
throw ApolloError.InvalidKeyCurve(CurveKey().property)
}

val keyType = properties[TypeKey().property]
Expand Down Expand Up @@ -148,17 +168,38 @@ class ApolloImpl : Apollo {
return keyPair.privateKey
}
}
throw ApolloError.InvalidKeyType(TypeKey().property, KeyTypes.values().map { it.type }.toTypedArray())
throw ApolloError.InvalidKeyType(TypeKey().property)
}

/**
* Checks if the provided data is associated with a private key identified by the given identifier.
*
* @param identifier The identifier for the private key.
* @param data The data to check.
* @return True if the data is associated with a private key, false otherwise.
*/
override fun isPrivateKeyData(identifier: String, data: ByteArray): Boolean {
return identifier.endsWith("priv")
}

/**
* Checks if the provided data is associated with a public key identified by the given identifier.
*
* @param identifier The identifier for the public key.
* @param data The data to check.
* @return True if the data is associated with a public key, false otherwise.
*/
override fun isPublicKeyData(identifier: String, data: ByteArray): Boolean {
return identifier.endsWith("pub")
}

/**
* Restores a private key based on the provided storable key.
*
* @param key The storable key to restore the private key from.
* @return The restored private key.
* @throws ApolloError.RestorationFailedNoIdentifierOrInvalid If the restoration identifier is missing or invalid.
*/
override fun restorePrivateKey(key: StorableKey): PrivateKey {
return when (key.restorationIdentifier) {
"secp256k1+priv" -> {
Expand All @@ -179,6 +220,13 @@ class ApolloImpl : Apollo {
}
}

/**
* Restores a public key based on the provided storable key.
*
* @param key The storable key to restore the public key from.
* @return The restored public key.
* @throws ApolloError.RestorationFailedNoIdentifierOrInvalid If the restoration identifier is missing or invalid.
*/
override fun restorePublicKey(key: StorableKey): PublicKey {
return when (key.restorationIdentifier) {
"secp256k1+pub" -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package io.iohk.atala.prism.walletsdk.apollo.config

import com.ionspin.kotlin.bignum.integer.BigInteger

/**
* The ECConfig object provides configuration properties and constants related to elliptic curve cryptography (ECC).
*/
object ECConfig {
val PRIVATE_KEY_BYTE_SIZE: Int = 32
internal val PUBLIC_KEY_COORDINATE_BYTE_SIZE: Int = 32
internal val PUBLIC_KEY_COMPRESSED_BYTE_SIZE: Int = PUBLIC_KEY_COORDINATE_BYTE_SIZE + 1
val SIGNATURE_MAX_BYTE_SIZE: Int = 72
val PUBLIC_KEY_BYTE_SIZE: Int = PUBLIC_KEY_COORDINATE_BYTE_SIZE * 2 + 1
const val PRIVATE_KEY_BYTE_SIZE: Int = 32
internal const val PUBLIC_KEY_COORDINATE_BYTE_SIZE: Int = 32
internal const val PUBLIC_KEY_COMPRESSED_BYTE_SIZE: Int = PUBLIC_KEY_COORDINATE_BYTE_SIZE + 1
const val SIGNATURE_MAX_BYTE_SIZE: Int = 72
const val PUBLIC_KEY_BYTE_SIZE: Int = PUBLIC_KEY_COORDINATE_BYTE_SIZE * 2 + 1

// Field characteristic p (prime) is equal to 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 1
internal val p = BigInteger.parseString("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package io.iohk.atala.prism.walletsdk.apollo.helpers

/**
* Pads the current ByteArray with the specified padValue at the beginning,
* making it equal to or larger than the specified length.
*
* @param length The desired length for the new ByteArray.
* @param padValue The value used to pad the ByteArray.
* @return The padded ByteArray with the specified length.
*/
fun ByteArray.padStart(length: Int, padValue: Byte): ByteArray {
return if (size >= length) {
this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package io.iohk.atala.prism.walletsdk.apollo.helpers

import java.math.BigInteger

/**
* Converts a Kotlin BigInteger to a Java BigInteger.
*
* @return the converted Java BigInteger.
*/
fun com.ionspin.kotlin.bignum.integer.BigInteger.toJavaBigInteger(): BigInteger {
return BigInteger(this.signum(), this.toByteArray())
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.KeyPair
import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PrivateKey
import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PublicKey

/**
* Represents a pair of Ed25519 private and public keys.
*/
class Ed25519KeyPair(
override var privateKey: PrivateKey,
override var publicKey: PublicKey
) : KeyPair() {

companion object {
/**
* Generates a pair of Ed25519 private and public keys.
*
* @return The generated Ed25519KeyPair.
*/
@JvmStatic
fun generateKeyPair(): Ed25519KeyPair {
val pair = KMMEdKeyPair.generateKeyPair()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PublicKey
import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.SignableKey
import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.StorableKey

/**
* Represents a private key for the Ed25519 algorithm.
*
* This class extends the abstract class PrivateKey and implements the interfaces SignableKey, StorableKey, and ExportableKey.
*
* @param nativeValue The raw byte array representing the private key.
* @property size The size of the private key in bytes.
* @property raw The raw byte array representing the private key.
* @property type The type of the key. Always set to KeyTypes.EC.
* @property keySpecification A mutable map representing the key specification.
*
* @constructor Creates an instance of Ed25519PrivateKey and initializes its properties.
*
* @param nativeValue The raw byte array representing the private key.
*/
class Ed25519PrivateKey(nativeValue: ByteArray) : PrivateKey(), SignableKey, StorableKey, ExportableKey {

override val type: KeyTypes = KeyTypes.EC
Expand All @@ -26,23 +41,44 @@ class Ed25519PrivateKey(nativeValue: ByteArray) : PrivateKey(), SignableKey, Sto
keySpecification[CurveKey().property] = Curve.ED25519.value
}

/**
* Returns the public key corresponding to this private key.
* @return the public key as a PublicKey object
*/
override fun publicKey(): PublicKey {
val public = KMMEdPrivateKey(raw).publicKey()
return Ed25519PublicKey(public.raw)
}

/**
* Signs a byte array message using the private key.
*
* @param message The message to be signed.
* @return The signature as a byte array.
*/
override fun sign(message: ByteArray): ByteArray {
val private = KMMEdPrivateKey(raw)
return private.sign(message)
}

/**
* Returns the PEM (Privacy-Enhanced Mail) representation of the private key.
* The key is encoded in base64 and wrapped with "BEGIN" and "END" markers.
*
* @return the PEM representation of the private key as a String
*/
override fun getPem(): String {
return PEMKey(
keyType = PEMKeyType.EC_PRIVATE_KEY,
keyData = raw
).pemEncoded()
}

/**
* Retrieves the JWK (JSON Web Key) representation of the private key.
*
* @return The JWK instance representing the private key.
*/
override fun getJwk(): JWK {
return JWK(
kty = "OKP",
Expand All @@ -51,6 +87,12 @@ class Ed25519PrivateKey(nativeValue: ByteArray) : PrivateKey(), SignableKey, Sto
)
}

/**
* Retrieves the JWK (JSON Web Key) representation of the private key with the specified key identifier (kid).
*
* @param kid The key identifier to be associated with the JWK.
* @return The JWK object representing the private key.
*/
override fun jwkWithKid(kid: String): JWK {
return JWK(
kty = "OKP",
Expand All @@ -60,8 +102,22 @@ class Ed25519PrivateKey(nativeValue: ByteArray) : PrivateKey(), SignableKey, Sto
)
}

/**
* Represents the storable data of a key.
*
* @property storableData The byte array representing the storable data.
* @see StorableKey
*/
override val storableData: ByteArray
get() = raw

/**
* This variable represents the restoration identifier for a key.
* It is a unique identifier used for restoring the key from storage.
*
* @property restorationIdentifier The restoration identifier for the key.
* @see StorableKey
*/
override val restorationIdentifier: String
get() = "ed25519+priv"
}
Loading

0 comments on commit 0077e5a

Please sign in to comment.