From ef4756ef2627a8db5b4388461503519c1d80c14f Mon Sep 17 00:00:00 2001 From: Jun Luo <4catcode@gmail.com> Date: Tue, 12 Nov 2024 20:23:40 +0800 Subject: [PATCH 1/4] Bump java-stellar-sdk to 1.0.0-beta1. --- gradle/libs.versions.toml | 6 ++- wallet-sdk/build.gradle.kts | 1 + .../org/stellar/walletsdk/anchor/Anchor.kt | 2 +- .../org/stellar/walletsdk/asset/AssetId.kt | 4 -- .../walletsdk/exception/StellarExceptions.kt | 22 ++++++----- .../walletsdk/exception/WalletException.kt | 6 +-- .../stellar/walletsdk/extension/Account.kt | 3 +- .../org/stellar/walletsdk/extension/Server.kt | 4 +- .../org/stellar/walletsdk/horizon/Stellar.kt | 31 ++++++--------- .../transaction/CommonTransactionBuilder.kt | 35 ++++++++++------- .../horizon/transaction/SponsoringBuilder.kt | 11 ++++-- .../horizon/transaction/TransactionBuilder.kt | 14 ++++++- .../stellar/walletsdk/recovery/Recovery.kt | 3 +- .../org/stellar/walletsdk/ConstantTest.kt | 18 +++++---- .../walletsdk/SubmitTransactionTest.kt | 39 ++++++++++--------- .../walletsdk/account/AddRemoveAssetTest.kt | 6 +-- .../walletsdk/account/AddRemoveSignerTest.kt | 2 +- 17 files changed, 114 insertions(+), 93 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 21b5d35c..0070407f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,11 +1,12 @@ [versions] # Library versions -bcastle = "1.77" +bcastle = "1.79" +bcutil = "1.79" coroutines = "1.6.4" google-gson = "2.8.9" hoplite = "2.7.0" jjwt = "0.12.5" -java-stellar-sdk = "0.43.2" +java-stellar-sdk = "1.0.0-beta1" dokka = "1.6.10" kotlin = "1.8.20" kotlinx-json = "1.5.0" @@ -23,6 +24,7 @@ detekt = "1.22.0" [libraries] bcastle = { module = "org.bouncycastle:bcprov-jdk18on", version.ref = "bcastle" } +bcutil = { module = "org.bouncycastle:bcutil-jdk18on", version.ref = "bcutil" } coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" } coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" } google-gson = { module = "com.google.code.gson:gson", version.ref = "google-gson" } diff --git a/wallet-sdk/build.gradle.kts b/wallet-sdk/build.gradle.kts index c5adecb4..01f857a1 100644 --- a/wallet-sdk/build.gradle.kts +++ b/wallet-sdk/build.gradle.kts @@ -37,6 +37,7 @@ dependencies { implementation(libs.toml4j) implementation(libs.jjwt) implementation(libs.bcastle) + implementation(libs.bcutil) testImplementation(libs.coroutines.test) testImplementation(libs.kotlin.junit) diff --git a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/anchor/Anchor.kt b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/anchor/Anchor.kt index 90d17e18..91b1ad04 100644 --- a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/anchor/Anchor.kt +++ b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/anchor/Anchor.kt @@ -4,7 +4,7 @@ package org.stellar.walletsdk.anchor import io.ktor.client.* import io.ktor.http.* -import org.stellar.sdk.* +import org.stellar.sdk.Network import org.stellar.walletsdk.* import org.stellar.walletsdk.auth.AuthToken import org.stellar.walletsdk.auth.Sep10 diff --git a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/asset/AssetId.kt b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/asset/AssetId.kt index 8ed17f15..7e696b93 100644 --- a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/asset/AssetId.kt +++ b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/asset/AssetId.kt @@ -4,7 +4,6 @@ import mu.KotlinLogging import org.stellar.sdk.Asset import org.stellar.sdk.AssetTypeCreditAlphaNum import org.stellar.sdk.AssetTypeNative -import org.stellar.sdk.AssetTypePoolShare val log = KotlinLogging.logger {} @@ -51,9 +50,6 @@ fun Asset.toAssetId(): StellarAssetId = is AssetTypeCreditAlphaNum -> { IssuedAssetId(this.code, this.issuer) } - is AssetTypePoolShare -> { - throw UnsupportedOperationException("Unsupported asset type") - } else -> { throw UnsupportedOperationException("Unknown asset type") } diff --git a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/exception/StellarExceptions.kt b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/exception/StellarExceptions.kt index 71b63312..b4415d07 100644 --- a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/exception/StellarExceptions.kt +++ b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/exception/StellarExceptions.kt @@ -2,7 +2,7 @@ package org.stellar.walletsdk.exception import java.math.BigDecimal import org.stellar.sdk.AbstractTransaction -import org.stellar.sdk.responses.SubmitTransactionResponse +import org.stellar.sdk.exception.BadRequestException sealed class StellarException : WalletException { constructor(message: String) : super(message) @@ -21,23 +21,25 @@ class AccountNotEnoughBalanceException( ) class TransactionSubmitFailedException( - val response: SubmitTransactionResponse, + val exception: BadRequestException, val transaction: AbstractTransaction ) : StellarException( - "Submit transaction failed with code ${response.resultCode ?: ""}" + - ".${response.operationsResultCodes ?. run { " Operation result codes: $this" } ?: ""}" + + "Submit transaction failed with code ${exception.resultCode ?: ""}" + + ".${exception.operationsResultCodes ?. run { " Operation result codes: $this" } ?: ""}" + " Transaction XDR: ${transaction.toEnvelopeXdrBase64()}" ) { - val transactionResultCode = response.resultCode - val operationsResultCodes = response.operationsResultCodes + val transactionResultCode = exception.resultCode + val operationsResultCodes = exception.operationsResultCodes } -private val SubmitTransactionResponse.resultCode: String? - get() = this.extras?.resultCodes?.transactionResultCode +private val BadRequestException.resultCode: String? + get() = this.problem?.extras?.resultCodes?.transactionResultCode -private val SubmitTransactionResponse.operationsResultCodes: List? +private val BadRequestException.operationsResultCodes: List? get() = - this.extras?.resultCodes?.operationsResultCodes?.run { if (this.isEmpty()) null else this } + this.problem?.extras?.resultCodes?.operationsResultCodes?.run { + if (this.isEmpty()) null else this + } class OperationsLimitExceededException : StellarException("Maximum limit is 200 operations") diff --git a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/exception/WalletException.kt b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/exception/WalletException.kt index bfc0e12a..5f25bf08 100644 --- a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/exception/WalletException.kt +++ b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/exception/WalletException.kt @@ -1,7 +1,7 @@ package org.stellar.walletsdk.exception import kotlinx.serialization.Serializable -import org.stellar.sdk.requests.ErrorResponse +import org.stellar.sdk.exception.NetworkException @Serializable data class AnchorErrorResponse(val error: String) @@ -12,7 +12,7 @@ sealed class WalletException : Exception { class AnchorRequestException(message: String, cause: Exception) : WalletException(message, cause) -class HorizonRequestFailedException(val response: ErrorResponse) : +class HorizonRequestFailedException(val response: NetworkException) : WalletException(response.body ?: response.message ?: "Horizon request failed") { - val errorCode = response.code + val errorCode: Int? = response.code } diff --git a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/extension/Account.kt b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/extension/Account.kt index 3f4e36f7..66d86e77 100644 --- a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/extension/Account.kt +++ b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/extension/Account.kt @@ -33,8 +33,7 @@ fun AccountResponse.reservedBalance(): String { val numSponsoring = numSponsoring.toBigDecimal() val numSponsored = numSponsored.toBigDecimal() - val sellingLiabilities = - balances.find { it.assetType == "native" }?.sellingLiabilities?.get() ?: "0" + val sellingLiabilities = balances.find { it.assetType == "native" }?.sellingLiabilities ?: "0" // (2 + numSubEntries + numSponsoring - numSponsored) * baseReserve + liabilities.selling return BigDecimal(BASE_RESERVE_MIN_COUNT) diff --git a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/extension/Server.kt b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/extension/Server.kt index 1675b209..1cd22bc4 100644 --- a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/extension/Server.kt +++ b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/extension/Server.kt @@ -1,7 +1,7 @@ package org.stellar.walletsdk.extension import org.stellar.sdk.Server -import org.stellar.sdk.requests.ErrorResponse +import org.stellar.sdk.exception.NetworkException import org.stellar.sdk.responses.AccountResponse import org.stellar.sdk.responses.operations.OperationResponse import org.stellar.walletsdk.* @@ -12,7 +12,7 @@ import org.stellar.walletsdk.exception.OperationsLimitExceededException private fun safeHorizonCall(body: () -> T): T { try { return body() - } catch (e: ErrorResponse) { + } catch (e: NetworkException) { throw HorizonRequestFailedException(e) } catch (e: Exception) { throw e diff --git a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/Stellar.kt b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/Stellar.kt index e398121a..4d8953da 100644 --- a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/Stellar.kt +++ b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/Stellar.kt @@ -4,7 +4,8 @@ import java.time.Duration import kotlin.math.min import mu.KotlinLogging import org.stellar.sdk.* -import org.stellar.sdk.responses.SubmitTransactionTimeoutResponseException +import org.stellar.sdk.exception.BadRequestException +import org.stellar.sdk.exception.RequestTimeoutException import org.stellar.walletsdk.Config import org.stellar.walletsdk.StellarConfiguration import org.stellar.walletsdk.anchor.MemoType @@ -87,10 +88,11 @@ internal constructor( transaction: Transaction, baseFee: ULong? = null ): FeeBumpTransaction { - return FeeBumpTransaction.Builder(transaction) - .setBaseFee((baseFee ?: cfg.stellar.baseFee).toLong()) - .setFeeAccount(feeAddress.address) - .build() + return FeeBumpTransaction.createWithBaseFee( + feeAddress.address, + (baseFee ?: cfg.stellar.baseFee).toLong(), + transaction + ) } /** @@ -113,35 +115,26 @@ internal constructor( "${signedTransaction.operations.size}, signatureCount = ${signedTransaction .signatures.size}" } - val response = server.submitTransaction(signedTransaction) - - if (!response.isSuccess) { - throw TransactionSubmitFailedException(response, signedTransaction) - } - log.debug { "Transaction submitted with hash ${response.hash}" } } is FeeBumpTransaction -> { log.debug { - "Submit fee bump transaction. Source account :${signedTransaction.feeAccount}. Inner transaction hash: " + + "Submit fee bump transaction. Source account :${signedTransaction.feeSource}. Inner transaction hash: " + "${signedTransaction.innerTransaction.hashHex()}." } - val response = server.submitTransaction(signedTransaction) - - if (!response.isSuccess) { - throw TransactionSubmitFailedException(response, signedTransaction) - } - log.debug { "Transaction submitted with hash ${response.hash}" } } else -> error("Unknown transaction type") } - } catch (e: SubmitTransactionTimeoutResponseException) { + } catch (e: BadRequestException) { + throw TransactionSubmitFailedException(e, signedTransaction) + } catch (e: RequestTimeoutException) { log.info { "Transaction ${signedTransaction.hashHex()} timed out. Resubmitting..." } return submitTransaction(signedTransaction) } + // Other exceptions are not caught and are propagated } /** diff --git a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/transaction/CommonTransactionBuilder.kt b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/transaction/CommonTransactionBuilder.kt index b1a7ec57..0f028f0a 100644 --- a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/transaction/CommonTransactionBuilder.kt +++ b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/transaction/CommonTransactionBuilder.kt @@ -1,7 +1,9 @@ package org.stellar.walletsdk.horizon.transaction +import java.math.BigDecimal import mu.KotlinLogging import org.stellar.sdk.* +import org.stellar.sdk.operations.* import org.stellar.walletsdk.DECIMAL_POINT_PRECISION import org.stellar.walletsdk.asset.IssuedAssetId import org.stellar.walletsdk.exception.HorizonRequestFailedException @@ -37,9 +39,10 @@ abstract class CommonTransactionBuilder(protected val sourceAddress: String) val signer = Signer.ed25519PublicKey(signerAddress.keyPair) - SetOptionsOperation.Builder() - .setSourceAccount(sourceAddress) - .setSigner(signer, signerWeight) + SetOptionsOperation.builder() + .sourceAccount(sourceAddress) + .signer(signer) + .signerWeight(signerWeight) .build() } @@ -70,7 +73,7 @@ abstract class CommonTransactionBuilder(protected val sourceAddress: String) fun lockAccountMasterKey() = building { log.debug { "Lock master key tx: accountAddress = $sourceAddress" } - SetOptionsOperation.Builder().setSourceAccount(sourceAddress).setMasterKeyWeight(0).build() + SetOptionsOperation.builder().sourceAccount(sourceAddress).masterKeyWeight(0).build() } /** @@ -91,9 +94,13 @@ abstract class CommonTransactionBuilder(protected val sourceAddress: String) "asset=$asset, trustLimit = $trustLimit" } - val stellarAsset = ChangeTrustAsset.createNonNativeAsset(asset.code, asset.issuer) + val stellarAsset = ChangeTrustAsset(Asset.createNonNativeAsset(asset.code, asset.issuer)) - ChangeTrustOperation.Builder(stellarAsset, trustLimit).setSourceAccount(sourceAddress).build() + ChangeTrustOperation.builder() + .asset(stellarAsset) + .limit(BigDecimal(trustLimit)) + .sourceAccount(sourceAddress) + .build() } /** @@ -108,11 +115,11 @@ abstract class CommonTransactionBuilder(protected val sourceAddress: String) } fun setThreshold(low: Int, medium: Int, high: Int) = building { - SetOptionsOperation.Builder() - .setSourceAccount(sourceAddress) - .setLowThreshold(low) - .setMediumThreshold(medium) - .setHighThreshold(high) + SetOptionsOperation.builder() + .sourceAccount(sourceAddress) + .lowThreshold(low) + .mediumThreshold(medium) + .highThreshold(high) .build() } @@ -126,8 +133,10 @@ abstract class CommonTransactionBuilder(protected val sourceAddress: String) "startBalance = $startingBalance" } - return CreateAccountOperation.Builder(newAccount.address, startingBalance.toString()) - .setSourceAccount(sourceAddress) + return CreateAccountOperation.builder() + .destination(newAccount.address) + .startingBalance(BigDecimal(startingBalance.toString())) + .sourceAccount(sourceAddress) .build() } } diff --git a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/transaction/SponsoringBuilder.kt b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/transaction/SponsoringBuilder.kt index 22bc9bbe..141e91bd 100644 --- a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/transaction/SponsoringBuilder.kt +++ b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/transaction/SponsoringBuilder.kt @@ -1,6 +1,6 @@ package org.stellar.walletsdk.horizon.transaction -import org.stellar.sdk.* +import org.stellar.sdk.operations.* import org.stellar.walletsdk.horizon.AccountKeyPair class SponsoringBuilder @@ -26,12 +26,15 @@ internal constructor( } private fun startSponsoring(address: String) = building { - BeginSponsoringFutureReservesOperation.Builder(address) - .setSourceAccount(sponsorAccount.address) + BeginSponsoringFutureReservesOperation.builder() + .sponsoredId(address) + .sourceAccount(sponsorAccount.address) .build() } - internal fun stopSponsoring() = building { EndSponsoringFutureReservesOperation(sourceAddress) } + internal fun stopSponsoring() = building { + EndSponsoringFutureReservesOperation.builder().sourceAccount(sourceAddress).build() + } /** * Adds operation to this builder diff --git a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/transaction/TransactionBuilder.kt b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/transaction/TransactionBuilder.kt index 4c1779c5..7595cc5d 100644 --- a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/transaction/TransactionBuilder.kt +++ b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/transaction/TransactionBuilder.kt @@ -1,7 +1,13 @@ package org.stellar.walletsdk.horizon.transaction -import org.stellar.sdk.* +import java.math.BigDecimal +import org.stellar.sdk.Network +import org.stellar.sdk.TimeBounds +import org.stellar.sdk.Transaction import org.stellar.sdk.TransactionBuilder as SdkBuilder +import org.stellar.sdk.TransactionPreconditions +import org.stellar.sdk.operations.Operation +import org.stellar.sdk.operations.PaymentOperation import org.stellar.sdk.responses.AccountResponse import org.stellar.walletsdk.Config import org.stellar.walletsdk.anchor.MemoType @@ -100,7 +106,11 @@ internal constructor( * @return formed transfer transaction */ fun transfer(destinationAddress: String, assetId: StellarAssetId, amount: String) = building { - PaymentOperation.Builder(destinationAddress, assetId.toAsset(), amount).build() + PaymentOperation.builder() + .destination(destinationAddress) + .asset(assetId.toAsset()) + .amount(BigDecimal(amount)) + .build() } /** diff --git a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/recovery/Recovery.kt b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/recovery/Recovery.kt index d81fefa0..139a6393 100644 --- a/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/recovery/Recovery.kt +++ b/wallet-sdk/src/main/kotlin/org/stellar/walletsdk/recovery/Recovery.kt @@ -2,7 +2,8 @@ package org.stellar.walletsdk.recovery import io.ktor.client.* import mu.KotlinLogging -import org.stellar.sdk.* +import org.stellar.sdk.KeyPair +import org.stellar.sdk.Transaction import org.stellar.sdk.xdr.DecoratedSignature import org.stellar.sdk.xdr.Signature import org.stellar.walletsdk.AccountThreshold diff --git a/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/ConstantTest.kt b/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/ConstantTest.kt index 8f6b3aa8..b68ea531 100644 --- a/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/ConstantTest.kt +++ b/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/ConstantTest.kt @@ -1,7 +1,8 @@ package org.stellar.walletsdk -import org.stellar.sdk.ClaimClaimableBalanceOperation -import org.stellar.sdk.CreateAccountOperation +import java.math.BigDecimal +import org.stellar.sdk.operations.ClaimClaimableBalanceOperation +import org.stellar.sdk.operations.CreateAccountOperation import org.stellar.walletsdk.asset.IssuedAssetId import org.stellar.walletsdk.horizon.SigningKeyPair import org.stellar.walletsdk.horizon.toPublicKeyPair @@ -32,14 +33,15 @@ const val ANCHOR_SERVICE_URL = "https://testanchor.stellar.org/sep24" const val ANCHOR_HOME_DOMAIN = "testanchor.stellar.org" val OP_CREATE_ACCOUNT: CreateAccountOperation = - CreateAccountOperation.Builder(ADDRESS_ACTIVE.address, "1") - .setSourceAccount(ADDRESS_INACTIVE.address) + CreateAccountOperation.builder() + .destination(ADDRESS_ACTIVE.address) + .startingBalance(BigDecimal("1")) + .sourceAccount(ADDRESS_INACTIVE.address) .build() val OP_CLAIM_CLAIMABLE_BALANCE: ClaimClaimableBalanceOperation = - ClaimClaimableBalanceOperation.Builder( - "000000009c05cd4bfc4db9774d0895be09929b199c0b7625d963e6203e0cdc0c6bb3bbae" - ) - .setSourceAccount(ADDRESS_ACTIVE.address) + ClaimClaimableBalanceOperation.builder() + .balanceId("000000009c05cd4bfc4db9774d0895be09929b199c0b7625d963e6203e0cdc0c6bb3bbae") + .sourceAccount(ADDRESS_ACTIVE.address) .build() // Source account GAMQTINWD3YPP3GLTQZ4M6FKCCSRGROQLIIRVECIFC6VEGL5F64CND22 diff --git a/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/SubmitTransactionTest.kt b/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/SubmitTransactionTest.kt index aed5356e..8112afed 100644 --- a/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/SubmitTransactionTest.kt +++ b/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/SubmitTransactionTest.kt @@ -9,12 +9,15 @@ import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.assertTrue import kotlinx.coroutines.runBlocking +import okio.IOException import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertDoesNotThrow import org.stellar.sdk.Server import org.stellar.sdk.Transaction -import org.stellar.sdk.responses.SubmitTransactionResponse -import org.stellar.sdk.responses.SubmitTransactionTimeoutResponseException +import org.stellar.sdk.exception.BadRequestException +import org.stellar.sdk.exception.RequestTimeoutException +import org.stellar.sdk.responses.Problem +import org.stellar.sdk.responses.TransactionResponse import org.stellar.walletsdk.exception.TransactionSubmitFailedException import org.stellar.walletsdk.helpers.stellarObjectFromJsonFile @@ -29,9 +32,8 @@ internal class SubmitTransactionTest { @Test fun `returns true on success`() { - val mockResponse = mockk() + val mockResponse = mockk() - every { mockResponse.isSuccess } returns true every { server.submitTransaction(any() as Transaction) } returns mockResponse assertDoesNotThrow { (runBlocking { wallet.stellar().submitTransaction(transaction) }) } @@ -41,12 +43,12 @@ internal class SubmitTransactionTest { @Test fun `throws exception with txn result code`() { val txnResultCode = "txn_failed_test" - val mockResponse = mockk() + val badRequestException = mockk() + every { badRequestException.problem?.extras?.resultCodes?.transactionResultCode } returns + txnResultCode + every { badRequestException.problem?.extras?.resultCodes?.operationsResultCodes } returns null - every { mockResponse.isSuccess } returns false - every { mockResponse.extras.resultCodes.transactionResultCode } returns txnResultCode - every { mockResponse.extras.resultCodes.operationsResultCodes } returns null - every { server.submitTransaction(any() as Transaction) } returns mockResponse + every { server.submitTransaction(any() as Transaction) } throws badRequestException val exception = assertFailsWith( @@ -74,12 +76,11 @@ internal class SubmitTransactionTest { @Test fun `resubmit works`() { - val mockResponse = mockk() - every { mockResponse.isSuccess } returns true + val mockResponse = mockk() every { server.submitTransaction(any() as Transaction) } throws - SubmitTransactionTimeoutResponseException() andThenThrows - SubmitTransactionTimeoutResponseException() andThen + RequestTimeoutException(IOException()) andThenThrows + RequestTimeoutException(IOException()) andThen mockResponse assertDoesNotThrow { (runBlocking { wallet.stellar().submitTransaction(transaction) }) } @@ -92,12 +93,14 @@ internal class SubmitTransactionTest { // 4. Verify account has been created @Test fun `rebuild works`() { - val expiredResponse: SubmitTransactionResponse = - stellarObjectFromJsonFile("expired_transaction.json") + val problem: Problem = stellarObjectFromJsonFile("expired_transaction.json") + + val badRequestException = mockk() + every { badRequestException.problem } returns problem - every { server.submitTransaction(any() as Transaction) } returns - expiredResponse andThen - expiredResponse andThenAnswer + every { server.submitTransaction(any() as Transaction) } throws + badRequestException andThenThrows + badRequestException andThenAnswer { callOriginal() } diff --git a/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/account/AddRemoveAssetTest.kt b/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/account/AddRemoveAssetTest.kt index 16ead329..fa4fc3ae 100644 --- a/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/account/AddRemoveAssetTest.kt +++ b/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/account/AddRemoveAssetTest.kt @@ -1,12 +1,13 @@ package org.stellar.walletsdk.account import io.mockk.spyk +import java.math.BigDecimal import kotlin.test.assertEquals import kotlinx.coroutines.runBlocking import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertDoesNotThrow -import org.stellar.sdk.ChangeTrustOperation import org.stellar.sdk.Server +import org.stellar.sdk.operations.ChangeTrustOperation import org.stellar.walletsdk.ADDRESS_ACTIVE import org.stellar.walletsdk.ASSET_USDC import org.stellar.walletsdk.HORIZON_URL @@ -61,7 +62,6 @@ internal class AddRemoveAssetTest { val transaction = runBlocking { stellar.transaction(ADDRESS_ACTIVE).removeAssetSupport(ASSET_USDC) }.build() val trustLimit = (transaction.operations[0] as ChangeTrustOperation).limit - - assertEquals("0", trustLimit) + assertEquals(0, trustLimit.compareTo(BigDecimal.ZERO)) } } diff --git a/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/account/AddRemoveSignerTest.kt b/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/account/AddRemoveSignerTest.kt index 977c6d5d..d1fc987a 100644 --- a/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/account/AddRemoveSignerTest.kt +++ b/wallet-sdk/src/test/kotlin/org/stellar/walletsdk/account/AddRemoveSignerTest.kt @@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertDoesNotThrow import org.junit.jupiter.api.assertThrows import org.stellar.sdk.Server -import org.stellar.sdk.SetOptionsOperation +import org.stellar.sdk.operations.SetOptionsOperation import org.stellar.walletsdk.* internal class AddRemoveSignerTest { From 0dd32406daf306c671185d7ee3b7c84b499558b8 Mon Sep 17 00:00:00 2001 From: Jun Luo <4catcode@gmail.com> Date: Tue, 12 Nov 2024 20:45:22 +0800 Subject: [PATCH 2/4] Fix deps. --- gradle/libs.versions.toml | 4 +--- wallet-sdk/build.gradle.kts | 9 +++++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0070407f..8a538868 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,6 @@ [versions] # Library versions -bcastle = "1.79" -bcutil = "1.79" +bcastle = "1.77" coroutines = "1.6.4" google-gson = "2.8.9" hoplite = "2.7.0" @@ -24,7 +23,6 @@ detekt = "1.22.0" [libraries] bcastle = { module = "org.bouncycastle:bcprov-jdk18on", version.ref = "bcastle" } -bcutil = { module = "org.bouncycastle:bcutil-jdk18on", version.ref = "bcutil" } coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" } coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" } google-gson = { module = "com.google.code.gson:gson", version.ref = "google-gson" } diff --git a/wallet-sdk/build.gradle.kts b/wallet-sdk/build.gradle.kts index 01f857a1..e9421040 100644 --- a/wallet-sdk/build.gradle.kts +++ b/wallet-sdk/build.gradle.kts @@ -28,7 +28,13 @@ configurations { dependencies { api(libs.coroutines.core) - api(libs.java.stellar.sdk) + api(libs.java.stellar.sdk) { + // The Java SDK uses version bcprov-jdk18on 1.78.1, but I think there are some issues with this version, + // so we had to exclude this dependency; the problem also appears in 1.79. + // https://github.com/bcgit/bc-java/issues/1621 + // Maybe I should downgrade the version to 1.77 in the final release. + exclude(group = "org.bouncycastle", module = "bcprov-jdk18on") + } api(libs.kotlin.datetime) api(libs.bundles.ktor.client) implementation(libs.kotlin.serialization.json) @@ -37,7 +43,6 @@ dependencies { implementation(libs.toml4j) implementation(libs.jjwt) implementation(libs.bcastle) - implementation(libs.bcutil) testImplementation(libs.coroutines.test) testImplementation(libs.kotlin.junit) From 42b7acbf01eb033dc1828a986d3b2e94cd4ac6ec Mon Sep 17 00:00:00 2001 From: Jun Luo <4catcode@gmail.com> Date: Wed, 13 Nov 2024 09:55:24 +0800 Subject: [PATCH 3/4] Bump bcastle to 1.79, add bcutil. --- gradle/libs.versions.toml | 5 ++++- wallet-sdk/build.gradle.kts | 9 ++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8a538868..3be4938e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,6 +1,7 @@ [versions] # Library versions -bcastle = "1.77" +bcastle = "1.79" +bcutil = "1.79" coroutines = "1.6.4" google-gson = "2.8.9" hoplite = "2.7.0" @@ -23,6 +24,8 @@ detekt = "1.22.0" [libraries] bcastle = { module = "org.bouncycastle:bcprov-jdk18on", version.ref = "bcastle" } +# EdECObjectIdentifiers is an internal class in bcastle, so we need to import bcutil. +bcutil = { module = "org.bouncycastle:bcutil-jdk18on", version.ref = "bcutil" } coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" } coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" } google-gson = { module = "com.google.code.gson:gson", version.ref = "google-gson" } diff --git a/wallet-sdk/build.gradle.kts b/wallet-sdk/build.gradle.kts index e9421040..01f857a1 100644 --- a/wallet-sdk/build.gradle.kts +++ b/wallet-sdk/build.gradle.kts @@ -28,13 +28,7 @@ configurations { dependencies { api(libs.coroutines.core) - api(libs.java.stellar.sdk) { - // The Java SDK uses version bcprov-jdk18on 1.78.1, but I think there are some issues with this version, - // so we had to exclude this dependency; the problem also appears in 1.79. - // https://github.com/bcgit/bc-java/issues/1621 - // Maybe I should downgrade the version to 1.77 in the final release. - exclude(group = "org.bouncycastle", module = "bcprov-jdk18on") - } + api(libs.java.stellar.sdk) api(libs.kotlin.datetime) api(libs.bundles.ktor.client) implementation(libs.kotlin.serialization.json) @@ -43,6 +37,7 @@ dependencies { implementation(libs.toml4j) implementation(libs.jjwt) implementation(libs.bcastle) + implementation(libs.bcutil) testImplementation(libs.coroutines.test) testImplementation(libs.kotlin.junit) From 945aa646547186564d3112fc4f3185c75108af02 Mon Sep 17 00:00:00 2001 From: Jun Luo <4catcode@gmail.com> Date: Thu, 14 Nov 2024 20:09:03 +0800 Subject: [PATCH 4/4] Bump stellar-sdk to 1.0.0-rc0. --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3be4938e..80d1711e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -6,7 +6,7 @@ coroutines = "1.6.4" google-gson = "2.8.9" hoplite = "2.7.0" jjwt = "0.12.5" -java-stellar-sdk = "1.0.0-beta1" +java-stellar-sdk = "1.0.0-rc0" dokka = "1.6.10" kotlin = "1.8.20" kotlinx-json = "1.5.0"