Skip to content

Commit b78cfc8

Browse files
authored
Merge pull request #102 from avianlabs/secretbox-nullability
make secretbox methods return nullable
2 parents 9fa91e2 + c2f1fcf commit b78cfc8

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

tweetnacl-multiplatform/src/commonMain/kotlin/net/avianlabs/solana/tweetnacl/TweetNaCl.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public interface TweetNaCl {
1616
*
1717
* @param message: The message to sign
1818
* @param secretKey: The secret key to sign the message with
19+
*
1920
* @return The signature of the message
2021
*/
2122
public fun sign(message: ByteArray, secretKey: ByteArray): ByteArray
@@ -24,6 +25,7 @@ public interface TweetNaCl {
2425
* Returns a new signing key pair generated deterministically from a seed
2526
*
2627
* @param seed: 32 byte seed. Must contain enough entropy to be secure.
28+
*
2729
* @return A new key pair generated from the seed
2830
*/
2931
public fun generateKey(seed: ByteArray): Ed25519Keypair
@@ -32,6 +34,7 @@ public interface TweetNaCl {
3234
* Returns whether the given publicKey falls in the Ed25519 elliptic curve
3335
*
3436
* @param publicKey: The public key to check
37+
*
3538
* @return Whether the public key is on the curve
3639
*/
3740
public fun isOnCurve(publicKey: ByteArray): Boolean
@@ -65,18 +68,20 @@ public interface TweetNaCl {
6568
*
6669
* @param message: The message to encrypt
6770
* @param nonce: The nonce to use for encryption
68-
* @return The encrypted message
71+
*
72+
* @return The encrypted message, or null if there was an error
6973
*/
70-
public fun box(message: ByteArray, nonce: ByteArray): ByteArray
74+
public fun box(message: ByteArray, nonce: ByteArray): ByteArray?
7175

7276
/**
7377
* Authenticates and decrypts the given secret box using the key and the nonce.
7478
*
7579
* @param box: The encrypted message
7680
* @param nonce: The nonce used for encryption
77-
* @return The decrypted message
81+
*
82+
* @return The decrypted message, or null if there was an error
7883
*/
79-
public fun open(box: ByteArray, nonce: ByteArray): ByteArray
84+
public fun open(box: ByteArray, nonce: ByteArray): ByteArray?
8085

8186
public companion object {
8287
public const val NONCE_BYTES: Int = 24

tweetnacl-multiplatform/src/commonTest/kotlin/SecretBoxTest.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.avianlabs.solana.tweetnacl
22

33
import kotlin.test.Test
4-
import kotlin.test.assertContentEquals
54
import kotlin.test.assertEquals
65

76
@OptIn(ExperimentalStdlibApi::class)
@@ -16,7 +15,7 @@ class SecretBoxTest {
1615

1716
assertEquals(
1817
"6b6608e77e7c4024147baa01d7576a3a5a2852a6278bac8eca4db1e6",
19-
result.toHexString(),
18+
result!!.toHexString(),
2019
)
2120
}
2221

@@ -27,10 +26,10 @@ class SecretBoxTest {
2726
"6b6608e77e7c4024147baa01d7576a3a5a2852a6278bac8eca4db1e6".hexToByteArray(),
2827
ByteArray(24)
2928
)
30-
29+
3130
assertEquals(
3231
"Hello world!",
33-
result.decodeToString(),
32+
result!!.decodeToString(),
3433
)
3534
}
36-
}
35+
}

tweetnacl-multiplatform/src/jvmMain/kotlin/net/avianlabs/solana/tweetnacl/TweetNaCl.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ internal actual fun generateKeyInternal(seed: ByteArray): Ed25519Keypair {
2020

2121
internal actual fun secretBoxInternal(secretKey: ByteArray): TweetNaCl.SecretBox =
2222
object : TweetNaCl.SecretBox {
23-
override fun box(message: ByteArray, nonce: ByteArray): ByteArray =
23+
override fun box(message: ByteArray, nonce: ByteArray): ByteArray? =
2424
TweetNaclFast.SecretBox(secretKey).box(message, nonce)
2525

26-
override fun open(box: ByteArray, nonce: ByteArray): ByteArray =
26+
override fun open(box: ByteArray, nonce: ByteArray): ByteArray? =
2727
TweetNaclFast.SecretBox(secretKey).open(box, nonce)
28-
}
28+
}

0 commit comments

Comments
 (0)