mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-11-10 13:47:49 +01:00
Parse and Show Sat Amount from Cashu tokens
This commit is contained in:
@@ -21,15 +21,29 @@ import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.core.content.ContextCompat.startActivity
|
||||
import com.google.gson.JsonParser
|
||||
import com.vitorpamplona.amethyst.R
|
||||
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
|
||||
import com.vitorpamplona.amethyst.ui.theme.QuoteBorder
|
||||
import com.vitorpamplona.amethyst.ui.theme.subtleBorder
|
||||
import java.util.Base64
|
||||
|
||||
@Composable
|
||||
fun CashuPreview(cashutoken: String, accountViewModel: AccountViewModel) {
|
||||
val context = LocalContext.current
|
||||
val lnaddress = accountViewModel.account.userProfile().info?.lud16
|
||||
val base64token = cashutoken.replace("cashuA", "")
|
||||
val cashu = JsonParser.parseString(String(Base64.getDecoder().decode(base64token)))
|
||||
|
||||
val token = cashu.asJsonObject.get("token").asJsonArray[0].asJsonObject
|
||||
val proofs = token["proofs"].asJsonArray
|
||||
val mint = token["mint"]
|
||||
|
||||
var totalamount = 0
|
||||
for (proof in proofs) {
|
||||
totalamount += proof.asJsonObject["amount"].asInt
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@@ -65,12 +79,28 @@ fun CashuPreview(cashutoken: String, accountViewModel: AccountViewModel) {
|
||||
|
||||
Divider()
|
||||
|
||||
totalamount?.let {
|
||||
Text(
|
||||
text = "$it ${stringResource(id = R.string.sats)}",
|
||||
fontSize = 25.sp,
|
||||
fontWeight = FontWeight.W500,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(vertical = 10.dp)
|
||||
)
|
||||
}
|
||||
|
||||
Button(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(vertical = 10.dp),
|
||||
onClick = {
|
||||
runCatching {
|
||||
// Alternative directly from client: call POST https://{mint}/melt with body
|
||||
// {
|
||||
// "proofs: {proofs},
|
||||
// "pr": {create ln invoice with totalamount * 0.98, at least -2 sats}
|
||||
// }
|
||||
val url = "https://redeem.cashu.me?token=$cashutoken&lightning=$lnaddress&autopay=true"
|
||||
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
|
||||
startActivity(context, intent, null)
|
||||
|
||||
@@ -457,7 +457,7 @@
|
||||
<string name="auth">Auth</string>
|
||||
<string name="payment">Payment</string>
|
||||
<string name="cashu">Cashu ecash</string>
|
||||
<string name="cashu_redeem">Redeem Sats</string>
|
||||
<string name="cashu_redeem">Redeem</string>
|
||||
|
||||
<string name="live_stream_live_tag">LIVE</string>
|
||||
<string name="live_stream_offline_tag">OFFLINE</string>
|
||||
|
||||
Reference in New Issue
Block a user