From 8508332f7418509fdbf5f0c96c29192369656ff0 Mon Sep 17 00:00:00 2001 From: overcat <4catcode@gmail.com> Date: Wed, 9 Aug 2023 12:01:03 +0800 Subject: [PATCH] Remove unnecessary to/fromBase64 code and use the one from the xdr package. --- .../org/stellar/sdk/AbstractTransaction.java | 34 ++------ src/main/java/org/stellar/sdk/Operation.java | 10 +-- .../java/org/stellar/sdk/SorobanServer.java | 82 +++++++------------ src/main/java/org/stellar/sdk/StrKey.java | 11 +-- .../org/stellar/sdk/TransactionBuilder.java | 8 +- src/main/java/org/stellar/sdk/Util.java | 52 +----------- .../org/stellar/sdk/SorobanServerTest.java | 6 +- .../stellar/sdk/TransactionBuilderTest.java | 4 +- src/test/java/org/stellar/sdk/UtilTest.java | 45 ---------- 9 files changed, 57 insertions(+), 195 deletions(-) delete mode 100644 src/test/java/org/stellar/sdk/UtilTest.java diff --git a/src/main/java/org/stellar/sdk/AbstractTransaction.java b/src/main/java/org/stellar/sdk/AbstractTransaction.java index 0cef65a2c..58e89cddf 100644 --- a/src/main/java/org/stellar/sdk/AbstractTransaction.java +++ b/src/main/java/org/stellar/sdk/AbstractTransaction.java @@ -4,8 +4,6 @@ import com.google.common.collect.ImmutableList; import com.google.common.io.BaseEncoding; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -15,8 +13,6 @@ import org.stellar.sdk.xdr.SignatureHint; import org.stellar.sdk.xdr.TransactionEnvelope; import org.stellar.sdk.xdr.TransactionSignaturePayload; -import org.stellar.sdk.xdr.XdrDataInputStream; -import org.stellar.sdk.xdr.XdrDataOutputStream; public abstract class AbstractTransaction { protected final Network mNetwork; @@ -120,13 +116,7 @@ public void addSignature(DecoratedSignature signature) { */ public String toEnvelopeXdrBase64() { try { - TransactionEnvelope envelope = this.toEnvelopeXdr(); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrOutputStream = new XdrDataOutputStream(outputStream); - TransactionEnvelope.encode(xdrOutputStream, envelope); - - BaseEncoding base64Encoding = BaseEncoding.base64(); - return base64Encoding.encode(outputStream.toByteArray()); + return this.toEnvelopeXdr().toXdrBase64(); } catch (IOException e) { throw new AssertionError(e); } @@ -176,11 +166,7 @@ public static AbstractTransaction fromEnvelopeXdr(TransactionEnvelope envelope, */ public static AbstractTransaction fromEnvelopeXdr( AccountConverter accountConverter, String envelope, Network network) throws IOException { - BaseEncoding base64Encoding = BaseEncoding.base64(); - byte[] bytes = base64Encoding.decode(envelope); - - TransactionEnvelope transactionEnvelope = - TransactionEnvelope.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes))); + TransactionEnvelope transactionEnvelope = TransactionEnvelope.fromXdrBase64(envelope); return fromEnvelopeXdr(accountConverter, transactionEnvelope, network); } @@ -201,15 +187,13 @@ public static byte[] getTransactionSignatureBase( TransactionSignaturePayload.TransactionSignaturePayloadTaggedTransaction taggedTransaction, Network network) { try { - TransactionSignaturePayload payload = new TransactionSignaturePayload(); - Hash hash = new Hash(); - hash.setHash(network.getNetworkId()); - payload.setNetworkId(hash); - payload.setTaggedTransaction(taggedTransaction); - ByteArrayOutputStream txOutputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrOutputStream = new XdrDataOutputStream(txOutputStream); - payload.encode(xdrOutputStream); - return txOutputStream.toByteArray(); + Hash networkIdHash = new Hash(network.getNetworkId()); + TransactionSignaturePayload payload = + new TransactionSignaturePayload.Builder() + .networkId(networkIdHash) + .taggedTransaction(taggedTransaction) + .build(); + return payload.toXdrByteArray(); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/main/java/org/stellar/sdk/Operation.java b/src/main/java/org/stellar/sdk/Operation.java index 54d479295..f9af93cb3 100644 --- a/src/main/java/org/stellar/sdk/Operation.java +++ b/src/main/java/org/stellar/sdk/Operation.java @@ -2,15 +2,12 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.io.BaseEncoding; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.math.BigDecimal; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import lombok.experimental.SuperBuilder; -import org.stellar.sdk.xdr.XdrDataOutputStream; /** Abstract class for operations. */ @SuperBuilder(toBuilder = true) @@ -51,12 +48,7 @@ public org.stellar.sdk.xdr.Operation toXdr() { /** Returns base64-encoded Operation XDR object. */ public String toXdrBase64(AccountConverter accountConverter) { try { - org.stellar.sdk.xdr.Operation operation = this.toXdr(accountConverter); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrOutputStream = new XdrDataOutputStream(outputStream); - org.stellar.sdk.xdr.Operation.encode(xdrOutputStream, operation); - BaseEncoding base64Encoding = BaseEncoding.base64(); - return base64Encoding.encode(outputStream.toByteArray()); + return toXdr(accountConverter).toXdrBase64(); } catch (IOException e) { throw new AssertionError(e); } diff --git a/src/main/java/org/stellar/sdk/SorobanServer.java b/src/main/java/org/stellar/sdk/SorobanServer.java index f1b81fe99..61c1d381e 100644 --- a/src/main/java/org/stellar/sdk/SorobanServer.java +++ b/src/main/java/org/stellar/sdk/SorobanServer.java @@ -1,10 +1,7 @@ package org.stellar.sdk; -import com.google.common.io.BaseEncoding; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; import java.util.ArrayList; @@ -14,7 +11,6 @@ import java.util.Optional; import java.util.UUID; import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; import javax.annotation.Nullable; import okhttp3.HttpUrl; import okhttp3.MediaType; @@ -48,8 +44,6 @@ import org.stellar.sdk.xdr.SCVal; import org.stellar.sdk.xdr.SorobanAuthorizationEntry; import org.stellar.sdk.xdr.SorobanTransactionData; -import org.stellar.sdk.xdr.XdrDataInputStream; -import org.stellar.sdk.xdr.XdrDataOutputStream; /** * Main class used to connect to the Soroban-RPC instance and exposes an interface for requests to @@ -122,8 +116,12 @@ public TransactionBuilderAccount getAccount(String accountId) if (entries == null || entries.isEmpty()) { throw new AccountNotFoundException(accountId); } - LedgerEntry.LedgerEntryData ledgerEntryData = - ledgerEntryDataFromXdrBase64(entries.get(0).getXdr()); + LedgerEntry.LedgerEntryData ledgerEntryData; + try { + ledgerEntryData = LedgerEntry.LedgerEntryData.fromXdrBase64(entries.get(0).getXdr()); + } catch (IOException e) { + throw new IllegalArgumentException("invalid ledgerEntryData: " + entries.get(0).getXdr(), e); + } long sequence = ledgerEntryData.getAccount().getSeqNum().getSequenceNumber().getInt64(); return new Account(accountId, sequence); } @@ -215,13 +213,21 @@ public Optional getContractData( * @throws SorobanRpcErrorResponse If the Soroban-RPC instance returns an error response. */ public GetLedgerEntriesResponse getLedgerEntries(Collection keys) - throws IOException, SorobanRpcErrorResponse { + throws SorobanRpcErrorResponse, IOException { if (keys.isEmpty()) { throw new IllegalArgumentException("At least one key must be provided."); } - List xdrKeys = - keys.stream().map(SorobanServer::ledgerKeyToXdrBase64).collect(Collectors.toList()); + List xdrKeys = new ArrayList<>(); + for (LedgerKey key : keys) { + String xdrBase64; + try { + xdrBase64 = key.toXdrBase64(); + } catch (IOException e) { + throw new IllegalArgumentException("invalid ledgerKey: " + key, e); + } + xdrKeys.add(xdrBase64); + } GetLedgerEntriesRequest params = new GetLedgerEntriesRequest(xdrKeys); return this.sendRequest( "getLedgerEntries", @@ -421,7 +427,11 @@ private Transaction assembleTransaction( List newEntries = new ArrayList<>(originalEntries); if (simulateHostFunctionResult.getAuth() != null) { for (String auth : simulateHostFunctionResult.getAuth()) { - newEntries.add(sorobanAuthorizationEntryFromXdrBase64(auth)); + try { + newEntries.add(SorobanAuthorizationEntry.fromXdrBase64(auth)); + } catch (IOException e) { + throw new IllegalArgumentException("invalid auth: " + auth, e); + } } } operation = @@ -432,8 +442,14 @@ private Transaction assembleTransaction( .build(); } - SorobanTransactionData sorobanData = - Util.sorobanTransactionDataToXDR(simulateTransactionResponse.getTransactionData()); + SorobanTransactionData sorobanData; + try { + sorobanData = + SorobanTransactionData.fromXdrBase64(simulateTransactionResponse.getTransactionData()); + } catch (IOException e) { + throw new IllegalArgumentException( + "invalid transactionData: " + simulateTransactionResponse.getTransactionData(), e); + } return new Transaction( transaction.getAccountConverter(), @@ -477,44 +493,6 @@ private static String generateRequestId() { return UUID.randomUUID().toString(); } - private static String ledgerKeyToXdrBase64(LedgerKey ledgerKey) { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream); - try { - ledgerKey.encode(xdrDataOutputStream); - } catch (IOException e) { - throw new IllegalArgumentException("invalid ledgerKey.", e); - } - BaseEncoding base64Encoding = BaseEncoding.base64(); - return base64Encoding.encode(byteArrayOutputStream.toByteArray()); - } - - private static LedgerEntry.LedgerEntryData ledgerEntryDataFromXdrBase64(String ledgerEntryData) { - BaseEncoding base64Encoding = BaseEncoding.base64(); - byte[] bytes = base64Encoding.decode(ledgerEntryData); - ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); - XdrDataInputStream xdrInputStream = new XdrDataInputStream(inputStream); - try { - return LedgerEntry.LedgerEntryData.decode(xdrInputStream); - } catch (IOException e) { - throw new IllegalArgumentException("invalid ledgerEntryData: " + ledgerEntryData, e); - } - } - - private static SorobanAuthorizationEntry sorobanAuthorizationEntryFromXdrBase64( - String sorobanAuthorizationEntry) { - BaseEncoding base64Encoding = BaseEncoding.base64(); - byte[] bytes = base64Encoding.decode(sorobanAuthorizationEntry); - ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); - XdrDataInputStream xdrInputStream = new XdrDataInputStream(inputStream); - try { - return SorobanAuthorizationEntry.decode(xdrInputStream); - } catch (IOException e) { - throw new IllegalArgumentException( - "invalid ledgerEntryData: " + sorobanAuthorizationEntry, e); - } - } - /** * Represents the "durability keyspace" that this ledger key belongs to, check {@link * SorobanServer#getContractData} for more details. diff --git a/src/main/java/org/stellar/sdk/StrKey.java b/src/main/java/org/stellar/sdk/StrKey.java index c6ab224f6..26db710b3 100644 --- a/src/main/java/org/stellar/sdk/StrKey.java +++ b/src/main/java/org/stellar/sdk/StrKey.java @@ -98,9 +98,7 @@ public static AccountID encodeToXDRAccountId(String data) { PublicKey publicKey = new PublicKey(); publicKey.setDiscriminant(PublicKeyType.PUBLIC_KEY_TYPE_ED25519); try { - publicKey.setEd25519( - Uint256.decode( - new XdrDataInputStream(new ByteArrayInputStream(decodeStellarAccountId(data))))); + publicKey.setEd25519(Uint256.fromXdrByteArray(decodeStellarAccountId(data))); } catch (IOException e) { throw new IllegalArgumentException("invalid address: " + data, e); } @@ -118,9 +116,7 @@ public static MuxedAccount encodeToXDRMuxedAccount(String data) { case ACCOUNT_ID: muxed.setDiscriminant(CryptoKeyType.KEY_TYPE_ED25519); try { - muxed.setEd25519( - Uint256.decode( - new XdrDataInputStream(new ByteArrayInputStream(decodeStellarAccountId(data))))); + muxed.setEd25519(Uint256.fromXdrByteArray(decodeStellarAccountId(data))); } catch (IOException e) { throw new IllegalArgumentException("invalid address: " + data, e); } @@ -176,8 +172,7 @@ public static SignedPayloadSigner decodeSignedPayload(char[] data) { byte[] signedPayloadRaw = decodeCheck(VersionByte.SIGNED_PAYLOAD, data); SignerKey.SignerKeyEd25519SignedPayload xdrPayloadSigner = - SignerKey.SignerKeyEd25519SignedPayload.decode( - new XdrDataInputStream(new ByteArrayInputStream(signedPayloadRaw))); + SignerKey.SignerKeyEd25519SignedPayload.fromXdrByteArray(signedPayloadRaw); return new SignedPayloadSigner( xdrPayloadSigner.getEd25519().getUint256(), xdrPayloadSigner.getPayload()); diff --git a/src/main/java/org/stellar/sdk/TransactionBuilder.java b/src/main/java/org/stellar/sdk/TransactionBuilder.java index 487034086..da85d14f5 100644 --- a/src/main/java/org/stellar/sdk/TransactionBuilder.java +++ b/src/main/java/org/stellar/sdk/TransactionBuilder.java @@ -5,6 +5,7 @@ import static org.stellar.sdk.TransactionPreconditions.TIMEOUT_INFINITE; import com.google.common.base.Function; +import java.io.IOException; import java.math.BigInteger; import java.util.Collection; import java.util.List; @@ -280,7 +281,12 @@ public TransactionBuilder setSorobanData(SorobanTransactionData sorobanData) { * @return Builder object so you can chain methods. */ public TransactionBuilder setSorobanData(String sorobanData) { - SorobanTransactionData data = Util.sorobanTransactionDataToXDR(sorobanData); + SorobanTransactionData data; + try { + data = SorobanTransactionData.fromXdrBase64(sorobanData); + } catch (IOException e) { + throw new IllegalArgumentException("Invalid Soroban data: " + sorobanData, e); + } return setSorobanData(data); } } diff --git a/src/main/java/org/stellar/sdk/Util.java b/src/main/java/org/stellar/sdk/Util.java index eaa458058..c855b34c2 100644 --- a/src/main/java/org/stellar/sdk/Util.java +++ b/src/main/java/org/stellar/sdk/Util.java @@ -1,16 +1,11 @@ package org.stellar.sdk; import com.google.common.io.BaseEncoding; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import org.stellar.sdk.xdr.ClaimableBalanceID; -import org.stellar.sdk.xdr.SorobanTransactionData; -import org.stellar.sdk.xdr.XdrDataInputStream; -import org.stellar.sdk.xdr.XdrDataOutputStream; public class Util { @@ -103,62 +98,19 @@ public static String getSdkVersion() { public static ClaimableBalanceID claimableBalanceIdToXDR(String balanceId) { byte[] balanceIdBytes = BaseEncoding.base16().lowerCase().decode(balanceId.toLowerCase()); - XdrDataInputStream balanceIdXdrDataInputStream = - new XdrDataInputStream(new ByteArrayInputStream(balanceIdBytes)); - try { - return ClaimableBalanceID.decode(balanceIdXdrDataInputStream); + return ClaimableBalanceID.fromXdrByteArray(balanceIdBytes); } catch (IOException e) { throw new IllegalArgumentException("invalid balanceId: " + balanceId, e); } } public static String xdrToClaimableBalanceId(ClaimableBalanceID balanceId) { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream); try { - balanceId.encode(xdrDataOutputStream); + return BaseEncoding.base16().lowerCase().encode(balanceId.toXdrByteArray()); } catch (IOException e) { throw new IllegalArgumentException("invalid claimClaimableBalanceOp.", e); } - return BaseEncoding.base16().lowerCase().encode(byteArrayOutputStream.toByteArray()); - } - - /** - * Convert {@link SorobanTransactionData} XDR String representation to {@link - * SorobanTransactionData} object. - * - * @param sorobanData XDR String representation of a {@link SorobanTransactionData} - * @return {@link SorobanTransactionData} - */ - public static SorobanTransactionData sorobanTransactionDataToXDR(String sorobanData) { - BaseEncoding base64Encoding = BaseEncoding.base64(); - byte[] bytes = base64Encoding.decode(sorobanData); - ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); - XdrDataInputStream xdrInputStream = new XdrDataInputStream(inputStream); - try { - return SorobanTransactionData.decode(xdrInputStream); - } catch (IOException e) { - throw new IllegalArgumentException("invalid sorobanData: " + sorobanData, e); - } - } - - /** - * Convert {@link SorobanTransactionData} object to XDR String representation. - * - * @param sorobanData {@link SorobanTransactionData} object - * @return XDR String representation of a {@link SorobanTransactionData} - */ - public static String xdrToSorobanTransactionData(SorobanTransactionData sorobanData) { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream); - try { - sorobanData.encode(xdrDataOutputStream); - } catch (IOException e) { - throw new IllegalArgumentException("invalid sorobanData.", e); - } - BaseEncoding base64Encoding = BaseEncoding.base64(); - return base64Encoding.encode(byteArrayOutputStream.toByteArray()); } public static AssetTypeCreditAlphaNum assertNonNativeAsset(Asset asset) { diff --git a/src/test/java/org/stellar/sdk/SorobanServerTest.java b/src/test/java/org/stellar/sdk/SorobanServerTest.java index 7aae8ae34..57bba78ee 100644 --- a/src/test/java/org/stellar/sdk/SorobanServerTest.java +++ b/src/test/java/org/stellar/sdk/SorobanServerTest.java @@ -843,7 +843,7 @@ public MockResponse dispatch(@NotNull RecordedRequest recordedRequest) Transaction newTx = server.prepareTransaction(transaction); SorobanTransactionData sorobanData = - Util.sorobanTransactionDataToXDR( + SorobanTransactionData.fromXdrBase64( "AAAAAAAAAAIAAAAGAAAAAcWLK/vE8FTnMk9r8gytPgJuQbutGm0gw9fUkY3tFlQRAAAAFAAAAAEAAAAAAAAAB300Hyg0HZG+Qie3zvsxLvugrNtFqd3AIntWy9bg2YvZAAAAAAAAAAEAAAAGAAAAAcWLK/vE8FTnMk9r8gytPgJuQbutGm0gw9fUkY3tFlQRAAAAEAAAAAEAAAACAAAADwAAAAdDb3VudGVyAAAAABIAAAAAAAAAAFi3xKLI8peqjz0kcSgf38zsr+SOVmMxPsGOEqc+ypihAAAAAQAAAAAAFcLDAAAF8AAAAQgAAAMcAAAAAAAAAJw="); InvokeHostFunctionOperation operation = InvokeHostFunctionOperation.builder() @@ -962,7 +962,7 @@ public MockResponse dispatch(@NotNull RecordedRequest recordedRequest) Transaction newTx = server.prepareTransaction(transaction); SorobanTransactionData sorobanData = - Util.sorobanTransactionDataToXDR( + SorobanTransactionData.fromXdrBase64( "AAAAAAAAAAIAAAAGAAAAAcWLK/vE8FTnMk9r8gytPgJuQbutGm0gw9fUkY3tFlQRAAAAFAAAAAEAAAAAAAAAB300Hyg0HZG+Qie3zvsxLvugrNtFqd3AIntWy9bg2YvZAAAAAAAAAAEAAAAGAAAAAcWLK/vE8FTnMk9r8gytPgJuQbutGm0gw9fUkY3tFlQRAAAAEAAAAAEAAAACAAAADwAAAAdDb3VudGVyAAAAABIAAAAAAAAAAFi3xKLI8peqjz0kcSgf38zsr+SOVmMxPsGOEqc+ypihAAAAAQAAAAAAFcLDAAAF8AAAAQgAAAMcAAAAAAAAAJw="); InvokeHostFunctionOperation operation = InvokeHostFunctionOperation.builder() @@ -1091,7 +1091,7 @@ public MockResponse dispatch(@NotNull RecordedRequest recordedRequest) Transaction newTx = server.prepareTransaction(transaction); SorobanTransactionData sorobanData = - Util.sorobanTransactionDataToXDR( + SorobanTransactionData.fromXdrBase64( "AAAAAAAAAAIAAAAGAAAAAcWLK/vE8FTnMk9r8gytPgJuQbutGm0gw9fUkY3tFlQRAAAAFAAAAAEAAAAAAAAAB300Hyg0HZG+Qie3zvsxLvugrNtFqd3AIntWy9bg2YvZAAAAAAAAAAEAAAAGAAAAAcWLK/vE8FTnMk9r8gytPgJuQbutGm0gw9fUkY3tFlQRAAAAEAAAAAEAAAACAAAADwAAAAdDb3VudGVyAAAAABIAAAAAAAAAAFi3xKLI8peqjz0kcSgf38zsr+SOVmMxPsGOEqc+ypihAAAAAQAAAAAAFcLDAAAF8AAAAQgAAAMcAAAAAAAAAJw="); InvokeHostFunctionOperation operation = InvokeHostFunctionOperation.builder() diff --git a/src/test/java/org/stellar/sdk/TransactionBuilderTest.java b/src/test/java/org/stellar/sdk/TransactionBuilderTest.java index 2e6ca7f84..8e525e676 100644 --- a/src/test/java/org/stellar/sdk/TransactionBuilderTest.java +++ b/src/test/java/org/stellar/sdk/TransactionBuilderTest.java @@ -1068,7 +1068,7 @@ public void voidBuilderSorobanDataXdrString() { } @Test - public void voidBuilderSorobanDataXdrObject() { + public void voidBuilderSorobanDataXdrObject() throws IOException { KeyPair source = KeyPair.fromSecretSeed("SCH27VUZZ6UAKB67BDNF6FA42YMBMQCBKXWGMFD5TZ6S5ZZCZFLRXKHS"); KeyPair destination = @@ -1102,7 +1102,7 @@ public void voidBuilderSorobanDataXdrObject() { .refundableFee(new Int64(100L)) .ext(new ExtensionPoint.Builder().discriminant(0).build()) .build(); - String sorobanDataString = Util.xdrToSorobanTransactionData(sorobanData); + String sorobanDataString = sorobanData.toXdrBase64(); CreateContractArgs createContractArgs = new CreateContractArgs.Builder() diff --git a/src/test/java/org/stellar/sdk/UtilTest.java b/src/test/java/org/stellar/sdk/UtilTest.java deleted file mode 100644 index b362c2e63..000000000 --- a/src/test/java/org/stellar/sdk/UtilTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.stellar.sdk; - -import static org.junit.Assert.assertEquals; -import static org.stellar.sdk.Util.xdrToSorobanTransactionData; - -import org.junit.Test; -import org.stellar.sdk.xdr.*; - -public class UtilTest { - @Test - public void testXdrToSorobanTransactionData() { - LedgerKey ledgerKey = - new LedgerKey.Builder() - .discriminant(LedgerEntryType.ACCOUNT) - .account( - new LedgerKey.LedgerKeyAccount.Builder() - .accountID( - KeyPair.fromAccountId( - "GB7TAYRUZGE6TVT7NHP5SMIZRNQA6PLM423EYISAOAP3MKYIQMVYP2JO") - .getXdrAccountId()) - .build()) - .build(); - SorobanTransactionData sorobanData = - new SorobanTransactionData.Builder() - .resources( - new SorobanResources.Builder() - .footprint( - new LedgerFootprint.Builder() - .readOnly(new LedgerKey[] {ledgerKey}) - .readWrite(new LedgerKey[] {}) - .build()) - .extendedMetaDataSizeBytes(new Uint32(new XdrUnsignedInteger(216))) - .readBytes(new Uint32(new XdrUnsignedInteger(699))) - .writeBytes(new Uint32(new XdrUnsignedInteger(0))) - .instructions(new Uint32(new XdrUnsignedInteger(34567))) - .build()) - .refundableFee(new Int64(100L)) - .ext(new ExtensionPoint.Builder().discriminant(0).build()) - .build(); - String data = xdrToSorobanTransactionData(sorobanData); - String expected = - "AAAAAAAAAAEAAAAAAAAAAH8wYjTJienWf2nf2TEZi2APPWzmtkwiQHAftisIgyuHAAAAAAAAhwcAAAK7AAAAAAAAANgAAAAAAAAAZA=="; - assertEquals(expected, data); - } -}