Skip to content

Commit

Permalink
Method reused. More unit tests added.
Browse files Browse the repository at this point in the history
  • Loading branch information
raf-rdx committed Jan 4, 2023
1 parent 0a0b0e5 commit 801c56a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/radixdlt/model/ECKeyPair.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class PrivateKey(val key: BigInteger, val curveType: EllipticCurveType) {
}

fun toHexString(): String {
return key.toBytesPadded(PRIVATE_KEY_SIZE).toHexString()
return keyByteArray().toHexString()
}

override fun hashCode(): Int {
Expand Down
19 changes: 19 additions & 0 deletions src/test/kotlin/com/radixdlt/slip10/PrivateKeyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.radixdlt.model.PrivateKey
import org.junit.jupiter.api.Test
import kotlin.random.Random
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class PrivateKeyTest {

Expand All @@ -32,4 +33,22 @@ class PrivateKeyTest {
val privateKey = PrivateKey(bytes, EllipticCurveType.Ed25519)
assertEquals(privateKey.keyByteArray().size, PRIVATE_KEY_SIZE)
}

@Test
fun `when byte array is 32 bytes but has only 1, verify the same is returned from keyByteArray`() {
val byteArray =
byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
val privateKey = PrivateKey(byteArray, EllipticCurveType.Ed25519)
assertTrue(privateKey.keyByteArray().contentEquals(byteArray))
}

@Test
fun `when byte array is only one byte (1), verify that output is also one but has 32 bytes length`() {
val byteArray =
byteArrayOf(1)
val outputByteArray =
byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
val privateKey = PrivateKey(byteArray, EllipticCurveType.Ed25519)
assertTrue(privateKey.keyByteArray().contentEquals(outputByteArray))
}
}

0 comments on commit 801c56a

Please sign in to comment.