use cashu token parser for receive, receive is not implemented in CDK yet

This commit is contained in:
callebtc
2025-07-15 02:21:41 +02:00
parent 30ba6a2280
commit f922a84fcc
@@ -5,6 +5,7 @@ import kotlinx.coroutines.Dispatchers
import android.util.Log import android.util.Log
import android.content.Context import android.content.Context
import com.bitchat.android.wallet.data.* import com.bitchat.android.wallet.data.*
import com.bitchat.android.parsing.CashuTokenParser
import uniffi.cdk_ffi.* import uniffi.cdk_ffi.*
import java.math.BigDecimal import java.math.BigDecimal
import java.util.* import java.util.*
@@ -283,8 +284,9 @@ class CashuService {
Log.d(TAG, "Token validation successful, amount: $amount") Log.d(TAG, "Token validation successful, amount: $amount")
// In real CDK implementation, tokens are auto-received when sent // receive with CDK:
// Here we're just validating and returning the amount wallet!!.receive(token)
Result.success(amount) Result.success(amount)
} catch (e: FfiException) { } catch (e: FfiException) {
@@ -481,20 +483,25 @@ class CashuService {
suspend fun decodeToken(token: String): Result<CashuToken> { suspend fun decodeToken(token: String): Result<CashuToken> {
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {
try { try {
// For now, do simple parsing - CDK might have better token parsing functions // Use the proper CashuTokenParser for real token parsing
val amount = parseTokenAmount(token) val parser = CashuTokenParser()
val memo = parseTokenMemo(token) val parsedToken = parser.parseToken(token)
val cashuToken = CashuToken( if (parsedToken != null) {
token = token, val cashuToken = CashuToken(
amount = BigDecimal(amount), token = token,
unit = "sat", amount = BigDecimal(parsedToken.amount),
mint = currentMintUrl ?: DEFAULT_MINT_URL, unit = parsedToken.unit,
memo = memo mint = parsedToken.mintUrl,
) memo = parsedToken.memo
)
Log.d(TAG, "Decoded token: amount=$amount")
Result.success(cashuToken) Log.d(TAG, "Decoded token: amount=${parsedToken.amount} ${parsedToken.unit} from ${parsedToken.mintUrl}")
Result.success(cashuToken)
} else {
Log.e(TAG, "Failed to parse token: invalid format")
Result.failure(Exception("Invalid token format"))
}
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Failed to decode token", e) Log.e(TAG, "Failed to decode token", e)
@@ -548,40 +555,5 @@ class CashuService {
} }
} }
private fun parseTokenAmount(token: String): Long {
return try {
when {
token.startsWith("cashuAdemo") -> {
// Extract amount from demo token
val parts = token.split("_")
if (parts.size >= 2) {
parts[0].removePrefix("cashuAdemo").toLongOrNull() ?: 1000L
} else {
1000L
}
}
token.startsWith("cashuA") -> {
// Real token parsing would be more complex
// For now, return default
1000L
}
else -> 1000L
}
} catch (e: Exception) {
Log.w(TAG, "Error parsing token amount", e)
1000L
}
}
private fun parseTokenMemo(token: String): String? {
return try {
when {
token.startsWith("cashuAdemo") -> "Demo token"
token.contains("memo") -> "Cashu token"
else -> null
}
} catch (e: Exception) {
null
}
}
} }