From 1b3e08d264e55c79e197fc4625dbdac24d9cd008 Mon Sep 17 00:00:00 2001 From: "S. Santos" Date: Wed, 22 May 2024 14:27:28 -0300 Subject: [PATCH] Update Kotlin client --- .../main/kotlin/BroadcastBackupTransaction.kt | 19 +++++++----- .../kotlin/src/main/kotlin/ListStatecoins.kt | 1 + clients/kotlin/src/main/kotlin/Withdraw.kt | 4 +-- clients/kotlin/src/main/kotlin/mercurylib.kt | 30 +++++++++++++++++++ lib/out-kotlin/com/mercurylayer/mercurylib.kt | 30 +++++++++++++++++++ 5 files changed, 75 insertions(+), 9 deletions(-) diff --git a/clients/kotlin/src/main/kotlin/BroadcastBackupTransaction.kt b/clients/kotlin/src/main/kotlin/BroadcastBackupTransaction.kt index 02a86849..9f302c17 100644 --- a/clients/kotlin/src/main/kotlin/BroadcastBackupTransaction.kt +++ b/clients/kotlin/src/main/kotlin/BroadcastBackupTransaction.kt @@ -35,11 +35,7 @@ class BroadcastBackupTransaction: CliktCommand(help = "Broadcast a backup transa val backupTxs = appContext.sqliteManager.getBackupTxs(statechainId) - val backupTx = if (backupTxs.isEmpty()) null else backupTxs.maxByOrNull { it.txN } - - if (backupTx == null) { - throw Exception("There is no backup transaction for the statechain id $statechainId") - } + // val backupTx = if (backupTxs.isEmpty()) null else backupTxs.maxByOrNull { it.txN } val coinsWithStatechainId = wallet.coins.filter { it.statechainId == statechainId } @@ -49,8 +45,17 @@ class BroadcastBackupTransaction: CliktCommand(help = "Broadcast a backup transa val coin = coinsWithStatechainId.sortedBy { it.locktime }.first() - if (coin.status != CoinStatus.CONFIRMED) { - throw Exception("Coin status must be CONFIRMED to broadcast the backup transaction. The current status is ${coin.status}") + if (coin.status != CoinStatus.CONFIRMED && coin.status != CoinStatus.IN_TRANSFER) { + throw Exception("Coin status must be CONFIRMED or IN_TRANSFER to transfer it. The current status is ${coin.status}"); + } + + var backupTx: BackupTx? = null + + try { + backupTx = latestBackupTxPaysToUserPubkey(backupTxs, coin, wallet.network) + } catch (e: Exception) { + println("Error: ${e.message}") + return } var feeRateSatsPerByte = feeRate diff --git a/clients/kotlin/src/main/kotlin/ListStatecoins.kt b/clients/kotlin/src/main/kotlin/ListStatecoins.kt index d5c6e1ba..45315d24 100644 --- a/clients/kotlin/src/main/kotlin/ListStatecoins.kt +++ b/clients/kotlin/src/main/kotlin/ListStatecoins.kt @@ -30,6 +30,7 @@ class ListStatecoins: CliktCommand(help = "List all wallet' statecoins") { put("status", coin.status.toString()) put("deposit_address", coin.aggregatedAddress ?: "Empty") put("statechain_address", coin.address ?: "Empty") + put("locktime", coin.locktime.toString()) }) } } diff --git a/clients/kotlin/src/main/kotlin/Withdraw.kt b/clients/kotlin/src/main/kotlin/Withdraw.kt index e262f9af..00e700e4 100644 --- a/clients/kotlin/src/main/kotlin/Withdraw.kt +++ b/clients/kotlin/src/main/kotlin/Withdraw.kt @@ -55,8 +55,8 @@ class Withdraw: CliktCommand(help = "Withdraw funds from a statecoin to a BTC ad throw Exception("coin.amount is None") } - if (coin.status != CoinStatus.CONFIRMED) { - throw Exception("Coin status must be CONFIRMED to withdraw it. The current status is ${coin.status}") + if (coin.status != CoinStatus.CONFIRMED && coin.status != CoinStatus.IN_TRANSFER) { + throw Exception("Coin status must be CONFIRMED or IN_TRANSFER to transfer it. The current status is ${coin.status}"); } val signedTx = Transaction.create( diff --git a/clients/kotlin/src/main/kotlin/mercurylib.kt b/clients/kotlin/src/main/kotlin/mercurylib.kt index e10455e5..68a42735 100644 --- a/clients/kotlin/src/main/kotlin/mercurylib.kt +++ b/clients/kotlin/src/main/kotlin/mercurylib.kt @@ -887,6 +887,13 @@ internal interface UniffiLib : Library { uniffi_out_err: UniffiRustCallStatus, ): Byte + fun uniffi_mercurylib_fn_func_latest_backup_tx_pays_to_user_pubkey( + `backupTxs`: RustBuffer.ByValue, + `coin`: RustBuffer.ByValue, + `network`: RustBuffer.ByValue, + uniffi_out_err: UniffiRustCallStatus, + ): RustBuffer.ByValue + fun uniffi_mercurylib_fn_func_new_backup_transaction( `encodedUnsignedTx`: RustBuffer.ByValue, `signatureHex`: RustBuffer.ByValue, @@ -1182,6 +1189,8 @@ internal interface UniffiLib : Library { fun uniffi_mercurylib_checksum_func_is_enclave_pubkey_part_of_coin(): Short + fun uniffi_mercurylib_checksum_func_latest_backup_tx_pays_to_user_pubkey(): Short + fun uniffi_mercurylib_checksum_func_new_backup_transaction(): Short fun uniffi_mercurylib_checksum_func_sign_message(): Short @@ -1276,6 +1285,9 @@ private fun uniffiCheckApiChecksums(lib: UniffiLib) { if (lib.uniffi_mercurylib_checksum_func_is_enclave_pubkey_part_of_coin() != 37041.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } + if (lib.uniffi_mercurylib_checksum_func_latest_backup_tx_pays_to_user_pubkey() != 19689.toShort()) { + throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") + } if (lib.uniffi_mercurylib_checksum_func_new_backup_transaction() != 56642.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } @@ -4394,6 +4406,24 @@ fun `isEnclavePubkeyPartOfCoin`( ) } +@Throws(MercuryException::class) +fun `latestBackupTxPaysToUserPubkey`( + `backupTxs`: List, + `coin`: Coin, + `network`: kotlin.String, +): BackupTx { + return FfiConverterTypeBackupTx.lift( + uniffiRustCallWithError(MercuryException) { _status -> + UniffiLib.INSTANCE.uniffi_mercurylib_fn_func_latest_backup_tx_pays_to_user_pubkey( + FfiConverterSequenceTypeBackupTx.lower(`backupTxs`), + FfiConverterTypeCoin.lower(`coin`), + FfiConverterString.lower(`network`), + _status, + ) + }, + ) +} + @Throws(MercuryException::class) fun `newBackupTransaction`( `encodedUnsignedTx`: kotlin.String, diff --git a/lib/out-kotlin/com/mercurylayer/mercurylib.kt b/lib/out-kotlin/com/mercurylayer/mercurylib.kt index e10455e5..68a42735 100644 --- a/lib/out-kotlin/com/mercurylayer/mercurylib.kt +++ b/lib/out-kotlin/com/mercurylayer/mercurylib.kt @@ -887,6 +887,13 @@ internal interface UniffiLib : Library { uniffi_out_err: UniffiRustCallStatus, ): Byte + fun uniffi_mercurylib_fn_func_latest_backup_tx_pays_to_user_pubkey( + `backupTxs`: RustBuffer.ByValue, + `coin`: RustBuffer.ByValue, + `network`: RustBuffer.ByValue, + uniffi_out_err: UniffiRustCallStatus, + ): RustBuffer.ByValue + fun uniffi_mercurylib_fn_func_new_backup_transaction( `encodedUnsignedTx`: RustBuffer.ByValue, `signatureHex`: RustBuffer.ByValue, @@ -1182,6 +1189,8 @@ internal interface UniffiLib : Library { fun uniffi_mercurylib_checksum_func_is_enclave_pubkey_part_of_coin(): Short + fun uniffi_mercurylib_checksum_func_latest_backup_tx_pays_to_user_pubkey(): Short + fun uniffi_mercurylib_checksum_func_new_backup_transaction(): Short fun uniffi_mercurylib_checksum_func_sign_message(): Short @@ -1276,6 +1285,9 @@ private fun uniffiCheckApiChecksums(lib: UniffiLib) { if (lib.uniffi_mercurylib_checksum_func_is_enclave_pubkey_part_of_coin() != 37041.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } + if (lib.uniffi_mercurylib_checksum_func_latest_backup_tx_pays_to_user_pubkey() != 19689.toShort()) { + throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") + } if (lib.uniffi_mercurylib_checksum_func_new_backup_transaction() != 56642.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } @@ -4394,6 +4406,24 @@ fun `isEnclavePubkeyPartOfCoin`( ) } +@Throws(MercuryException::class) +fun `latestBackupTxPaysToUserPubkey`( + `backupTxs`: List, + `coin`: Coin, + `network`: kotlin.String, +): BackupTx { + return FfiConverterTypeBackupTx.lift( + uniffiRustCallWithError(MercuryException) { _status -> + UniffiLib.INSTANCE.uniffi_mercurylib_fn_func_latest_backup_tx_pays_to_user_pubkey( + FfiConverterSequenceTypeBackupTx.lower(`backupTxs`), + FfiConverterTypeCoin.lower(`coin`), + FfiConverterString.lower(`network`), + _status, + ) + }, + ) +} + @Throws(MercuryException::class) fun `newBackupTransaction`( `encodedUnsignedTx`: kotlin.String,