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

Add the fromXdrBase64, fromXdrByteArray, toXdrBase64 and toXdrByteArray methods to the XDR classes. #501

Closed
overcat opened this issue Aug 6, 2023 · 3 comments

Comments

@overcat
Copy link
Member

overcat commented Aug 6, 2023

What problem does your feature solve?

  1. Currently, there is a large amount of template code in the current codebase. Moreover, as the project develops, similar template code may increase. I believe that we should generate corresponding methods in the XDR classes.
    Some examples:
  1. When users use the getTransaction method to retrieve the result of a Soroban transaction, there are many XDRs that need to be parsed. Adding these two methods can facilitate users in parsing them.

What would you like to see?

Add the fromXdrBase64 and toXdrBase64 methods to the XDR classes.

diff --git a/src/main/java/org/stellar/sdk/xdr/AccountEntry.java b/src/main/java/org/stellar/sdk/xdr/AccountEntry.java
index 57ed904d..03be0dd1 100644
--- a/src/main/java/org/stellar/sdk/xdr/AccountEntry.java
+++ b/src/main/java/org/stellar/sdk/xdr/AccountEntry.java
@@ -6,6 +6,9 @@
 import static org.stellar.sdk.xdr.Constants.*;
 
 import com.google.common.base.Objects;
+import com.google.common.io.BaseEncoding;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.Arrays;
 
@@ -227,6 +230,32 @@ public boolean equals(Object object) {
         && Objects.equal(this.ext, other.ext);
   }
 
+  @Override
+  public String toXdrBase64() throws IOException {
+    BaseEncoding base64Encoding = BaseEncoding.base64();
+    return base64Encoding.encode(toXdrByteArray());
+  }
+
+  @Override
+  public byte[] toXdrByteArray() throws IOException {
+    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+    XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream);
+    encode(xdrDataOutputStream);
+    return byteArrayOutputStream.toByteArray();
+  }
+
+  public static AccountEntry fromXdrBase64(String xdr) throws IOException {
+    BaseEncoding base64Encoding = BaseEncoding.base64();
+    byte[] bytes = base64Encoding.decode(xdr);
+    return fromXdrByteArray(bytes);
+  }
+
+  public static AccountEntry fromXdrByteArray(byte[] xdr) throws IOException {
+    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr);
+    XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
+    return decode(xdrDataInputStream);
+  }
+
   public static final class Builder {
     private AccountID accountID;
     private Int64 balance;

What alternatives are there?

N/A

@overcat
Copy link
Member Author

overcat commented Aug 6, 2023

@sreuland
Copy link
Contributor

sreuland commented Aug 7, 2023

@overcat, ok, so in xdrgen, you need stellar/xdrgen#159 stellar/xdrgen#160 stellar/xdrgen#161 and stellar/xdrgen#163 merged now regardless just to enable ongoing development in java sdk soroban feature branch? @tamirms , any chance you have checked any of these xdrgen pr's yet, I will as well, but don't have much experience there.

and then after those xdrgen all merge, this remainder request for optimizing Base64 methods can be considered, but it's not blocking?

@overcat
Copy link
Member Author

overcat commented Aug 7, 2023

These changes(stellar/xdrgen#159 stellar/xdrgen#160 stellar/xdrgen#161) have been applied to #481.
The changes(stellar/xdrgen#163) have been applied to #498.

These changes are all small, and I believe we can quickly review them.

If I want to create a PR for this issue, it will result in changes to XDR. However, there are also changes to XDR in #498. This is not a big problem, but I prefer to wait for these PRs to be merged before creating a new one.

@overcat overcat changed the title Add the fromXdrBase64 and toXdrBase64 methods to the XDR classes. Add the fromXdrBase64, fromXdrByteArray, toXdrBase64 and toXdrByteArray methods to the XDR classes. Aug 9, 2023
@overcat overcat closed this as completed Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants