Fixing hex and time functions

This commit is contained in:
Vitor Pamplona
2026-04-06 09:23:51 -04:00
parent 5727d50fce
commit a1145e2a28
3 changed files with 9 additions and 7 deletions
@@ -41,6 +41,7 @@ import com.vitorpamplona.amethyst.ui.navigation.navs.INav
import com.vitorpamplona.amethyst.ui.navigation.routes.Route import com.vitorpamplona.amethyst.ui.navigation.routes.Route
import com.vitorpamplona.amethyst.ui.navigation.topbars.CreatingTopBar import com.vitorpamplona.amethyst.ui.navigation.topbars.CreatingTopBar
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlin.random.Random import kotlin.random.Random
@@ -61,7 +62,7 @@ fun CreateGroupScreen(
onPost = { onPost = {
isCreating = true isCreating = true
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
val nostrGroupId = Random.nextBytes(32).joinToString("") { "%02x".format(it) } val nostrGroupId = Random.nextBytes(32).toHexKey()
accountViewModel.createMarmotGroup(nostrGroupId) accountViewModel.createMarmotGroup(nostrGroupId)
if (groupName.isNotBlank()) { if (groupName.isNotBlank()) {
accountViewModel.account.marmotGroupList accountViewModel.account.marmotGroupList
@@ -30,6 +30,7 @@ import com.vitorpamplona.quartz.marmot.mls.tree.Credential
import com.vitorpamplona.quartz.marmot.mls.tree.LeafNode import com.vitorpamplona.quartz.marmot.mls.tree.LeafNode
import com.vitorpamplona.quartz.marmot.mls.tree.LeafNodeSource import com.vitorpamplona.quartz.marmot.mls.tree.LeafNodeSource
import com.vitorpamplona.quartz.marmot.mls.tree.Lifetime import com.vitorpamplona.quartz.marmot.mls.tree.Lifetime
import com.vitorpamplona.quartz.utils.TimeUtils
/** /**
* Manages KeyPackage creation and rotation lifecycle (MIP-00). * Manages KeyPackage creation and rotation lifecycle (MIP-00).
@@ -190,7 +191,7 @@ class KeyPackageRotationManager {
identity: ByteArray, identity: ByteArray,
signingKey: ByteArray, signingKey: ByteArray,
): LeafNode { ): LeafNode {
val now = System.currentTimeMillis() / 1000 val now = TimeUtils.now()
val unsigned = val unsigned =
LeafNode( LeafNode(
encryptionKey = encryptionKey, encryptionKey = encryptionKey,
@@ -55,6 +55,8 @@ import com.vitorpamplona.quartz.marmot.mls.tree.LeafNodeSource
import com.vitorpamplona.quartz.marmot.mls.tree.Lifetime import com.vitorpamplona.quartz.marmot.mls.tree.Lifetime
import com.vitorpamplona.quartz.marmot.mls.tree.RatchetTree import com.vitorpamplona.quartz.marmot.mls.tree.RatchetTree
import com.vitorpamplona.quartz.marmot.mls.tree.UpdatePathNode import com.vitorpamplona.quartz.marmot.mls.tree.UpdatePathNode
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import com.vitorpamplona.quartz.utils.TimeUtils
import com.vitorpamplona.quartz.utils.mac.MacInstance import com.vitorpamplona.quartz.utils.mac.MacInstance
/** /**
@@ -170,11 +172,9 @@ class MlsGroup private constructor(
pskId: ByteArray, pskId: ByteArray,
psk: ByteArray, psk: ByteArray,
) { ) {
pskStore[pskId.toHexId()] = psk pskStore[pskId.toHexKey()] = psk
} }
private fun ByteArray.toHexId(): String = joinToString("") { "%02x".format(it) }
/** /**
* Get the list of members (leaf index -> LeafNode). * Get the list of members (leaf index -> LeafNode).
*/ */
@@ -845,7 +845,7 @@ class MlsGroup private constructor(
var pskSecret = ByteArray(MlsCryptoProvider.HASH_OUTPUT_LENGTH) var pskSecret = ByteArray(MlsCryptoProvider.HASH_OUTPUT_LENGTH)
for (pskProposal in pskProposals) { for (pskProposal in pskProposals) {
val pskValue = val pskValue =
pskStore[pskProposal.pskId.toHexId()] pskStore[pskProposal.pskId.toHexKey()]
?: ByteArray(MlsCryptoProvider.HASH_OUTPUT_LENGTH) // Unknown PSK = zeros ?: ByteArray(MlsCryptoProvider.HASH_OUTPUT_LENGTH) // Unknown PSK = zeros
pskSecret = MlsCryptoProvider.hkdfExtract(pskSecret, pskValue) pskSecret = MlsCryptoProvider.hkdfExtract(pskSecret, pskValue)
} }
@@ -990,7 +990,7 @@ class MlsGroup private constructor(
// Validate KeyPackage lifetime (RFC 9420 Section 10.1) // Validate KeyPackage lifetime (RFC 9420 Section 10.1)
val lifetime = proposal.keyPackage.leafNode.lifetime val lifetime = proposal.keyPackage.leafNode.lifetime
if (lifetime != null) { if (lifetime != null) {
val now = System.currentTimeMillis() / 1000 val now = TimeUtils.now()
require(now >= lifetime.notBefore && now <= lifetime.notAfter) { require(now >= lifetime.notBefore && now <= lifetime.notAfter) {
"KeyPackage lifetime expired or not yet valid" "KeyPackage lifetime expired or not yet valid"
} }