Skip to content

Commit fa59e07

Browse files
committed
refactor: replace reflection in deriveSigningKey
1 parent 306ee77 commit fa59e07

1 file changed

Lines changed: 17 additions & 22 deletions

File tree

app/src/main/java/to/bitkit/services/RNBackupClient.kt

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import org.ldk.structs.KeysManager
2424
import org.lightningdevkit.ldknode.Network
2525
import to.bitkit.data.keychain.Keychain
2626
import to.bitkit.di.IoDispatcher
27-
import to.bitkit.di.json
2827
import to.bitkit.env.Env
2928
import to.bitkit.utils.AppError
3029
import to.bitkit.utils.Crypto
3130
import to.bitkit.utils.Logger
3231
import javax.crypto.Cipher
32+
import javax.crypto.Mac
3333
import javax.crypto.spec.GCMParameterSpec
3434
import javax.crypto.spec.SecretKeySpec
3535
import javax.inject.Inject
@@ -59,7 +59,7 @@ class RNBackupClient @Inject constructor(
5959
suspend fun listFiles(fileGroup: String? = "ldk"): RNBackupListResponse? = withContext(ioDispatcher) {
6060
runCatching {
6161
val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name)
62-
?: throw RNBackupError.NotSetup
62+
?: throw RNBackupError.NotSetup()
6363
val passphrase = keychain.loadString(Keychain.Key.BIP39_PASSPHRASE.name)
6464

6565
val bearer = authenticate(mnemonic, passphrase)
@@ -81,7 +81,7 @@ class RNBackupClient @Inject constructor(
8181
suspend fun retrieve(label: String, fileGroup: String? = null): ByteArray? = withContext(ioDispatcher) {
8282
runCatching {
8383
val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name)
84-
?: throw RNBackupError.NotSetup
84+
?: throw RNBackupError.NotSetup()
8585
val passphrase = keychain.loadString(Keychain.Key.BIP39_PASSPHRASE.name)
8686

8787
val bearer = authenticate(mnemonic, passphrase)
@@ -108,8 +108,7 @@ class RNBackupClient @Inject constructor(
108108

109109
suspend fun retrieveChannelMonitor(channelId: String): ByteArray? = withContext(ioDispatcher) {
110110
runCatching {
111-
val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name)
112-
?: throw RNBackupError.NotSetup
111+
val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name) ?: throw RNBackupError.NotSetup()
113112
val passphrase = keychain.loadString(Keychain.Key.BIP39_PASSPHRASE.name)
114113

115114
val bearer = authenticate(mnemonic, passphrase)
@@ -236,7 +235,7 @@ class RNBackupClient @Inject constructor(
236235
}
237236

238237
if (!challengeResponse.status.isSuccess()) {
239-
throw RNBackupError.AuthFailed
238+
throw RNBackupError.AuthFailed()
240239
}
241240

242241
val challengeResult = challengeResponse.body<AuthChallengeResponse>()
@@ -254,7 +253,7 @@ class RNBackupClient @Inject constructor(
254253
}
255254

256255
if (!authResponse.status.isSuccess()) {
257-
throw RNBackupError.AuthFailed
256+
throw RNBackupError.AuthFailed()
258257
}
259258

260259
authResponse.body<AuthBearerResponse>().also { cachedBearer = it }
@@ -283,12 +282,7 @@ class RNBackupClient @Inject constructor(
283282

284283
return runCatching {
285284
val keysManager = KeysManager.of(bip32Seed, seconds, nanoSeconds)
286-
val method = keysManager.javaClass.getMethod("get_node_secret_key")
287-
when (val nodeSecretKey = method.invoke(keysManager)) {
288-
is ByteArray -> nodeSecretKey
289-
is List<*> -> nodeSecretKey.map { (it as UByte).toByte() }.toByteArray()
290-
else -> throw ClassCastException("Unexpected type: ${nodeSecretKey?.javaClass?.name}")
291-
}
285+
keysManager._node_secret_key
292286
}.getOrElse { bip32Seed }
293287
}
294288

@@ -306,9 +300,12 @@ class RNBackupClient @Inject constructor(
306300
return (generator.generateDerivedParameters(512) as KeyParameter).key
307301
}
308302

303+
@Suppress("SpellCheckingInspection")
309304
private fun deriveMasterKey(seed: ByteArray): ByteArray {
310-
val hmac = javax.crypto.Mac.getInstance("HmacSHA512")
311-
val keySpec = javax.crypto.spec.SecretKeySpec("Bitcoin seed".toByteArray(), "HmacSHA512")
305+
@Suppress("SpellCheckingInspection")
306+
val algorithm = "HmacSHA512"
307+
val hmac = Mac.getInstance(algorithm)
308+
val keySpec = SecretKeySpec("Bitcoin seed".toByteArray(), algorithm)
312309
hmac.init(keySpec)
313310
val i = hmac.doFinal(seed)
314311
return i.sliceArray(0 until 32)
@@ -332,9 +329,7 @@ class RNBackupClient @Inject constructor(
332329
return cipher.doFinal(ciphertext + tag)
333330
}
334331

335-
private fun ByteArray.toHex(): String {
336-
return this.joinToString("") { "%02x".format(it) }
337-
}
332+
private fun ByteArray.toHex(): String = this.joinToString("") { "%02x".format(it) }
338333
}
339334

340335
@Serializable
@@ -355,8 +350,8 @@ data class RNBackupListResponse(
355350
)
356351

357352
sealed class RNBackupError(message: String) : AppError(message) {
358-
data object NotSetup : RNBackupError("RN backup client not setup")
359-
data object AuthFailed : RNBackupError("Authentication failed")
360-
data class RequestFailed(override val message: String) : RNBackupError(message)
361-
data class DecryptFailed(override val message: String) : RNBackupError(message)
353+
class NotSetup : RNBackupError("RN backup client not setup")
354+
class AuthFailed : RNBackupError("Authentication failed")
355+
class RequestFailed(override val message: String) : RNBackupError(message)
356+
class DecryptFailed(override val message: String) : RNBackupError(message)
362357
}

0 commit comments

Comments
 (0)