diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml
index 7232b68b..9322683b 100644
--- a/composeApp/src/commonMain/composeResources/values/strings.xml
+++ b/composeApp/src/commonMain/composeResources/values/strings.xml
@@ -200,6 +200,10 @@
Everyone has answered. There is nobody left to ask.
One member has not answered. They may never have received it.
%1$s members have not answered. They may never have received it.
+ Asked the signers who have not signed their part to take another look
+ One signer has not sent their part. They may never have received the signer set.
+ %1$s signers have not sent their part. They may never have received the signer set.
+ Every signer has sent their part. There is nobody left to ask.
Asked again just now. You can ask again in a few minutes.
Read to the end of the list to sign.
Recovery phrase
diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/FrostSigningSessionDao.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/FrostSigningSessionDao.kt
index 57fcd390..64d64176 100644
--- a/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/FrostSigningSessionDao.kt
+++ b/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/FrostSigningSessionDao.kt
@@ -94,10 +94,11 @@ interface FrostSigningSessionDao {
/**
* Every message a session has received, of every kind.
*
- * What a resend reads to work out who has said nothing at all: a member with
- * any row here has answered -- offered a nonce, signed their part, or
- * refused -- and is not somebody to knock on the door of. See
- * `FrostSigningManager.silentMembers`.
+ * What a resend reads to work out who it is still waiting on. Which rows
+ * count as an answer depends on what the session is collecting: while it
+ * wants nonces, any row at all -- a nonce, a partial signature, a refusal;
+ * once the signers are chosen, only their partial signature or their
+ * refusal. See `FrostSigningManager.membersOwing`.
*/
@Query("SELECT * FROM FrostSignerMessage WHERE sessionId = :sessionId ORDER BY createdAt ASC")
suspend fun getMessagesForSession(sessionId: String): List
diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseFrostSigningRepository.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseFrostSigningRepository.kt
index aa9daa47..f1164c4a 100644
--- a/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseFrostSigningRepository.kt
+++ b/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseFrostSigningRepository.kt
@@ -128,6 +128,16 @@ class DatabaseFrostSigningRepository(
false
}
+ override suspend fun membersOwing(session: FrostSigningSession): Set = try {
+ FrostSigningManager.membersOwing(database = database, session = session)
+ } catch (e: Throwable) {
+ // Empty rather than rethrown: this is read on every emission of the
+ // signing screen, and a session whose ceremony cannot be read should
+ // offer no knock rather than take the screen down.
+ logger.e("Error reading who signing session ${session.id} is waiting on", e)
+ emptySet()
+ }
+
override suspend fun approve(localChatRoom: LocalChatRoom, sessionId: String) {
try {
FrostSigningManager.approve(
diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/managers/FrostSigningManager.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/managers/FrostSigningManager.kt
index 6e4c23f5..62f31dab 100644
--- a/composeApp/src/commonMain/kotlin/to/curare/compose/managers/FrostSigningManager.kt
+++ b/composeApp/src/commonMain/kotlin/to/curare/compose/managers/FrostSigningManager.kt
@@ -1905,6 +1905,15 @@ object FrostSigningManager {
* have that member believe the resender coordinates -- and then drop the
* real coordinator's signer set through [isFromCoordinator].
*
+ * ### Who it goes to
+ *
+ * [membersOwing], which asks what the stage makes sense of: who has said
+ * nothing at all while nonces are being collected, and which of the chosen
+ * signers has yet to sign once they have been chosen. The first question
+ * asked at the second stage names only members who owe nothing, which is
+ * how a session could sit in [FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES]
+ * with a control that refused to knock.
+ *
* ### The minimum the stage requires
*
* | stage | what goes out |
@@ -1962,9 +1971,9 @@ object FrostSigningManager {
}
}
- val silent = silentMembers(database, session)
- if (silent.isEmpty()) {
- logger.i("Session $sessionId has heard from every share holder; nothing to resend")
+ val owed = membersOwing(database, session)
+ if (owed.isEmpty()) {
+ logger.i("Session $sessionId is waiting on nobody; nothing to resend")
return false
}
@@ -1981,8 +1990,8 @@ object FrostSigningManager {
}
logger.i(
- "Resending session $sessionId at ${session.stage} to ${silent.size} member(s) " +
- "(${silent.joinToString { it.take(8) }})"
+ "Resending session $sessionId at ${session.stage} to ${owed.size} member(s) " +
+ "(${owed.joinToString { it.take(8) }})"
)
// The proposal first, so a device that has nothing has somewhere to file
@@ -1994,7 +2003,7 @@ object FrostSigningManager {
kind = FrostSigningEvents.PROPOSAL,
content = FrostSigningEvents.encodeProposal(unsigned),
includeKey = true,
- silentMembers = silent.toList()
+ silentMembers = owed.toList()
)
when (session.stage) {
@@ -2030,13 +2039,14 @@ object FrostSigningManager {
}
update(database, session) { it.copy(resentAt = now) }
- announceResent(database, session, silent.size)
+ announceResent(database, session, owed.size)
return true
}
/**
- * The share holders this session has heard nothing at all from.
+ * The share holders this session is still waiting on, which is a different
+ * set of people at each stage.
*
* Off the ceremony rather than off the room, like [recordDecline]'s own
* count and `FrostSigningUIState.Loaded.members`: a room can hold members
@@ -2044,23 +2054,69 @@ object FrostSigningManager {
* room is the parent, which holds everybody the subgroup is not. A door that
* cannot answer is not one to knock on.
*
- * A member who declined is not silent. They answered, and the session is
- * counting their refusal; knocking again would be asking a question they
- * have already closed.
+ * ### The question changes when the stage does
+ *
+ * While nonces are being collected the question is who has said *nothing*,
+ * and a member with any message on file has answered it.
+ *
+ * Past that point that reading is not merely imprecise, it is inverted. A
+ * session reaches [FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES] only
+ * because enough members offered a nonce, so everybody who could be holding
+ * it up has been heard from -- and asking who is silent returns the members
+ * who were never chosen and owe nothing, while the chosen signer whose
+ * partial signature went missing is never named. That is a knock on the
+ * wrong door; and once the unchosen have answered or refused it is no knock
+ * at all, because the set is empty and [resend] gives up before it reaches
+ * the stage it was written for.
+ *
+ * So the later stage asks its own question: of the signers the coordinator
+ * chose, who has yet to send a partial signature. Naming them is the half of
+ * a resend that only they can carry out -- a partial signature is read by
+ * nobody but the coordinator, so one lost on the way here can be repeated by
+ * no other device. See [republishOwn].
+ *
+ * [FrostSigningStage.COMPLETE] keeps the first reading, and correctly: the
+ * signature is the coordinator's own and nobody answers it, so having heard
+ * nothing at all is still exactly what it means to have missed the session.
+ *
+ * A member who declined is never here, at either stage. They answered, and
+ * the session is counting their refusal; knocking again would be asking a
+ * question they have already closed.
*/
- private suspend fun silentMembers(
+ suspend fun membersOwing(
database: CurareDatabase,
session: FrostSigningSession
): Set {
val key = database.dkgSessionDao().getSessionById(session.dkgSessionId) ?: return emptySet()
- val holders = signerIds(database, key).keys
+ val holders = signerIds(database, key)
- val heardFrom = database.frostSigningSessionDao()
+ val collectingPartials = session.stage == FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES
+ val chosen = session.signerIdList()?.toSet()
+
+ // Whose answer the session is short of: the chosen signers once there
+ // are any, every share holder before that. A stage that says signers
+ // have been chosen and names none is nothing to knock about -- it cannot
+ // happen, since [advance] writes the ids before it moves the stage, and
+ // guessing at the set would be guessing at who is owed.
+ val asked = when {
+ !collectingPartials -> holders.keys
+ chosen != null -> holders.filterValues { it in chosen }.keys
+ else -> return emptySet()
+ }
+
+ val answered = database.frostSigningSessionDao()
.getMessagesForSession(session.id)
+ .filter { message ->
+ // A nonce is the previous stage's answer. Counting it here is
+ // what made every signer look like somebody who had replied.
+ !collectingPartials ||
+ message.kind == FrostSigningEvents.PARTIAL_SIGNATURE ||
+ message.kind == FrostSigningEvents.DECLINE
+ }
.map { it.signerPublicKey }
.toSet()
- return holders - heardFrom - session.userPublicKey
+ return asked - answered - session.userPublicKey
}
/**
@@ -2160,15 +2216,26 @@ object FrostSigningManager {
private suspend fun announceResent(
database: CurareDatabase,
session: FrostSigningSession,
- silent: Int
+ owed: Int
) {
+ val one = owed == 1
+ val who = if (one) "the member" else "the $owed members"
+
+ // What they are short of, not just that they are quiet. Past the nonces
+ // the members being asked have answered once already, and a line saying
+ // they have not answered would be the screen contradicting itself.
+ val what = when (session.stage) {
+ FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES ->
+ if (one) "who has not signed their part" else "who have not signed their part"
+
+ else -> if (one) "who has not answered" else "who have not answered"
+ }
+
announce(
database = database,
session = session,
messageType = ChatMessage.TYPE_FROST_RESENT,
- content = "asked " +
- (if (silent == 1) "the member" else "the $silent members") +
- " who have not answered to take another look.",
+ content = "asked $who $what to take another look.",
actor = session.coordinatorPublicKey
)
}
diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/repository/FrostSigningRepository.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/repository/FrostSigningRepository.kt
index 41ec4783..86144636 100644
--- a/composeApp/src/commonMain/kotlin/to/curare/compose/repository/FrostSigningRepository.kt
+++ b/composeApp/src/commonMain/kotlin/to/curare/compose/repository/FrostSigningRepository.kt
@@ -146,10 +146,22 @@ interface FrostSigningRepository {
* already said.
*
* Returns false when there was nothing to do: not this device's session,
- * already abandoned, nobody quiet, or inside the cooldown.
+ * already abandoned, nobody owed, or inside the cooldown.
*/
suspend fun resend(localChatRoom: LocalChatRoom, sessionId: String): Boolean
+ /**
+ * The share holders a session is still waiting on, as the manager counts
+ * them -- who has said nothing while it collects nonces, and which chosen
+ * signer has yet to sign once it collects partial signatures.
+ *
+ * Asked rather than derived from the messages a screen already holds. The
+ * same set decides whether [resend] publishes anything, and a second
+ * reading of it is a second thing to keep in step: a screen offering a knock
+ * the manager would refuse, or hiding one it would make.
+ */
+ suspend fun membersOwing(session: FrostSigningSession): Set
+
/** Agrees to sign, letting the session publish this device's part and run on. */
suspend fun approve(localChatRoom: LocalChatRoom, sessionId: String)
@@ -218,6 +230,8 @@ interface FrostSigningRepository {
sessionId: String
): Boolean = false
+ override suspend fun membersOwing(session: FrostSigningSession): Set = emptySet()
+
override suspend fun approve(localChatRoom: LocalChatRoom, sessionId: String) = Unit
override suspend fun decline(localChatRoom: LocalChatRoom, sessionId: String) = Unit
diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/FrostSigningScreen.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/FrostSigningScreen.kt
index f1254e56..fd37bd5c 100644
--- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/FrostSigningScreen.kt
+++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/FrostSigningScreen.kt
@@ -63,10 +63,14 @@ import org.jetbrains.compose.resources.stringResource
import curare.composeapp.generated.resources.ask_again
import curare.composeapp.generated.resources.asked_again
import curare.composeapp.generated.resources.asked_again_recently
+import curare.composeapp.generated.resources.asked_again_to_sign
import curare.composeapp.generated.resources.declined_to_sign
import curare.composeapp.generated.resources.everyone_has_answered_this_session
+import curare.composeapp.generated.resources.everyone_has_signed_this_session
import curare.composeapp.generated.resources.members_have_not_answered
import curare.composeapp.generated.resources.one_member_has_not_answered
+import curare.composeapp.generated.resources.one_signer_has_not_signed
+import curare.composeapp.generated.resources.signers_have_not_signed
import curare.composeapp.generated.resources.don_t_sign
import curare.composeapp.generated.resources.has_not_taken_part_yet
import curare.composeapp.generated.resources.members
@@ -128,8 +132,17 @@ fun FrostSigningScreen(
// read here rather than in the handler. The notifier takes the caller's
// scope on purpose -- see rememberNotifier.
val notify = rememberNotifier(rememberCoroutineScope())
+
+ // Four messages for two outcomes, because the knock is not for the same
+ // thing at every stage: before the signers are chosen it asks members who
+ // have said nothing to answer, and after it asks the chosen signers for the
+ // one part nobody else can send. All four are read here, before it is known
+ // which stage the session is at, because `stringResource` is composable and
+ // an onClick lambda is not.
val askedAgain = stringResource(Res.string.asked_again)
+ val askedAgainToSign = stringResource(Res.string.asked_again_to_sign)
val everyoneAnswered = stringResource(Res.string.everyone_has_answered_this_session)
+ val everyoneSigned = stringResource(Res.string.everyone_has_signed_this_session)
Scaffold(
snackbarHost = { SnackbarHost(LocalSnackbarHostState.current) },
@@ -289,11 +302,26 @@ fun FrostSigningScreen(
if (state.canResend) {
HorizontalDivider()
+ // What they are short of, which is not the same thing at
+ // both stages: past the nonces the members named here
+ // have answered once already, and the rows above say so.
+ val collectingPartials =
+ session.stage == FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES
+
Text(
- text = if (state.silent.size == 1) {
- stringResource(Res.string.one_member_has_not_answered)
- } else {
- stringResource(
+ text = when {
+ collectingPartials && state.silent.size == 1 ->
+ stringResource(Res.string.one_signer_has_not_signed)
+
+ collectingPartials -> stringResource(
+ Res.string.signers_have_not_signed,
+ state.silent.size
+ )
+
+ state.silent.size == 1 ->
+ stringResource(Res.string.one_member_has_not_answered)
+
+ else -> stringResource(
Res.string.members_have_not_answered,
state.silent.size
)
@@ -318,7 +346,14 @@ fun FrostSigningScreen(
enabled = !waiting && !frostSigningViewModel.isActionPending.value,
onClick = {
frostSigningViewModel.resend { sent ->
- notify(if (sent) askedAgain else everyoneAnswered)
+ notify(
+ when {
+ sent && collectingPartials -> askedAgainToSign
+ sent -> askedAgain
+ collectingPartials -> everyoneSigned
+ else -> everyoneAnswered
+ }
+ )
}
}
) {
diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/FrostSigningViewModel.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/FrostSigningViewModel.kt
index 08c0a212..646d9c8c 100644
--- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/FrostSigningViewModel.kt
+++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/FrostSigningViewModel.kt
@@ -77,6 +77,12 @@ class FrostSigningViewModel(
shareHolders
) { session, messages, items, holders -> Observed(session, messages, items, holders) }
.collect { (session, messages, items, holders) ->
+ // Read per emission rather than derived from `messages`: who
+ // the session is waiting on is the manager's rule, and it is
+ // not the same rule at every stage. See
+ // `FrostSigningRepository.membersOwing`.
+ val owed = session?.let { frostSigningRepository.membersOwing(it) } ?: emptySet()
+
frostSigningUIState = FrostSigningUIState.Loaded(
localChatRoom = localChatRoom,
session = session,
@@ -93,7 +99,8 @@ class FrostSigningViewModel(
declined = messages
.filter { it.kind == FrostSigningEvents.DECLINE }
.map { it.signerPublicKey }
- .toSet()
+ .toSet(),
+ silent = owed
)
}
}
diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/state/FrostSigningUIState.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/state/FrostSigningUIState.kt
index 55e267a3..ac95317d 100644
--- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/state/FrostSigningUIState.kt
+++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/state/FrostSigningUIState.kt
@@ -44,6 +44,18 @@ sealed interface FrostSigningUIState {
* the session has been collected, and so until [members] has anyone in it.
*/
val shareHolders: Set = emptySet(),
+
+ /**
+ * The share holders the session is still waiting on, and so whose door
+ * there is any point knocking on.
+ *
+ * Asked of the manager rather than worked out here, the way
+ * [FrostSigningManager.isAwaitingApproval] is. It decides what a resend
+ * does, and what counts as an answer changes with the stage -- a nonce
+ * settles the question while nonces are being collected and not after --
+ * so a second reading of it here is a second thing to keep in step.
+ */
+ val silent: Set = emptySet(),
): FrostSigningUIState {
/**
* The members the session can be answered by, as rows to draw.
@@ -81,21 +93,6 @@ sealed interface FrostSigningUIState {
return inRoom + elsewhere
}
- /**
- * The share holders this session has heard nothing at all from.
- *
- * Whose door to knock on, as a set rather than as a list of rows: it is
- * what decides whether there is anything to knock about. A member who
- * refused is not here -- they answered, and asking again would be asking
- * a question they have closed.
- *
- * Derived rather than collected. Everything it is made of is already on
- * this state, and a second flow for it would be a second thing that can
- * disagree with the rows the screen draws.
- */
- val silent: Set get() =
- shareHolders - offeredNonce - signed - declined - localChatRoom.chatRoom.userPublicKey
-
/**
* Whether this device may ask the quiet members to take another look.
*
diff --git a/composeApp/src/commonTest/kotlin/to/curare/compose/ui/view/state/FrostSigningResendStateTest.kt b/composeApp/src/commonTest/kotlin/to/curare/compose/ui/view/state/FrostSigningResendStateTest.kt
index 322ca688..fbf8c68e 100644
--- a/composeApp/src/commonTest/kotlin/to/curare/compose/ui/view/state/FrostSigningResendStateTest.kt
+++ b/composeApp/src/commonTest/kotlin/to/curare/compose/ui/view/state/FrostSigningResendStateTest.kt
@@ -17,19 +17,22 @@ import to.curare.compose.database.model.types.FrostSigningStage
import to.curare.compose.managers.FrostSigningManager
/**
- * When the signing screen offers to ask the quiet members again, and who it
- * counts as quiet.
+ * When the signing screen offers to ask the members it is waiting on again.
*
- * Both are derived on the state rather than collected, because everything they
- * are made of is already there -- the share holders, who offered a nonce, who
- * signed, who refused. A second flow for either would be a second thing that
- * can disagree with the rows drawn right above the button.
+ * *Who* those members are is not decided here. That rule is the manager's, it is
+ * not the same rule at every stage -- a nonce answers the question while nonces
+ * are being collected and not after -- and the same reading decides whether a
+ * resend publishes anything, so a second one here would be a second thing to
+ * keep in step: a screen offering a knock the manager would refuse, or hiding
+ * one it would make. See `FrostSigningManager.membersOwing` and the tests over
+ * it in `FrostSigningResendJvmTest`.
*
- * The limits are what this pins. The offer is the coordinator's and nobody
- * else's: `acceptProposal` takes a session's coordinator from the author of the
- * proposal that opened it, so a resend from anybody else would have a member who
- * was missing the session believe the resender coordinates -- and then drop the
- * real coordinator's signer set.
+ * What is left here is when the offer stands at all, and the limits are what it
+ * pins. The offer is the coordinator's and nobody else's: `acceptProposal` takes
+ * a session's coordinator from the author of the proposal that opened it, so a
+ * resend from anybody else would have a member who was missing the session
+ * believe the resender coordinates -- and then drop the real coordinator's
+ * signer set.
*/
class FrostSigningResendStateTest {
private val roomId = "11".repeat(32)
@@ -95,6 +98,7 @@ class FrostSigningResendStateTest {
signed: Set = emptySet(),
declined: Set = emptySet(),
items: List = listOf(item),
+ silent: Set = setOf(bob, carol),
) = FrostSigningUIState.Loaded(
localChatRoom = room(owner),
session = session(owner, coordinator, stage, resentAt),
@@ -103,30 +107,24 @@ class FrostSigningResendStateTest {
offeredNonce = offeredNonce,
signed = signed,
declined = declined,
+ silent = silent,
)
@Test
- fun `the quiet members are the share holders nobody has heard from`() {
- val state = loaded(offeredNonce = setOf(bob))
+ fun `the offer follows the manager's answer rather than the rows`() {
+ // Every share holder has answered as far as the rows above the button
+ // can tell -- and the manager says Bob still owes a partial signature,
+ // which is what a session past the nonces waits on. The screen takes
+ // that answer; working one out from the rows is how the knock came to be
+ // refused exactly when it was needed.
+ val state = loaded(
+ stage = FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES,
+ offeredNonce = setOf(bob, carol),
+ silent = setOf(bob),
+ )
- // Alice is this device: her own answer is not something to knock about.
- assertEquals(setOf(carol), state.silent)
- }
-
- @Test
- fun `a member who refused is not quiet`() {
- // They answered. Asking again would be asking a question they closed.
- assertEquals(setOf(carol), loaded(declined = setOf(bob)).silent)
- }
-
- @Test
- fun `a member who signed their part is not quiet`() {
- assertEquals(setOf(carol), loaded(signed = setOf(bob)).silent)
- }
-
- @Test
- fun `a session nobody has answered is quiet in everybody but its proposer`() {
- assertEquals(setOf(bob, carol), loaded().silent)
+ assertEquals(setOf(bob), state.silent)
+ assertTrue(state.canResend)
}
@Test
@@ -141,10 +139,9 @@ class FrostSigningResendStateTest {
}
@Test
- fun `a session everybody has answered has nothing to resend`() {
- val state = loaded(offeredNonce = setOf(bob), declined = setOf(carol))
+ fun `a session waiting on nobody has nothing to resend`() {
+ val state = loaded(offeredNonce = setOf(bob), declined = setOf(carol), silent = emptySet())
- assertTrue(state.silent.isEmpty())
assertFalse(state.canResend)
}
diff --git a/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningResendJvmTest.kt b/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningResendJvmTest.kt
index cb5d53d1..909f32f1 100644
--- a/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningResendJvmTest.kt
+++ b/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningResendJvmTest.kt
@@ -4,10 +4,13 @@ import androidx.room3.Room
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.Kind
+import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray
+import fr.acinq.bitcoin.ByteVector
import fr.acinq.bitcoin.ByteVector32
import fr.acinq.bitcoin.PrivateKey
import fr.acinq.bitcoin.crypto.frost.Frost
import fr.acinq.bitcoin.crypto.frost.KeyMaterial
+import fr.acinq.bitcoin.crypto.frost.SecretNonce
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -54,6 +57,7 @@ import to.curare.compose.nostr.frost.FrostSigningEvents
* tests below are mostly about the limits of that: the same bytes and not new
* ones, nothing at all from a device whose owner has not decided, and nothing
* from a nudge that did not come from the coordinator.
+
*/
class FrostSigningResendJvmTest {
private val participants = 3
@@ -265,6 +269,68 @@ class FrostSigningResendJvmTest {
private suspend fun resend(sessionId: String) =
FrostSigningManager.resend(database = db, localChatRoom = room, sessionId = sessionId)
+ /**
+ * A public nonce as [member]'s own device would have published it.
+ *
+ * Real rather than filler, because the coordinator aggregates what arrives:
+ * a nonce that does not parse leaves the session where it was, so a test
+ * wanting the stage after this one has to offer something genuine.
+ *
+ * [sessionRandom] is the one thing a remote member's device does not share
+ * with this one. Theirs is theirs; any value gives a usable nonce, and the
+ * seed only has to differ per member so two of them never collide.
+ */
+ private fun publicNonceOf(member: HexKey): String {
+ val index = members.indexOf(member)
+ val cache = SharedKeyDerivation
+ .derive(thresholdPublicKey, SharedKeyDerivation.MARMOT_ADMIN_GROUP_PATH)
+ .cache
+
+ val (_, publicNonce) = SecretNonce.generate(
+ sessionRandom = ByteVector32("b".repeat(63) + "${index + 1}"),
+ secretShare = keyMaterial.secretShares[index],
+ publicShare = keyMaterial.publicShares[index],
+ tweakedThresholdPublicKey = cache.tweakedPublicKey,
+ message = ByteVector(unsignedEvent().id.hexToByteArray()),
+ extraInput = null
+ )
+
+ return publicNonce.data.toHex()
+ }
+
+ /**
+ * Alice's session once Bob has offered a nonce: the signer set chosen, her
+ * own part signed, and the session waiting on his.
+ *
+ * The stage is reached rather than written. A session put into
+ * [FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES] by hand, with no nonce
+ * on file from anybody, is a state the protocol cannot produce -- and it is
+ * precisely the difference: the members this stage waits on are the ones who
+ * have already been heard from once.
+ */
+ private suspend fun waitingOnBobsPart(): FrostSigningSession {
+ val session = propose()
+ receive(session.id, FrostSigningEvents.NONCE, bob, publicNonceOf(bob))
+
+ val reached = session(session.id)
+ assertEquals(FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES, reached.stage)
+ assertEquals(listOf(0, 1), reached.signerIdList())
+
+ return reached
+ }
+
+ /** Files a signer's message without running the session, the way a stored one would read. */
+ private suspend fun fileMessage(sessionId: String, kind: Kind, member: HexKey, payload: String) {
+ db.frostSigningSessionDao().upsert(
+ FrostSignerMessage(
+ sessionId = sessionId,
+ signerPublicKey = member,
+ kind = kind,
+ payload = payload
+ )
+ )
+ }
+
private suspend fun session(id: String) = db.frostSigningSessionDao().getSessionById(id)!!
private suspend fun transcript(type: String) = db.chatMessageDao()
@@ -722,29 +788,20 @@ class FrostSigningResendJvmTest {
@Test
fun `a resend while collecting partial signatures repeats the signer set`() = runBlocking {
openDevice()
- val session = propose()
+ val session = waitingOnBobsPart()
// The middle branch of the stage table, and the one a member stalls on:
// they have the proposal and their nonce is in, but the signer set that
// tells them what to sign against never arrived.
- db.frostSigningSessionDao().upsertItems(
- db.frostSigningSessionDao().getItems(session.id).map {
- it.copy(aggregatedNonce = "ab".repeat(33))
- }
- )
- db.frostSigningSessionDao().upsert(
- session(session.id).copy(
- signerIds = "0,1",
- stage = FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES
- )
- )
+ val aggregated = db.frostSigningSessionDao().getItems(session.id).single().aggregatedNonce
assertTrue(resend(session.id))
+ // Two: the one `advance` sent when it chose the signers, and the repeat.
val signerSet = queued(FrostSigningEvents.SIGNER_SET)
- assertEquals(1, signerSet.size)
- assertEquals("ab".repeat(33), signerSet.single().content)
- assertEquals(listOf(0, 1), FrostSigningEvents.parseSignerIds(signerSet.single().tags))
+ assertEquals(2, signerSet.size)
+ assertTrue(signerSet.all { it.content == aggregated })
+ assertTrue(signerSet.all { FrostSigningEvents.parseSignerIds(it.tags) == listOf(0, 1) })
// And the proposal with it, because a device holding neither can file
// neither: a signer set naming ids it has no items for is dropped.
@@ -752,6 +809,65 @@ class FrostSigningResendJvmTest {
assertTrue(queued(FrostSigningEvents.SIGNATURE).isEmpty())
}
+ @Test
+ fun `the knock at the signing stage is for the signer who has not signed`() = runBlocking {
+ openDevice()
+ val session = waitingOnBobsPart()
+
+ // Bob offered a nonce, which is why the session got this far, and that
+ // used to be enough to count him as answered -- leaving the knock for
+ // Carol, who was never chosen and owes nothing.
+ assertEquals(setOf(bob), FrostSigningManager.membersOwing(db, session))
+
+ // By id rather than by time: the proposal and its repeat routinely share
+ // a `createdAt`, since it is stored to the second.
+ val resent = queuedBy(FrostSigningEvents.PROPOSAL) { assertTrue(resend(session.id)) }
+
+ assertEquals(listOf(bob), FrostSigningEvents.parseSilentMembers(resent.tags))
+ }
+
+ @Test
+ fun `a signer who has sent their part is not knocked on again`() = runBlocking {
+ openDevice()
+ val session = waitingOnBobsPart()
+
+ // Filed rather than received: a partial signature that is not Bob's real
+ // one would fail the aggregation and abandon the session, and what this
+ // is about is the bookkeeping either side of that.
+ fileMessage(session.id, FrostSigningEvents.PARTIAL_SIGNATURE, bob, "9".repeat(64))
+
+ // Carol never answered at all, and is still not who this stage waits on.
+ assertTrue(FrostSigningManager.membersOwing(db, session).isEmpty())
+ assertFalse(resend(session.id))
+
+ assertEquals(1, queued(FrostSigningEvents.PROPOSAL).size)
+ }
+
+ @Test
+ fun `a chosen signer who refused is not knocked on either`() = runBlocking {
+ openDevice()
+ val session = waitingOnBobsPart()
+
+ fileMessage(session.id, FrostSigningEvents.DECLINE, bob, "Declined to sign")
+
+ // They answered the question this stage asks, even though the answer
+ // leaves the session needing a different signer.
+ assertTrue(FrostSigningManager.membersOwing(db, session).isEmpty())
+ assertFalse(resend(session.id))
+ }
+
+ @Test
+ fun `the transcript says what the signers have not sent`() = runBlocking {
+ openDevice()
+ val session = waitingOnBobsPart()
+
+ assertTrue(resend(session.id))
+
+ val line = transcript(ChatMessage.TYPE_FROST_RESENT).single()
+ assertTrue(line.content.contains("has not signed their part"), "got: ${line.content}")
+ assertFalse(line.content.contains("not answered"), "got: ${line.content}")
+ }
+
@Test
fun `a resend sends no signer set before one has been chosen`() = runBlocking {
openDevice()