Skip to content

Commit

Permalink
Update Horizon Transaction response (#275)
Browse files Browse the repository at this point in the history
In the Horizon 1.1.0 release the following attributes were added to the Horizon transaction response:

fee_account (the account which paid the transaction fee)
fee_bump_transaction (only present in Protocol 13 fee bump transactions)
inner_transaction (only present in Protocol 13 fee bump transactions)
In the upcoming Horizon 1.2.0 release a base 64 encoding of the bytes in a transaction memo will be included in the Horizon transaction response.

This commit updates the Transaction class in the Java SDK so that it can be used to parse the new Horizon transaction response.
  • Loading branch information
tamirms authored Apr 21, 2020
1 parent 48b28e6 commit 8bbb22d
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package org.stellar.sdk.responses;

import com.google.common.io.BaseEncoding;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.*;

import org.stellar.sdk.Memo;
import org.stellar.sdk.xdr.TransactionEnvelope;
Expand Down Expand Up @@ -35,19 +30,28 @@ public TransactionResponse deserialize(JsonElement json, Type typeOfT, JsonDeser
// representation of a transaction. That's why we need to handle a special case
// here.
if (memoType.equals("text")) {
// we obtain the memo text from the xdr because the bytes may not be valid utf8
String envelopeXdr = json.getAsJsonObject().get("envelope_xdr").getAsString();
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(envelopeXdr);
TransactionEnvelope transactionEnvelope = null;
try {
transactionEnvelope = TransactionEnvelope.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
} catch (IOException e) {
// JsonDeserializer<TransactionResponse> cannot throw IOExceptions
// so we must throw it as a runtime exception
throw new RuntimeException(e);
JsonObject jsonResponse = json.getAsJsonObject();

if (jsonResponse.has("memo_bytes")) {
// we obtain the memo text from the "memo_bytes" field because the original byte sequence may not be valid utf8
String memoBase64 = json.getAsJsonObject().get("memo_bytes").getAsString();
memo = Memo.text(base64Encoding.decode(memoBase64));
} else {
// memo_bytes is not available because horizon is running a version older than 1.2.0
// so we will recover the bytes from the xdr
String envelopeXdr = json.getAsJsonObject().get("envelope_xdr").getAsString();
byte[] bytes = base64Encoding.decode(envelopeXdr);
TransactionEnvelope transactionEnvelope = null;
try {
transactionEnvelope = TransactionEnvelope.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
} catch (IOException e) {
// JsonDeserializer<TransactionResponse> cannot throw IOExceptions
// so we must throw it as a runtime exception
throw new RuntimeException(e);
}
memo = Memo.text(transactionEnvelope.getTx().getMemo().getText().getBytes());
}
memo = Memo.text(transactionEnvelope.getTx().getMemo().getText().getBytes());
} else {
String memoValue = json.getAsJsonObject().get("memo").getAsString();
BaseEncoding base64Encoding = BaseEncoding.base64();
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/org/stellar/sdk/responses/TransactionResponse.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.stellar.sdk.responses;

import com.google.common.base.Optional;
import com.google.gson.annotations.SerializedName;

import org.stellar.sdk.Memo;

import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

/**
Expand All @@ -21,6 +24,8 @@ public class TransactionResponse extends Response implements Pageable {
private final String createdAt;
@SerializedName("source_account")
private final String sourceAccount;
@SerializedName("fee_account")
private final String feeAccount;
@SerializedName("successful")
private final Boolean successful;
@SerializedName("paging_token")
Expand All @@ -39,6 +44,12 @@ public class TransactionResponse extends Response implements Pageable {
private final String resultXdr;
@SerializedName("result_meta_xdr")
private final String resultMetaXdr;
@SerializedName("signatures")
private final List<String> signatures;
@SerializedName("fee_bump_transaction")
private final FeeBumpTransaction feeBumpTransaction;
@SerializedName("inner_transaction")
private final InnerTransaction innerTransaction;
@SerializedName("_links")
private final Links links;

Expand All @@ -51,6 +62,7 @@ public class TransactionResponse extends Response implements Pageable {
Long ledger,
String createdAt,
String sourceAccount,
String feeAccount,
Boolean successful,
String pagingToken,
Long sourceAccountSequence,
Expand All @@ -61,12 +73,16 @@ public class TransactionResponse extends Response implements Pageable {
String resultXdr,
String resultMetaXdr,
Memo memo,
List<String> signatures,
FeeBumpTransaction feeBumpTransaction,
InnerTransaction innerTransaction,
Links links
) {
this.hash = hash;
this.ledger = ledger;
this.createdAt = createdAt;
this.sourceAccount = sourceAccount;
this.feeAccount = feeAccount;
this.successful = successful;
this.pagingToken = pagingToken;
this.sourceAccountSequence = sourceAccountSequence;
Expand All @@ -77,6 +93,9 @@ public class TransactionResponse extends Response implements Pageable {
this.resultXdr = resultXdr;
this.resultMetaXdr = resultMetaXdr;
this.memo = memo;
this.signatures = signatures;
this.feeBumpTransaction = feeBumpTransaction;
this.innerTransaction = innerTransaction;
this.links = links;
}

Expand All @@ -96,6 +115,22 @@ public String getSourceAccount() {
return sourceAccount;
}

public String getFeeAccount() {
return feeAccount;
}

public List<String> getSignatures() {
return signatures;
}

public Optional<FeeBumpTransaction> getFeeBump() {
return Optional.fromNullable(this.feeBumpTransaction);
}

public Optional<InnerTransaction> getInner() {
return Optional.fromNullable(this.innerTransaction);
}

public String getPagingToken() {
return pagingToken;
}
Expand Down Expand Up @@ -148,6 +183,66 @@ public Links getLinks() {
return links;
}

/**
* FeeBumpTransaction is only present in a TransactionResponse if the transaction is a fee bump transaction or is
* wrapped by a fee bump transaction. The object has two fields: the hash of the fee bump transaction and the
* signatures present in the fee bump transaction envelope.
*/
public static class FeeBumpTransaction {
@SerializedName("hash")
private final String hash;
@SerializedName("signatures")
private final List<String> signatures;

FeeBumpTransaction(String hash, List<String> signatures) {
this.hash = hash;
this.signatures = signatures;
}

public String getHash() {
return hash;
}

public List<String> getSignatures() {
return signatures;
}
}

/**
* InnerTransaction is only present in a TransactionResponse if the transaction is a fee bump transaction or is
* wrapped by a fee bump transaction. The object has three fields: the hash of the inner transaction wrapped by the
* fee bump transaction, the max fee set in the inner transaction, and the signatures present in the inner
* transaction envelope.
*/
public static class InnerTransaction {
@SerializedName("hash")
private final String hash;
@SerializedName("signatures")
private final List<String> signatures;
@SerializedName("max_fee")
private final Long maxFee;


InnerTransaction(String hash, List<String> signatures, Long maxFee) {
this.hash = hash;
this.signatures = signatures;
this.maxFee = maxFee;
}

public String getHash() {
return hash;
}

public List<String> getSignatures() {
return signatures;
}

public Long getMaxFee() {
return maxFee;
}

}

/**
* Links connected to transaction.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
package org.stellar.sdk.responses;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import junit.framework.TestCase;

import org.junit.Test;
import org.stellar.sdk.MemoHash;
import org.stellar.sdk.MemoNone;

public class TransactionDeserializerTest extends TestCase {
@Test
public void testDeserializeFeeBump() {
TransactionResponse transaction = GsonSingleton.getInstance().fromJson(jsonFeeBump, TransactionResponse.class);
assertEquals(transaction.getHash(), "3dfef7d7226995b504f2827cc63d45ad41e9687bb0a8abcf08ba755fedca0352");
assertEquals(transaction.getLedger(), Long.valueOf(123));
assertEquals(transaction.isSuccessful(), Boolean.TRUE);
assertEquals(transaction.getSourceAccount(), "GABQGAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB2MX");
assertEquals(transaction.getFeeAccount(), "GABAEAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABGKJ");
assertEquals(transaction.getSourceAccountSequence(), Long.valueOf(97));
assertEquals(transaction.getMaxFee(), Long.valueOf(776));
assertEquals(transaction.getFeeCharged(), Long.valueOf(123));
assertEquals(transaction.getOperationCount(), Integer.valueOf(1));
assertEquals(transaction.getSignatures(), ImmutableList.of("Hh4e"));

TransactionResponse.FeeBumpTransaction feeBumpTransaction = transaction.getFeeBump().get();
assertEquals(feeBumpTransaction.getHash(), "3dfef7d7226995b504f2827cc63d45ad41e9687bb0a8abcf08ba755fedca0352");
assertEquals(feeBumpTransaction.getSignatures(), ImmutableList.of("Hh4e"));

TransactionResponse.InnerTransaction innerTransaction = transaction.getInner().get();
assertEquals(innerTransaction.getHash(), "e98869bba8bce08c10b78406202127f3888c25454cd37b02600862452751f526");
assertEquals(innerTransaction.getMaxFee(), Long.valueOf(99));
assertEquals(innerTransaction.getSignatures(), ImmutableList.of("FBQU"));
}

@Test
public void testDeserialize() {
TransactionResponse transaction = GsonSingleton.getInstance().fromJson(json, TransactionResponse.class);
Expand All @@ -23,7 +49,9 @@ public void testDeserialize() {
assertEquals(transaction.getEnvelopeXdr(), "AAAAAKgfpXwD1fWpPmZL+GkzWcBmhRQH7ouPsoTN3RoaGCfrAAAAZAAIbkcAAB9WAAAAAAAAAANRBBZE6D1qyGjISUGLY5Ldvp31PwAAAAAAAAAAAAAAAAAAAAEAAAABAAAAAP1qe44j+i4uIT+arbD4QDQBt8ryEeJd7a0jskQ3nwDeAAAAAAAAAADA7RnarSzCwj3OT+M2btCMFpVBdqxJS+Sr00qBjtFv7gAAAABLCs/QAAAAAAAAAAEaGCfrAAAAQG/56Cj2J8W/KCZr+oC4sWND1CTGWfaccHNtuibQH8kZIb+qBSDY94g7hiaAXrlIeg9b7oz/XuP3x9MWYw2jtwM=");
assertEquals(transaction.getResultXdr(), "AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAA=");
assertEquals(transaction.getResultMetaXdr(), "AAAAAAAAAAEAAAACAAAAAAAN+SAAAAAAAAAAAMDtGdqtLMLCPc5P4zZu0IwWlUF2rElL5KvTSoGO0W/uAAAAAEsKz9AADfkgAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAQAN+SAAAAAAAAAAAP1qe44j+i4uIT+arbD4QDQBt8ryEeJd7a0jskQ3nwDeAAHp6WMr55YACD1BAAAAHgAAAAoAAAAAAAAAAAAAAAABAAAAAAAACgAAAAARC07BokpLTOF+/vVKBwiAlop7hHGJTNeGGlY4MoPykwAAAAEAAAAAK+Lzfd3yDD+Ov0GbYu1g7SaIBrKZeBUxoCunkLuI7aoAAAABAAAAAERmsKL73CyLV/HvjyQCERDXXpWE70Xhyb6MR5qPO3yQAAAAAQAAAABSORGwAdyuanN3sNOHqNSpACyYdkUM3L8VafUu69EvEgAAAAEAAAAAeCzqJNkMM/jLvyuMIfyFHljBlLCtDyj17RMycPuNtRMAAAABAAAAAIEi4R7juq15ymL00DNlAddunyFT4FyUD4muC4t3bobdAAAAAQAAAACaNpLL5YMfjOTdXVEqrAh99LM12sN6He6pHgCRAa1f1QAAAAEAAAAAqB+lfAPV9ak+Zkv4aTNZwGaFFAfui4+yhM3dGhoYJ+sAAAABAAAAAMNJrEvdMg6M+M+n4BDIdzsVSj/ZI9SvAp7mOOsvAD/WAAAAAQAAAADbHA6xiKB1+G79mVqpsHMOleOqKa5mxDpP5KEp/Xdz9wAAAAEAAAAAAAAAAA==");

assertEquals(transaction.getSignatures(), ImmutableList.of("b/noKPYnxb8oJmv6gLixY0PUJMZZ9pxwc226JtAfyRkhv6oFINj3iDuGJoBeuUh6D1vujP9e4/fH0xZjDaO3Aw=="));
assertEquals(transaction.getFeeBump(), Optional.<TransactionResponse.FeeBumpTransaction>absent());
assertEquals(transaction.getInner(), Optional.<TransactionResponse.InnerTransaction>absent());
assertTrue(transaction.getMemo() instanceof MemoHash);
MemoHash memo = (MemoHash) transaction.getMemo();
assertEquals("51041644e83d6ac868c849418b6392ddbe9df53f000000000000000000000000", memo.getHexValue());
Expand Down Expand Up @@ -136,4 +164,70 @@ public void testDeserializeWithoutMemo() {
" \"b/noKPYnxb8oJmv6gLixY0PUJMZZ9pxwc226JtAfyRkhv6oFINj3iDuGJoBeuUh6D1vujP9e4/fH0xZjDaO3Aw==\"\n" +
" ]\n" +
"}";

String jsonFeeBump = "{\n" +
" \"_links\": {\n" +
" \"self\": {\n" +
" \"href\": \"http://localhost/transactions/3dfef7d7226995b504f2827cc63d45ad41e9687bb0a8abcf08ba755fedca0352\"\n" +
" },\n" +
" \"account\": {\n" +
" \"href\": \"http://localhost/accounts/GABQGAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB2MX\"\n" +
" },\n" +
" \"ledger\": {\n" +
" \"href\": \"http://localhost/ledgers/123\"\n" +
" },\n" +
" \"operations\": {\n" +
" \"href\": \"http://localhost/transactions/3dfef7d7226995b504f2827cc63d45ad41e9687bb0a8abcf08ba755fedca0352/operations{?cursor,limit,order}\",\n" +
" \"templated\": true\n" +
" },\n" +
" \"effects\": {\n" +
" \"href\": \"http://localhost/transactions/3dfef7d7226995b504f2827cc63d45ad41e9687bb0a8abcf08ba755fedca0352/effects{?cursor,limit,order}\",\n" +
" \"templated\": true\n" +
" },\n" +
" \"precedes\": {\n" +
" \"href\": \"http://localhost/transactions?order=asc\\u0026cursor=528280981504\"\n" +
" },\n" +
" \"succeeds\": {\n" +
" \"href\": \"http://localhost/transactions?order=desc\\u0026cursor=528280981504\"\n" +
" },\n" +
" \"transaction\": {\n" +
" \"href\": \"http://localhost/transactions/3dfef7d7226995b504f2827cc63d45ad41e9687bb0a8abcf08ba755fedca0352\"\n" +
" }\n" +
" },\n" +
" \"id\": \"3dfef7d7226995b504f2827cc63d45ad41e9687bb0a8abcf08ba755fedca0352\",\n" +
" \"paging_token\": \"528280981504\",\n" +
" \"successful\": true,\n" +
" \"hash\": \"3dfef7d7226995b504f2827cc63d45ad41e9687bb0a8abcf08ba755fedca0352\",\n" +
" \"ledger\": 123,\n" +
" \"created_at\": \"2020-04-21T10:21:26Z\",\n" +
" \"source_account\": \"GABQGAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB2MX\",\n" +
" \"source_account_sequence\": \"97\",\n" +
" \"fee_account\": \"GABAEAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABGKJ\",\n" +
" \"fee_charged\": 123,\n" +
" \"max_fee\": 776,\n" +
" \"operation_count\": 1,\n" +
" \"envelope_xdr\": \"AAAABQAAAAACAgIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMIAAAAAgAAAAADAwMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGMAAAAAAAAAYQAAAAEAAAAAAAAAAgAAAAAAAAAEAAAAAAAAAAEAAAAAAAAACwAAAAAAAABiAAAAAAAAAAECAgICAAAAAxQUFAAAAAAAAAAAAQMDAwMAAAADHh4eAA==\",\n" +
" \"result_xdr\": \"AAAAAAAAAHsAAAAB6Yhpu6i84IwQt4QGICEn84iMJUVM03sCYAhiRSdR9SYAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAsAAAAAAAAAAAAAAAA=\",\n" +
" \"result_meta_xdr\": \"AAAAAQAAAAAAAAAA\",\n" +
" \"fee_meta_xdr\": \"AAAAAA==\",\n" +
" \"memo_type\": \"none\",\n" +
" \"signatures\": [\n" +
" \"Hh4e\"\n" +
" ],\n" +
" \"valid_after\": \"1970-01-01T00:00:02Z\",\n" +
" \"valid_before\": \"1970-01-01T00:00:04Z\",\n" +
" \"fee_bump_transaction\": {\n" +
" \"hash\": \"3dfef7d7226995b504f2827cc63d45ad41e9687bb0a8abcf08ba755fedca0352\",\n" +
" \"signatures\": [\n" +
" \"Hh4e\"\n" +
" ]\n" +
" },\n" +
" \"inner_transaction\": {\n" +
" \"hash\": \"e98869bba8bce08c10b78406202127f3888c25454cd37b02600862452751f526\",\n" +
" \"signatures\": [\n" +
" \"FBQU\"\n" +
" ],\n" +
" \"max_fee\": \"99\"\n" +
" }\n" +
"}";
}
Loading

0 comments on commit 8bbb22d

Please sign in to comment.