-
Notifications
You must be signed in to change notification settings - Fork 159
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
Add the fromXdrBase64, fromXdrByteArray, toXdrBase64 and toXdrByteArray methods to the XDR classes. #503
Add the fromXdrBase64, fromXdrByteArray, toXdrBase64 and toXdrByteArray methods to the XDR classes. #503
Changes from all commits
b7df35d
fe8adfb
fb44420
db3b707
3278dcc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
@@ -220,8 +218,16 @@ public GetLedgerEntriesResponse getLedgerEntries(Collection<LedgerKey> keys) | |
throw new IllegalArgumentException("At least one key must be provided."); | ||
} | ||
|
||
List<String> xdrKeys = | ||
keys.stream().map(SorobanServer::ledgerKeyToXdrBase64).collect(Collectors.toList()); | ||
List<String> xdrKeys = new ArrayList<>(keys.size()); | ||
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<SorobanAuthorizationEntry> newEntries = new ArrayList<>(originalEntries); | ||
if (simulateHostFunctionResult.getAuth() != null) { | ||
for (String auth : simulateHostFunctionResult.getAuth()) { | ||
newEntries.add(sorobanAuthorizationEntryFromXdrBase64(auth)); | ||
try { | ||
newEntries.add(SorobanAuthorizationEntry.fromXdrBase64(auth)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @overcat , please refer to the js soroban client for a reference on how it merges simulation results to original transaction, https://github.com/stellar/js-soroban-client/blob/main/src/transaction.ts#L21 the java sdk should be consistent to how that handles merging auths, the js sdk will not apply simulated auths if the original tx had auths. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} 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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than declare
ledgerEntryData
outside of try/catch, just use it inside when it's valid.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally prefer to include as little code as possible within the try block. Assuming there is some other business logic before returning Account, should we also place it within the try block?