Skip to content

Commit 6d02eb0

Browse files
authored
Throw KeyException instead of IllegalArgumentException (#573)
1 parent 9fba851 commit 6d02eb0

File tree

22 files changed

+55
-79
lines changed

22 files changed

+55
-79
lines changed

build-logic/src/main/kotlin/dokka.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ rootProject.dependencies { dokka(project(project.path)) }
2626

2727
extensions.configure<DokkaExtension> {
2828
dokkaPublications.configureEach {
29-
suppressInheritedMembers.set(true)
29+
suppressObviousFunctions.set(true)
3030
}
3131

3232
dokkaSourceSets.configureEach {
3333
includes.from("README.md")
3434
enableKotlinStdLibDocumentationLink.set(false)
3535

3636
externalDocumentationLinks {
37+
register(project.path + ":error") {
38+
url.set(URI("https://error.kotlincrypto.org/"))
39+
}
3740
register(project.path + ":kmp-file") {
3841
url.set(URI("https://kmp-file.matthewnelson.io/"))
3942
}

library/runtime-core/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ kmpConfiguration {
3838
api(libs.kmp.tor.common.api)
3939
implementation(libs.kmp.tor.common.core)
4040
implementation(libs.kotlinx.coroutines.core)
41+
api(kotlincrypto.error.error)
4142
}
4243
}
4344
sourceSetTest {

library/runtime-core/src/commonMain/kotlin/io/matthewnelson/kmp/tor/runtime/core/config/TorSetting.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ public class TorSetting private constructor(
394394
}
395395
}
396396

397+
/** @suppress */
397398
protected companion object {
398399
@JvmSynthetic
399400
internal val INIT = Any()

library/runtime-core/src/commonMain/kotlin/io/matthewnelson/kmp/tor/runtime/core/config/builder/BuilderScopeAutoBoolean.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class BuilderScopeAutoBoolean: TorSetting.BuilderScope {
6767
// TorOption.default is checked for "auto", "1", or "0"
6868
// in tests to ensure proper default is always had for
6969
// TorSetting.BuilderScope.argument, and mitigate runtime
70-
// overhead a `require` block would have here. Test fails
70+
// overhead of a `require` block would have here. Test fails
7171
// if default for given TorOption is invalid.
7272
return BuilderScopeAutoBoolean(this)
7373
}

library/runtime-core/src/commonMain/kotlin/io/matthewnelson/kmp/tor/runtime/core/ctrl/ClientAuthEntry.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package io.matthewnelson.kmp.tor.runtime.core.ctrl
1818
import io.matthewnelson.immutable.collections.toImmutableSet
1919
import io.matthewnelson.kmp.tor.runtime.core.net.OnionAddress
2020
import io.matthewnelson.kmp.tor.runtime.core.key.AuthKey
21+
import org.kotlincrypto.error.KeyException
2122
import kotlin.jvm.JvmField
2223
import kotlin.jvm.JvmStatic
2324

@@ -44,10 +45,9 @@ public class ClientAuthEntry private constructor(
4445
/**
4546
* Creates a new [ClientAuthEntry] for provided key(s).
4647
*
47-
* @throws [IllegalArgumentException] if key types are incompatible.
48+
* @throws [KeyException] if key types are incompatible.
4849
* */
4950
@JvmStatic
50-
@Throws(IllegalArgumentException::class)
5151
public fun of(
5252
address: OnionAddress,
5353
privateKey: AuthKey.Private,
@@ -56,10 +56,12 @@ public class ClientAuthEntry private constructor(
5656
): ClientAuthEntry {
5757
val addressKey = address.asPublicKey()
5858

59-
require(privateKey.isCompatibleWith(addressKey)) {
60-
"Incompatible key types." +
61-
" AddressKey.Public[${addressKey.algorithm()}]." +
62-
" AuthKey.Private[${privateKey.algorithm()}]"
59+
if (!privateKey.isCompatibleWith(addressKey)) {
60+
throw KeyException(
61+
"Incompatible key types." +
62+
" AddressKey.Public[${addressKey.algorithm()}]." +
63+
" AuthKey.Private[${privateKey.algorithm()}]"
64+
)
6365
}
6466

6567
return ClientAuthEntry(

library/runtime-core/src/commonMain/kotlin/io/matthewnelson/kmp/tor/runtime/core/ctrl/HiddenServiceEntry.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package io.matthewnelson.kmp.tor.runtime.core.ctrl
1818
import io.matthewnelson.immutable.collections.toImmutableSet
1919
import io.matthewnelson.kmp.tor.runtime.core.key.AddressKey
2020
import io.matthewnelson.kmp.tor.runtime.core.key.AuthKey
21+
import org.kotlincrypto.error.KeyException
2122
import kotlin.jvm.JvmField
2223
import kotlin.jvm.JvmStatic
2324

@@ -44,10 +45,9 @@ public class HiddenServiceEntry private constructor(
4445
/**
4546
* Creates a new [HiddenServiceEntry] for provided key(s).
4647
*
47-
* @throws [IllegalArgumentException] if key algorithms do not match.
48+
* @throws [KeyException] if key algorithms do not match.
4849
* */
4950
@JvmStatic
50-
@Throws(IllegalArgumentException::class)
5151
public fun of(
5252
publicKey: AddressKey.Public,
5353
privateKey: AddressKey.Private?,
@@ -60,10 +60,12 @@ public class HiddenServiceEntry private constructor(
6060
val aPublic = publicKey.algorithm()
6161
val aPrivate = privateKey.algorithm()
6262

63-
require(aPublic == aPrivate) {
64-
"Incompatible key types. " +
65-
"AddressKey.PublicKey[$aPublic]. " +
66-
"AddressKey.PrivateKey[$aPrivate]"
63+
if (aPublic != aPrivate) {
64+
throw KeyException(
65+
"Incompatible key types. " +
66+
"AddressKey.PublicKey[$aPublic]. " +
67+
"AddressKey.PrivateKey[$aPrivate]"
68+
)
6769
}
6870

6971
return HiddenServiceEntry(

library/runtime-core/src/commonMain/kotlin/io/matthewnelson/kmp/tor/runtime/core/internal/-StringExt.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
package io.matthewnelson.kmp.tor.runtime.core.internal
1919

20-
import io.matthewnelson.encoding.base16.Base16
21-
import io.matthewnelson.encoding.base32.Base32
22-
import io.matthewnelson.encoding.base64.Base64
2320
import io.matthewnelson.encoding.core.Decoder
2421
import io.matthewnelson.encoding.core.Decoder.Companion.decodeToByteArrayOrNull
2522

library/runtime-core/src/commonMain/kotlin/io/matthewnelson/kmp/tor/runtime/core/key/ED25519_V3.kt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import io.matthewnelson.kmp.tor.runtime.core.net.OnionAddress.V3.Companion.toOni
2222
import io.matthewnelson.kmp.tor.runtime.core.internal.tryDecodeOrNull
2323
import io.matthewnelson.kmp.tor.runtime.core.key.ED25519_V3.PublicKey.Companion.toED25519_V3PublicKey
2424
import io.matthewnelson.kmp.tor.runtime.core.key.ED25519_V3.PublicKey.Companion.toED25519_V3PublicKeyOrNull
25+
import org.kotlincrypto.error.InvalidKeyException
2526
import kotlin.jvm.JvmName
2627
import kotlin.jvm.JvmStatic
2728

@@ -60,28 +61,26 @@ public object ED25519_V3: KeyType.Address<ED25519_V3.PublicKey, ED25519_V3.Priva
6061
* address itself, or Base 16/32/64 encoded raw value.
6162
*
6263
* @return [ED25519_V3.PublicKey]
63-
* @throws [IllegalArgumentException] if no key is found
64+
* @throws [InvalidKeyException] if no key is found
6465
* */
6566
@JvmStatic
6667
@JvmName("get")
67-
@Throws(IllegalArgumentException::class)
6868
public fun String.toED25519_V3PublicKey(): PublicKey {
6969
return toED25519_V3PublicKeyOrNull()
70-
?: throw IllegalArgumentException("$this is not an ${algorithm()} public key")
70+
?: throw InvalidKeyException("$this is not an ${algorithm()} public key")
7171
}
7272

7373
/**
7474
* Transforms provided bytes into a ED25519-V3 public key.
7575
*
7676
* @return [ED25519_V3.PublicKey]
77-
* @throws [IllegalArgumentException] if byte array size is inappropriate
77+
* @throws [InvalidKeyException] if byte array size is inappropriate
7878
* */
7979
@JvmStatic
8080
@JvmName("get")
81-
@Throws(IllegalArgumentException::class)
8281
public fun ByteArray.toED25519_V3PublicKey(): PublicKey {
8382
return toED25519_V3PublicKeyOrNull()
84-
?: throw IllegalArgumentException("bytes are not an ${algorithm()} public key")
83+
?: throw InvalidKeyException("bytes are not an ${algorithm()} public key")
8584
}
8685

8786
/**
@@ -147,28 +146,26 @@ public object ED25519_V3: KeyType.Address<ED25519_V3.PublicKey, ED25519_V3.Priva
147146
* String can be a Base 16/32/64 encoded raw value.
148147
*
149148
* @return [ED25519_V3.PrivateKey]
150-
* @throws [IllegalArgumentException] if no key is found
149+
* @throws [InvalidKeyException] if no key is found
151150
* */
152151
@JvmStatic
153152
@JvmName("get")
154-
@Throws(IllegalArgumentException::class)
155153
public fun String.toED25519_V3PrivateKey(): PrivateKey {
156154
return toED25519_V3PrivateKeyOrNull()
157-
?: throw IllegalArgumentException("Tried base 16/32/64 decoding, but failed to find a $BYTE_SIZE byte key")
155+
?: throw InvalidKeyException("Tried base 16/32/64 decoding, but failed to find a $BYTE_SIZE byte key")
158156
}
159157

160158
/**
161159
* Transforms provided bytes into a ED25519-V3 private key.
162160
*
163161
* @return [ED25519_V3.PrivateKey]
164-
* @throws [IllegalArgumentException] if byte array size is inappropriate
162+
* @throws [InvalidKeyException] if byte array size is inappropriate
165163
* */
166164
@JvmStatic
167165
@JvmName("get")
168-
@Throws(IllegalArgumentException::class)
169166
public fun ByteArray.toED25519_V3PrivateKey(): PrivateKey {
170167
return toED25519_V3PrivateKeyOrNull()
171-
?: throw IllegalArgumentException("Invalid key size. Must be $BYTE_SIZE bytes")
168+
?: throw InvalidKeyException("Invalid key size. Must be $BYTE_SIZE bytes")
172169
}
173170

174171
/**

library/runtime-core/src/commonMain/kotlin/io/matthewnelson/kmp/tor/runtime/core/key/X25519.kt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package io.matthewnelson.kmp.tor.runtime.core.key
1818
import io.matthewnelson.kmp.tor.runtime.core.internal.tryDecodeOrNull
1919
import io.matthewnelson.kmp.tor.runtime.core.key.X25519.PublicKey.Companion.toX25519PublicKey
2020
import io.matthewnelson.kmp.tor.runtime.core.key.X25519.PublicKey.Companion.toX25519PublicKeyOrNull
21+
import org.kotlincrypto.error.InvalidKeyException
2122
import kotlin.jvm.JvmName
2223
import kotlin.jvm.JvmStatic
2324
import kotlin.jvm.JvmSynthetic
@@ -60,28 +61,26 @@ public object X25519: KeyType.Auth<X25519.PublicKey, X25519.PrivateKey>() {
6061
* String can be a Base 16/32/64 encoded raw value.
6162
*
6263
* @return [X25519.PublicKey]
63-
* @throws [IllegalArgumentException] if no key is found
64+
* @throws [InvalidKeyException] if no key is found
6465
* */
6566
@JvmStatic
6667
@JvmName("get")
67-
@Throws(IllegalArgumentException::class)
6868
public fun String.toX25519PublicKey(): PublicKey {
6969
return toX25519PublicKeyOrNull()
70-
?: throw IllegalArgumentException("Tried base 16/32/64 decoding, but failed to find a $BYTE_SIZE byte key")
70+
?: throw InvalidKeyException("Tried base 16/32/64 decoding, but failed to find a $BYTE_SIZE byte key")
7171
}
7272

7373
/**
7474
* Transforms provided bytes into a x25519 public key.
7575
*
7676
* @return [X25519.PublicKey]
77-
* @throws [IllegalArgumentException] if byte array size is inappropriate
77+
* @throws [InvalidKeyException] if byte array size is inappropriate
7878
* */
7979
@JvmStatic
8080
@JvmName("get")
81-
@Throws(IllegalArgumentException::class)
8281
public fun ByteArray.toX25519PublicKey(): PublicKey {
8382
return toX25519PublicKeyOrNull()
84-
?: throw IllegalArgumentException("Invalid key size. Must be $BYTE_SIZE bytes")
83+
?: throw InvalidKeyException("Invalid key size. Must be $BYTE_SIZE bytes")
8584
}
8685

8786
/**
@@ -143,28 +142,26 @@ public object X25519: KeyType.Auth<X25519.PublicKey, X25519.PrivateKey>() {
143142
* String can be a Base 16/32/64 encoded raw value.
144143
*
145144
* @return [X25519.PrivateKey]
146-
* @throws [IllegalArgumentException] if no key is found
145+
* @throws [InvalidKeyException] if no key is found
147146
* */
148147
@JvmStatic
149148
@JvmName("get")
150-
@Throws(IllegalArgumentException::class)
151149
public fun String.toX25519PrivateKey(): PrivateKey {
152150
return toX25519PrivateKeyOrNull()
153-
?: throw IllegalArgumentException("Tried base 16/32/64 decoding, but failed to find a $BYTE_SIZE byte key")
151+
?: throw InvalidKeyException("Tried base 16/32/64 decoding, but failed to find a $BYTE_SIZE byte key")
154152
}
155153

156154
/**
157155
* Transforms provided bytes into a x25519 private key.
158156
*
159157
* @return [X25519.PrivateKey]
160-
* @throws [IllegalArgumentException] if byte array size is inappropriate
158+
* @throws [InvalidKeyException] if byte array size is inappropriate
161159
* */
162160
@JvmStatic
163161
@JvmName("get")
164-
@Throws(IllegalArgumentException::class)
165162
public fun ByteArray.toX25519PrivateKey(): PrivateKey {
166163
return toX25519PrivateKeyOrNull()
167-
?: throw IllegalArgumentException("Invalid key size. Must be $BYTE_SIZE bytes")
164+
?: throw InvalidKeyException("Invalid key size. Must be $BYTE_SIZE bytes")
168165
}
169166

170167
/**

library/runtime-core/src/commonMain/kotlin/io/matthewnelson/kmp/tor/runtime/core/net/IPAddress.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public sealed class IPAddress private constructor(
8080
* */
8181
@JvmStatic
8282
@JvmName("get")
83-
@Throws(IllegalArgumentException::class)
8483
public fun String.toIPAddress(): IPAddress {
8584
return toIPAddressOrNull()
8685
?: throw IllegalArgumentException("$this does not contain an IP address")
@@ -94,7 +93,6 @@ public sealed class IPAddress private constructor(
9493
* */
9594
@JvmStatic
9695
@JvmName("get")
97-
@Throws(IllegalArgumentException::class)
9896
public fun ByteArray.toIPAddress(): IPAddress {
9997
return toIPAddressOrNull()
10098
?: throw IllegalArgumentException("Invalid array size[$size]")
@@ -163,7 +161,6 @@ public sealed class IPAddress private constructor(
163161
* */
164162
@JvmStatic
165163
@JvmName("get")
166-
@Throws(IllegalArgumentException::class)
167164
public fun String.toIPAddressV4(): V4 {
168165
return toIPAddressV4OrNull()
169166
?: throw IllegalArgumentException("$this does not contain an IPv4 address")
@@ -177,7 +174,6 @@ public sealed class IPAddress private constructor(
177174
* */
178175
@JvmStatic
179176
@JvmName("get")
180-
@Throws(IllegalArgumentException::class)
181177
public fun ByteArray.toIPAddressV4(): V4 {
182178
return toIPAddressV4(copy = true)
183179
}
@@ -322,7 +318,6 @@ public sealed class IPAddress private constructor(
322318
* string, or an integer less than 1.
323319
* */
324320
@JvmStatic
325-
@Throws(IllegalArgumentException::class)
326321
public fun of(scope: String?): AnyHost {
327322
if (scope == null) return NoScope
328323
val msg = scope.isValidScopeOrErrorMessage()
@@ -345,7 +340,6 @@ public sealed class IPAddress private constructor(
345340
* */
346341
@JvmStatic
347342
@JvmName("get")
348-
@Throws(IllegalArgumentException::class)
349343
public fun String.toIPAddressV6(): V6 {
350344
return toIPAddressV6OrNull()
351345
?: throw IllegalArgumentException("$this does not contain a valid IPv6 address")
@@ -362,7 +356,6 @@ public sealed class IPAddress private constructor(
362356
@JvmStatic
363357
@JvmOverloads
364358
@JvmName("get")
365-
@Throws(IllegalArgumentException::class)
366359
public fun ByteArray.toIPAddressV6(scope: String? = null): V6 {
367360
return toIPAddressV6(scope, copy = true)
368361
}

0 commit comments

Comments
 (0)