New: improve reply-to support in memos.

This commit is contained in:
Kevin Gorham 2020-08-01 02:45:18 -04:00
parent 8f9da23f27
commit 6f44dbcb8b
No known key found for this signature in database
GPG Key ID: CCA55602DF49FC38
2 changed files with 32 additions and 10 deletions

View File

@ -8,7 +8,7 @@ import cash.z.ecc.android.R
import cash.z.ecc.android.ext.goneIf
import cash.z.ecc.android.ext.toAppColor
import cash.z.ecc.android.ui.MainActivity
import cash.z.ecc.android.ui.util.INCLUDE_MEMO_PREFIX
import cash.z.ecc.android.ui.util.INCLUDE_MEMO_PREFIXES_RECOGNIZED
import cash.z.ecc.android.ui.util.toUtf8Memo
import cash.z.ecc.android.sdk.db.entity.ConfirmedTransaction
import cash.z.ecc.android.sdk.ext.*
@ -100,21 +100,28 @@ class TransactionViewHolder<T : ConfirmedTransaction>(itemView: View) : Recycler
private suspend fun getSender(transaction: ConfirmedTransaction): String {
val memo = transaction.memo.toUtf8Memo()
val who = extractValidAddress(memo, INCLUDE_MEMO_PREFIX)
?: extractValidAddress(memo, "sent from:")
?: "Unknown"
val who = extractValidAddress(memo)?.toAbbreviatedAddress() ?: "Unknown"
return "$who paid you"
}
private fun extractAddress(memo: String?) =
addressRegex.findAll(memo ?: "").lastOrNull()?.value
private suspend fun extractValidAddress(memo: String?, delimiter: String): String? {
private suspend fun extractValidAddress(memo: String?): String? {
if (memo == null || memo.length < 25) return null
// note: cannot use substringAfterLast because we need to ignore case
return memo?.lastIndexOf(delimiter, ignoreCase = true)?.let { i ->
memo.substring(i + delimiter.length).trimStart()
}?.validateAddress()
try {
INCLUDE_MEMO_PREFIXES_RECOGNIZED.forEach { prefix ->
memo.lastIndexOf(prefix, ignoreCase = true).takeUnless { it == -1 }?.let { lastIndex ->
memo.substring(lastIndex + prefix.length).trimStart().validateAddress()?.let { address ->
return@extractValidAddress address
}
}
}
} catch(t: Throwable) { }
return null
}
private fun onTransactionClicked(transaction: ConfirmedTransaction) {

View File

@ -2,8 +2,23 @@ package cash.z.ecc.android.ui.util
import java.nio.charset.StandardCharsets
/**
* The prefix that this wallet uses whenever the user chooses to include their address in the memo.
* This is the one we standardize around.
*/
const val INCLUDE_MEMO_PREFIX_STANDARD = "Reply-To:"
const val INCLUDE_MEMO_PREFIX = "Reply-To:"
/**
* The non-standard prefixes that we will parse if other wallets send them our way.
*/
val INCLUDE_MEMO_PREFIXES_RECOGNIZED = arrayOf(
INCLUDE_MEMO_PREFIX_STANDARD, // standard
"reply-to", // standard w/o colon
"reply to:", // space instead of dash
"reply to", // space instead of dash w/o colon
"sent from:", // previous standard
"sent from" // previous standard w/o colon
)
inline fun ByteArray?.toUtf8Memo(): String {
// TODO: make this more official but for now, this will do