Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions app/src/main/java/to/bitkit/models/PubkyProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import to.bitkit.ext.ellipsisMiddle
import com.synonym.paykit.PaykitProfile as SdkPaykitProfile
import com.synonym.paykit.PubkyProfile as SdkPubkyProfile

Expand All @@ -24,8 +23,6 @@ data class PubkyProfile(
val status: String?,
) {
companion object {
private const val TRUNCATED_PK_LENGTH = 11

fun fromPubkyProfile(publicKey: String, sdkProfile: SdkPubkyProfile): PubkyProfile {
return PubkyProfile(
publicKey = publicKey,
Expand All @@ -43,7 +40,7 @@ data class PubkyProfile(

fun placeholder(publicKey: String) = PubkyProfile(
publicKey = publicKey,
name = publicKey.ellipsisMiddle(TRUNCATED_PK_LENGTH),
name = PubkyPublicKeyFormat.display(publicKey),
bio = "",
imageUrl = null,
links = emptyList(),
Expand All @@ -57,7 +54,7 @@ data class PubkyProfile(
imageUrl: String?,
) = PubkyProfile(
publicKey = publicKey,
name = name ?: publicKey.ellipsisMiddle(TRUNCATED_PK_LENGTH),
name = name ?: PubkyPublicKeyFormat.display(publicKey),
bio = "",
imageUrl = imageUrl,
links = emptyList(),
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/to/bitkit/models/PubkyPublicKeyFormat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ object PubkyPublicKeyFormat {
private const val redactedLength = 16
const val maximumInputLength = 57

/** Prefixes stripped from a key before it is shortened for display. */
private val displayPrefixes = listOf("pubky", "pk:")

fun bounded(input: String): String {
return input
.trim()
Expand All @@ -40,7 +43,7 @@ object PubkyPublicKeyFormat {
}

fun display(input: String): String {
val rawKey = bounded(input).removePrefix("pubky")
val rawKey = displayPrefixes.fold(bounded(input)) { key, prefix -> key.removePrefix(prefix) }
return if (rawKey.length > displayEdgeLength * 2) {
"${rawKey.take(displayEdgeLength)}...${rawKey.takeLast(displayEdgeLength)}"
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ private fun ColumnScope.SuccessContent(

SuccessDescriptionText(
serviceName = uiState.serviceName,
truncatedKey = uiState.profile?.authDisplayPublicKey.orEmpty(),
truncatedKey = uiState.profile?.truncatedPublicKey.orEmpty(),
)
VerticalSpacer(16.dp)

Expand Down Expand Up @@ -558,7 +558,7 @@ private fun ProfileCard(profile: PubkyProfile) {
.padding(24.dp),
) {
Text13Up(
text = profile.authDisplayPublicKey,
text = profile.truncatedPublicKey,
color = Colors.White64,
)
VerticalSpacer(16.dp)
Expand Down Expand Up @@ -591,14 +591,6 @@ private fun ProfileCard(profile: PubkyProfile) {
}
}

private val PubkyProfile.authDisplayPublicKey: String
get() = pubkyAuthDisplayPublicKey(publicKey)

internal fun pubkyAuthDisplayPublicKey(publicKey: String): String {
val rawKey = publicKey.removePrefix("pubky")
return if (rawKey.length > 8) "${rawKey.take(4)}...${rawKey.takeLast(4)}" else rawKey
}

@Preview(showSystemUi = true)
@Composable
private fun WatchOnlyConsentPreview() {
Expand Down
22 changes: 22 additions & 0 deletions app/src/test/java/to/bitkit/models/PubkyPublicKeyFormatTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,31 @@ class PubkyPublicKeyFormatTest {
assertEquals("3rsd...yw5x", PubkyPublicKeyFormat.display(key))
}

@Test
fun `display never shows the pubky prefix`() {
val display = PubkyPublicKeyFormat.display("pubkydeadbeef3rsduhcxpw74snwyct86m38c63j3pq8x4ycqxyo1")

assertEquals("dead...xyo1", display)
assertFalse(display.startsWith("pubky"))
}

@Test
fun `display strips a pk prefix`() {
assertEquals("dead...xyo1", PubkyPublicKeyFormat.display("pk:deadbeef3rsduhcxpw74snwyct86m38c63j3pq8x4ycqxyo1"))
}

@Test
fun `display shortens an unprefixed key`() {
assertEquals("dead...xyo1", PubkyPublicKeyFormat.display("deadbeef3rsduhcxpw74snwyct86m38c63j3pq8x4ycqxyo1"))
}

@Test
fun `display leaves a short raw key unchanged`() {
assertEquals("short", PubkyPublicKeyFormat.display("pubkyshort"))
assertEquals("short", PubkyPublicKeyFormat.display("pk:short"))
assertEquals("short", PubkyPublicKeyFormat.display("short"))
assertEquals("01234567", PubkyPublicKeyFormat.display("pubky01234567"))
assertEquals("", PubkyPublicKeyFormat.display("pubky"))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,6 @@ class PubkyAuthApprovalViewModelTest : BaseUnitTest() {
verifyBlocking(pubkyRepo, never()) { approveAuthWithCompanionClaim(any(), any(), any()) }
}

@Test
fun `auth display public key omits pubky prefix`() {
assertEquals("3rsd...w5xg", pubkyAuthDisplayPublicKey("pubky3rsd123456789w5xg"))
assertEquals("3rsd...w5xg", pubkyAuthDisplayPublicKey("3rsd123456789w5xg"))
}

@Test
fun `confirmAuthorize is ignored when load has not completed`() = test {
val authUrl = "pubkyauth://signin?caps=/pub/bitkit.to/:rw"
Expand Down
1 change: 1 addition & 0 deletions changelog.d/next/1456.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Shortened contact keys in payment sheets and auth prompts now omit the pubky prefix.
Loading