Skip to content

Commit

Permalink
docs: add comments to code
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianIOHK committed Nov 29, 2023
1 parent 6b8299b commit 7f16819
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ class HDKey(
return this.deriveChild(BigIntegerWrapper(BigInteger(index)))
}

/**
* Method to get the KMMECSecp256k1PrivateKey from HDKey
*
* @return KMMECSecp256k1PrivateKey
*/
fun getKMMSecp256k1PrivateKey(): KMMECSecp256k1PrivateKey {
privateKey?.let {
return KMMECSecp256k1PrivateKey.secp256k1FromByteArray(privateKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import io.iohk.atala.prism.apollo.hashing.PBKDF2SHA512
import io.iohk.atala.prism.apollo.securerandom.SecureRandom
import org.kotlincrypto.hash.sha2.SHA256

/**
* This helper provides a set of methods to work with Mnemonics
*/
final class MnemonicHelper {
companion object {
private const val SEED_ENTROPY_BITS_24_WORDS = 256
Expand All @@ -13,15 +16,33 @@ final class MnemonicHelper {

class InvalidMnemonicCode(code: String) : RuntimeException(code)

/**
* It validates if a provided list of works is a valid mnemonic
*
* @return Boolean
*/
fun isValidMnemonicCode(code: List<String>): Boolean {
return code.all { it in MnemonicCodeEnglish.wordList }
}

/**
* It creates a random list of works
*
* @return List<String> as mnemonics
*/
fun createRandomMnemonics(): List<String> {
val entropyBytes = SecureRandom.generateSeed(SEED_ENTROPY_BITS_24_WORDS / 8)
return toMnemonicCode(MnemonicCodeEnglish.wordList, entropyBytes)
}

/**
* Creates a seed from a mnemonics list and a passphrase.
*
* @param mnemonics The word list used for generating the mnemonic code.
* @param passphrase Passphrase used to derive the mnemonic string
* @throws InvalidMnemonicCode if the list of mnemonics is invalid
* @return ByteArray representing the seed raw value
*/
@Throws(InvalidMnemonicCode::class)
fun createSeed(mnemonics: List<String>, passphrase: String = "AtalaPrism"): ByteArray {
if (!isValidMnemonicCode(mnemonics)) {
Expand All @@ -36,11 +57,25 @@ final class MnemonicHelper {
)
}

/**
* Create random seed with a passphrase
*
* @param String passphrase to include into the seed creation
* @return ByteArray seed raw value
*/
fun createRandomSeed(passphrase: String = "AtalaPrism"): ByteArray {
val mnemonics = this.createRandomMnemonics()
return this.createSeed(mnemonics, passphrase)
}

/**
* Converts a byte array of entropy into a mnemonic code (word list) based on the specified word list.
*
* @param words The word list to be used for generating the mnemonic code. This list should conform to the BIP-39 standard.
* @param entropy The entropy byte array, which is used to generate the mnemonic code. The length of this array must be a multiple of 4 bytes (32 bits).
* @throws Exception if the entropy is empty or its length is not a multiple of 32 bits.
* @return A list of strings representing the mnemonic code.
*/
fun toMnemonicCode(words: List<String>, entropy: ByteArray): List<String> {
if (entropy.size % 4 > 0) {
throw Exception("Entropy length not multiple of 32 bits.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,60 @@
package io.iohk.atala.prism.apollo.secp256k1

/**
* This class provides various Secp256k1 cryptographic functionalities such as creating public keys, signing data,
* verifying signatures, and compressing or decompressing public keys.
*/
expect class Secp256k1Lib constructor() {
/**
* Creates a public key from a given private key.
*
* @param privateKey The private key in byte array format.
* @param compressed A boolean indicating whether the public key should be compressed.
* @return A byte array representing the public key.
*/
fun createPublicKey(privateKey: ByteArray, compressed: Boolean): ByteArray

/**
* Derives a new private key from an existing private key and derived bytes.
*
* @param privateKeyBytes The original private key in byte array format.
* @param derivedPrivateKeyBytes The byte array used for deriving the new private key.
* @return A byte array representing the derived private key, or null if derivation fails.
*/
fun derivePrivateKey(privateKeyBytes: ByteArray, derivedPrivateKeyBytes: ByteArray): ByteArray?

/**
* Signs data using a given private key.
*
* @param privateKey The private key used for signing, in byte array format.
* @param data The data to be signed, in byte array format.
* @return A byte array representing the signature.
*/
fun sign(privateKey: ByteArray, data: ByteArray): ByteArray

/**
* Verifies a signature against a public key and data.
*
* @param publicKey The public key in byte array format.
* @param signature The signature to be verified, in byte array format.
* @param data The data against which the signature will be verified, in byte array format.
* @return A boolean indicating whether the signature is valid.
*/
fun verify(publicKey: ByteArray, signature: ByteArray, data: ByteArray): Boolean

/**
* Decompresses a compressed public key.
*
* @param compressed The compressed public key in byte array format.
* @return A byte array representing the uncompressed public key.
*/
fun uncompressPublicKey(compressed: ByteArray): ByteArray

/**
* Compresses an uncompressed public key.
*
* @param uncompressed The uncompressed public key in byte array format.
* @return A byte array representing the compressed public key.
*/
fun compressPublicKey(uncompressed: ByteArray): ByteArray
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@ package io.iohk.atala.prism.apollo.utils
import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport

/**
* Interface defining the functionality for generating Ed25519 key pairs.
*/
@ExperimentalJsExport
@JsExport
interface Ed25519KeyPairGeneration {

/**
* Generates an Ed25519 key pair.
*
* @return A [KMMEdKeyPair] instance representing the generated public and private keys.
*/
fun generateKeyPair(): KMMEdKeyPair
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package io.iohk.atala.prism.apollo.utils
import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport

/**
* Representation of an EC curve point
*/
@OptIn(ExperimentalJsExport::class)
@JsExport
data class KMMECPoint(val x: ByteArray, val y: ByteArray) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ interface KMMECSecp256k1PrivateKeyCommonStaticInterface {
}
}

/**
* Definition of the KMMECSecp256k1PublicKey functionality
*/
@OptIn(ExperimentalJsExport::class)
@JsExport
class KMMECSecp256k1PrivateKey : Encodable {
Expand All @@ -33,11 +36,21 @@ class KMMECSecp256k1PrivateKey : Encodable {
this.raw = raw
}

/**
* Method to fetch the KMMECSecp256k1PublicKey
*
* @return KMMECSecp256k1PublicKey
*/
fun getPublicKey(): KMMECSecp256k1PublicKey {
val pubKeyBytes = Secp256k1Lib().createPublicKey(raw, false)
return KMMECSecp256k1PublicKey(pubKeyBytes)
}

/**
* Method to get the encoded raw value
*
* @return ByteArray representing the raw value of this KMMECSecp256k1PrivateKey
*/
override fun getEncoded(): ByteArray {
return raw
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ interface KMMECSecp256k1PublicKeyCommonStaticInterface {
}
}

/**
* Definition of the KMMECSecp256k1PublicKey functionality
*/
@OptIn(ExperimentalJsExport::class)
@JsExport
class KMMECSecp256k1PublicKey {
Expand All @@ -64,6 +67,11 @@ class KMMECSecp256k1PublicKey {
this.raw = raw
}

/**
* Method to get the CurvePoint of KMMECSecp256k1PublicKey
*
* @return KMMECPoint
*/
fun getCurvePoint(): KMMECPoint {
if (raw.size != 65) {
throw IllegalArgumentException("Public key should be 65 bytes long")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@ package io.iohk.atala.prism.apollo.utils

import kotlin.js.ExperimentalJsExport

/**
* Interface defining the functionality for generating KMMEd key pairs.
*/
@ExperimentalJsExport
expect class KMMEdKeyPair(privateKey: KMMEdPrivateKey, publicKey: KMMEdPublicKey) {
val privateKey: KMMEdPrivateKey
val publicKey: KMMEdPublicKey

companion object : Ed25519KeyPairGeneration

/**
* Method to sign a provided message
*
* @param message A ByteArray with the message to be signed
* @return A ByteArray conforming the signed message
*/
fun sign(message: ByteArray): ByteArray

/**
* Method to verify a provided message and signature
*
* @param message A ByteArray with the original message
* @param sig The signature to be verified
* @return A boolean that tell us if the signature matches the original message
*/
fun verify(message: ByteArray, sig: ByteArray): Boolean
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package io.iohk.atala.prism.apollo.utils

/**
* Definition of the KMMEdPrivateKey functionality
*/
public expect class KMMEdPrivateKey {

/**
* Method to sign a message using this KMMEdPrivateKey
*
* @param message ByteArray representing the message to be signed
* @return ByteArray representing the signed message
*/
fun sign(message: ByteArray): ByteArray
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package io.iohk.atala.prism.apollo.utils

/**
* Definition of the KMMEdPublicKey functionality
*/
public expect class KMMEdPublicKey {

/**
* Method to verify a signature against the original message
*
* @param message ByteArray representing the original message
* @param sig ByteArray representing the signed message
* @return Boolean that tell us if the signature matches the original message
*/
fun verify(message: ByteArray, sig: ByteArray): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package io.iohk.atala.prism.apollo.utils

import kotlin.js.ExperimentalJsExport

/**
* Interface defining the functionality for generating KMMX25519 key pairs.
*/
@OptIn(ExperimentalJsExport::class)
expect class KMMX25519KeyPair(privateKey: KMMX25519PrivateKey, publicKey: KMMX25519PublicKey) {
val privateKey: KMMX25519PrivateKey
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package io.iohk.atala.prism.apollo.utils

/**
* Definition of the KMMX25519PrivateKey functionality
*/
expect class KMMX25519PrivateKey
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package io.iohk.atala.prism.apollo.utils

/**
* Definition of the KMMX25519PublicKey functionality
*/
expect class KMMX25519PublicKey
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package io.iohk.atala.prism.apollo.utils
import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport

/**
* Interface defining the functionality for generating X25519 key pairs.
*/
@ExperimentalJsExport
@JsExport
interface X25519KeyPairGeneration {
Expand Down

0 comments on commit 7f16819

Please sign in to comment.