Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump java-stellar-sdk to 1.0.0-rc0. #155

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -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-rc0"
dokka = "1.6.10"
kotlin = "1.8.20"
kotlinx-json = "1.5.0"
Expand All @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions wallet-sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down Expand Up @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 ?: "<unknown>"}" +
".${response.operationsResultCodes ?. run { " Operation result codes: $this" } ?: ""}" +
"Submit transaction failed with code ${exception.resultCode ?: "<unknown>"}" +
".${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<String>?
private val BadRequestException.operationsResultCodes: List<String>?
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")
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*
Expand All @@ -12,7 +12,7 @@ import org.stellar.walletsdk.exception.OperationsLimitExceededException
private fun <T> safeHorizonCall(body: () -> T): T {
try {
return body()
} catch (e: ErrorResponse) {
} catch (e: NetworkException) {
throw HorizonRequestFailedException(e)
} catch (e: Exception) {
throw e
Expand Down
31 changes: 12 additions & 19 deletions wallet-sdk/src/main/kotlin/org/stellar/walletsdk/horizon/Stellar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
}

/**
Expand All @@ -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
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -37,9 +39,10 @@ abstract class CommonTransactionBuilder<T>(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()
}

Expand Down Expand Up @@ -70,7 +73,7 @@ abstract class CommonTransactionBuilder<T>(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()
}

/**
Expand All @@ -91,9 +94,13 @@ abstract class CommonTransactionBuilder<T>(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()
}

/**
Expand All @@ -108,11 +115,11 @@ abstract class CommonTransactionBuilder<T>(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()
}

Expand All @@ -126,8 +133,10 @@ abstract class CommonTransactionBuilder<T>(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()
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions wallet-sdk/src/test/kotlin/org/stellar/walletsdk/ConstantTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading