Skip to content

Commit

Permalink
fix: JS wrap EdHdKey.initFromSeed in companion to make static
Browse files Browse the repository at this point in the history
Signed-off-by: Curtis Harding <curtis.harding@iohk.io>
  • Loading branch information
curtis-h committed May 3, 2024
1 parent c7aac59 commit e17fb3c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.kotlincrypto.hash.sha2.SHA512
import kotlin.experimental.and
import kotlin.experimental.or


fun convertSecretKeyToX25519(secretKey: ByteArray): ByteArray {
// Hash the first 32 bytes of the Ed25519 secret key
val hashed = SHA512().digest(secretKey.sliceArray(0 until 32))
Expand All @@ -14,4 +13,4 @@ fun convertSecretKeyToX25519(secretKey: ByteArray): ByteArray {
hashed[31] = hashed[31] or 64.toByte()
// Return the first 32 bytes of the hash as the X25519 secret key
return hashed.sliceArray(0 until 32)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ public expect class KMMEdPrivateKey {
* @return KMMX25519PrivateKey private key
*/
fun x25519PrivateKey(): KMMX25519PrivateKey

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@ class EdHDKey(
val depth: Int = 0,
val index: BigIntegerWrapper = BigIntegerWrapper(0)
) {
/**
* Constructs a new EdHDKey object from a seed
*
* @param seed The seed used to derive the private key and chain code.
* @throws IllegalArgumentException if the seed length is not equal to 64.
*/
fun initFromSeed(seed: ByteArray): EdHDKey {
require(seed.size == 64) {
"Seed expected byte length to be ${ECConfig.PRIVATE_KEY_BYTE_SIZE}"
}
companion object {
/**
* Constructs a new EdHDKey object from a seed
*
* @param seed The seed used to derive the private key and chain code.
* @throws IllegalArgumentException if the seed length is not equal to 64.
*/
fun initFromSeed(seed: ByteArray): EdHDKey {
require(seed.size == 64) {
"Seed expected byte length to be ${ECConfig.PRIVATE_KEY_BYTE_SIZE}"
}

val key = seed.sliceArray(0 until 32)
val chainCode = seed.sliceArray(32 until seed.size)
val wrapper = ed25519_bip32.XPrvWrapper.from_nonextended_noforce(key, chainCode)
val key = seed.sliceArray(0 until 32)
val chainCode = seed.sliceArray(32 until seed.size)
val wrapper = ed25519_bip32.XPrvWrapper.from_nonextended_noforce(key, chainCode)

return EdHDKey(
privateKey = wrapper.extended_secret_key(),
chainCode = wrapper.chain_code()
)
return EdHDKey(
privateKey = wrapper.extended_secret_key(),
chainCode = wrapper.chain_code()
)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.iohk.atala.prism.apollo.utils

import io.iohk.atala.prism.apollo.base64.base64UrlDecodedBytes
import io.iohk.atala.prism.apollo.utils.Curve25519Parser.encodedLength
import io.iohk.atala.prism.apollo.utils.Curve25519Parser.rawLength
import node.buffer.Buffer

/**
Expand All @@ -13,6 +15,7 @@ import node.buffer.Buffer
@OptIn(ExperimentalJsExport::class)
@JsExport
object Curve25519Parser {
val extendedLength = 64
val encodedLength = 43
val rawLength = 32

Expand All @@ -29,6 +32,10 @@ object Curve25519Parser {
return Buffer.from(buffer.toByteArray().decodeToString().base64UrlDecodedBytes)
}

if (buffer.length == extendedLength) {
return buffer
}

if (buffer.length == rawLength) {
return buffer
}
Expand Down

0 comments on commit e17fb3c

Please sign in to comment.