Skip to content

Commit

Permalink
feat(BAE): updating jsMain Ed25519 keys for use in TS
Browse files Browse the repository at this point in the history
  • Loading branch information
curtis-h committed Sep 29, 2023
1 parent 0270f38 commit 85ca03b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.iohk.atala.prism.apollo.utils

public expect class KMMEdPrivateKey {
fun publicKey(): KMMEdPublicKey
fun sign(message: ByteArray): ByteArray
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ actual class KMMEdKeyPair actual constructor(
override fun generateKeyPair(): KMMEdKeyPair {
val ed25519 = eddsa("ed25519")
val rnd = rand(32)
val secret = Buffer.from(rnd).toByteArray()
val secret = Buffer.from(rnd)
val keypair = ed25519.keyFromSecret(secret)
val public = keypair.getPublic()

return KMMEdKeyPair(KMMEdPrivateKey(secret), KMMEdPublicKey(public))
return KMMEdKeyPair(KMMEdPrivateKey(secret.toByteArray()), KMMEdPublicKey(public))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
package io.iohk.atala.prism.apollo.utils

import io.iohk.atala.prism.apollo.base64.base64UrlDecodedBytes
import io.iohk.atala.prism.apollo.utils.external.eddsa
import node.buffer.Buffer

@ExperimentalJsExport
@JsExport
actual class KMMEdPrivateKey(val raw: ByteArray) {
actual class KMMEdPrivateKey(bytes: ByteArray) {
val raw: Buffer
private val keyPair: eddsa.KeyPair

init {
val ed25519 = eddsa("ed25519")

raw = this.parseRaw(bytes)
keyPair = ed25519.keyFromSecret(raw)
}

private fun parseRaw(bytes: ByteArray): Buffer {
val buffer = Buffer.from(bytes)

if (buffer.length === 43) {
return Buffer.from(buffer.toByteArray().decodeToString().base64UrlDecodedBytes)
}

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

throw Error("invalid raw key");
}

actual fun publicKey(): KMMEdPublicKey {
return KMMEdPublicKey(keyPair.getPublic())
}

actual fun sign(message: ByteArray): ByteArray {
val sig = keyPair.sign(Buffer.from(message))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
package io.iohk.atala.prism.apollo.utils

import io.iohk.atala.prism.apollo.base64.base64UrlDecodedBytes
import io.iohk.atala.prism.apollo.utils.external.eddsa
import node.buffer.Buffer
import node.buffer.BufferEncoding

@ExperimentalJsExport
@JsExport
actual class KMMEdPublicKey(val raw: ByteArray) {
actual class KMMEdPublicKey(bytes: ByteArray) {
val raw: Buffer
private val keyPair: eddsa.KeyPair

init {
val ed25519 = eddsa("ed25519")

keyPair = ed25519.keyFromPublic(raw)
raw = parseRaw(bytes)
val pub = raw.toString(BufferEncoding.hex)

// TODO: Report a bug in elliptic, this method is not expecting a Buffer (bytes)
// Internally it expects to find an array, if not Buffer.slice.concat fails when Array.slice.concat doesn't
// Must keep this...
keyPair = ed25519.keyFromPublic(pub)
}

private fun parseRaw(bytes: ByteArray): Buffer {
val buffer = Buffer.from(bytes)

if (buffer.length === 43) {
return Buffer.from(buffer.toByteArray().decodeToString().base64UrlDecodedBytes)
}

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

throw Error("invalid raw key");
}

actual fun verify(message: ByteArray, sig: ByteArray): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ open external class eddsa(name: String /* "ed25519" */) {
open fun keyFromPublic(pub: _eddsa_KeyPair): _eddsa_KeyPair
open fun keyFromPublic(pub: base.BasePoint): _eddsa_KeyPair
open fun keyFromSecret(secret: String): _eddsa_KeyPair
open fun keyFromSecret(secret: ByteArray): _eddsa_KeyPair
open fun keyFromSecret(secret: Buffer): _eddsa_KeyPair
open fun makeSignature(sig: _eddsa_Signature): _eddsa_Signature
open fun makeSignature(sig: String): _eddsa_Signature
open fun decodePoint(bytes: String): base.BasePoint
Expand Down

0 comments on commit 85ca03b

Please sign in to comment.