-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(BAE): updating jsMain X25519 keys for use in TS
- Loading branch information
Showing
11 changed files
with
238 additions
and
37 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
...mmetric-encryption/src/commonMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdKeyPair.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
1 change: 0 additions & 1 deletion
1
...tric-encryption/src/commonMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
public expect class KMMEdPrivateKey { | ||
fun publicKey(): KMMEdPublicKey | ||
fun sign(message: ByteArray): ByteArray | ||
} |
3 changes: 3 additions & 0 deletions
3
...ric-encryption/src/commonMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519KeyPair.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
32 changes: 32 additions & 0 deletions
32
...mmetric-encryption/src/jsMain/kotlin/io/iohk/atala/prism/apollo/utils/Curve25519Parser.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,32 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
import io.iohk.atala.prism.apollo.base64.base64UrlDecodedBytes | ||
import node.buffer.Buffer | ||
|
||
@ExperimentalJsExport | ||
@JsExport | ||
object Curve25519Parser { | ||
val encodedLength = 43 | ||
val rawLength = 32 | ||
|
||
/** | ||
* @function parseRaw | ||
* resolve the raw key value from a given ByteArray | ||
* @param bytes - ByteArray to be parsed, either Encoded or Raw format | ||
* @throws Error - if bytes is neither Encoded nor Raw | ||
* @return Buffer - raw key value | ||
*/ | ||
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") | ||
} | ||
} |
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
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
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
35 changes: 34 additions & 1 deletion
35
...tric-encryption/src/jsMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.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 |
---|---|---|
@@ -1,5 +1,38 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
import io.iohk.atala.prism.apollo.base64.base64UrlEncoded | ||
import io.iohk.atala.prism.apollo.utils.external.KeyPair | ||
import io.iohk.atala.prism.apollo.utils.external.generateKeyPairFromSeed | ||
import node.buffer.Buffer | ||
|
||
@ExperimentalJsExport | ||
@JsExport | ||
actual class KMMX25519PrivateKey(val raw: ByteArray) | ||
actual class KMMX25519PrivateKey(bytes: ByteArray) { | ||
val raw: Buffer | ||
|
||
init { | ||
raw = Curve25519Parser.parseRaw(bytes) | ||
} | ||
|
||
/** | ||
* @function getEncoded | ||
* @return Buffer - base64UrlEncoded version of the raw value | ||
*/ | ||
fun getEncoded(): Buffer { | ||
return Buffer.from(raw.toByteArray().base64UrlEncoded) | ||
} | ||
|
||
/** | ||
* @function publicKey | ||
* @return KMMX25519PublicKey - corresponding PublicKey | ||
*/ | ||
fun publicKey(): KMMX25519PublicKey { | ||
val publicBytes = getInstance().publicKey.buffer.toByteArray() | ||
|
||
return KMMX25519PublicKey(publicBytes) | ||
} | ||
|
||
private fun getInstance(): KeyPair { | ||
return generateKeyPairFromSeed(raw) | ||
} | ||
} |
18 changes: 17 additions & 1 deletion
18
...etric-encryption/src/jsMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PublicKey.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 |
---|---|---|
@@ -1,5 +1,21 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
import io.iohk.atala.prism.apollo.base64.base64UrlEncoded | ||
import node.buffer.Buffer | ||
|
||
@ExperimentalJsExport | ||
@JsExport | ||
actual class KMMX25519PublicKey(val raw: ByteArray) | ||
actual class KMMX25519PublicKey(bytes: ByteArray) { | ||
val raw: Buffer | ||
init { | ||
raw = Curve25519Parser.parseRaw(bytes) | ||
} | ||
|
||
/** | ||
* @function getEncoded | ||
* @return Buffer - base64UrlEncoded version of the raw value | ||
*/ | ||
fun getEncoded(): Buffer { | ||
return Buffer.from(raw.toByteArray().base64UrlEncoded.encodeToByteArray()) | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
...symmetric-encryption/src/jsTest/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519Tests.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,58 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
import node.buffer.Buffer | ||
import kotlin.test.Test | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertTrue | ||
|
||
class KMMX25519Tests { | ||
private val raw = arrayOf(51, 115, 246, 68, 98, 108, 130, 79, 66, 173, 201, 51, 112, 98, 163, 196, 188, 34, 100, 148, 28, 98, 236, 251, 234, 41, 3, 175, 80, 1, 64, 152) | ||
private val rawBytes = Buffer.from(raw).toByteArray() | ||
private val encoded = arrayOf(77, 51, 80, 50, 82, 71, 74, 115, 103, 107, 57, 67, 114, 99, 107, 122, 99, 71, 75, 106, 120, 76, 119, 105, 90, 74, 81, 99, 89, 117, 122, 55, 54, 105, 107, 68, 114, 49, 65, 66, 81, 74, 103) | ||
private val encodedBytes = Buffer.from(encoded).toByteArray() | ||
private val publicRaw = arrayOf(212, 97, 242, 116, 254, 39, 85, 254, 32, 125, 72, 58, 203, 231, 151, 68, 217, 36, 15, 137, 108, 58, 150, 193, 48, 67, 203, 34, 115, 180, 148, 27) | ||
private val publicRawBytes = Buffer.from(publicRaw).toByteArray() | ||
private val publicEncoded = arrayOf(49, 71, 72, 121, 100, 80, 52, 110, 86, 102, 52, 103, 102, 85, 103, 54, 121, 45, 101, 88, 82, 78, 107, 107, 68, 52, 108, 115, 79, 112, 98, 66, 77, 69, 80, 76, 73, 110, 79, 48, 108, 66, 115) | ||
private val publicEncodedBytes = Buffer.from(publicEncoded).toByteArray() | ||
|
||
@Test | ||
fun testGenerateKeyPair() { | ||
val keyPair = KMMX25519KeyPair.generateKeyPair() | ||
|
||
assertNotNull(keyPair) | ||
assertNotNull(keyPair.privateKey) | ||
assertNotNull(keyPair.publicKey) | ||
} | ||
|
||
@Test | ||
fun testConstructorRaw() { | ||
val key = KMMX25519PrivateKey(rawBytes) | ||
|
||
assertTrue(key.raw.toByteArray() contentEquals rawBytes) | ||
assertTrue(key.getEncoded().toByteArray() contentEquals encodedBytes) | ||
} | ||
|
||
@Test | ||
fun testConstructorEncoded() { | ||
val key = KMMX25519PrivateKey(encodedBytes) | ||
|
||
assertTrue(key.raw.toByteArray() contentEquals rawBytes) | ||
assertTrue(key.getEncoded().toByteArray() contentEquals encodedBytes) | ||
} | ||
|
||
@Test | ||
fun testGetEncoded() { | ||
val key = KMMX25519PrivateKey(rawBytes) | ||
|
||
assertTrue(key.getEncoded().toByteArray() contentEquals encodedBytes) | ||
} | ||
|
||
@Test | ||
fun testPublicKey() { | ||
val privateKey = KMMX25519PrivateKey(rawBytes) | ||
val publicKey = privateKey.publicKey() | ||
|
||
assertTrue(publicKey.raw.toByteArray() contentEquals publicRawBytes) | ||
assertTrue(publicKey.getEncoded().toByteArray() contentEquals publicEncodedBytes) | ||
} | ||
} |